def _handle(self, obj, pid): if obj: if isinstance(obj, Connect) and len(self._players) < self._pnum: self._players[pid] = Player(pid, obj.name, PlayerStatus(False, None)) if len(self._players) == self._pnum: self._game = Game(self._players.values()) self._send_all(self._game) elif isinstance(obj, PlayerStatus): self._full_map[pid] = obj.map self._filtered_map[pid] = Map(10, [], [], []) self._game.set_player_status(pid, obj) self._send_all(self._game) elif isinstance(obj, GameStart): if not self._game.started: # TODO: Verify ships positioning self._game.started = True self._send_all(self._game) self._send_current_state() elif isinstance(obj, PlayerAction): if self._game.started and self._turn == pid: log.info("Game turn, verifying...") enemy = 0 if self._turn == 1 else 1 ship = self._full_map[enemy].shoot(obj.position) if ship: # hit log.info('Hit on %s' % obj.position) result = 'hit' self._filtered_map[enemy].hits.append(obj.position) self._scores[pid] += 1 if ship.destroyed: log.info('Ship %s destroyed' % ship) self._filtered_map[enemy].ships.append(ship) else: # miss log.info('Miss on %s' % obj.position) result = 'miss' self._filtered_map[enemy].misses.append(obj.position) self._turn = enemy self._send_current_state(result)
class GameServer(Server): def __init__(self): self._turn = 0 self._full_map = {} self._filtered_map = {} self._scores = defaultdict(int) super(GameServer, self).__init__(2) def _send_current_state(self, result=None): # Send result to player #1 maps = { 0: self._full_map[0], 1: self._filtered_map[1] } self._send(self._clients[0], GameStatus(self._turn, maps, result, self._scores)) # Send result to player #2 maps = { 0: self._filtered_map[0], 1: self._full_map[1] } self._send(self._clients[1], GameStatus(self._turn, maps, result, self._scores)) for i in xrange(2): if self._scores[i] == 30: self._send_all(Result(i)) self.stop() def _handle(self, obj, pid): if obj: if isinstance(obj, Connect) and len(self._players) < self._pnum: self._players[pid] = Player(pid, obj.name, PlayerStatus(False, None)) if len(self._players) == self._pnum: self._game = Game(self._players.values()) self._send_all(self._game) elif isinstance(obj, PlayerStatus): self._full_map[pid] = obj.map self._filtered_map[pid] = Map(10, [], [], []) self._game.set_player_status(pid, obj) self._send_all(self._game) elif isinstance(obj, GameStart): if not self._game.started: # TODO: Verify ships positioning self._game.started = True self._send_all(self._game) self._send_current_state() elif isinstance(obj, PlayerAction): if self._game.started and self._turn == pid: log.info("Game turn, verifying...") enemy = 0 if self._turn == 1 else 1 ship = self._full_map[enemy].shoot(obj.position) if ship: # hit log.info('Hit on %s' % obj.position) result = 'hit' self._filtered_map[enemy].hits.append(obj.position) self._scores[pid] += 1 if ship.destroyed: log.info('Ship %s destroyed' % ship) self._filtered_map[enemy].ships.append(ship) else: # miss log.info('Miss on %s' % obj.position) result = 'miss' self._filtered_map[enemy].misses.append(obj.position) self._turn = enemy self._send_current_state(result)