1
|
|
|
from unittest import mock |
2
|
|
|
import pytest |
3
|
|
|
|
4
|
|
|
from server import GameStatsService, LobbyConnection |
5
|
|
|
from server.games import Game |
6
|
|
|
from server.gameconnection import GameConnection, GameConnectionState |
|
|
|
|
7
|
|
|
from tests import CoroMock |
8
|
|
|
|
9
|
|
|
@pytest.fixture() |
10
|
|
|
def lobbythread(): |
11
|
|
|
return mock.Mock( |
12
|
|
|
sendJSON=lambda obj: None |
13
|
|
|
) |
14
|
|
|
|
15
|
|
View Code Duplication |
|
|
|
|
|
16
|
|
|
@pytest.fixture |
17
|
|
|
def game_connection(request, game, loop, player_service, players, game_service, transport): |
18
|
|
|
from server import GameConnection, LobbyConnection |
|
|
|
|
19
|
|
|
conn = GameConnection(loop=loop, |
20
|
|
|
lobby_connection=mock.create_autospec(LobbyConnection(loop)), |
21
|
|
|
player_service=player_service, |
22
|
|
|
games=game_service) |
23
|
|
|
conn._transport = transport |
|
|
|
|
24
|
|
|
conn.player = players.hosting |
25
|
|
|
conn.game = game |
26
|
|
|
conn.lobby = mock.Mock(spec=LobbyConnection) |
27
|
|
|
|
28
|
|
|
def fin(): |
29
|
|
|
conn.abort() |
30
|
|
|
|
31
|
|
|
request.addfinalizer(fin) |
32
|
|
|
return conn |
33
|
|
|
|
34
|
|
View Code Duplication |
|
|
|
|
|
35
|
|
|
@pytest.fixture |
36
|
|
|
def mock_game_connection(state=GameConnectionState.INITIALIZING, player=None): |
37
|
|
|
gc = mock.create_autospec(spec=GameConnection) |
38
|
|
|
gc.state = state |
39
|
|
|
gc.player = player |
40
|
|
|
return gc |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
@pytest.fixture() |
44
|
|
|
def game_stats_service(): |
45
|
|
|
service = mock.Mock(spec=GameStatsService) |
46
|
|
|
service.process_game_stats = CoroMock() |
47
|
|
|
return service |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
@pytest.fixture |
51
|
|
|
def connections(loop, player_service, game_service, transport, game): |
52
|
|
|
from server import GameConnection |
|
|
|
|
53
|
|
|
|
54
|
|
|
def make_connection(player, connectivity): |
55
|
|
|
lc = LobbyConnection(loop) |
56
|
|
|
lc.protocol = mock.Mock() |
57
|
|
|
conn = GameConnection(loop=loop, |
58
|
|
|
lobby_connection=lc, |
59
|
|
|
player_service=player_service, |
60
|
|
|
games=game_service) |
61
|
|
|
conn.player = player |
62
|
|
|
conn.game = game |
63
|
|
|
conn._transport = transport |
|
|
|
|
64
|
|
|
conn._connectivity_state.set_result(connectivity) |
|
|
|
|
65
|
|
|
return conn |
66
|
|
|
|
67
|
|
|
return mock.Mock( |
68
|
|
|
make_connection=make_connection |
69
|
|
|
) |
70
|
|
|
|
71
|
|
|
def add_connected_player(game: Game, player): |
72
|
|
|
game.game_service.player_service[player.id] = player |
73
|
|
|
gc = mock_game_connection(state=GameConnectionState.CONNECTED_TO_HOST, player=player) |
74
|
|
|
game.set_player_option(player.id, 'Army', 0) |
75
|
|
|
game.set_player_option(player.id, 'StartSpot', 0) |
76
|
|
|
game.set_player_option(player.id, 'Team', 0) |
77
|
|
|
game.set_player_option(player.id, 'Faction', 0) |
78
|
|
|
game.set_player_option(player.id, 'Color', 0) |
79
|
|
|
game.add_game_connection(gc) |
80
|
|
|
return gc |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
def add_connected_players(game: Game, players): |
84
|
|
|
""" |
85
|
|
|
Utility to add players with army and StartSpot indexed by a list |
86
|
|
|
""" |
87
|
|
|
for army, player in enumerate(players): |
88
|
|
|
add_connected_player(game, player) |
89
|
|
|
game.set_player_option(player.id, 'Army', army) |
90
|
|
|
game.set_player_option(player.id, 'StartSpot', army) |
91
|
|
|
game.set_player_option(player.id, 'Team', army) |
92
|
|
|
game.set_player_option(player.id, 'Faction', 0) |
93
|
|
|
game.set_player_option(player.id, 'Color', 0) |
94
|
|
|
game.host = players[0] |
95
|
|
|
|
96
|
|
|
|