def test_simple_game(self): level = load_level("levels/level1.json") game_room = GameRoom([level], 2, self._send_func) game_room.add_player(1) game_room.add_player(2) game_room.handle_client_message(1, json.dumps({"action": "start"})) for i in xrange(len(level.edges)): game_room.handle_client_message(1, json.dumps({"action": "fill", "data": {"edge_id": i}}))
def register(self, client): """Register a WebSocket connection for the game.""" if self.pickup_game_room is None or self.pickup_game_room.did_room_start: app.logger.info("Creating new room") self.next_room_id += 1 print "Creating new room", self.next_room_id patterns = [load_level(fn) for fn in get_level_names()] self.pickup_game_room = GameRoom(patterns, 2, self.send, room_id=self.next_room_id) self.game_rooms.append(self.pickup_game_room) self.pickup_game_room.add_player(client) self.clients_to_rooms[client] = self.pickup_game_room
def load(self, botv, *, session=False): if session: # CODE TO RESUME SESSION WILL GO HERE -- I can help with this if necessary pass else: self.players = {} self.accessroles = list(self.botv.accessroles) self.itemroles = list(self.botv.itemroles) self.rooms = {} for room in self.botv.rooms: self.rooms[room['id']] = GameRoom(room) self.server = botv.server
class GameBackend(object): """Interface for registering and updating WebSocket clients.""" def __init__(self): self.message_queue = list() self.pickup_game_room = None """:type GameRoom""" self.game_rooms = [] self.clients_to_rooms = {} self.next_room_id = 0 def register(self, client): """Register a WebSocket connection for the game.""" if self.pickup_game_room is None or self.pickup_game_room.did_room_start: app.logger.info("Creating new room") self.next_room_id += 1 print "Creating new room", self.next_room_id patterns = [load_level(fn) for fn in get_level_names()] self.pickup_game_room = GameRoom(patterns, 2, self.send, room_id=self.next_room_id) self.game_rooms.append(self.pickup_game_room) self.pickup_game_room.add_player(client) self.clients_to_rooms[client] = self.pickup_game_room def unregister(self, client): if client in self.clients_to_rooms: self.clients_to_rooms[client].remove_player(client) self.clients_to_rooms.pop(client) def _send_impl(self, client, data): """Send given data to the registered client. Automatically discards invalid connections.""" try: client.send(data) except Exception, e: app.logger.info("Client disconnected: " + e.message) self.unregister(client)
def test_lose_level(self): level = load_level("levels/level1.json") game_room = GameRoom([level], 2, self._send_func, 2, 3) game_room.add_player(1) game_room.add_player(2) game_room.handle_client_message(1, json.dumps({"action": "start"})) game_room.handle_client_message(2, json.dumps({"action": "start_level"})) game_room.update() self.assertTrue(game_room.is_in_level) from time import sleep sleep(3) game_room.update()
def test_multiple_levels(self): levels = [load_level(fn) for fn in get_level_names()] game_room = GameRoom(levels, 2, self._send_func) game_room.add_player(1) game_room.add_player(2) game_room.handle_client_message(1, json.dumps({"action": "start"})) for level in levels: game_room.handle_client_message(2, json.dumps({"action": "start_level"})) for i in xrange(len(level.edges)): game_room.handle_client_message(1, json.dumps({"action": "fill", "data": {"edge_id": i}})) game_room.handle_client_message(2, json.dumps({"action": "start_level"})) self.assertFalse(game_room.is_room_active)
def test_disconnect_before_start(self): level = load_level("levels/level1.json") game_room = GameRoom([level], 2, self._send_func) game_room.add_player(1) game_room.add_player(2) game_room.add_player(3) game_room.remove_player(1) game_room.add_player(4) game_room.handle_client_message(2, json.dumps({"action": "start"})) game_room.handle_client_message(2, json.dumps({"action": "start_level"})) for i in xrange(len(level.edges)): game_room.handle_client_message(4, json.dumps({"action": "fill", "data": {"edge_id": i}}))
class GameLauncher: if __name__ == '__main__': groom = GameRoom() groom.play()