Skip to content

Commit

Permalink
shortest path updated
Browse files Browse the repository at this point in the history
  • Loading branch information
erfanfi79 committed Feb 8, 2020
1 parent c7e05bf commit 099c7e2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
17 changes: 10 additions & 7 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,22 @@ 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
self.ap = ap
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
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down
21 changes: 15 additions & 6 deletions world.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 099c7e2

Please sign in to comment.