From 099c7e215ee8aae2c3b3d71b93f8fe9bfe1073b2 Mon Sep 17 00:00:00 2001 From: ErfanFaravani Date: Sat, 8 Feb 2020 18:49:05 +0330 Subject: [PATCH] shortest path updated --- model.py | 17 ++++++++++------- world.py | 21 +++++++++++++++------ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/model.py b/model.py index f58355a..48b106f 100644 --- a/model.py +++ b/model.py @@ -29,7 +29,8 @@ def add_unit_in_cell(self, row, column, unit): class Player: def __init__(self, player_id, deck, hand, ap, king, paths_from_player, path_to_friend, units, cast_area_spell, cast_unit_spell, duplicate_units, hasted_units, played_units, - died_units, spells, range_upgraded_unit = None, damage_upgraded_unit = None): + died_units, spells, range_upgraded_unit=None, damage_upgraded_unit=None): + self._spells_dict = {} self.player_id = player_id self.deck = deck self.hand = hand @@ -37,13 +38,13 @@ def __init__(self, player_id, deck, hand, ap, king, paths_from_player, path_to_f self.king = king self.paths_from_player = paths_from_player self.path_to_friend = path_to_friend - self.units = units # alive units + self.units = units # alive units self.cast_area_spell = cast_area_spell self.cast_unit_spell = cast_unit_spell self.duplicate_units = duplicate_units self.hasted_units = hasted_units self.played_units = played_units # units that played last turn - self.died_units = died_units # units that died last turn + self.died_units = died_units # units that died last turn self.spells = spells self.range_upgraded_unit = range_upgraded_unit # unit that last turn the player upgraded range of it self.damage_upgraded_unit = damage_upgraded_unit # unit that last turn the player upgraded damage of it @@ -56,16 +57,17 @@ def get_hp(self): def set_spells(self, spells): self.spells = spells - self._spells_dict = {} for spell in spells: if spell.type_id in self._spells_dict: self._spells_dict[spell.type_id] += 1 else: self._spells_dict[spell.type_id] = 1 - def get_spell_count(self, spell): - if spell.type_id in self._spells_dict: - return self._spells_dict[spell.type_id] + def get_spell_count(self, spell=None, spell_id=None): + if spell is not None: + spell_id = spell.type_id + if spell_id in self._spells_dict: + return self._spells_dict[spell_id] return 0 def get_spells(self): @@ -175,6 +177,7 @@ def __str__(self): "type : {} | " \ "type id : {}>".format(self.type, self.type_id) + class Cell: def __init__(self, row=0, col=0): self.row = row diff --git a/world.py b/world.py index 924998c..c053cd2 100644 --- a/world.py +++ b/world.py @@ -387,21 +387,22 @@ def get_cell_units(self, cell=None, row=None, col=None): # return the shortest path from player_id fortress to cell # this path is in the available path list # path may cross from friend - def get_shortest_path_to_cell(self, player_id, cell=None, row=None, col=None): + def get_shortest_path_to_cell(self, from_player_id=None, from_player=None, cell=None, row=None, col=None): if len(list(self.shortest_path.values())) == 0: self._pre_process_shortest_path() - + if from_player is not None: + from_player_id = from_player.player_id if cell is None: if row is None or col is None: return cell = self.map.get_cell(row, col) - shortest_path_to_cell = self.shortest_path.get(player_id) + shortest_path_to_cell = self.shortest_path.get(from_player_id) if shortest_path_to_cell[cell.row][cell.col] == -1: - shortest_path_from_friend = self.shortest_path.get(self.get_friend_by_id(player_id)) + shortest_path_from_friend = self.shortest_path.get(self.get_friend_by_id(from_player_id)) if shortest_path_from_friend[cell.row][cell.col] == -1: return None - return len(p.path_to_friend.cells) + shortest_path_from_friend[cell.row][cell.col] + return shortest_path_from_friend[cell.row][cell.col] return shortest_path_to_cell[cell.row][cell.col] @@ -431,11 +432,19 @@ def get_current_turn(self): # return player.king.hp # put unit_id in path_id in position 'index' all spells of one kind have the same id - def cast_unit_spell(self, unit_id, path_id, cell, spell=None, spell_id=None): + def cast_unit_spell(self, unit=None, unit_id=None, path=None, path_id=None, cell=None, row=None, col=None, + spell=None, + spell_id=None): if spell is None and spell_id is None: return None if spell is None: spell = self.get_spell_by_type_id(spell_id) + if row is not None and col is not None: + cell = Cell(row, col) + if unit is not None: + unit_id = unit.unit_id + if path is not None: + path_id = path.path_id message = Message(type="castSpell", turn=self.get_current_turn(), info={ "typeId": spell.type,