Skip to content

Commit

Permalink
spell and spell count fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
HamidrezaKmK committed Feb 4, 2020
1 parent ed79dc3 commit b7411f7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
8 changes: 8 additions & 0 deletions AI.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ def path_to_friend_check(self, world):

def turn(self, world):
print("turn")
if world.get_current_turn() == 12:
print("turn 12 baby!")
print(len(world.get_me().get_spells()))
for spell in world.get_me().get_spells():
print(spell)
print(world.get_me().get_spell_count(spell))
print("---")

if world.get_current_turn() == 20:
print("its turn 2!!!")
world.put_unit(base_unit=world.get_me().hand[0], path=world.get_me().paths_from_player[0])
Expand Down
1 change: 1 addition & 0 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def handle_message(self, message):
new_world._handle_turn_message(message[ServerConstants.KEY_INFO])
threading.Thread(target=self.launch_on_thread, args=(self.client.turn, new_world)).start()


elif message[ServerConstants.KEY_TYPE] == ServerConstants.MESSAGE_TYPE_SHUTDOWN:
self.terminate()

Expand Down
27 changes: 26 additions & 1 deletion model.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ 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, range_upgraded_unit = None, damage_upgraded_unit = None):
died_units, spells, range_upgraded_unit = None, damage_upgraded_unit = None):
self.player_id = player_id
self.deck = deck
self.hand = hand
Expand All @@ -44,6 +44,7 @@ def __init__(self, player_id, deck, hand, ap, king, paths_from_player, path_to_f
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.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 @@ -53,6 +54,23 @@ def is_alive(self):
def get_hp(self):
return self.king.hp

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]
return 0

def get_spells(self):
return self.spells

def __str__(self):
return "<Player | " \
"player id : {} | " \
Expand Down Expand Up @@ -149,6 +167,13 @@ def is_unit_spell(self):
def is_area_spell(self):
return not self.is_unit_spell()

def __eq__(self, other):
return self.type_id == other.type_id

def __str__(self):
return "<Spell | " \
"type : {} | " \
"type id : {}>".format(self.type, self.type_id)

class Cell:
def __init__(self, row=0, col=0):
Expand Down
20 changes: 4 additions & 16 deletions world.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def _map_init(self, map_msg):
played_units=[],
died_units=[],
range_upgraded_unit=None,
damage_upgraded_unit=None) for i in range(4)]
damage_upgraded_unit=None,
spells=[]) for i in range(4)]

for player in self.players:
player.paths_from_player.remove(player.path_to_friend)
Expand Down Expand Up @@ -281,8 +282,8 @@ def _handle_turn_message(self, msg):
available_range_upgrades=msg["availableRangeUpgrades"],
available_damage_upgrades=msg["availableDamageUpgrades"])

self.player.spells = msg["mySpells"]
self.player_friend.spells = msg["friendSpells"]
self.player.set_spells([self.get_spell_by_id(spell_id=spell_id) for spell_id in msg["mySpells"]])
self.player_friend.set_spells([self.get_spell_by_id(spell_id=spell_id) for spell_id in msg["friendSpells"]])

self.start_time = self.get_current_time_millis()

Expand Down Expand Up @@ -505,19 +506,6 @@ def get_range_upgrade_number(self, player_id):
def get_damage_upgrade_number(self, player_id):
return self.turn_updates.available_damage_upgrade

def get_spells_list(self):
return self.player.spells

# get current available spells as a dictionary
def get_spells(self):
return_dict = dict()
for spell in self.player.spells:
if spell in return_dict:
return_dict[spell] += 1
else:
return_dict[spell] = 1
return return_dict

# returns the spell given in that turn
def get_received_spell(self):
spell_type_id = self.turn_updates.received_spell
Expand Down

0 comments on commit b7411f7

Please sign in to comment.