def update(self, game_state_line): """Update the game state. Gets called from Game.""" line = game_state_line.split("#")[0] tokens = line.split() if len(tokens) < 5: # Garbage - ignore return if tokens[0] == "P": if len(tokens) != 6: raise ParsingException("Invalid format in gamestate: '%s'" % (game_state_line, )) id = _make_id(*tokens[1:3]) if id in self.planet_id_map: self._update_planet(id, tokens[3:5]) else: new_planet = self.planet_class(self, self.planet_id, *tokens[1:]) self._planets[self.planet_id] = new_planet self.planet_id_map[id] = self.planet_id self._cache['p']['o'][new_planet.owner].add(new_planet) self._cache['p']['g'][new_planet.growth_rate].add(new_planet) self.planet_id += 1 elif tokens[0] == "F": if len(tokens) != 7: raise ParsingException("Invalid format in gamestate: '%s'" % (game_state_line, )) self._add_fleet(*tokens[1:])
def turn_done(self): toPrint = False _fleets = {} for id, fleet in self._fleets.items(): fleet.turns_remaining -= 1 new_id = _make_id(fleet.owner.id, fleet.ship_count, fleet.source.id, fleet.destination.id, fleet.trip_length, fleet.turns_remaining) if fleet.turns_remaining == 0: self._cache['f']['o'][fleet.owner].remove(fleet) self._cache['f']['s'][fleet.source].remove(fleet) self._cache['f']['d'][fleet.destination].remove(fleet) else: _fleets[new_id] = fleet self._fleets = _fleets self._old_enemy_fleets = Fleets(self.enemy_fleets) _attacks = {} for id, attack in self._attacks.items(): attack.turns_to_wait -= 1 attack.turns_remaining = int(attack.trip_length) + int(attack.turns_to_wait) if attack.turns_to_wait < 0: self._cache['a']['o'][attack.owner].remove(attack) self._cache['a']['s'][attack.source].remove(attack) self._cache['a']['d'][attack.destination].remove(attack) else: _attacks[id] = attack self._attacks = _attacks
def turn_done(self): toPrint = False _fleets = {} for id, fleet in self._fleets.items(): fleet.turns_remaining -= 1 new_id = _make_id(fleet.owner.id, fleet.ship_count, fleet.source.id, fleet.destination.id, fleet.trip_length, fleet.turns_remaining) if fleet.turns_remaining == 0: self._cache['f']['o'][fleet.owner].remove(fleet) self._cache['f']['s'][fleet.source].remove(fleet) self._cache['f']['d'][fleet.destination].remove(fleet) else: _fleets[new_id] = fleet self._fleets = _fleets self._old_enemy_fleets = Fleets(self.enemy_fleets) _attacks = {} for id, attack in self._attacks.items(): attack.turns_to_wait -= 1 attack.turns_remaining = int(attack.trip_length) + int( attack.turns_to_wait) if attack.turns_to_wait < 0: self._cache['a']['o'][attack.owner].remove(attack) self._cache['a']['s'][attack.source].remove(attack) self._cache['a']['d'][attack.destination].remove(attack) else: _attacks[id] = attack self._attacks = _attacks
def _add_fleet(self, *args): id = _make_id(*args) if id in self._fleets: # (fleets already updated by turn_done) return self._fleets[id] else: new_fleet = self.fleet_class(self, id, *args) self._fleets[id] = new_fleet self._cache['f']['o'][new_fleet.owner].add(new_fleet) self._cache['f']['s'][new_fleet.source].add(new_fleet) self._cache['f']['d'][new_fleet.destination].add(new_fleet) return new_fleet
def turn_done(self): _fleets = {} for id, fleet in self._fleets.items(): fleet.turns_remaining -= 1 # Ugh.. Since fleets have no id in the engine we have to make sure they match next turn new_id = _make_id(fleet.owner.id, fleet.ship_count, fleet.source.id, fleet.destination.id, fleet.trip_length, fleet.turns_remaining) if fleet.turns_remaining == 0: self._cache['f']['o'][fleet.owner].remove(fleet) self._cache['f']['s'][fleet.source].remove(fleet) self._cache['f']['d'][fleet.destination].remove(fleet) else: _fleets[new_id] = fleet self._fleets = _fleets
def update(self, game_state_line): """Update the game state. Gets called from Game.""" line = game_state_line.split("#")[0] tokens = line.split() if len(tokens) < 5: # Garbage - ignore return if tokens[0] == "P": if len(tokens) != 6: raise ParsingException("Invalid format in gamestate: '%s'" % (game_state_line,)) id = _make_id(*tokens[1:3]) if id in self.planet_id_map: self._update_planet(id, tokens[3:5]) else: new_planet = self.planet_class(self, self.planet_id, *tokens[1:]) self._planets[self.planet_id] = new_planet self.planet_id_map[id] = self.planet_id self._cache['p']['o'][new_planet.owner].add(new_planet) self._cache['p']['g'][new_planet.growth_rate].add(new_planet) self.planet_id += 1 elif tokens[0] == "F": if len(tokens) != 7: raise ParsingException("Invalid format in gamestate: '%s'" % (game_state_line,)) self._add_fleet(*tokens[1:])