def replay_start(): data = json.loads(request.data) history_id = data['historyId'] print 'replay start', data game_history = db_service.get_game_history(history_id) if game_history is None: return json.dumps({ 'success': False, 'message': 'Replay does not exist.', }) # create game and add to game states replay = Replay.from_json_obj(game_history.replay) if replay.players[2].startswith('c'): level = int(replay.players[2][2:]) campaign_level = campaign.get_level(level) game = Game(Speed(replay.speed), replay.players, board=Board.from_str(campaign_level.board), is_campaign=True) else: game = Game(Speed(replay.speed), replay.players) for player in replay.players: game.mark_ready(player) game_id = generate_game_id() game_states[game_id] = GameState(game_id, game, {}, {}, replay) return json.dumps({ 'success': True, 'gameId': game_id, })
def replay_start(): data = json.loads(request.data) history_id = data['historyId'] print 'replay start', data game_history = db_service.get_game_history(history_id) if game_history is None: return json.dumps({ 'success': False, 'message': 'Replay does not exist.', }) # create game and add to game states replay = Replay.from_json_obj(game_history.replay) game = Game(Speed(replay.speed), replay.players) for player in replay.players: game.mark_ready(player) game_id = generate_game_id() game_states[game_id] = GameState(game_id, game, {}, {}, replay) return json.dumps({ 'success': True, 'gameId': game_id, })
def reset(data): data = json.loads(data) game_id = data['gameId'] player_key = data['playerKey'] print 'reset', data if game_id not in game_states: return game_state = game_states[game_id] auth_player = get_auth_player(game_state, player_key) # only authenticated players can reset game if auth_player is not None: old_game = game_state.game game = Game(old_game.speed, old_game.players) for player in game_state.bots: game.mark_ready(player) game_state.game = game emit('resetack', { 'game': game.to_json_obj(), }, room=game_id, json=True)
def new(): data = json.loads(request.data) speed = data['speed'] bots = data.get('bots', {}) bots = {int(player): ai.get_bot(difficulty) for player, difficulty in bots.iteritems()} username = data.get('username') print 'new game', data # generate game ID and player keys game_id = generate_game_id() player_keys = {i: str(uuid.uuid4()) for i in xrange(1, 3) if i not in bots} # if logged in, add current user to game players = {i: 'b:%s' % bot.difficulty for i, bot in bots.iteritems()} if current_user.is_authenticated: players[1] = 'u:%s' % current_user.user_id db_service.update_user_current_game(current_user.user_id, game_id, player_keys[1]) # check opponent if username is not None: user = db_service.get_user_by_username(username) if user is None: return json.dumps({ 'success': False, 'message': 'User to invite does not exist.', }) if user.current_game is not None: return json.dumps({ 'success': False, 'message': 'User to invite is already in a game.', }) players[2] = 'u:%s' % user.user_id db_service.update_user_current_game(user.user_id, game_id, player_keys[2]) socketio.emit('invite', '', room=str(user.user_id)) for i in xrange(1, 3): if i not in players: players[i] = 'o' # create game and add to game states game = Game(Speed(speed), players) for player in bots: game.mark_ready(player) game_states[game_id] = GameState(game_id, game, player_keys, bots) return json.dumps({ 'success': True, 'gameId': game_id, 'playerKeys': player_keys, })
def reset(data): data = json.loads(data) game_id = data['gameId'] player_key = data['playerKey'] print 'reset', data if game_id not in game_states: emit('notfound') return game_state = game_states[game_id] auth_player = get_auth_player(game_state, player_key) # can't reset an in-progress multiplayer game if game_state.level is None and not game_state.game.finished: return # only authenticated players can reset game if auth_player is not None: db_service.remove_active_game(context.SERVER, game_id) board = None if game_state.level is not None: campaign_level = campaign.get_level(game_state.level) board = Board.from_str(campaign_level.board) old_game = game_state.game game = Game(old_game.speed, old_game.players, num_players=old_game.num_players, board=board, is_campaign=old_game.is_campaign, debug=old_game.debug) for player in game_state.bots: game.mark_ready(player) game_state.game = game emit('resetack', { 'game': game.to_json_obj(), }, room=game_id, json=True)
def campaign_start(): data = json.loads(request.data) level = data['level'] print 'campaign start', data if not current_user.is_authenticated: return json.dumps({ 'success': False, 'message': 'User is not logged in.', }) # check if user is already in game user_id = current_user.user_id user = db_service.get_user_by_id(user_id) if user.current_game: game_id = user.current_game['gameId'] if game_id in game_states: game_state = game_states[game_id] if not game_state.game.started or game_state.game.finished: del game_states[game_id] else: return json.dumps({ 'success': False, 'message': 'User already in game.', }) # check that user has access to this level progress = db_service.get_campaign_progress(user_id) belt = level / 8 + 1 if belt > 1 and not progress.belts_completed[str(belt - 1)]: return json.dumps({ 'success': False, 'message': 'User does not have access to this level.', }) # create game and add to game states campaign_level = campaign.get_level(level) players = {1: 'u:%s' % user_id, 2: 'c:%s' % level} game = Game(Speed(campaign_level.speed), players, board=Board.from_str(campaign_level.board), is_campaign=True) game.mark_ready(2) game_id = generate_game_id() player_keys = {1: str(uuid.uuid4())} bots = {2: ai.get_bot('campaign')} game_states[game_id] = GameState(game_id, game, player_keys, bots, level=level) # update user current game db_service.update_user_current_game(user_id, game_id, player_keys[1]) return json.dumps({ 'success': True, 'gameId': game_id, 'playerKeys': player_keys, })