def new_game(cls, player_id, max_players=4): uuidstr = str(uuid.uuid4()) game = cls(uuid=uuidstr, max_players=int(max_players), creator=player_id) game.players = [str(player_id)] game = game.save() publish('lobby', json.dumps({'type': 'game_created', 'message': game.to_dict()})) return game
def start(self): if self.started: return self self.started = True self.start_time = datetime.datetime.utcnow() publish('lobby', json.dumps({'type': 'game_started', 'message': {}})) publish(self.uuid + '-room', json.dumps({'type': 'game_started', 'message': {}})) return self.save()
def remove_player(self, player_id): """Removes a player from the game""" try: del self.players[self.players.index(player_id)] except KeyError: return self else: game = self.save() publish(self.uuid + '-room', json.dumps({'type': 'players_changed', 'message': {}})) publish('lobby', json.dumps({'type': 'game_changed', 'message': {}})) return game
def start(self): if self.started: return self self.started = True self.start_time = datetime.datetime.utcnow() publish('lobby', json.dumps({'type': 'game_started', 'message': {}})) publish(self.uuid + '-room', json.dumps({ 'type': 'game_started', 'message': {} })) return self.save()
def on_chat(self, message): # validate that user is actually allowed to perform these actions (we # should probably replace this with actual channel auth negotiation at # the namespace level) game_id = message.pop('game_id') if not self.validate(game_id): self.emit('error', {'error': 'unauthorized'}) return # augment the chat message with some additional info and push it into redis message['timestamp'] = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S') + ' UTC' message['name'] = self.request.name channel_id = game_id + '-room' publish(channel_id, json.dumps({'type': 'chat', 'message': message}))
def on_chat(self, message): # validate that user is actually allowed to perform these actions (we # should probably replace this with actual channel auth negotiation at # the namespace level) game_id = message.pop('game_id') if not self.validate(game_id): self.emit('error', {'error': 'unauthorized'}) return # augment the chat message with some additional info and push it into redis message['timestamp'] = datetime.datetime.utcnow().strftime( '%Y-%m-%d %H:%M:%S') + ' UTC' message['name'] = self.request.name channel_id = game_id + '-room' publish(channel_id, json.dumps({'type': 'chat', 'message': message}))
def add_player(self, player_id): """Adds a player to the game""" if len(self.players) >= self.max_players: return self # publish a players changed event to any connected clients publish(self.uuid + '-room', json.dumps({'type': 'players_changed', 'message': {}})) # check if user is already in this game if player_id in self.players: return self # add player to the game and save self.players.append(player_id) game = self.save() publish('lobby', json.dumps({'type': 'game_changed', 'message': {}})) return game
def new_game(cls, player_id, max_players=4): uuidstr = str(uuid.uuid4()) game = cls(uuid=uuidstr, max_players=int(max_players), creator=player_id) game.players = [str(player_id)] game = game.save() publish( 'lobby', json.dumps({ 'type': 'game_created', 'message': game.to_dict() })) return game
def remove(self): publish( 'lobby', json.dumps({ 'type': 'game_deleted', 'message': { 'game_id': self.uuid } })) publish( self.uuid + '-room', json.dumps({ 'type': 'game_deleted', 'message': { 'game_id': self.uuid } })) return self.delete()
def remove_player(self, player_id): """Removes a player from the game""" try: del self.players[self.players.index(player_id)] except KeyError: return self else: game = self.save() publish(self.uuid + '-room', json.dumps({ 'type': 'players_changed', 'message': {} })) publish('lobby', json.dumps({ 'type': 'game_changed', 'message': {} })) return game
def add_player(self, player_id): """Adds a player to the game""" if len(self.players) >= self.max_players: return self # publish a players changed event to any connected clients publish(self.uuid + '-room', json.dumps({ 'type': 'players_changed', 'message': {} })) # check if user is already in this game if player_id in self.players: return self # add player to the game and save self.players.append(player_id) game = self.save() publish('lobby', json.dumps({'type': 'game_changed', 'message': {}})) return game
def remove(self): publish('lobby', json.dumps({'type': 'game_deleted', 'message': {'game_id': self.uuid}})) publish(self.uuid + '-room', json.dumps({'type': 'game_deleted', 'message': {'game_id': self.uuid}})) return self.delete()