-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
163 lines (132 loc) · 3.86 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import select
import socket
import card
import comm
import game
class Client(socket.socket):
"""
Game client
Attributes:
i (int): the client index in the server
game (game.GameThread): the running content
"""
def __init__(self, host, port):
"""
Args:
host (str):
port (int):
"""
super().__init__()
self.connect((host, port))
self.setblocking(True)
try:
self.i = comm.recv(self)
except ConnectionRefusedError:
self.i = b''
if self.i == b'' or self.i == -1:
self.close()
print("Cannot reach the server")
return
tokens_center = comm.recv(self)
dices_val = comm.recv(self)
characters = comm.recv(self)
areas = comm.recv(self)
active_player = comm.recv(self)
self.setblocking(False)
self.game = game.Game(self, tokens_center, dices_val, characters, areas, active_player)
self.game.run()
def poll(self):
"""
Tries to read data from the server
Returns:
None
"""
r, _, _ = select.select([self], [], [], 0)
if r:
msg = comm.recv(self)
if msg == b'':
print("Lost connection from server")
self.close()
return
if msg[0] == 'token':
self.game.tokens[msg[1]].center = msg[2]
elif msg[0] == 'dices':
self.game.dices[0].roll_to(msg[1][0])
self.game.dices[1].roll_to(msg[1][1])
elif msg[0] == 'reveal':
self.game.characters[msg[1]].revealed = True
elif msg[0] == 'turn':
self.game.active_player.i = msg[1]
elif msg[0] == 'draw':
if card.TYPES[msg[2]] == card.CardVision:
if self.i == msg[1]:
self.send_vision(msg[3], self.game.cards[msg[2]].draw(msg[3], msg[1]))
else:
self.game.cards[msg[2]].draw(msg[3], msg[1])
elif msg[0] == 'vision':
self.game.cards[card.TYPES.index(card.CardVision)].answer(msg[1], msg[2])
elif msg[0] == 'take':
self.game.characters[msg[1]].equipments.append(self.game.characters[msg[2]].equipments.pop(msg[3]))
else:
print(msg)
def close(self):
"""
Closes the client
Returns:
None
"""
self.game.running = False
super().close()
def send_token(self, i):
"""
Send the coordinates of the token i
Args:
i (int):
Returns:
None
"""
comm.send(self, ['token', i, self.game.tokens[i].center])
def roll_dice(self):
"""
Ask for a dice roll
Returns:
None
"""
comm.send(self, ['dices'])
def reveal(self):
"""
Ask to reveal the character
Returns:
None
"""
comm.send(self, ['reveal'])
def end_turn(self):
"""
Ask to end the turn
Returns:
None
"""
comm.send(self, ['turn'])
def draw(self, i):
"""
Ask to draw a card from pile i
Args:
i (int):
Returns:
None
"""
comm.send(self, ['draw', i])
def send_vision(self, i_vision, i_player):
"""
Send the vision i_vision to the player i_player
Args:
i_vision (int):
i_player (int):
Returns:
None
"""
comm.send(self, ['vision', i_vision, i_player])
def take_equipment(self, i_player, i_equipment):
comm.send(self, ['take', i_player, i_equipment])
if __name__ == '__main__':
Client('', 1616)