-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstate.py
More file actions
239 lines (200 loc) · 6.79 KB
/
state.py
File metadata and controls
239 lines (200 loc) · 6.79 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import asyncio
from dataclasses import dataclass
from datetime import datetime
from typing import Any, Optional
import json
from inscription import InscriptionError
from logs import date, getLogger, stateFilename
from status import ClientStatus, MatchStatus
from utils import clock
log = getLogger("state")
class StateError(Exception):
pass
@dataclass
class Client:
name: str
port: int
ip: str
matricules: frozenset
status: ClientStatus = ClientStatus.PENDING
busy: bool = False
def __str__(self):
return self.name
@dataclass
class Message:
name: str
message: str
class Chat:
def __init__(self):
self.messages = []
def addMessage(self, msg: Message):
self.messages.append(msg)
if len(self.messages) > 10:
self.messages.pop(0)
@dataclass
class Match:
clients: list[Client]
badMoves: list[int]
moves: int = 0
start: Optional[float] = None
end: Optional[float] = None
status: MatchStatus = MatchStatus.PENDING
winner: Optional[Client] = None
task: Optional[asyncio.Task] = None
state: Optional[Any] = None
chat: Optional[Chat] = None
def __init__(self, client1: Client, client2: Client):
self.clients = [client1, client2]
self.badMoves = [0, 0]
def __str__(self):
return "{} VS {}".format(*[client.name for client in self.clients])
def __getstate__(self):
D = dict(self.__dict__)
D["task"] = None
D["chat"] = None
return D
async def reset(self):
if self.task is not None:
task = self.task
task.cancel()
self.task = None
try:
await task
except asyncio.CancelledError as e:
assert self.chat is not None, "Chat is None"
self.chat.addMessage(Message("Admin", str(e)))
finally:
for client in self.clients:
client.busy = False
self.moves = 0
self.start = None
self.end = None
self.status = MatchStatus.PENDING
self.winner = None
self.task = None
self.state = None
self.chat = None
self.badMoves = [0, 0]
log.info("Match {} Reset".format(self))
class ClientNotFoundError(Exception):
pass
@dataclass
class _State:
clients: dict[frozenset[str], Client]
matches: list[Match]
date: datetime = date
async def removeClient(self, matricules: frozenset[str]):
client = self.clients[matricules]
for match in list(self.matches):
if client in match.clients:
if match.status != MatchStatus.PENDING:
await match.reset()
self.matches.remove(match)
self.clients.pop(matricules)
log.info("Client {} Removed".format(client.name))
async def addClient(
self, name: str, port: int, matricules: frozenset[str], ip: str
) -> Client:
if matricules in self.clients:
oldClient = self.clients[matricules]
oldClient.name = name
oldClient.port = port
oldClient.ip = ip
return oldClient
else:
names = [c.name for c in self.clients.values()]
if name in names:
raise InscriptionError(f"Name '{name}' already used")
client = Client(name=name, port=port, matricules=matricules, ip=ip)
for other in self.clients.values():
# players = [other, client]
# random.shuffle(players)
# self.matches.append(Match(*players))
self.matches.append(Match(client, other))
self.matches.append(Match(other, client))
self.clients[client.matricules] = client
return client
@property
def remainingMatches(self) -> int:
return len(
list(filter(lambda match: match.status != MatchStatus.DONE, self.matches))
)
@property
def runningMatches(self) -> int:
return len(list(filter(lambda match: match.task is not None, self.matches)))
@property
def matchCount(self):
return len(self.matches)
def getPoints(self, client: Client) -> int:
res = 0
for match in self.matches:
if client in match.clients:
if match.status == MatchStatus.DONE:
if match.winner == client:
res += 3
elif match.winner is None:
res += 1
return res
def getBadMoves(self, client: Client) -> int:
res = 0
for match in self.matches:
if client in match.clients:
i = match.clients.index(client)
res += match.badMoves[i]
return res
def getMatchCount(self, client: Client) -> int:
res = 0
for match in self.matches:
if client in match.clients:
if match.status == MatchStatus.DONE:
res += 1
return res
State = _State(clients={}, matches=[])
log.info("State Created")
async def dumpState():
log.info("State Dumper Started")
tic = clock(1)
while True:
await tic()
clients = []
for client in State.clients.values():
clients.append(
{
"matricules": list(client.matricules),
"name": client.name,
"points": State.getPoints(client),
"bad_moves": State.getBadMoves(client),
"match_count": State.getMatchCount(client),
}
)
matches = []
for match in State.matches:
dumped_match = {
"clients": [
{"name": c.name, "matricules": list(c.matricules)}
for c in match.clients
],
"bad_moves": match.badMoves,
"moves": match.moves,
"status": match.status.name,
}
if match.start is not None and match.end is not None:
dumped_match["time"] = match.end - match.start
if match.winner is None:
dumped_match["winner"] = None
else:
dumped_match["winner"] = {
"name": match.winner.name,
"matricules": list(match.winner.matricules),
}
matches.append(dumped_match)
with open(stateFilename, "w", encoding="utf8") as file:
s = {
"clients": list(reversed(sorted(clients, key=lambda c: c["points"]))),
"matches": matches,
}
try:
to_write = json.dumps(s, indent=2)
file.write(to_write)
except Exception as e:
file.write(str(e))