-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
131 lines (105 loc) · 4.81 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
from enum import Enum
from display import Display, Media
from led_button import LedButton
from music_player import MusicPlayer
from stopwatch import StopwatchState
from player_module import PlayerModule
import logging, sys
import asyncio
import time
from voice import Voice
class Game:
def __init__(self, voice: Voice, led_matrix: Display, start_button: LedButton, music_player: MusicPlayer):
self.voice = voice
self.led_matrix = led_matrix
self.start_button = start_button
self.time_modules = []
self.lights_are_out = False
self.finished_players = {}
self.music_player = music_player
self.music_player.f1Theme()
self.music_is_fading = False
def add_player_module(self, module: PlayerModule):
module.button_callback = self.player_button_pressed
self.time_modules.append(module)
async def blink_player_buttons(self):
[await asyncio.tasks.create_task(time_module.led_button.start_blink(500)) for time_module in self.time_modules]
async def turn_off_non_playing(self):
[await asyncio.tasks.create_task(player.led_button.off()) for player in self.get_players_not_playing()]
async def start_button_pressed(self):
logging.info(f"Start button pressed")
if self.get_state() is GameState.WaitingForPlayers:
await self.blink_player_buttons()
if self.get_state() is GameState.Ready:
await self.start_game()
elif self.get_state() is GameState.Stopped:
await self.reset_game()
else:
logging.info("Can not start")
async def player_button_pressed(self, color, state: StopwatchState, time):
logging.info(f"{color}: {time} - {state}")
state = self.get_state()
if state is GameState.WaitingForPlayers or state is GameState.Ready:
await self.join_player(color)
elif state is GameState.Started:
await self.finish_player(color)
async def join_player(self, color):
logging.info(f"Player joined: {color}")
await self.voice.speak(f"{color} player joined the game.")
player: PlayerModule = list(filter(lambda x: (x.stopwatch.color is color), self.time_modules))[0]
await player.stopwatch.reset()
await player.led_button.on()
async def finish_player(self, color):
player: PlayerModule = list(filter(lambda x: (x.stopwatch.color is color), self.time_modules))[0]
await player.stopwatch.stop()
logging.info(f"Player finished: {color} ({player.stopwatch.calculated_time})")
self.finished_players[color] = player.stopwatch.calculated_time
async def start_game(self):
self.music_is_fading = True
self.music_player.fade(fade_time_ms=9000, fade_callback=self.music_has_faded, stop_music=True)
asyncio.tasks.create_task(self.voice.speak("Game is starting"))
await self.turn_off_non_playing()
await self.start_button.off()
self.lights_are_out = False
self.led_matrix.show(Media.StartSequence, callback=self.lights_out)
while self.music_is_fading is True:
await asyncio.sleep(.01)
self.music_player.f1ThemeStart()
while self.lights_are_out is False:
await asyncio.sleep(.01)
start_time = time.time_ns()
[await player.start(start_time) for player in self.get_players_ready()]
def lights_out(self):
self.lights_are_out = True
def music_has_faded(self):
self.music_is_fading = False
#async def start_formation_lap(self):
async def reset_game(self):
[await time_module.off() for time_module in self.time_modules]
def get_players(self):
return self.time_modules
def get_players_started(self):
return list(filter(lambda x: (x.stopwatch.state == StopwatchState.Started), self.time_modules))
def get_players_finished(self):
return list(filter(lambda x: (x.stopwatch.state == StopwatchState.Stopped), self.time_modules))
def get_players_ready(self):
return list(filter(lambda x: (x.stopwatch.state == StopwatchState.Reset), self.time_modules))
def get_players_not_playing(self):
return list(filter(lambda x: (x.stopwatch.state == StopwatchState.Off), self.time_modules))
def get_state(self):
players_started = self.get_players_started()
if any(players_started):
return GameState.Started
players_finished = self.get_players_finished()
if any(players_finished):
return GameState.Stopped
players_ready = self.get_players_ready()
if any(players_ready):
return GameState.Ready
else:
return GameState.WaitingForPlayers
class GameState(Enum):
WaitingForPlayers = 0,
Ready = 1
Started = 2,
Stopped = 3