def game_info(): print "Getting game info" try: game_id = request.args.get('game_id') return jsonify({ 'data': Game.get_by_key(game_id).serialize_objectives(), 'status': 200 }) except: return getErrorJson(sys.exc_info())
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 end_game(): print "Ending game" try: data = json.loads(request.data) print data name = data['game_id'] game = Game.get_by_key(game_id) game.end() return jsonify({'status': 200}) except: return getErrorJson(sys.exc_info())
def start_game(): print 'Starting game' try: data = json.loads(request.data) print data name = data['game_id'] game = Game.get_by_key(game_id) game.start() return jsonify({'status': 200}) except: return getErrorJson(sys.exc_info())
def delete_objective(): print "Deleting objective" try: data = json.loads(request.data) print data game_id = data['game_id'] game = Game.get_by_key(game_id) game.delete_objective(data['objective_id']) return jsonify({'status': 200}) except: return getErrorJson(sys.exc_info())
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 add_objective(): print 'Adding objective' try: data = json.loads(request.data) print data game_id = data['game_id'] game = Game.get_by_key(game_id) name = data['objective']['name'] description = data['objective']['description'] location = data['objective']['location'] objective = game.add_objective(location, name, description) return jsonify({'status': 200, 'id': objective.id}) except: return getErrorJson(sys.exc_info())
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())