-
Notifications
You must be signed in to change notification settings - Fork 6
/
services.py
580 lines (432 loc) · 15.3 KB
/
services.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
## @package PyHoot.services
# All the services get the method we need and return the data
## @file services.py Implementation of @ref PyHoot.services
"""All the services get the method we need and return the data
"""
## @file services.py All the services get the method we need and return
import os.path
from xml.etree import ElementTree
from . import base, game, util
class Service(base.Base):
"""Base class to all services"""
## The uri path needed to use this services
NAME = 'base'
def __init__(self):
"""Initialization"""
super(Service, self).__init__()
## Did we read everything from read?
self.finished_reading = False
## How much did we read from read
self.read_pointer = 0
self._content_page = None
def content(self):
"""The body of the service"""
return ""
def close(self):
"""Do nothing, supposed to close an open file"""
pass
def headers(self, extra):
"""Headers of the service, base if for any HTTP page"""
if self._content_page is None:
self._content_page = self.content()
return util.create_headers_response(200,
len(self._content_page),
extra,
".html")
def read_buff(self, buff_size):
"""return the content page and update self._finished_reading"""
if self._content_page is None:
self._content_page = self.content()
self.read_pointer += buff_size
return self._content_page[self.read_pointer - buff_size:
self.read_pointer]
def get_status(self):
"""Return self._finished_reading"""
return self.finished_reading
class XMLService(Service):
"""Service that return xml file"""
def __init__(self):
"""Initialization"""
super(XMLService, self).__init__()
def headers(self, extra):
"""Headers of the service, base if for any HTTP page"""
if self._content_page is None:
self._content_page = self.content()
return util.create_headers_response(200,
len(self._content_page),
extra,
".xml")
class register_quiz(Service):
"""Register a quiz to a master and to the system
GameMaster"""
## URI
NAME = "/register_quiz"
def __init__(self, quiz_name, common, base_directory, pid=None):
"""Initialization"""
super(register_quiz, self).__init__()
self._quiz_name = quiz_name[0]
if pid is not None:
util.remove_from_sysyem(
common,
pid,
)
g = self.register_master(quiz_name[0], common, base_directory)
## Master Player ID
self.master_pid = g.pid
def headers(self, extra):
"""Headers of the service, base if for any HTTP page"""
extra.update({"Location": "/quiz.html",
"Set-Cookie": "pid=%s" % self.master_pid})
return util.create_headers_response(302, extra_headers=extra)
def register_master(self, quiz_name, common, base_directory):
"""Creating GamePlayer object, registering it and returning it"""
m = game.GameMaster(quiz_name, common, base_directory)
common.pid_client[m.pid] = m
common.join_number[m.join_number] = m
return m
class homepage(Service):
"""Redirect to home.html"""
## URI
NAME = "/" # This is the homepage
def headers(self, extra):
"""Headers of the service, base if for any HTTP page"""
extra.update({"Location": "/home.html"})
return util.create_headers_response(302, extra_headers=extra)
class answer(Service):
"""Return the Answer from ["A", "B", "C", "D"]
GamePlayer"""
## URI
NAME = "/answer"
def __init__(self, letter, game):
"""Initialization"""
super(answer, self).__init__()
game.answer = letter[0]
class getnames(XMLService):
"""Get all the names of all the player connected to the system
GamMaster"""
## URI
NAME = "/getnames"
def __init__(self, game, common):
"""Initialization"""
super(getnames, self).__init__()
self._game = game
self._common = common
def content(self):
"""The content of the service"""
root = ElementTree.Element("Root")
for player in self._game.get_player_dict().values():
name = player.name
if name is not None:
ElementTree.SubElement(root, "player", {"name": name})
return util.to_string(root)
class diconnect_user(Service):
"""Disconnecting user from system
GamePlayer"""
## URI
NAME = "/diconnect_user"
def __init__(self, pid, common, game):
"""Initialization"""
super(diconnect_user, self).__init__()
try:
util.remove_from_sysyem(common, pid)
except AttributeError:
pass
class check_test(XMLService):
"""Check if a specific test is running"""
## URI
NAME = "/check_test"
def __init__(self, join_number, common):
"""Initialization"""
super(check_test, self).__init__()
## Join number
self.data = int(join_number[0])
## Common database
self.common = common
def content(self):
"""The content of the service"""
return util.boolean_to_xml(
self.data in self.common.join_number and
self.common.join_number[self.data].TYPE == "MASTER"
)
class check_name(XMLService):
"""Check if name not taken
GamePlayer"""
## URI
NAME = "/check_name"
def __init__(self, join_number, name, common):
"""Initialization"""
super(check_name, self).__init__()
## The join number
self.data = int(join_number[0])
## The name
self.name = name[0]
## Common library
self.common = common
def content(self):
"""The content of the service"""
ans = "False"
if self.data in self.common.join_number:
master = self.common.join_number[self.data]
if master.TYPE == "MASTER":
name_list = []
for player in master.get_player_dict().values():
name_list.append(player.name)
if self.name not in name_list:
ans = "True"
return util.boolean_to_xml(ans)
class join(Service):
"""Register player to master
GamePlayer"""
## URI
NAME = "/join"
def __init__(self, join_number, name, common, pid=None):
"""Initialization"""
super(join, self).__init__()
if pid is not None:
util.remove_from_sysyem(common, pid)
## Player ID
self.player_pid = self.register_player(
int(join_number[0]),
name[0],
common
).pid
def headers(self, extra):
"""Headers of the service, base if for any HTTP page"""
extra.update({"Location": "/game.html",
"Set-Cookie": "pid=%s" % self.player_pid})
return util.create_headers_response(302, extra_headers=extra)
def register_player(self, join_number, name, common):
"""Creating GamePlayer object, registering it and returning it"""
g = game.GamePlayer(common.join_number[join_number], common, name)
common.pid_client[g.pid] = g
g.game_master.add_player(g.pid, g)
return g
class check_test_exist(XMLService):
"""Check if the test exist on the system
GameMaster"""
## URI
NAME = "/check_test_exist"
def __init__(self, quiz_name, base_directory):
"""Initialization"""
super(check_test_exist, self).__init__()
self._quiz_name = quiz_name[0]
self._base_directory = base_directory
def content(self):
"""The content of the service"""
return util.boolean_to_xml(
os.path.isfile(
os.path.normpath("%s\Quizes\%s.xml" %
(self._base_directory,
os.path.normpath(self._quiz_name))
)
)
)
class new(Service):
"""Send you to / new.html"""
## URI
NAME = "/new" # This is the homepage
def headers(self, extra):
"""Headers of the service, base if for any HTTP page"""
extra.update({"Location": "/new.html"})
return util.create_headers_response(302, extra_headers=extra)
class get_join_number(XMLService):
"""Return the join number to the game
GameMaster"""
## URI
NAME = "/get_join_number"
def __init__(self, pid, common):
"""Initialization"""
super(get_join_number, self).__init__()
self._join_number = str(common.pid_client[pid].join_number)
def content(self):
"""The content of the service"""
root = ElementTree.Element("Root")
ElementTree.SubElement(
root, "join_number").text = self._join_number
return util.to_string(root)
class get_information(XMLService):
"""Return the information about the question: It's name and how many
questions
GameMaster"""
## URI
NAME = "/get_information"
def __init__(self, game):
"""Initialization"""
super(get_information, self).__init__()
self._game = game
def content(self):
"""The content of the service"""
return self._game.get_information()
class set_timer_change(Service):
"""Set the timer for change part
GameMaster\GamePlayer"""
## URI
NAME = "/set_timer_change"
def __init__(self, game, new_time):
"""Initialization"""
super(set_timer_change, self).__init__()
game.set_time_change(int(new_time[0]))
class check_timer_change(XMLService):
"""Check if the timer is ringing, got to 0
GameMaster\Player"""
## URI
NAME = "/check_timer_change"
def __init__(self, game):
"""Initialization"""
super(check_timer_change, self).__init__()
self._game = game
def content(self):
"""The content of the service"""
return util.boolean_to_xml(self._game.check_timer_change())
class order_move_all_players(Service):
"""Order all the players to move to next part
GameMaster"""
## URI
NAME = "/order_move_all_players"
def __init__(self, game):
"""Initialization"""
super(order_move_all_players, self).__init__()
for player in game.get_player_dict().values():
player.order_move_to_next_page()
class order_move_all_not_answered(Service):
"""Order all the players who didn't answer to move to next part
GameMaster"""
## URI
NAME = "/order_move_all_not_answered"
def __init__(self, game):
"""Initialization"""
super(order_move_all_not_answered, self).__init__()
for player in game.get_player_dict().values():
if player.answer is None:
player.order_move_to_next_page()
class check_move_next_page(XMLService):
"""Check if you need to move to next part
GameMaster / GamePlayer"""
## URI
NAME = "/check_move_next_page"
def __init__(self, game):
"""Initialization"""
super(check_move_next_page, self).__init__()
self._game = game
def content(self):
"""The content of the service"""
return util.boolean_to_xml(self._game.get_move_to_next_page())
class moved_to_next_page(Service):
"""Confirmation that he moved to next part
GameMaster / GamePlayer"""
## URI
NAME = "/moved_to_next_question"
def __init__(self, game):
"""Initialization"""
super(moved_to_next_page, self).__init__()
game.moved_to_next_page()
class move_to_next_question(XMLService):
"""Order move to next part"""
## URI
NAME = "/move_to_next_question"
def __init__(self, game):
"""Initialization"""
super(move_to_next_question, self).__init__()
self._game = game
game.move_to_next_question()
def content(self):
"""The content of the service"""
root = ElementTree.Element("Root")
ElementTree.SubElement(
root,
"question",
{"number_of_questions": str(self._game.get_left_questions())}
)
return util.to_string(root)
class get_question(XMLService):
"""Return the question
GameMaster"""
## URI
NAME = "/get_question"
def __init__(self, game):
"""Initialization"""
super(get_question, self).__init__()
self._game = game
def content(self):
"""The content of the service"""
return self._game.get_question()
class get_xml_leaderboard(XMLService):
"""Return the leaderboard"""
## URI
NAME = "/get_xml_leaderboard"
def __init__(self, game):
"""Initialization"""
super(get_xml_leaderboard, self).__init__()
self._game = game
def content(self):
"""The content of the service"""
return self._game.get_xml_leaderboard()
class check_move_question(XMLService):
"""Check if the user need to move to next question"""
## URI
NAME = "/check_move_question"
def __init__(self, game):
"""Initialization"""
super(check_move_question, self).__init__()
self._game = game
def content(self):
"""The content of the service"""
return util.boolean_to_xml(
self._game.check_timer_change() or
self._game.check_all_players_answered()
)
class get_score(XMLService):
"""Return the score of the player who asked for it
GamePlayer"""
## URI
NAME = "/get_score"
def __init__(self, game):
"""Initialization"""
super(get_score, self).__init__()
self._game = game
def content(self):
"""The content of the service"""
root = ElementTree.Element("Root")
ElementTree.SubElement(
root,
"score_place",
{
"score": str(self._game.get_score()),
"place": str(self._game.get_place())
}
)
return util.to_string(root)
class start_question(Service):
"""Save the time the question started
GameMaster"""
## URI
NAME = "/start_question"
def __init__(self, game):
"""Initialization"""
super(start_question, self).__init__()
game.start_question()
class get_answers(XMLService):
"""Return the right answers"""
## URI
NAME = "/get_answers"
def __init__(self, game):
"""Initialization"""
super(get_answers, self).__init__()
self._game = game
def content(self):
"""The content of the service"""
root = ElementTree.Element("Root")
for letter in self._game.get_answers():
ElementTree.SubElement(root, "answer", {"answer": letter})
return util.to_string(root)
class get_title(XMLService):
"""Return the title of the question"""
## URI
NAME = "/get_title"
def __init__(self, game):
"""Initialization"""
super(get_title, self).__init__()
self._game = game
def content(self):
"""The content of the service"""
return self._game.get_title()