From 326de3ccb5564cae859e56111276e685568791b0 Mon Sep 17 00:00:00 2001 From: ErfanFaravani Date: Mon, 3 Feb 2020 08:15:48 +0330 Subject: [PATCH 1/3] changes to world base on clientv5.1 doc --- world.py | 331 +++++++++++++++++++++++++++---------------------------- 1 file changed, 165 insertions(+), 166 deletions(-) diff --git a/world.py b/world.py index 678ef58..d7c1b0e 100644 --- a/world.py +++ b/world.py @@ -270,11 +270,11 @@ def choose_deck(self, type_ids=None, base_units=None): message.info = {"units": [unit.type_id for unit in base_units]} self.queue.put(message) - def get_my_id(self): - return self.player.player_id + def get_me(self): + return self.player - def get_friend_id(self): - return self.player_friend.player_id + def get_friend(self): + return self.player_friend def get_friend_by_id(self, player_id): if self.player.player_id == player_id: @@ -288,55 +288,51 @@ def get_friend_by_id(self, player_id): else: return None - def get_first_enemy_id(self): - return self.player_first_enemy.player_id + def get_first_enemy(self): + return self.player_first_enemy - def get_second_enemy_id(self): - return self.player_second_enemy.player_id + def get_second_enemy(self): + return self.player_second_enemy # returns a cell that is the fortress of player with player_id - def get_player_position(self, player_id): - player = self.get_player_by_id(player_id) - if player is not None: - return player.king.center - - # return a list of paths starting from the fortress of player with player_id - # the beginning is from player_id fortress cell - def get_paths_from_player(self, player_id): - paths = [] - player_king_cell = self.get_player_position(player_id) - for p in self.map.paths: - first_cell = p.cells[0] - last_cell = p.cells[len(p.cells) - 1] - if first_cell == player_king_cell: - paths.append(p) - continue - if last_cell == player_king_cell: - p.cells.reverse() - paths.append(p) - continue - return paths - - # returns the path from player_id to its friend beginning from player_id's fortress - def get_path_to_friend(self, player_id): - player_king_cell = self.get_player_position(player_id) - friend_king_cell = self.get_friend_by_id(player_id).king.center - - for p in self.map.paths: - first_cell = p.cells[0] - last_cell = p.cells[len(p.cells) - 1] - if first_cell == player_king_cell and last_cell == friend_king_cell: - return p - if last_cell == player_king_cell and first_cell == friend_king_cell: - p.cells.reverse() - return p - - def get_map_row_num(self): - return self.map.row_count - - def get_map_col_num(self): - return self.map.column_count - # return a list of paths crossing one cell + # def get_player_position(self, player_id): + # player = self.get_player_by_id(player_id) + # if player is not None: + # return player.king.center + # + # # return a list of paths starting from the fortress of player with player_id + # # the beginning is from player_id fortress cell + # def get_paths_from_player(self, player_id): + # paths = [] + # player_king_cell = self.get_player_position(player_id) + # for p in self.map.paths: + # first_cell = p.cells[0] + # last_cell = p.cells[len(p.cells) - 1] + # if first_cell == player_king_cell: + # paths.append(p) + # continue + # if last_cell == player_king_cell: + # p.cells.reverse() + # paths.append(p) + # continue + # return paths + # + # # returns the path from player_id to its friend beginning from player_id's fortress + # def get_path_to_friend(self, player_id): + # player_king_cell = self.get_player_position(player_id) + # friend_king_cell = self.get_friend_by_id(player_id).king.center + # + # for p in self.map.paths: + # first_cell = p.cells[0] + # last_cell = p.cells[len(p.cells) - 1] + # if first_cell == player_king_cell and last_cell == friend_king_cell: + # return p + # if last_cell == player_king_cell and first_cell == friend_king_cell: + # p.cells.reverse() + # return p + + def get_map(self): + return self.map # return a list of paths crossing one cell def get_paths_crossing_cell(self, cell=None, row=None, col=None): @@ -351,10 +347,10 @@ def get_paths_crossing_cell(self, cell=None, row=None, col=None): paths.append(p) return paths - # return units of player that are currently in map - def get_player_units(self, player_id): - player = self.get_player_by_id(player_id) - return player.units + # # return units of player that are currently in map + # def get_player_units(self, player_id): + # player = self.get_player_by_id(player_id) + # return player.units # return a list of units in a cell def get_cell_units(self, cell=None, row=None, col=None): @@ -382,21 +378,21 @@ def get_shortest_path_to_cell(self, player_id, cell=None, row=None, col=None): return shortest_path_to_cell[cell.row][cell.col] - # returns the limit of ap for each player - def get_max_ap(self): - return self.game_constants.max_ap - - # get remaining ap - def get_remaining_ap(self): - return self.player.ap - - # returns a list of units in hand - def get_hand(self): - return self.player.hand - - # returns a list of units in deck - def get_deck(self): - return self.player.deck + # # returns the limit of ap for each player + # def get_max_ap(self): + # return self.game_constants.max_ap + # + # # get remaining ap + # def get_remaining_ap(self): + # return self.player.ap + # + # # returns a list of units in hand + # def get_hand(self): + # return self.player.hand + # + # # returns a list of units in deck + # def get_deck(self): + # return self.player.deck # place unit with type_id in path_id def put_unit(self, type_id=None, path_id=None, base_unit=None, path=None): @@ -418,26 +414,26 @@ def put_unit(self, type_id=None, path_id=None, base_unit=None, path=None): def get_current_turn(self): return self.current_turn - # return the limit of turns - def get_max_turns(self): - return self.game_constants.max_turns - - # return the time left to pick units and put in deck in the first turn - def get_pick_timeout(self): - return self.game_constants.pick_timeout - - # a constant limit for each turn - def get_turn_timeout(self): - return self.game_constants.turn_timeout + # # return the limit of turns + # def get_max_turns(self): + # return self.game_constants.max_turns + # + # # return the time left to pick units and put in deck in the first turn + # def get_pick_timeout(self): + # return self.game_constants.pick_timeout + # + # # a constant limit for each turn + # def get_turn_timeout(self): + # return self.game_constants.turn_timeout # returns the time left for turn (miliseconds) def get_remaining_time(self): return self.get_turn_timeout() - self.get_time_past() # returns the health point remaining for each player - def get_player_hp(self, player_id): - player = self.get_player_by_id(player_id) - return player.king.hp + # def get_player_hp(self, player_id): + # player = self.get_player_by_id(player_id) + # 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, index, spell=None, spell_id=None): @@ -511,15 +507,15 @@ def _is_unit_targeted(self, unit, spell_target): return True return False - def get_cast_spells_on_unit(self, unit=None, unit_id=None): - ls = [] - if unit_id is None: - unit_id = unit.unit_id - for cast_spell in self.cast_spells: - if cast_spell is isinstance(CastUnitSpell): - if unit_id == cast_spell.unit_id: - ls.append(unit_id) - return ls + # def get_cast_spells_on_unit(self, unit=None, unit_id=None): + # ls = [] + # if unit_id is None: + # unit_id = unit.unit_id + # for cast_spell in self.cast_spells: + # if cast_spell is isinstance(CastUnitSpell): + # if unit_id == cast_spell.unit_id: + # ls.append(unit_id) + # return ls # every once in a while you can upgrade, this returns the remaining time for upgrade def get_remaining_turns_to_upgrade(self): @@ -530,14 +526,14 @@ def get_remaining_turns_to_get_spell(self): return self.game_constants.turns_to_spell # returns area spells that are casted in last turn and returns other players spells also - def get_cast_area_spell(self, player_id): - return [cast_spell_i for cast_spell_i in self.cast_spells - if cast_spell_i.caster_id and isinstance(cast_spell_i, CastAreaSpell)] - - # returns unit spells that are casted in last turn and returns other players spells also - def get_cast_unit_spell(self, player_id): - return [cast_spell_i for cast_spell_i in self.cast_spells - if cast_spell_i.caster_id and isinstance(cast_spell_i, CastUnitSpell)] + # def get_cast_area_spell(self, player_id): + # return [cast_spell_i for cast_spell_i in self.cast_spells + # if cast_spell_i.caster_id and isinstance(cast_spell_i, CastAreaSpell)] + # + # # returns unit spells that are casted in last turn and returns other players spells also + # def get_cast_unit_spell(self, player_id): + # return [cast_spell_i for cast_spell_i in self.cast_spells + # if cast_spell_i.caster_id and isinstance(cast_spell_i, CastUnitSpell)] # returns a list of spells casted on a cell def get_range_upgrade_number(self, player_id): @@ -597,12 +593,12 @@ def upgrade_unit_damage(self, unit=None, unit_id=None): "unitId": unit_id })) - def get_player_duplicate_unit(self, player_id): - unit_list = [] - for u in self.get_player_by_id(player_id).units: - if u.is_clone: - unit_list.append(u) - return unit_list + # def get_player_duplicate_unit(self, player_id): + # unit_list = [] + # for u in self.get_player_by_id(player_id).units: + # if u.is_clone: + # unit_list.append(u) + # return unit_list def get_player_hasted_units(self, player_id): return [unit for unit in self.get_player_by_id(player_id=player_id).units if unit.is_hasted > 0] @@ -610,46 +606,46 @@ def get_player_hasted_units(self, player_id): def get_player_played_units(self, player_id): return [unit for unit in self.get_player_by_id(player_id=player_id).units if unit.was_played_this_turn] - def get_unit_target(self, unit=None, unit_id=None): - if unit_id is None: - if unit is None: - return None - unit_id = unit.unit_id - - target_id = self.get_unit_by_id(unit_id).target_id - unit = self.get_unit_by_id(target_id) - return unit - - def get_unit_target_cell(self, unit=None, unit_id=None): - if unit_id is None: - if unit is None: - return None - unit_id = unit.unit_id - - target_id = self.get_unit_by_id(unit_id).target_id - cell = self.get_unit_by_id(unit_id).target_cell - unit = self.get_unit_by_id(target_id) - if unit is None: - return None - - return cell - - def get_king_target(self, player_id): - king = self.get_player_by_id(player_id).king - return self.get_unit_by_id(king.target_id) - - def get_king_target_cell(self, player_id): - king = self.get_player_by_id(player_id).king - return king.target_cell - - def get_king_unit_is_attacking_to(self, unit=None, unit_id=None): - if unit is not None: - unit_id = unit.unit_id - unit = self.get_unit_by_id(unit_id) - for p in self.players: - if unit.target_id == p.player_id: - return p.player_id - return -1 + # def get_unit_target(self, unit=None, unit_id=None): + # if unit_id is None: + # if unit is None: + # return None + # unit_id = unit.unit_id + # + # target_id = self.get_unit_by_id(unit_id).target_id + # unit = self.get_unit_by_id(target_id) + # return unit + # + # def get_unit_target_cell(self, unit=None, unit_id=None): + # if unit_id is None: + # if unit is None: + # return None + # unit_id = unit.unit_id + # + # target_id = self.get_unit_by_id(unit_id).target_id + # cell = self.get_unit_by_id(unit_id).target_cell + # unit = self.get_unit_by_id(target_id) + # if unit is None: + # return None + # + # return cell + # + # def get_king_target(self, player_id): + # king = self.get_player_by_id(player_id).king + # return self.get_unit_by_id(king.target_id) + # + # def get_king_target_cell(self, player_id): + # king = self.get_player_by_id(player_id).king + # return king.target_cell + # + # def get_king_unit_is_attacking_to(self, unit=None, unit_id=None): + # if unit is not None: + # unit_id = unit.unit_id + # unit = self.get_unit_by_id(unit_id) + # for p in self.players: + # if unit.target_id == p.player_id: + # return p.player_id + # return -1 def get_all_base_unit(self): return copy.deepcopy(self.base_units) @@ -670,23 +666,26 @@ def get_base_unit_by_id(self, type_id): return bu return None - def get_player_died_units(self, player_id): - return self.get_player_by_id(player_id).dead_units - - def has_player_used_ranged_upgrade(self, player_id): - for u in self.get_player_by_id(player_id).dead_units: - if u.was_range_upgraded: - return True - for u in self.get_player_by_id(player_id).units: - if u.was_range_upgraded: - return True - return False - - def has_player_used_damage_upgrade(self, player_id): - for u in self.get_player_by_id(player_id).dead_units: - if u.was_damage_upgraded: - return True - for u in self.get_player_by_id(player_id).units: - if u.was_damage_upgraded: - return True - return False + # def get_player_died_units(self, player_id): + # return self.get_player_by_id(player_id).dead_units + # + # def has_player_used_ranged_upgrade(self, player_id): + # for u in self.get_player_by_id(player_id).dead_units: + # if u.was_range_upgraded: + # return True + # for u in self.get_player_by_id(player_id).units: + # if u.was_range_upgraded: + # return True + # return False + # + # def has_player_used_damage_upgrade(self, player_id): + # for u in self.get_player_by_id(player_id).dead_units: + # if u.was_damage_upgraded: + # return True + # for u in self.get_player_by_id(player_id).units: + # if u.was_damage_upgraded: + # return True + # return False + + def get_game_constants(self): + return self.game_constants From dd2f585c0e84a4598c2e0a937b2b53d39bf6206d Mon Sep 17 00:00:00 2001 From: ErfanFaravani Date: Mon, 3 Feb 2020 08:48:14 +0330 Subject: [PATCH 2/3] changes to model.py base on clientv5.1 doc --- model.py | 83 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 25 deletions(-) diff --git a/model.py b/model.py index 8d8e948..47ba343 100644 --- a/model.py +++ b/model.py @@ -2,9 +2,9 @@ class Map: - def __init__(self, row_count, column_count, paths, kings, cells): - self.row_count = row_count - self.column_count = column_count + def __init__(self, row_num, column_num, paths, kings, cells): + self.row_num = row_num + self.column_num = column_num self.paths = paths self.units = [] self.kings = kings @@ -31,14 +31,31 @@ def add_unit_in_cell(self, row, column, unit): class Player: def __init__(self, player_id, king): self.player_id = player_id + self.king = king self.deck = None self.hand = [] - self.spells = [] self.ap = 0 + self.paths_from_player = [] + self.path_to_friend = None + self.cast_area_spell = None + self.cast_unit_spell = None + self.duplicate_units = None + self.hasted_units = None + self.units = [] # alive units + self.played_units = [] # units that played last turn + self.died_units = [] + self.range_upgraded_unit = None # unit that last turn the player upgraded range of it + self.damage_upgraded_unit = None # unit that last turn the player upgraded damage of it + + # deleted fields + self.spells = [] self.upgrade_tokens = 0 - self.king = king - self.units = [] - self.dead_units = [] + + def is_alive(self): + return self.king.is_alive + + def get_hp(self): + return self.king.hp def __str__(self): return "".format(self.path_id, ["({}, {})".format(cell.row, cell.col) for cell in self.cells]) + "cells: {}>".format(self.id, ["({}, {})".format(cell.row, cell.col) for cell in self.cells]) class Deck: @@ -162,23 +190,28 @@ def __init__(self): class BaseUnit: - def __init__(self, type_id, max_hp, base_attack, base_range, target, is_flying, is_multiple): + def __init__(self, type_id, max_hp, base_attack, base_range, target_type, is_flying, is_multiple): self.type_id = type_id self.max_hp = max_hp + self.ap = 0 + self.attack = 0 self.base_attack = base_attack self.base_range = base_range - self.target = target self.is_flying = is_flying self.is_multiple = is_multiple + self.target_type = target_type class King: - def __init__(self, target_id, center=None, hp=0, attack=0, range=0): + def __init__(self, target, center=None, hp=0, attack=0, range=0): self.center = center self.hp = hp self.attack = attack self.range = range - self.target_id = target_id + self.target = target + self.target_cell = None + self.is_alive = True + self.player_id = 0 class Message: From 617ba13e1af5e9fc18f3071ddb0bbd30f267a3b1 Mon Sep 17 00:00:00 2001 From: ErfanFaravani Date: Mon, 3 Feb 2020 08:55:45 +0330 Subject: [PATCH 3/3] changes to world.py base on clientv5.1 doc --- world.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/world.py b/world.py index d7c1b0e..905fd52 100644 --- a/world.py +++ b/world.py @@ -160,6 +160,10 @@ def _handle_turn_units(self, msg, is_dead_unit=False): 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: + tc = Cell(row=unit_msg["targetCell"]["row"], col=unit_msg["targetCell"]["col"]) + else: + tc = 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"]), @@ -168,14 +172,16 @@ def _handle_turn_units(self, msg, is_dead_unit=False): range_level=unit_msg["rangeLevel"], was_damage_upgraded=unit_msg["wasDamageUpgraded"], was_range_upgraded=unit_msg["wasRangeUpgraded"], - is_hasted=unit_msg("isHasted"), - is_clone=unit_msg("isClone"), - active_poisons=unit_msg["activePoisons"], - range=unit_msg("range"), - attack=unit_msg("attack"), - was_played_this_turn=unit_msg("wasPlayedThisTurn"), - target_id=unit_msg["target"], - target_cell=Cell(row=unit_msg["targetCell"]["row"], col=unit_msg["targetCell"]["col"])) + is_hasted=unit_msg["isHasted"], + is_clone=unit_msg["isDuplicate"], + # is_clone=unit_msg.keys().isdisjoint("isClone") and unit_msg["isClone"], + # active_poisons=unit_msg["activePoisons"], + # active_poisons=unit_msg.keys().isdisjoint("activePoisons") and unit_msg["activePoisons"], + range=unit_msg["range"], + attack=unit_msg["attack"], + was_played_this_turn=unit_msg["wasPlayedThisTurn"], + target=unit_msg["target"], + target_cell=tc) if not is_dead_unit: self.map.add_unit_in_cell(unit.cell.row, unit.cell.col, unit) player.units.append(unit) @@ -689,3 +695,6 @@ def get_base_unit_by_id(self, type_id): def get_game_constants(self): return self.game_constants + + def get_paths_from_player(self, player_id): + pass