def connection(message): print "Connection: ", message['data'], request.sid if message['data']['type'] == 'player': if 'playerId' in message['data']: player = Player.get_by_id(message['data']['playerId']) print "Player", player.name, "reconnected" print "Updating player sid to:", request.sid player.update_sid(request.sid) emit('connection', {'data': { 'objectives complete': player.objectives_complete }}) else: name = message['data']['name'] # Use actual game code when implemented # game = Game.get_by_key(message['data']['game key']) player = game1.add_player(name, request.sid) emit('connection', {'data': {'id': player.id}}) # DEBUG print "Current players: " for p in game1.player_list(): print p.name, print
def disconnect(): player = Player.get_by_id(request.sid) if player: print player.name, "disconnected.", request.sid if player.active: game = Game.get_by_key(player.game_key) print "Deactivating player" game.deactivate_player(player) else: print "Someone disconnected", request.sid
def rank(): player = Player.get_by_id(request.sid) print player.name, "requested its rank." game = Game.get_by_key(player.game_key) rank = game.get_player_rank(player) emit('rank', {'data': { 'rank': rank, 'num players': len(game.player_list()) }})
def rank(message): player = Player.get_by_id(request.sid) print player.name, "completed an objective: ", message game = Game.get_by_key(player.game_key) print "game:", game.name objective = Objective.get_by_id(message['data']['objectiveId']) print "completing: ", objective.name if player.id in objective.players_completed: print "Player ", player.name, "is trying to complete objective", objective.name, "twice!" return time = message['data']['time'] pic_url = message['data']['url'] game.player_complete_objective(player, objective, time, pic_url) print "Process completed objective"
def game_stats(): print "Getting game stats" try: data = json.loads(request.data) print data game_id = data['game_id'] game = Game.get_by_key(game_id) objective_stats = [] for objective in game.objective_list(): players_complete = [{ 'id': player_id, 'name': Player.get_by_id(player_id).name } for player_id in objective.players_completed] objective_stats.append({ 'id': objective.id, 'name': objective.name, 'players_complete': players_complete }) player_stats = [] for player in game.player_list(): player_info = player.serialize() player_info['rank'] = game.get_player_rank(player) player_stats.append(player_info) return jsonify({ 'data': { 'objective_stats': objective_stats, 'player_stats': player_stats }, 'status': 200 }) except: return getErrorJson(sys.exc_info())