-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaction.py
103 lines (78 loc) · 2.24 KB
/
action.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from .utils import *
def unburyCard(card):
if card.queue in ({QUEUE_USER_BURIED}, {QUEUE_SCHED_BURIED}):
card.queue = card.type
card.flush()
return True
return False
def buryCard(card):
if card.queue not in ({QUEUE_USER_BURIED}, {QUEUE_SCHED_BURIED}):
card.queue = QUEUE_USER_BURIED
card.flush()
return True
return False
def unsuspendCard(card):
if card.queue == QUEUE_SUSPENDED:
card.queue = card.type
card.flush()
return True
return False
def suspendCard(card):
if card.queue != QUEUE_SUSPENDED:
card.queue = QUEUE_SUSPENDED
card.flush()
return True
return False
def flag(flagNumber):
def aux(card):
if card.flag % 8 == flagNumber:
card.flag -= card.flag % 8
card.flush()
return True
return False
actionMethods = {
"suspend": suspendCard,
"unsuspend": unsuspendCard,
"bury": buryCard,
"unbury": unburyCard,
** {f"flag{i}": flag(i) for i in range(5)},
}
def applyActionToCard(card, method):
if card is not None:
return method(card)
return False
def applyActionToNote(note, action):
actionName = action.get("action")
method = actionMethods.get(actionName)
if method is None:
return
cardTypes = action["cards"]
if isinstance(cardTypes, str):
cardTypes = [cardTypes]
someChange = False
for cardName in cardTypes:
card = getCard(note, cardName)
someChange = applyActionToCard(card, method) or someChange
return someChange
def applyActions(note, actions):
someChange = False
if isinstance(actions, dict):
actions = [actions]
for action in actions:
if applyActionToNote(note, action):
someChange = True
return someChange
# Reverse
def reverseActionName(actionName):
if actionName.startswith("un"):
return actionName[2:]
return f"un{actionName}"
def reverseAtomicAction(action):
return {
**action,
"action": reverseActionName(action["action"])
}
def reverseAction(actions):
if isinstance(actions, list):
actions = [actions]
return [reverseAtomicAction(atomicAction) for atomicAction in actions]