-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
167 lines (140 loc) · 4.25 KB
/
game.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
# -*- coding: utf-8 -*-
import datetime
import re
PHASE = [
'LIGA',
'ELIMINATORIA',
'OCTAVOS',
'CUARTOS',
'BRONCE',
'SEMIFINAL',
'FINAL'
]
CATEGORY = [
'OTRO',
'SENIOR',
'PRO',
'SUPERLIGA',
'SUPERLIGA2',
'PRIMERA',
'SEGUNDA',
'JUVENIL',
'CADETE',
'INFANTIL',
'ALEVIN',
'AFICIONADOS'
]
DIVISION = [
'MASCULINA',
'FEMENINA',
'MIXTO'
]
game = {
'date': '01-01-1970',
'time': '0:00',
'competition': '',
'category': 'SEGUNDA',
'division': 'MASCULINA',
'phase': 'LIGA',
'pool': '-',
'team1': {
"name": "",
"set1": 0,
"set2": 0,
"set3": 0,
"set4": 0,
"set5": 0
},
'team2': {
"name": "",
"set1": 0,
"set2": 0,
"set3": 0,
"set4": 0,
"set5": 0
},
'hall': '',
'city': ''
}
class GameParser:
def checkDate(self, date):
valid = re.compile(r"^[0-9]{2}[/-][0-9]{2}[/-][0-9]{4}$")
if valid.match(date) is None:
raise ValueError(date + ' no es un formato de fecha correcto (DD-MM-AAAA)')
tokens = re.findall(r"[\d]+", date)
day = int(tokens[0])
month = int(tokens[1])
year = int(tokens[2])
try:
mydate = datetime.date(year, month, day)
except:
raise ValueError('La fecha introducida (%s) no es correcta' % date)
if mydate < datetime.date.today():
check = raw_input('La fecha %s está en el pasado. '
'¿Continuar de todos modos? (s/[n]): ' % date)
if check not in ['s', 'y', 'S', 'Y']:
print("Abortando...")
return
return "%02d-%02d-%04d" % (day, month, year)
def checkTime(self, time):
valid = re.compile(r"^[012]?[0-9]:[0-5][0-9]$")
if valid.match(time) is None:
raise ValueError(time + ' no se reconoce como una hora correcta (hh:mm)')
tokens = time.split(':')
if int(tokens[0]) > 24 or int(tokens[1]) > 59:
raise ValueError('La hora introducida (%s) no es correcta' % time)
return time
def checkCategory(self, category):
category = category.upper()
if category not in CATEGORY:
raise ValueError("Categoría no válida: '%s'" % category)
return category
def checkDivision(self, division):
division = division.upper()
if division not in DIVISION:
raise ValueError("División no válida: '%s'" % division)
return division
def checkPool(self, pool):
if len(pool) > 1:
raise ValueError("Grupo no válido: '%s'" % pool)
if pool == '-' or pool.isalnum():
return pool
else:
raise ValueError("Grupo no válido '%s'" % pool)
def checkPhase(self, phase):
phase = phase.upper()
if phase not in PHASE:
raise ValueError("Fase no válida: '%s'" % phase)
return phase
def parseGame(self, line):
"""
date time competition category division phase pool local visitor (hall city)
"""
fields = line.split(",")
if len(fields) < 9:
raise ValueError("Falta algún campo en la línea")
game = {}
game['date'] = self.checkDate(fields[0].strip())
game['time'] = self.checkTime(fields[1].strip())
game['competition'] = fields[2].strip()
game['category'] = self.checkCategory(fields[3].strip())
game['division'] = self.checkDivision(fields[4].strip())
game['phase'] = self.checkPhase(fields[5].strip())
game['pool'] = self.checkPool(fields[6].strip())
game['team1'] = {}
game['team1']['name'] = fields[7].strip()
game['team1']['set1'] = 0
game['team1']['set2'] = 0
game['team1']['set3'] = 0
game['team1']['set4'] = 0
game['team1']['set5'] = 0
game['team2'] = {}
game['team2']['name'] = fields[8].strip()
game['team2']['set1'] = 0
game['team2']['set2'] = 0
game['team2']['set3'] = 0
game['team2']['set4'] = 0
game['team2']['set5'] = 0
# game['hall'] = fields[9].strip()
# game['city'] = fields[10].strip()
return game