コード例 #1
0
def test_should_broadcast_game_won_when_someone_wins():
    controller = GameController()

    controller.handle(HelloServerMsg(0, 'Piotr'), 0)
    controller.handle(HelloServerMsg(1, 'Marta'), 1)
    controller.handle(WantToJoinMsg(0), 0)
    controller.handle(WantToJoinMsg(1), 1)
    controller.handle(StartGameMsg(0), 0)

    controller.game.board.move(Turtle('YELLOW'), 8)

    actual = controller.handle(PlayCardMsg(0, 28, None), 0)

    expected_msgs = [
        MsgToSend(0,
                  message='game won',
                  winner_name='Marta',
                  sorted_list_of_player_places=['Marta', 'Piotr'],
                  sorted_list_of_players_turtle_colors=['YELLOW', 'GREEN']),
        MsgToSend(1,
                  message='game won',
                  winner_name='Marta',
                  sorted_list_of_player_places=['Marta', 'Piotr'],
                  sorted_list_of_players_turtle_colors=['YELLOW', 'GREEN'])
    ]

    for expected_msg in expected_msgs:
        assert expected_msg in actual
コード例 #2
0
def test_deserialize_should_deserialize_start_game_msg():
    deserializer = MessageDeserializer()
    msg_json = json.dumps({'message': 'start the game', 'player_id': 0})

    actual = deserializer.deserialize(msg_json)

    assert actual == StartGameMsg(0)
コード例 #3
0
def test_raises_when_player_tries_to_pose_as_somebody_else_on_start_game():
    controller = GameController()

    controller.handle(HelloServerMsg(0, 'Piotr'), 0)
    controller.handle(HelloServerMsg(1, 'Marta'), 1)
    controller.handle(WantToJoinMsg(0), 0)
    controller.handle(WantToJoinMsg(1), 1)

    with pytest.raises(ValueError):
        controller.handle(StartGameMsg(0), 1)
コード例 #4
0
def test_should_raise_when_not_first_player_tries_to_start_the_game():
    controller = GameController()

    controller.handle(HelloServerMsg(0, 'Piotr'), 0)
    controller.handle(HelloServerMsg(1, 'Marta'), 1)
    controller.handle(WantToJoinMsg(0), 0)
    controller.handle(WantToJoinMsg(1), 1)

    with pytest.raises(ValueError):
        controller.handle(StartGameMsg(1), 1)
コード例 #5
0
def test_raises_when_player_tries_to_pose_as_sb_else_on_ready_to_receive():
    controller = GameController()

    controller.handle(HelloServerMsg(0, 'Piotr'), 0)
    controller.handle(HelloServerMsg(1, 'Marta'), 1)
    controller.handle(WantToJoinMsg(0), 0)
    controller.handle(WantToJoinMsg(1), 1)
    controller.handle(StartGameMsg(0), 0)

    with pytest.raises(ValueError):
        controller.handle(ReadyToReceiveGameState(0), 1)
コード例 #6
0
def test_should_rasie_when_player_tries_to_join_to_ongoing_game():
    controller = GameController()

    controller.handle(HelloServerMsg(0, 'Piotr'), 0)
    controller.handle(HelloServerMsg(1, 'Marta'), 1)
    controller.handle(WantToJoinMsg(0), 0)
    controller.handle(WantToJoinMsg(1), 1)
    controller.handle(StartGameMsg(0), 0)
    controller.handle(HelloServerMsg(2, 'Other'), 2)

    with pytest.raises(ValueError):
        controller.handle(WantToJoinMsg(2), 2)
コード例 #7
0
def test_should_emit_ongoing_when_new_player_connects_after_game_started():
    controller = GameController()

    controller.handle(HelloServerMsg(0, 'Piotr'), 0)
    controller.handle(HelloServerMsg(1, 'Marta'), 1)
    controller.handle(WantToJoinMsg(0), 0)
    controller.handle(WantToJoinMsg(1), 1)
    controller.handle(StartGameMsg(0), 0)

    actual = controller.handle(HelloServerMsg(2, 'Other'), 2)

    assert actual == MsgToSend(2,
                               message='hello client',
                               status='ongoing',
                               list_of_players_in_room=['Piotr', 'Marta'])
コード例 #8
0
def test_should_emit_full_game_state_on_ready_to_receive_game_state():
    controller = GameController()

    controller.handle(HelloServerMsg(0, 'Piotr'), 0)
    controller.handle(HelloServerMsg(1, 'Marta'), 1)
    controller.handle(WantToJoinMsg(0), 0)
    controller.handle(WantToJoinMsg(1), 1)
    controller.handle(StartGameMsg(0), 0)

    actual = controller.handle(ReadyToReceiveGameState(0), 0)

    expected = MsgToSend(0,
                         message='full game state',
                         board={
                             'turtles_in_game_positions':
                             [[] for _ in range(9)],
                             'turtles_on_start_positions': [['RED'], ['GREEN'],
                                                            ['BLUE'],
                                                            ['PURPLE'],
                                                            ['YELLOW']],
                         },
                         players_names=['Piotr', 'Marta'],
                         active_player_idx=0,
                         player_cards=[{
                             "card_id": 28,
                             "color": "YELLOW",
                             "action": "PLUS"
                         }, {
                             "card_id": 12,
                             "color": "RED",
                             "action": "PLUS"
                         }, {
                             "card_id": 45,
                             "color": "RAINBOW",
                             "action": "MINUS"
                         }, {
                             "card_id": 41,
                             "color": "RAINBOW",
                             "action": "PLUS"
                         }, {
                             "card_id": 38,
                             "color": "PURPLE",
                             "action": "MINUS"
                         }],
                         player_turtle_color='GREEN',
                         recently_played_card=None)

    assert actual == expected
