Ejemplo n.º 1
0
def new_game(current_user):
    """
   Start a new game
   swagger_from_file: src/swagger/game_start.yml
   """

    data = request.get_json(silent=True)
    schema = GameSettings()

    try:
        settings = schema.load(data)
    except ValidationError as err:
        return jsonify(err.messages), 400

    rows = settings["rows"]
    columns = settings["columns"]
    mines = settings["mines"]

    service = GameService()
    try:
        service.start_game(current_user.id, rows, columns, mines)
        db.session.add(service.game)
        db.session.commit()
    except InvalidGameSettingsException as exc:
        return jsonify({"message": str(exc)}), 400

    return jsonify(service.encode_game_info())
Ejemplo n.º 2
0
def test_start_game_should_populate_game_object():
    service = GameService()
    service.start_game(1)
    game = service.game
    assert game.columns > 0
    assert game.rows > 0
    assert game.mines_left > 0
    assert game.board != None
    assert game.user_id == 1
Ejemplo n.º 3
0
def test_toggle_uncovered_cell_should_do_nothing():
    service = GameService(get_mock_game())
    service.start_game(1, 10, 10, 20)

    board = service.game.board

    board[0][0]["value"] = 0
    service.clear(0, 0)
    assert board[0][0]["status"] == "U"
    service.toggle(0, 0)
    assert board[0][0]["status"] == "U"
Ejemplo n.º 4
0
def test_mask_board():
    mock_game = get_mock_game()
    service = GameService(mock_game)
    service.start_game(1, 10, 10, 20)
    board = service.game.board
    board[0][0]["status"] = "U"
    board[1][1]["status"] = "F"
    board[2][2]["status"] = "?"

    masked = service._mask_board()

    assert isinstance(masked[0][0], int)
    assert masked[1][1] == "F"
    assert masked[2][2] == "?"
    assert masked[3][3] == "C"
Ejemplo n.º 5
0
def test_toggle_covered_cell():
    service = GameService(get_mock_game())
    service.start_game(1, 10, 10, 20)

    board = service.game.board

    assert board[0][0]["status"] == "C"
    service.toggle(0, 0)
    assert board[0][0]["status"] == "F"
    service.toggle(0, 0)
    assert board[0][0]["status"] == "?"
    service.toggle(0, 0)
    assert board[0][0]["status"] == "C"

    board[0][0]["value"] = 0
    service.clear