-
Notifications
You must be signed in to change notification settings - Fork 3
/
Wacky2Unit.py
executable file
·380 lines (278 loc) · 11.1 KB
/
Wacky2Unit.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python3
import numpy
import functools
class Team:
def __init__(self, ID, points, players):
self.ID = ID
self.points = points
self.players = players
def player_exists(self, playerId):
for player in self.players:
if player.ID == playerId:
return True
return False
def ability(self):
res = 0
for player in self.players:
res += player.ability
return res
def play_match_ability(self):
return self.ability() + self.points
def spirit_permutation(self, playerId=None):
upper_bound = len(self.players) if not (playerId and self.player_exists(playerId)) else ([i.ID for i in self.players].index(
playerId) + 1)
res = numpy.array([0,1,2,3,4])
for player in self.players[:upper_bound]:
res = res[player.spirit]
return res
# Called strength in t_permutation
# sympy permutations start from zero, therefore Python 'ai' is cpp 'a[i] + 1'
def spirit(self):
s = 0
perm = self.spirit_permutation()
for i, ai in enumerate(perm):
s += (i * 1) * ((ai + 1) + 1)
return s
def increment_player_games(self):
for player in self.players:
player.games_played += 1
def has_goalkeeper(self):
for player in self.players:
if player.is_goalkeeper:
return True
return False
class Player:
def __init__(self, ID, games_played, team, cards, is_goalkeeper, ability, spirit):
self.ID = ID
self.games_played = games_played
self.team = team
self.cards = cards
self.is_goalkeeper = is_goalkeeper
self.ability = ability
self.spirit = spirit
class Wacky2Unit:
def __init__(self):
self.expected = []
self.input = []
self.teams = {}
self.players = {}
self.removed_players = {}
def __add_input(self, input_list):
self.input.append(' '.join([str(x) for x in input_list]))
def __add_expected(self, keycode, status, more=None):
self.expected.append(str(keycode) + ': ' + str(status) + ('' if more == None else ', ' + str(more)))
def __add_expected_raw(self, text):
self.expected.append(text)
def write(self, in_file, out_file):
f_test = open(in_file, 'w')
f_expected = open(out_file, 'w')
f_test.write('\n'.join(self.input))
f_expected.write('\n'.join(self.expected) + '\n')
f_test.close()
f_expected.close()
def clear(self):
self.expected = []
self.input = []
self.teams = {}
self.players = {}
self.removed_players = {}
# ----------------------------------------
def __permutation_string(self, perm):
return ','.join(str(e + 1) for e in list(perm))
def __is_valid_permutation(self, perm):
if len(list(perm)) != 5: return False
histogram = {e: 0 for e in range(0,5)}
for e in perm:
histogram[e] += 1
for e in range(0,5):
if histogram[e] != 1:
return False
return True
def add_team(self, teamId):
self.__add_input(['add_team', teamId])
if teamId <= 0:
self.__add_expected('add_team', 'INVALID_INPUT')
return
if teamId in self.teams:
self.__add_expected('add_team', 'FAILURE')
return
self.teams[teamId] = Team(ID=teamId, points=0, players=[])
self.__add_expected('add_team', 'SUCCESS')
def remove_team(self, teamId):
self.__add_input(['remove_team', teamId])
if teamId <= 0:
self.__add_expected('remove_team', 'INVALID_INPUT')
return
if teamId not in self.teams:
self.__add_expected('remove_team', 'FAILURE')
return
# Remove players from tournament, add to removed players
for player in self.teams[teamId].players:
del self.players[player.ID]
self.removed_players[player.ID] = player
del self.teams[teamId]
self.__add_expected('remove_team', 'SUCCESS')
def add_player(self, playerId, teamId, spirit, gamesPlayed, ability, cards, goalKeeper):
spirit = numpy.array([e - 1 for e in spirit])
self.__add_input(
['add_player', playerId, teamId, self.__permutation_string(spirit), gamesPlayed, ability, cards, 'true' if goalKeeper else 'false'])
invalid_input = False
if playerId <= 0 or teamId <= 0:
self.__add_expected('add_player', 'INVALID_INPUT')
return
if not self.__is_valid_permutation(spirit):
self.__add_expected('add_player', 'INVALID_INPUT')
return
if gamesPlayed < 0 or cards < 0:
self.__add_expected('add_player', 'INVALID_INPUT')
return
if (playerId in self.players) or (playerId in self.removed_players) or (teamId not in self.teams):
self.__add_expected('add_player', 'FAILURE')
return
team = self.teams[teamId]
self.players[playerId] = Player(
ID=playerId,
games_played=gamesPlayed,
team=team,
cards=cards,
is_goalkeeper=goalKeeper,
ability=ability,
spirit=spirit
)
team.players.append(self.players[playerId])
self.__add_expected('add_player', 'SUCCESS')
def play_match(self, teamId1, teamId2):
self.__add_input(['play_match', teamId1, teamId2])
if (teamId1 <= 0) or (teamId2 <= 0) or (teamId1 == teamId2):
self.__add_expected('play_match', 'INVALID_INPUT')
return
if (teamId1 not in self.teams) or (teamId2 not in self.teams):
self.__add_expected('play_match', 'FAILURE')
return
team1 = self.teams[teamId1]
team2 = self.teams[teamId2]
if not (team1.has_goalkeeper() and team2.has_goalkeeper()):
self.__add_expected('play_match', 'FAILURE')
return
TIE, ABILITY1, SPIRIT1, ABILITY2, SPIRIT2 = range(0, 5)
winner = None
winner_code = TIE
if team1.play_match_ability() != team2.play_match_ability():
winner_code = ABILITY1 if team1.play_match_ability() > team2.play_match_ability() else ABILITY2
else:
str1 = team1.spirit()
str2 = team2.spirit()
if str1 != str2:
winner_code = SPIRIT1 if str1 > str2 else SPIRIT2
if winner_code in [ABILITY1, SPIRIT1]:
team1.points += 3
elif winner_code in [ABILITY2, SPIRIT2]:
team2.points += 3
else:
team1.points += 1
team2.points += 1
# Increment games_played for every player
team1.increment_player_games()
team2.increment_player_games()
self.__add_expected('play_match', 'SUCCESS', winner_code)
def num_played_games_for_player(self, playerId):
func = 'num_played_games_for_player'
self.__add_input([func, playerId])
if playerId <= 0:
self.__add_expected(func, 'INVALID_INPUT')
return
if playerId in self.players:
self.__add_expected(func, 'SUCCESS', self.players[playerId].games_played)
return
if playerId in self.removed_players:
self.__add_expected(func, 'SUCCESS', self.removed_players[playerId].games_played)
return
self.__add_expected(func, 'FAILURE')
def add_player_cards(self, playerId, cards):
func = 'add_player_cards'
self.__add_input([func, playerId, cards])
if playerId <= 0 or cards < 0:
self.__add_expected(func, 'INVALID_INPUT')
return
if playerId not in self.players:
self.__add_expected(func, 'FAILURE')
return
self.players[playerId].cards += cards
self.__add_expected(func, 'SUCCESS')
def get_player_cards(self, playerId):
func = 'get_player_cards'
self.__add_input([func, playerId])
if playerId <= 0:
self.__add_expected(func, 'INVALID_INPUT')
return
if playerId in self.players:
self.__add_expected(func, 'SUCCESS', self.players[playerId].cards)
return
if playerId in self.removed_players:
self.__add_expected(func, 'SUCCESS', self.removed_players[playerId].cards)
return
self.__add_expected(func, 'FAILURE')
def get_team_points(self, teamId):
func = 'get_team_points'
self.__add_input([func, teamId])
if teamId <= 0:
self.__add_expected(func, 'INVALID_INPUT')
return
if teamId not in self.teams:
self.__add_expected(func, 'FAILURE')
return
self.__add_expected(func, 'SUCCESS',
self.teams[teamId].points)
def get_ith_pointless_ability(self, i):
func = 'get_ith_pointless_ability'
self.__add_input([func, i])
if (i < 0) or (len(self.teams) == 0) or (len(self.teams) <= i):
self.__add_expected(func, 'FAILURE')
return
teams_by_ability = [
{
'teamId': ID,
'ability': self.teams[ID].ability()
}
for ID in self.teams
]
def sort_dicts_by_ability(d1, d2):
if d1['ability'] != d2['ability']:
return -1 if d1['ability'] < d2['ability'] else 1
return -1 if d1['teamId'] < d2['teamId'] else 1
teams_by_ability.sort(key=functools.cmp_to_key(sort_dicts_by_ability))
self.__add_expected(func, 'SUCCESS', teams_by_ability[i]['teamId'])
def get_partial_spirit(self, playerId):
func = 'get_partial_spirit'
self.__add_input([func, playerId])
if playerId <= 0:
self.__add_expected(func, 'INVALID_INPUT')
return
if playerId not in self.players:
self.__add_expected(func, 'FAILURE')
return
perm = self.players[playerId].team.spirit_permutation(playerId=playerId)
self.__add_expected(func, 'SUCCESS', self.__permutation_string(perm))
def buy_team(self, buyerId, boughtId):
func = 'buy_team'
self.__add_input([func, buyerId, boughtId])
if (buyerId <= 0) or (boughtId <= 0) or (buyerId == boughtId):
self.__add_expected(func, 'INVALID_INPUT')
return
if (buyerId not in self.teams) or (boughtId not in self.teams):
self.__add_expected(func, 'FAILURE')
return
buyer = self.teams[buyerId]
bought = self.teams[boughtId]
# All players are being added according to their
# original order in their corresponding team.
#
# buyerId.players are 'veterans' and therefore appear first
# in the chronological order.
buyer.players += bought.players
buyer.points += bought.points
# Update bought team player's 'team' prop
for player in bought.players:
player.team = buyer
del self.teams[boughtId]
self.__add_expected(func, 'SUCCESS')