[84123db] | 1 | import json
|
---|
[37ae3b7] | 2 | from copy import deepcopy
|
---|
[3b76475] | 3 | from flowtimer.Phase import Phase
|
---|
[84123db] | 4 |
|
---|
| 5 |
|
---|
| 6 | class RecurringPhaseSequence:
|
---|
| 7 |
|
---|
[37ae3b7] | 8 | @classmethod
|
---|
| 9 | def from_json(cls, a_json_string):
|
---|
| 10 | def custom_object_hook(d):
|
---|
[daa2276] | 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'])
|
---|
[37ae3b7] | 15 | return d
|
---|
| 16 | return json.loads(a_json_string, object_hook=custom_object_hook)
|
---|
| 17 |
|
---|
| 18 | @classmethod
|
---|
| 19 | def default_json_string(cls):
|
---|
[daa2276] | 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})
|
---|
[37ae3b7] | 25 |
|
---|
[f959488] | 26 | @classmethod
|
---|
| 27 | def default(cls):
|
---|
| 28 | return cls.from_json(cls.default_json_string())
|
---|
| 29 |
|
---|
[84123db] | 30 | def __init__(self, phase_list, repetitions):
|
---|
[f959488] | 31 | assert repetitions > 0
|
---|
| 32 | assert phase_list is not []
|
---|
| 33 | self.state = "initial"
|
---|
[84123db] | 34 | self.phase_list = phase_list
|
---|
[f959488] | 35 | self.current_phase = phase_list[0]
|
---|
| 36 | self.initial_repetitions = repetitions
|
---|
| 37 | self.passes_left = repetitions
|
---|
[84123db] | 38 |
|
---|
| 39 | def to_json(self):
|
---|
| 40 | return json.dumps(self.__dict__, default=lambda each: each.to_json())
|
---|
| 41 |
|
---|
[daa2276] | 42 | def current_phase_number(self):
|
---|
| 43 | return self.phase_list.index(self.current_phase)
|
---|
| 44 |
|
---|
| 45 | def phases_left_in_pass(self):
|
---|
| 46 | return len(self.upcoming_phases_in_pass())
|
---|
| 47 |
|
---|
[f959488] | 48 | def upcoming_phases_in_pass(self):
|
---|
[daa2276] | 49 | if self.current_phase_number() < len(self.phase_list) - 1:
|
---|
| 50 | return self.phase_list[self.current_phase_number()+1:]
|
---|
[f959488] | 51 | return []
|
---|
| 52 |
|
---|
[daa2276] | 53 | @property
|
---|
[f959488] | 54 | def ticks_left(self):
|
---|
[daa2276] | 55 | return (
|
---|
| 56 | (self.passes_left-1) * sum([each.initial_ticks for each in self.phase_list]) +
|
---|
| 57 | self.current_phase.ticks_left +
|
---|
| 58 | sum([each.ticks_left for each in self.upcoming_phases_in_pass()]))
|
---|
[f959488] | 59 |
|
---|
| 60 | def finished(self):
|
---|
| 61 | return (self.passes_left < 1) and (not self.upcoming_phases_in_pass())
|
---|
| 62 |
|
---|
| 63 | def abort(self):
|
---|
| 64 | self.current_phase.abort()
|
---|
| 65 | self.state = "finished"
|
---|
| 66 |
|
---|
| 67 | def tick(self, ticks):
|
---|
| 68 | if not self.finished():
|
---|
| 69 | result = self.current_phase.tick(ticks)
|
---|
| 70 | print("Current phase", self.current_phase)
|
---|
| 71 | print("Passes left:", self.passes_left)
|
---|
| 72 | if self.current_phase.finished():
|
---|
| 73 | if self.upcoming_phases_in_pass():
|
---|
| 74 | self.current_phase.reset()
|
---|
| 75 | self.current_phase = self.upcoming_phases_in_pass()[0]
|
---|
| 76 | self.current_phase.start()
|
---|
| 77 | print("New phase:", self.current_phase)
|
---|
| 78 | self.tick(abs(result))
|
---|
| 79 | return True
|
---|
| 80 | self.passes_left -= 1
|
---|
| 81 | if self.finished():
|
---|
| 82 | self.state = "finished"
|
---|
| 83 | else:
|
---|
| 84 | self.current_phase.reset()
|
---|
| 85 | self.current_phase = self.phase_list[0]
|
---|
| 86 | return True
|
---|
| 87 |
|
---|
[84123db] | 88 | def unrolled(self):
|
---|
[daa2276] | 89 | return [deepcopy(seq) for seq in [each for each in self.initial_repetitions * self.phase_list]]
|
---|