-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuzzergame.py
More file actions
141 lines (116 loc) · 4.13 KB
/
Copy pathbuzzergame.py
File metadata and controls
141 lines (116 loc) · 4.13 KB
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
import time
from logger import Logger
class BuzzerHardwareWrapper:
def __init__(self, annode, r, g, b, button, is_rgb_led):
self.annode = annode
self.r = r
self.g = g
self.b = b
self.button = button
self.is_rgb_led = is_rgb_led
self.annode.off()
def off(self):
if self.is_rgb_led:
self.annode.off()
self.r.off()
self.b.off()
self.g.off()
else:
self.r.off()
def on(self):
if self.is_rgb_led:
self.annode.off()
self.r.off()
self.b.on()
self.g.on()
else:
self.r.on()
def blink_r_g_b(self):
if self.is_rgb_led:
self.annode.off()
self.r.on()
time.sleep(0.25)
self.r.off()
self.b.on()
time.sleep(0.25)
self.b.off()
self.g.on()
time.sleep(0.25)
self.g.off()
else:
self.r.on()
def blink_green(self):
if self.is_rgb_led:
self.annode.off()
self.g.on()
time.sleep(1)
self.g.off()
self.g.on()
time.sleep(1)
self.g.off()
self.g.on()
time.sleep(1)
self.g.off()
else:
self.g.on()
def blink_red(self):
if self.is_rgb_led:
self.annode.off()
self.r.on()
time.sleep(1)
self.r.off()
self.r.on()
time.sleep(1)
self.r.off()
self.r.on()
time.sleep(1)
self.r.off()
else:
self.r.on()
class BuzzerGame:
def __init__(self, hwWrapper, clientHandler):
self.logger = Logger()
self.caller = "BuzzerGame"
self.hwWrapper = hwWrapper
self.is_game_active = True
self.clientHandler = clientHandler
self.clientHandler.client_timeout_handler = self.__handle_client_timeout__
self.clientHandler.client_said_hello_handler = self.__handle_client_hello__
def switch_on_led(self):
self.hwWrapper.on()
self.logger.debug(self.caller, "LED switched on")
def switch_off_led(self):
self.hwWrapper.off()
self.logger.debug(self.caller, "LED switched off")
def stop_game(self):
self.switch_off_led()
self.is_game_active = False
self.switch_off_led()
self.clientHandler.clear_all_commands()
def start_game(self):
self.is_game_active = True
self.__start_light_for_random_client__()
def __handle_client_hello__(self, client_name):
client = self.clientHandler.clients[client_name]
self.logger.debug(self.caller, "received a command [{}] from client [{}]".format(client.command, client.name))
if client.command == "light_on" and client.button_pressed is True:
self.logger.info(self.caller, "Someone hit the right buzzer [{}] :). Going to active another one".format(client.name))
client.reset()
self.__handle_filled_command_queue()
def __handle_client_timeout__(self, client_name):
self.logger.warning(self.caller, "Client [{}] has reached a timeout".format(client_name))
self.__handle_filled_command_queue()
def __handle_filled_command_queue(self):
if self.is_game_active and self.clientHandler.any_client_with_a_command() is False:
self.logger.debug(self.caller, "There are no clients left with a command ... ")
self.__start_light_for_random_client__()
def __start_light_for_random_client__(self):
if self.clientHandler.any_clients():
client = self.clientHandler.get_random_client()
self.clientHandler.add_command(client.name, "light_on")
self.logger.info(self.caller, "Start a new game and Client [{}] should switch on the light".format(client.name))
else:
self.logger.info(self.caller, "Couldnt start game because no clients have registered so far...")
def __start_game_in_thread__(self):
self.switch_on_led()
return ""