-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTLeague.py
79 lines (61 loc) · 2.08 KB
/
TTLeague.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
import time
import json
class TTLeague:
def __init__(self, setCount=3):
self.setCount = setCount
def __str__(self):
return str(self.__dict__)
class Match:
def __init__(self, player1, player2):
# type: (Player, Player) -> None
self.timestamp = int(time.time() * 1000)
self.games = []
self.player1 = player1
self.player2 = player2
def add_game(self, game):
# type: (Game) -> None
self.games.append(game)
# data format for each match data: { w: 'Horst', l: 'Steffen Krause', diff: 2, date: 1490262281602 }
def match_data_as_json(self):
# type: (None) -> str
data = self.get_match_data()
return json.dumps(data)
def match_data_for_log(self):
# type: (None) -> str
data = self.get_match_data()
log = '{} - {}; '.format(self.player1.name, self.player2.name)
for g in self.games:
log += '{:d}:{:d},'.format(g.home, g.guest)
log = log[:-1] # remove last ,
return log
def get_match_data(self):
# type: (None) -> dict
data = []
for g in self.games:
if g.home > g.guest:
w = self.player1.name
l = self.player2.name
diff = g.home - g.guest
else:
w = self.player2.name
l = self.player1.name
diff = g.guest - g.home
self.timestamp += 1
data.append({'w': w, 'l': l, 'diff': diff, 'date': self.timestamp})
return data
def __str__(self):
games_str = json.dumps([g.__dict__ for g in self.games])
return "Match: {} vs {}; Sets: {}".format(str(self.player1), str(self.player2), games_str)
class Game:
def __init__(self, home=0, guest=0):
self.home = home
self.guest = guest
def __str__(self):
return str(self.__dict__)
class Player:
def __init__(self, nfcTag="", name="", elo=0):
self.nfcTag = nfcTag
self.name = name
self.elo = elo
def __str__(self):
return str(self.__dict__)