-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrainCrampTimerRuleSet.py
54 lines (40 loc) · 1.73 KB
/
BrainCrampTimerRuleSet.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
from core.rules import RuleSet
from core.context import TimeTrigger
from datetime import datetime, timedelta
class BrainCrampTimerRuleSet(RuleSet):
def __init__(self, name):
super().__init__(name)
self.reset()
def reset(self):
self.bars = {
'|1': None,
'|2': None,
'|3': None,
'|4': None,
'|5': None,
}
self.interval = 0.0
def examine(self, context):
triggered = super().examine(context)
now = datetime.now()
for trigger in triggered:
if trigger == '|1':
self.reset()
context.time_triggers.clear()
self.bars[trigger] = now
context.time_triggers.append(TimeTrigger(now, '!1.1'))
context.time_trigger_next = context.time_triggers.popleft()
elif trigger.startswith('|'):
current_bar = int(trigger[-1])
previous_bar = '%s%d' % (trigger[:-1], current_bar - 1)
self.bars[trigger] = now
if self.bars[previous_bar]:
self.interval = (now - self.bars[previous_bar]).total_seconds()
eighth = self.interval / 8
for i in range(8):
context.time_triggers.append(TimeTrigger(now + timedelta(seconds=(i * eighth)), '!%d.%d' % (current_bar, i + 1)))
context.time_trigger_next = context.time_triggers.popleft()
if current_bar == 5:
for i in range(8):
context.time_triggers.append(TimeTrigger(now + timedelta(seconds=((i + 8) * eighth)), '!%d.%d' % (current_bar + 1, i + 1)))
return triggered