예제 #1
0
def update_game(game_id):
    """Update the specified game (make a move or end a game)."""
    game = Game(game_id)
    game.load_game()
    if request.json.get("game_status") == Game.DISCONNECTED:
        game.game_over(won=False)
        return {"message": "OK"}, status.HTTP_200_OK, RESPONSE_HEADERS

    column = request.json["column"]
    name = request.json["name"]
    move_result = game.move(name, column)

    if move_result is None:
        message = "Bad request, column full."
        status_code = status.HTTP_400_BAD_REQUEST
    elif move_result is True:
        message = Game.WON
        status_code = status.HTTP_200_OK
    else:
        message = "OK"
        status_code = status.HTTP_200_OK

    response_data = {"message": message}
    response_data.update(game.game)
    response_body = json.dumps(response_data, cls=DecimalEncoder)
    return response_body, status_code, RESPONSE_HEADERS
예제 #2
0
 def test_game_over_disconnected(self, mock_db):
     game = Game("1")
     game.game = {}
     game.game_over(won=False)
     mock_db.save_game.assert_called_once_with(
         "1", {"game_status": "disconnected"})
예제 #3
0
 def test_game_over_won(self, mock_db):
     game = Game("1")
     game.game = {}
     game.game_over()
     mock_db.save_game.assert_called_once_with("1", {"game_status": "won"})