-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
334 lines (252 loc) · 9.43 KB
/
main.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
import json
from flask import Flask, render_template, request, send_from_directory
from flask_socketio import SocketIO, emit
import os.path
import argparse
parser = argparse.ArgumentParser(description='Scripts options.')
parser.add_argument('--debug-off', dest='is_debug_on', action='store_false')
args = parser.parse_args()
is_debug_on = args.is_debug_on
GAMEDIR = "data"
MAX_SET = 5
app = Flask(__name__,
static_folder='static',
template_folder='templates')
app.config["SECRET_KEY"] = "H4MWdBLKVT8dnn8bpd3wx6FwvV8BnZ"
socketio = SocketIO(app, debug=is_debug_on, cors_allowed_origins='*')
def get_team_name(team):
filename = f"{GAMEDIR}/team_{team}_name"
with open(filename, 'r') as f:
name = f.read()
f.close()
return name.upper()
def read_set_num():
filename = f"{GAMEDIR}/set_num"
with open(filename, 'r') as f:
val = f.read()
f.close()
return val
def read_score_file(team, set_num):
filename = f"{GAMEDIR}/team_{team}_set_{set_num}"
with open(filename, 'r') as f:
score = f.read()
f.close()
return score
def get_logo_mode():
filename = f"{GAMEDIR}/logo_mode"
with open(filename, 'r') as f:
val = f.read()
f.close()
print("ok")
return val
def update_team_names(names):
for team in ['a', 'b']:
filename = f"{GAMEDIR}/team_{team}_name"
with open(filename, 'w') as f:
f.write(names[team])
f.close()
def update_team_colors(colors):
for team in ['a', 'b']:
filename = f"{GAMEDIR}/team_{team}_color"
with open(filename, 'w') as f:
f.write(colors[team])
f.close()
def gen_filenames():
file_lst = []
for team in ['a', 'b']:
for set_num in range(1, 6):
filename = f"team_{team}_set_{set_num}"
file_lst.append(filename)
name = f"team_{team}_name"
file_lst.append(name)
logo = f"team_{team}_logo"
file_lst.append(logo)
color = f"team_{team}_color"
file_lst.append(color)
file_lst.append("set_num")
file_lst.append("logo_mode")
return file_lst
def get_team_logo(team):
filename = f"{GAMEDIR}/team_{team}_logo"
with open(filename, 'r') as f:
logo = f.read()
f.close()
return logo
def get_team_color(team):
filename = f"{GAMEDIR}/team_{team}_color"
with open(filename, 'r') as f:
color = f.read()
f.close()
return color
def logos():
# only jpg png and svg files accepted. dumb and simple
# get list from files list
img = [f for f in os.listdir("static/logos") if
f.lower().endswith('.jpg') or f.lower().endswith('.jpeg') or f.lower().endswith('.png') or f.lower().endswith('.svg')]
logos = {'a': get_team_logo('a'), 'b': get_team_logo('b')}
return {"files": sorted(img), "logos": logos, "logo_mode": get_logo_mode()}
class Game:
def __init__(self):
self.score = None
self.set_num = None
self.filenames = gen_filenames()
self.read_score_files()
def read_score_files(self):
self.create_files()
self.score = {}
self.set_num = int(read_set_num())
for team in ['a', 'b']:
tmp_dict = {}
for set_num in range(1, self.set_num + 1):
tmp_dict[set_num] = int(read_score_file(team=team, set_num=set_num))
tmp_dict["name"] = get_team_name(team)
tmp_dict["logo"] = get_team_logo(team)
tmp_dict["color"] = get_team_color(team)
self.score[team] = tmp_dict
self.score['set'] = self.set_num
self.score["logo_mode"] = get_logo_mode()
def create_files(self):
for f in self.filenames:
if not os.path.exists(f"{GAMEDIR}/{f}"):
print(f + " does not exists, creating...")
value = "0"
if f == "set_num":
value = "1"
if f == "logo_mode":
value = "logo" # allowed values: logo / color / both
if f.endswith("_name"):
value = "Team name"
filename = f"{GAMEDIR}/{f}"
with open(filename, 'x') as f:
f.write(str(value))
f.close()
def new_game(self):
self.delete_files()
self.read_score_files()
def delete_files(self):
for f in self.filenames:
filename = f"{GAMEDIR}/{f}"
if os.path.exists(filename):
if filename.endswith("_name") or filename.endswith("_logo") or filename.endswith("_color") or filename.endswith("_mode"):
continue
print("deleting file {}".format(f))
os.remove(filename)
def increase(self, team):
val = int(self.score[team][self.set_num]) + 1
self.update_score_file(team=team, score=val)
self.score[team][self.set_num] = read_score_file(team=team, set_num=self.set_num)
# print(u"--- Increased score from team {team} ---".format(team=team))
def decrease(self, team):
point = int(self.score[team][self.set_num])
val = 0 if point < 1 else point - 1
self.update_score_file(team=team, score=val)
self.score[team][self.set_num] = read_score_file(team=team, set_num=self.set_num)
# print(u"--- Decreased score from team {team} ---".format(team=team))
def update_score_file(self, team, score):
filename = f"{GAMEDIR}/team_{team}_set_{self.set_num}"
with open(filename, 'w') as f:
f.write(str(score))
f.close()
def change_set(self, value):
self.score['previous_set_value'] = self.set_num
tmp_set = int(self.set_num) + value
if tmp_set < 1:
tmp_set = 1
if tmp_set > MAX_SET:
tmp_set = MAX_SET
self.score['set'] = self.set_num = tmp_set
# print("{}/{}".format(self.set_num, self.score['previous_set_value']))
filename = f"{GAMEDIR}/set_num"
with open(filename, 'w') as f:
f.write(str(self.set_num))
f.close()
for team in ['a', 'b']:
if self.set_num not in self.score[team]:
self.score[team][self.set_num] = 0 # initialise score for new set
def update_team_logos(self, data):
for team, logo in data.items():
filename = u"{gamedir}/team_{team}_logo".format(gamedir=GAMEDIR, team=team)
with open(filename, 'w') as f:
f.write(str(logo))
f.close()
self.score[team]['logo'] = logo
def set_logo_mode(self, logo_mode):
filename = f"{GAMEDIR}/logo_mode"
with open(filename, 'w') as f:
f.write(logo_mode)
f.close()
self.score["logo_mode"] = logo_mode
def get_score(self, team):
return self.score[team][self.set_num]
def get_all(self):
return self.score
# WebSockets
@socketio.on('connect')
def on_connect():
print('Connected to the server')
emit('all', json.dumps(scoreboard.get_all()), broadcast=True)
emit('setup', json.dumps(logos()), broadcast=True)
@socketio.on('increment')
def handle_increment(team):
scoreboard.increase(team)
emit('all', json.dumps(scoreboard.get_all()), broadcast=True)
@socketio.on('decrement')
def handle_decrement(team):
scoreboard.decrease(team)
emit('all', json.dumps(scoreboard.get_all()), broadcast=True)
@socketio.on('increment_set')
def handle_increment_set():
scoreboard.change_set(1)
emit('all', json.dumps(scoreboard.get_all()), broadcast=True)
@socketio.on('decrement_set')
def handle_decrement_set():
scoreboard.change_set(-1)
emit('all', json.dumps(scoreboard.get_all()), broadcast=True)
@socketio.on('team_names')
def handle_team_names(names):
update_team_names(names)
scoreboard.read_score_files()
emit('all', json.dumps(scoreboard.get_all()), broadcast=True)
@socketio.on('team_colors')
def handle_team_colors(colors):
update_team_colors(colors)
scoreboard.read_score_files()
emit('logos_colors', json.dumps(scoreboard.get_all()), broadcast=True)
@socketio.on('team_logos')
def team_logos(data):
scoreboard.update_team_logos(data)
emit('logos_colors', json.dumps(scoreboard.get_all()), broadcast=True)
@socketio.on('logo_mode')
def handle_logo_mode(logo_mode):
scoreboard.set_logo_mode(logo_mode)
emit('logos_colors', json.dumps(scoreboard.get_all()), broadcast=True)
@socketio.on('new_game')
def handle_new_game():
scoreboard.new_game()
emit('all', json.dumps(scoreboard.get_all()), broadcast=True)
# emit('logos_colors', json.dumps(scoreboard.get_all()), broadcast=True)
@socketio.on('reload')
def handle_reload():
scoreboard.read_score_files()
emit('all', json.dumps(scoreboard.get_all()), broadcast=True)
scoreboard = Game()
# FLASK Routes
@app.route('/')
def index():
return render_template('index.html')
@app.route('/setup')
def setup():
return render_template('setup.html')
@app.route('/remote')
def remote():
return render_template('remote.html')
@app.route('/score')
def score():
# print(request.headers.get('User-Agent'))
return render_template('score.html')
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
if __name__ == '__main__':
socketio.run(app.run(debug=is_debug_on, host="0.0.0.0", port=3000))