-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.py
197 lines (134 loc) · 5.86 KB
/
timer.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
import pygame
import time
import calcutils
import crf
from assets.puzzles import puzzle, abstract_puzzle
from assets.stackmat import stackmat
DEVICE_NUM = 30
class Timer:
def __init__(self, game):
self.started_timestamp = time.time_ns()
self.running = False
self.ms = 0
self.timing_method = 0 # 0 = spacebar, 1 = stackmat
self.ready = 0
self.started_timestamp_spacebar = time.time_ns()
self.stackmat = None
self.error = None
self.game = game
self.current_scramble = ""
self.puzzle: abstract_puzzle.AbstractPuzzle = puzzle.load_puzzle(self.game.config.current_event)
self.rescramble()
self.crf_handler = crf.CrfHandler()
self.reset(False)
def reset(self, run):
self.started_timestamp = time.time_ns()
self.running = run
self.error = None
if self.stackmat is not None:
self.stackmat.close()
if self.timing_method == 0:
self.stackmat = None
elif self.timing_method == 1:
try:
self.stackmat = stackmat.Stackmat(DEVICE_NUM)
except Exception as e:
self.error = f'Error initialising stackmat: {repr(e)}'
else:
assert False, f"Unknown timing method {self.timing_method}"
def update(self, events: list[pygame.event.Event]):
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if self.running:
self.stop()
else:
self.started_timestamp_spacebar = time.time_ns()
self.ready = 1
self.ms = 0
if event.key == pygame.K_s and not event.mod & pygame.KMOD_CTRL:
self.timing_method = 0 if self.timing_method == 1 else 1
self.reset(False)
if event.key == pygame.K_r:
self.rescramble()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
if self.ready == 1:
self.ready = 0
elif self.ready == 2:
self.ready = 3
self.on_start()
if self.timing_method == 0:
if pygame.key.get_pressed()[pygame.K_SPACE]:
if round(time.time_ns() - self.started_timestamp_spacebar) / 1e6 > 550 and self.started_timestamp_spacebar != 0:
self.ready = 2
self.started_timestamp = time.time_ns()
if self.running:
self.ms = round((time.time_ns() - self.started_timestamp) / 1e6)
elif self.timing_method == 1:
if self.stackmat is not None and self.stackmat.state is not None:
self.ms = self.stackmat.state.time
self.error = None
if not self.running:
if self.stackmat.state.state == "S":
self.ready = -1
elif self.stackmat.state.state == "L" or self.stackmat.state.state == "R" or self.stackmat.state.state == "I":
if self.stackmat.state.time <= 0:
if self.ready < 2:
self.ready = 0
else:
self.ready = 3
self.on_start()
elif self.stackmat.state.state == "C":
if self.stackmat.state.time <= 0:
self.ready = 1
elif self.stackmat.state.state == "A":
if self.stackmat.state.time <= 0:
self.ready = 2
elif self.stackmat.state.state == " ":
self.ready = 3
self.on_start()
else:
if self.stackmat.state.frozen or self.stackmat.state.state == "S": # otherwise without frozen statement, it won't detect if timer is stopped because the timer doesn't override S (if left sensor is being pressed, it sends L even if the timer has stopped)
# print("freeze detected!")
self.stop()
else:
self.ms = -1
if self.error is None:
self.error = 'No data from stackmat!'
else:
assert False, f"Unknown timing method {self.timing_method}"
def on_start(self):
self.running = True
self.game.on_timer_start()
def stop(self):
self.running = False
self.ready = -1
self.started_timestamp_spacebar = 0
self.crf_handler.write(crf.Result([self.crf_handler.get_index(), self.ms, self.current_scramble, "none"]))
self.rescramble()
self.refresh_stats()
self.game.on_timer_stop()
def refresh_stats(self):
for stat in self.game.time_stats.stats:
stat.refresh(self.crf_handler.get_all())
def get_color(self): # ???
pass
def rescramble(self):
self.current_scramble = self.puzzle.scrambler.get_random_scramble()
self.puzzle.emulator.reset()
self.puzzle.emulator.convert_scramble(self.current_scramble)
def refresh_puzzle(self):
self.puzzle: abstract_puzzle.AbstractPuzzle = puzzle.load_puzzle(self.game.config.current_event)
class TimeStats:
def __init__(self):
self.stats = [TimeStats.Stat("ao", 5),
TimeStats.Stat("ao", 12)]
class Stat:
def __init__(self, type, amount):
self.type = type
self.amount = amount
self.ms = 0
def refresh(self, times: list[crf.Result]):
if self.type == "ao":
self.ms = calcutils.get_average_of(times, self.amount)