def sync_selected_unit(self): unit = self.battle.selected_unit if unit: actions = get_available_actions(unit) action_list = [{ 'x': cell.x, 'y': cell.y, 'type': action_type } for cell, action_type in actions.items()] self.shared_commands.append( Command( 'update-selected-unit', { 'actions': action_list, 'briefInfo': { 'atkMin': prototypes[unit.type]['atk']['min'], 'atkMax': prototypes[unit.type]['atk']['max'], 'def': prototypes[unit.type]['def'], 'extraDef': get_cell_defence_bonus(unit), 'level': unit.level, }, 'x': unit.x, 'y': unit.y, })) else: self.shared_commands.append(Command('clear-selected-unit', {}))
def handle_click_on_cell(self, x: int, y: int): cell = Cell(x, y) if self.battle.selected_unit: actions = get_available_actions(self.battle.selected_unit) else: actions = {} if cell in actions.keys(): action = actions[cell] print(action) self.clear_selected_unit_actions() if action == 'move': self.move_unit(self.battle.selected_unit, cell) elif action == 'fix-building': self.fix_building(self.battle.selected_unit) elif action == 'occupy-building': self.occupy_building(self.battle.selected_unit) elif action == 'attack-unit': unit = Unit.query.filter_by(battle_id=self.battle.id, x=x, y=y).one() self.attack_unit(self.battle.selected_unit, unit) self.sync_units_active() return self unit = Unit.query.filter_by( battle_id=self.battle.id, x=x, y=y, owner=self.battle.active_player).one_or_none() if unit: self.select_unit(unit) return self store = Building.query.filter_by( battle_id=self.battle.id, x=x, y=y, type='castle', owner=self.battle.active_player).one_or_none() if store: self.commands.append( Command( 'open-store', { 'storeCell': { 'x': store.x, 'y': store.y }, 'items': get_units_to_buy(self.battle) })) return self self.clear_selected_unit() return self
def sync_units_active(self): units = self.battle.active_player.units.all() self.shared_commands.append( Command( 'update-units', { 'units': [{ 'id': unit.id, 'changes': { 'active': len(get_available_actions(unit)) } } for unit in units] }))
def add_unit(unit: Unit): return Command( 'add-unit', { 'unit': { 'id': unit.id, 'x': unit.x, 'y': unit.y, 'type': unit.type, 'color': unit.owner.color if unit.owner else None, 'level': unit.level, 'health': unit.health, 'state': 'waiting', 'active': len(get_available_actions(unit)) } })
def collect_battle_data(self): buildings = self.battle.buildings.outerjoin(Building.owner) self.commands.append( Command( 'update-map', { 'width': self.battle.map_width, 'height': self.battle.map_height, 'terrain': { '{0},{1}'.format(str(t.x), str(t.y)): t.type for t in self.battle.terrain }, 'buildings': [{ 'x': b.x, 'y': b.y, 'type': b.type } for b in buildings] })) self.commands.append( Command( 'update-status', { 'color': self.battle.active_player.color, 'unitCount': self.battle.active_player.unit_count, 'unitLimit': self.battle.active_player.unit_limit, 'money': self.battle.active_player.money, 'winnerTeam': self.battle.winner_team, })) self.commands.append( Command( 'add-buildings', { 'buildings': [{ 'id': b.id, 'x': b.x, 'y': b.y, 'type': b.type, 'state': b.state, 'color': b.owner.color if b.owner else None, } for b in buildings] })) graves = self.battle.graves self.commands.append( Command( 'add-graves', {'graves': [{ 'id': b.id, 'x': b.x, 'y': b.y } for b in graves]})) units = self.battle.units.outerjoin(Unit.owner) self.commands.append( Command( 'add-units', { 'units': [{ 'id': b.id, 'x': b.x, 'y': b.y, 'type': b.type, 'color': b.owner.color if b.owner else None, 'level': b.level, 'health': b.health, 'state': 'waiting', 'active': len(get_available_actions(b)) } for b in units] })) self.sync_selected_unit() return self