def _handle_turn_units(self, msg, is_dead_unit=False): if not is_dead_unit: self.map.clear_units() for player in self.players: player.units.clear() player.played_units.clear() player.hasted_units.clear() player.duplicate_units.clear() player.range_upgraded_unit = None player.damage_upgraded_unit = None else: for player in self.players: player.died_units.clear() for unit_msg in msg: unit_id = unit_msg["unitId"] player = self.get_player_by_id(player_id=unit_msg["playerId"]) base_unit = self.base_units[unit_msg["typeId"]] if not unit_msg['target'] == -1: target_cell = Cell(row=unit_msg["targetCell"]["row"], col=unit_msg["targetCell"]["col"]) else: target_cell = None unit = Unit(unit_id=unit_id, base_unit=base_unit, cell=self.map.get_cell(unit_msg["cell"]["row"], unit_msg["cell"]["col"]), path=self.map.get_path_by_id(unit_msg["pathId"]), hp=unit_msg["hp"], damage_level=unit_msg["damageLevel"], range_level=unit_msg["rangeLevel"], is_duplicate=unit_msg["isDuplicate"], is_hasted=unit_msg["isHasted"], range=unit_msg["range"], attack=unit_msg["attack"], target=unit_msg["target"], target_cell=target_cell, affected_spells=[self.get_cast_spell_by_id(cast_spell_id) for cast_spell_id in unit_msg["affectedSpells"]], target_if_king=None if self.get_player_by_id( unit_msg["target"]) is None else self.get_player_by_id(unit_msg["target"]).king, player_id=unit_msg["playerId"]) if not is_dead_unit: self.map.add_unit_in_cell(unit.cell.row, unit.cell.col, unit) player.units.append(unit) if unit_msg["wasDamageUpgraded"]: player.damage_upgraded_unit = unit if unit_msg["wasRangeUpgraded"]: player.range_upgraded_unit = unit if unit_msg["wasPlayedThisTurn"]: player.played_units.append(unit) if unit.is_hasted: player.hasted_units.append(unit) if unit.is_duplicate: player.duplicate_units.append(unit) else: player.died_units.append(unit) for unit in self.map.units: if unit.target == -1 or unit.target_if_king is not None: unit.target = None else: unit.target = self.map.get_unit_by_id(unit.target)
def _handle_turn_units(self, msg, is_dead_unit=False): if not is_dead_unit: self._map._clear_units() for player in self._players: player.units.clear() player.played_units.clear() player.hasted_units.clear() player.duplicate_units.clear() player.range_upgraded_unit = None player.damage_upgraded_unit = None else: for player in self._players: player.died_units.clear() unit_input_list = [] for unit_msg in msg: unit_id = unit_msg["unitId"] player = self.get_player_by_id(player_id=unit_msg["playerId"]) base_unit = self._base_units[unit_msg["typeId"]] if not unit_msg['target'] == -1: target_cell = Cell(row=unit_msg["targetCell"]["row"], col=unit_msg["targetCell"]["col"]) else: target_cell = None unit = Unit( unit_id=unit_id, base_unit=base_unit, cell=self._map.get_cell(unit_msg["cell"]["row"], unit_msg["cell"]["col"]), path=self._map.get_path_by_id(unit_msg["pathId"]), hp=unit_msg["hp"], damage_level=unit_msg["damageLevel"], range_level=unit_msg["rangeLevel"], is_duplicate=unit_msg["isDuplicate"], is_hasted=unit_msg["isHasted"], range=unit_msg["range"], attack=unit_msg["attack"], target=None, # will be set later when all units are in set target_cell=target_cell, affected_spells=[ self.get_cast_spell_by_id(cast_spell_id) for cast_spell_id in unit_msg["affectedSpells"] ], target_if_king=None if self.get_player_by_id(unit_msg["target"]) is None else self.get_player_by_id(unit_msg["target"]).king, player_id=unit_msg["playerId"]) unit_input_list.append(unit) if unit.path is not None: if self.get_player_by_id( unit.player_id ).king.center in unit.path.cells and unit.path.cells[ 0] != self.get_player_by_id( unit.player_id).king.center: unit.path = Path(path=unit.path) unit.path.cells.reverse() if self._get_friend_by_id( unit.player_id ).king.center in unit.path.cells and unit.path.cells[ 0] != self._get_friend_by_id( unit.player_id).king.center: unit.path = Path(path=unit.path) unit.path.cells.reverse() if not is_dead_unit: self._map._add_unit_in_cell(unit.cell.row, unit.cell.col, unit) player.units.append(unit) if unit_msg["wasDamageUpgraded"]: player.damage_upgraded_unit = unit if unit_msg["wasRangeUpgraded"]: player.range_upgraded_unit = unit if unit_msg["wasPlayedThisTurn"]: player.played_units.append(unit) if unit.is_hasted: player.hasted_units.append(unit) if unit.is_duplicate: player.duplicate_units.append(unit) else: player.died_units.append(unit) for i in range(len(unit_input_list)): unit = unit_input_list[i] if unit.target_if_king is not None: unit.target = None else: unit.target = self.get_unit_by_id(msg[i]["target"])