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