def add_player_to_game(game_id): game = utils.get_game_from_id(game_id) player_id = request.form['player_id'] player = utils.get_player_from_id(player_id) position = request.get_json(force=True).get('position', None) try: game.add_player(player, position) except: # todo: exc handling LOGGER.error("Error adding player to game: " + sys.exc_info()[0]) return make_response("Error adding player to game: " + jsonify(utils.game_schema.dump(game).data)) try: db.session.commit() except: # todo: exc handling LOGGER.error("Error committing updated player: " + sys.exc_info()[0]) raise try: join_room(room=game_id, namespace=GAME_NAMESPACE, sid=player_id) except Exception: LOGGER.exception("error joining game room") return make_response("Unable to join game", 505) return make_response( "Joined game " + str(utils.game_schema.dump(game).data), 200)
def validate_and_push_move(gameid, move): game = utils.get_game_from_id(gameid) if not game.validate_move(move): raise ValueError('invalid move') game.make_move(move) LOGGER.info("pushed move on board " + str(game.id) + "_" + move.board_code + ": " + move.move.uci()) return True
def on_join(self, data): game_id = data.get('game_id', None) position = data.get('position', None) if game_id is None or position is None: raise KeyError('invalid join game request') try: game = utils.get_game_from_id(game_id) game.add_player(current_user, position) db.session.commit() join_room(room=game_id, sid=current_user.id) leave_room(room='__view') emit('refresh', room='__view') except: # todo: exc handling LOGGER.error("Error adding player to game: " + sys.exc_info()[0]) raise emit('status', {'message': current_user.id + ' has joined at position ' + position, 'status': 'join', 'position': position}, room=game_id)
def view(gameid): LOGGER.info("viewing board") game = utils.get_game_from_id(gameid) return game.board_a.board.fen() + " and " + game.board_b.board.fen()
def finish_game_and_update(game_id): game = utils.get_game_from_id(game_id) game.finish_game(0) db.session.commit() return make_response("ended game", 200)