コード例 #9
0
def test_should_broadcast_game_ready_to_start_to_all_in_room_after_start():
    controller = GameController()

    controller.handle(HelloServerMsg(0, 'Piotr'), 0)
    controller.handle(HelloServerMsg(1, 'Marta'), 1)
    controller.handle(WantToJoinMsg(0), 0)
    controller.handle(WantToJoinMsg(1), 1)

    actual = controller.handle(StartGameMsg(0), 0)

    expected = [
        MsgToSend(0, message='game ready to start', player_idx=0),
        MsgToSend(1, message='game ready to start', player_idx=1)
    ]

    for msg in expected:
        assert msg in actual
コード例 #10
0
def test_should_broadcast_ongoing_to_all_outside_the_room_after_game_start():
    controller = GameController()

    controller.handle(HelloServerMsg(0, 'Piotr'), 0)
    controller.handle(HelloServerMsg(1, 'Marta'), 1)
    controller.handle(HelloServerMsg(2, 'Other'), 2)
    controller.handle(WantToJoinMsg(0), 0)
    controller.handle(WantToJoinMsg(1), 1)

    actual = controller.handle(StartGameMsg(0), 0)

    expected_msg = MsgToSend(2,
                             message='hello client',
                             status='ongoing',
                             list_of_players_in_room=['Piotr', 'Marta'])

    assert expected_msg in actual
コード例 #11
0
def test_should_emit_can_resume_when_player_reconnects_during_the_game():
    controller = GameController()

    controller.handle(HelloServerMsg(0, 'Piotr'), 0)
    controller.handle(WantToJoinMsg(0), 0)
    controller.handle(HelloServerMsg(1, 'Marta'), 1)
    controller.handle(WantToJoinMsg(1), 1)
    controller.handle(StartGameMsg(0), 0)

    controller.disconnected(0)

    actual = controller.handle(HelloServerMsg(0, 'Piotr'), 1)

    assert actual == MsgToSend(1,
                               message='hello client',
                               status='can resume',
                               list_of_players_in_room=['Piotr', 'Marta'])
コード例 #12
0
def test_should_broadcast_game_state_update_after_player_moved():
    controller = GameController()

    controller.handle(HelloServerMsg(0, 'Piotr'), 0)
    controller.handle(HelloServerMsg(1, 'Marta'), 1)
    controller.handle(WantToJoinMsg(0), 0)
    controller.handle(WantToJoinMsg(1), 1)
    controller.handle(StartGameMsg(0), 0)

    actual = controller.handle(PlayCardMsg(0, 28, None), 0)

    expected_msgs = [
        MsgToSend(0,
                  message='game state updated',
                  board={
                      'turtles_in_game_positions':
                      [['YELLOW']] + [[] for _ in range(8)],
                      'turtles_on_start_positions': [['RED'], ['GREEN'],
                                                     ['BLUE'], ['PURPLE']],
                  },
                  active_player_idx=1,
                  recently_played_card={
                      "card_id": 28,
                      "color": "YELLOW",
                      "action": "PLUS"
                  }),
        MsgToSend(1,
                  message='game state updated',
                  board={
                      'turtles_in_game_positions':
                      [['YELLOW']] + [[] for _ in range(8)],
                      'turtles_on_start_positions': [['RED'], ['GREEN'],
                                                     ['BLUE'], ['PURPLE']],
                  },
                  active_player_idx=1,
                  recently_played_card={
                      "card_id": 28,
                      "color": "YELLOW",
                      "action": "PLUS"
                  }),
    ]

    for expected_msg in expected_msgs:
        assert expected_msg in actual
コード例 #13
0
def test_should_emit_room_updated_when_player_plays_again_after_won():
    controller = GameController()

    controller.handle(HelloServerMsg(0, 'Piotr'), 0)
    controller.handle(HelloServerMsg(1, 'Marta'), 1)
    controller.handle(WantToJoinMsg(0), 0)
    controller.handle(WantToJoinMsg(1), 1)
    controller.handle(StartGameMsg(0), 0)

    controller.game.board.move(Turtle('YELLOW'), 8)

    controller.handle(PlayCardMsg(0, 28, None), 0)

    actual = controller.handle(WantToJoinMsg(0), 0)

    expected = MsgToSend(0,
                         message='room update',
                         list_of_players_in_room=['Piotr'])

    assert expected in actual
コード例 #14
0
def test_should_emit_player_cards_updated_to_player_who_played_the_card():
    controller = GameController()

    controller.handle(HelloServerMsg(0, 'Piotr'), 0)
    controller.handle(HelloServerMsg(1, 'Marta'), 1)
    controller.handle(WantToJoinMsg(0), 0)
    controller.handle(WantToJoinMsg(1), 1)
    controller.handle(StartGameMsg(0), 0)

    actual = controller.handle(PlayCardMsg(0, 28, None), 0)

    expected = MsgToSend(0,
                         message='player cards updated',
                         player_cards=[{
                             "card_id": 33,
                             "color": "PURPLE",
                             "action": "PLUS"
                         }, {
                             "card_id": 12,
                             "color": "RED",
                             "action": "PLUS"
                         }, {
                             "card_id": 45,
                             "color": "RAINBOW",
                             "action": "MINUS"
                         }, {
                             "card_id": 41,
                             "color": "RAINBOW",
                             "action": "PLUS"
                         }, {
                             "card_id": 38,
                             "color": "PURPLE",
                             "action": "MINUS"
                         }])

    assert expected in actual
コード例 #15
0
def test_should_raise_when_player_who_is_not_in_room_tries_to_start_the_game():
    controller = GameController()

    controller.handle(HelloServerMsg(0, 'Piotr'), 0)
    with pytest.raises(ValueError):
        controller.handle(StartGameMsg(0), 0)