source: flowtimer/flowtimer/RecurringPhaseSequence.py@ 44c3377

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

initial_repetitions method added to Phase

  • Property mode set to 100644
File size: 3.3 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['phase_list'], d['initial_repetitions'])
15 return d
16 return json.loads(a_json_string, object_hook=custom_object_hook)
17
18 @classmethod
19 def default_json_string(cls):
20 return json.dumps({"phase_list": [{"title": "Huddle", "initial_ticks": 10},
21 {"title": "Tasking", "initial_ticks": 5},
22 {"title": "Work", "initial_ticks": 45},
23 {"title": "Break", "initial_ticks": 15}],
24 "initial_repetitions": 3})
25
26 @classmethod
27 def default(cls):
28 return cls.from_json(cls.default_json_string())
29
30 def __init__(self, phase_list, repetitions):
31 assert repetitions > 0
32 assert phase_list is not []
33 self.state = "initial"
34 self.phase_list = phase_list
35 self.current_phase = phase_list[0]
36 self.initial_repetitions = repetitions
37 self.passes_left = repetitions
38
39 def to_json(self):
40 return json.dumps(self.__dict__, default=lambda each: each.to_json())
41
42 def title(self):
43 return self.current_phase.title
44
45 def current_phase_number(self):
46 return self.phase_list.index(self.current_phase)
47
48 def phases_left_in_pass(self):
49 return len(self.upcoming_phases_in_pass())
50
51 def upcoming_phases_in_pass(self):
52 if self.current_phase_number() < len(self.phase_list) - 1:
53 return self.phase_list[self.current_phase_number()+1:]
54 return []
55
56 @property
57 def ticks_left(self):
58 return (
59 (self.passes_left-1) * sum([each.initial_ticks for each in self.phase_list]) +
60 self.current_phase.ticks_left +
61 sum([each.ticks_left for each in self.upcoming_phases_in_pass()]))
62
63 def finished(self):
64 return (self.passes_left < 1) and (not self.upcoming_phases_in_pass())
65
66 def abort(self):
67 self.current_phase.abort()
68 self.state = "finished"
69
70 def tick(self, ticks):
71 if not self.finished():
72 result = self.current_phase.tick(ticks)
73 print("Current phase", self.current_phase)
74 print("Passes left:", self.passes_left)
75 if self.current_phase.finished():
76 if self.upcoming_phases_in_pass():
77 self.current_phase.reset()
78 self.current_phase = self.upcoming_phases_in_pass()[0]
79 self.current_phase.start()
80 print("New phase:", self.current_phase)
81 self.tick(abs(result))
82 return True
83 self.passes_left -= 1
84 if self.finished():
85 self.state = "finished"
86 else:
87 self.current_phase.reset()
88 self.current_phase = self.phase_list[0]
89 return True
90
91 def unrolled(self):
92 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.