source: flowtimer/flowtimer/RecurringPhaseSequence.py@ 5741f6d

guix
Last change on this file since 5741f6d was 5741f6d, checked in by Enrico Schwass <ennoausberlin@…>, 9 months ago

add is_sequence to Phase and RecurringPhaseSequence

  • Property mode set to 100644
File size: 3.5 KB
Line 
1import json
2from copy import deepcopy
3from flowtimer.Phase import Phase
4
5
6class RecurringPhaseSequence:
7
8 @classmethod
9 def from_json(cls, a_json_string):
10 def custom_object_hook(d):
11 if 'title' in d and 'initial_ticks' in d:
12 return Phase(d['title'], d['initial_ticks'])
13 if 'phase_list' in d and 'initial_repetitions' in d:
14 return RecurringPhaseSequence(d["title"], d['phase_list'], d['initial_repetitions'])
15 print("Wrong format")
16 return d
17 return json.loads(a_json_string, object_hook=custom_object_hook)
18
19 @classmethod
20 def default_json_string(cls):
21 return json.dumps({"title": "default",
22 "phase_list": [{"title": "Huddle", "initial_ticks": 10},
23 {"title": "Tasking", "initial_ticks": 5},
24 {"title": "Work", "initial_ticks": 45},
25 {"title": "Break", "initial_ticks": 15}],
26 "initial_repetitions": 3})
27
28 @classmethod
29 def default(cls):
30 return cls.from_json(cls.default_json_string())
31
32 def __init__(self, title, phase_list, repetitions):
33 assert repetitions > 0
34 assert phase_list is not []
35 self._title = title
36 self.state = "initial"
37 self.phase_list = phase_list
38 self.current_phase = phase_list[0]
39 self.initial_repetitions = repetitions
40 self.passes_left = repetitions
41
42 def to_json(self):
43 return json.dumps(self.__dict__, default=lambda each: each.to_json())
44
45 @property
46 def title(self):
47 return self.current_phase.title
48
49 def is_sequence(self):
50 return True
51
52 def current_phase_number(self):
53 return self.phase_list.index(self.current_phase)
54
55 def phases_left_in_pass(self):
56 return len(self.upcoming_phases_in_pass())
57
58 def upcoming_phases_in_pass(self):
59 if self.current_phase_number() < len(self.phase_list) - 1:
60 return self.phase_list[self.current_phase_number()+1:]
61 return []
62
63 @property
64 def initial_ticks(self):
65 return sum([each.initial_ticks for each in self.phase_list])
66
67 @property
68 def ticks_left(self):
69 return (
70 (self.passes_left-1) * sum([each.initial_ticks for each in self.phase_list]) +
71 self.current_phase.ticks_left +
72 sum([each.ticks_left for each in self.upcoming_phases_in_pass()]))
73
74 def finished(self):
75 return (self.passes_left < 1) and (not self.upcoming_phases_in_pass())
76
77 def abort(self):
78 self.current_phase.abort()
79 self.state = "finished"
80
81 def start(self):
82 self.state = "running"
83
84 def tick(self, ticks):
85 if not self.finished():
86 result = self.current_phase.tick(ticks)
87 if self.current_phase.finished():
88 if self.upcoming_phases_in_pass():
89 self.current_phase.reset()
90 self.current_phase = self.upcoming_phases_in_pass()[0]
91 self.current_phase.start()
92 self.tick(abs(result))
93 return True
94 self.passes_left -= 1
95 if self.finished():
96 self.state = "finished"
97 else:
98 self.current_phase.reset()
99 self.current_phase = self.phase_list[0]
100 return True
101
102 def unrolled(self):
103 return [deepcopy(seq) for seq in [each for each in self.initial_repetitions * self.phase_list]]
Note: See TracBrowser for help on using the repository browser.