source: flowtimer/flowtimer/RecurringPhaseSequence.py@ d310de3

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

phase_list renamed to phases

Signed-off-by: Enrico Schwass <ennoausberlin@…>

  • Property mode set to 100644
File size: 4.6 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 'phases' in d and 'initial_repetitions' in d:
14 return RecurringPhaseSequence(d["title"], d['phases'], 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 "phases": [{"title": "Tasking", "initial_ticks": 5},
23 {"title": "Work", "initial_ticks": 45},
24 {"title": "Break", "initial_ticks": 15}],
25 "initial_repetitions": 3})
26
27 @classmethod
28 def default(cls):
29 return cls.from_json(cls.default_json_string())
30
31 def __init__(self, title, phases, repetitions):
32 assert repetitions > 0
33 assert phases is not []
34 self._title = title
35 self.state = "initial"
36 self.phases = phases
37 self.current_phase = phases[0]
38 self.initial_repetitions = repetitions
39 self.passes_left = repetitions
40
41 def to_json(self):
42 return json.dumps(self.__dict__, default=lambda each: each.to_json())
43
44 @property
45 def title(self):
46 return self._title
47
48 def __str__(self):
49 return ("Sequence title:" + self.title + "\n" + str(self.current_phase))
50
51 def is_sequence(self):
52 return True
53
54 def current_phase_number(self):
55 return self.phases.index(self.current_phase)
56
57 def phases_left_in_pass(self):
58 return len(self.upcoming_phases_in_pass())
59
60 def upcoming_phases_in_pass(self):
61 if self.current_phase_number() < len(self.phases) - 1:
62 return self.phases[self.current_phase_number()+1:]
63 return []
64
65 @property
66 def initial_ticks(self):
67 return sum([each.initial_ticks for each in self.phases])
68
69 @property
70 def ticks_left(self):
71 return (
72 (self.passes_left-1) * sum([each.initial_ticks for each in self.phases]) +
73 self.current_phase.ticks_left +
74 sum([each.ticks_left for each in self.upcoming_phases_in_pass()]))
75
76 def completed(self):
77 return ((self.passes_left < 1) and
78 (not self.upcoming_phases_in_pass() and
79 self.current_phase.completed()))
80
81 def abort(self):
82 self.current_phase.abort()
83 self.state = "aborted"
84
85 def start(self):
86 self.state = "running"
87
88 def skip(self):
89 if self.upcoming_phases_in_pass():
90 self.current_phase.reset()
91 self.current_phase = self.upcoming_phases_in_pass()[0]
92 return
93 else:
94 print("Sequence finished")
95 if self.passes_left == 0:
96 self.abort()
97 return
98 else:
99 self.passes_left -= 1
100 self.current_phase.reset()
101 self.current_phase = self.phases[0]
102
103 def advance_to_next_phase(self):
104 current_index = self.current_phase_number()
105 if current_index < len(self.phases) - 1:
106 # Move to the next phase in the sequence
107 self.current_phase.reset()
108 self.current_phase = self.phases[current_index + 1]
109 else:
110 # Completed a full sequence; check if more repetitions are needed
111 self.passes_left -= 1
112 if self.passes_left < 1:
113 self.state = "completed"
114 else:
115 self.current_phase.reset()
116 self.current_phase = self.phases[0] # Reset to the first phase
117
118 def tick(self, ticks):
119 print("Before tick")
120 print(self.current_phase)
121 print("Tick:", ticks)
122 if not self.completed():
123 result = self.current_phase.tick(ticks)
124 print("Result", result, "\n")
125 if self.current_phase.completed():
126 print(self.current_phase.title, "Phase Completed")
127 self.advance_to_next_phase()
128 print("Advanced to", self.current_phase, "\n")
129 result = self.tick(abs(result))
130 return
131 if self.completed():
132 self.advance_to_next_phase()
133 print("Passes left:", self.passes_left)
134
135 def unrolled(self):
136 return [deepcopy(seq) for seq in [each for each in self.initial_repetitions * self.phases]]
Note: See TracBrowser for help on using the repository browser.