generated from NCKU-CCS/TOC-Project-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm.py
382 lines (350 loc) · 14.5 KB
/
fsm.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from notes import containNotes, noteToNumber, notesToChord, chordList, chordToNote, chordListAlt, notesToScale, scaleList, scaleListAlt, scaleToNote
from transitions.extensions import GraphMachine
from utils import send_text_message, send_menu_carousel, send_chord, send_not_found, send_chord_note, send_scale, send_scale_note, send_fsm, send_demo
class TocMachine():
def __init__(self):
self.notes = []
self.chord = -1
self.scale = -1
self.machine = GraphMachine(
model=self,
**{
"states":["start", "menu", "chord", "chordResult", "chordNote", "chordNoteRootnote", "chordNoteType"
, "scale", "scaleResult", "scaleNote", "scaleNoteRootnote", "scaleNoteType", "fsm", "demo"],
"transitions":[
{
"trigger": "advance",
"source": "*",
"dest": "fsm",
"conditions": "is_going_to_fsm",
},
{
"trigger": "advance",
"source": "*",
"dest": "menu",
"conditions": "is_going_to_menu",
},
{
"trigger": "advance",
"source": "menu",
"dest": "chord",
"conditions": "is_going_to_chord",
},
{
"trigger": "advance",
"source": "menu",
"dest": "chordNote",
"conditions": "is_going_to_chordNote",
},
{
"trigger": "advance",
"source": "chord",
"dest": "chordResult",
"conditions": "is_going_to_chordResult",
},
{
"trigger": "advance",
"source": "chordResult",
"dest": "chordResult",
"conditions": "is_going_to_chordResult",
},
{
"trigger": "advance",
"source": "chordNote",
"dest": "chordNoteRootnote",
"conditions": "is_going_to_chordNoteRootnote",
},
{
"trigger": "advance",
"source": "chordNoteRootnote",
"dest": "chordNoteType",
"conditions": "is_going_to_chordNoteType",
},
{
"trigger": "advance",
"source": "chordNoteType",
"dest": "chordNote",
"conditions": "is_going_back_to_chordNote",
},
{
"trigger": "advance",
"source": "chordNoteType",
"dest": "chordNoteType",
"conditions": "is_going_to_chordNoteType",
},
{
"trigger": "advance",
"source": "chordNoteType",
"dest": "chordNoteRootnote",
"conditions": "is_going_to_chordNoteRootnote",
},
{
"trigger": "advance",
"source": "menu",
"dest": "scale",
"conditions": "is_going_to_scale",
},
{
"trigger": "advance",
"source": "menu",
"dest": "scaleNote",
"conditions": "is_going_to_scaleNote",
},
{
"trigger": "advance",
"source": "scale",
"dest": "scaleResult",
"conditions": "is_going_to_scaleResult",
},
{
"trigger": "advance",
"source": "scaleResult",
"dest": "scaleResult",
"conditions": "is_going_to_scaleResult",
},
{
"trigger": "advance",
"source": "scaleNote",
"dest": "scaleNoteRootnote",
"conditions": "is_going_to_scaleNoteRootnote",
},
{
"trigger": "advance",
"source": "scaleNoteRootnote",
"dest": "scaleNoteType",
"conditions": "is_going_to_scaleNoteType",
},
{
"trigger": "advance",
"source": "scaleNoteType",
"dest": "scaleNote",
"conditions": "is_going_back_to_scaleNote",
},
{
"trigger": "advance",
"source": "scaleNoteType",
"dest": "scaleNoteType",
"conditions": "is_going_to_scaleNoteType",
},
{
"trigger": "advance",
"source": "scaleNoteType",
"dest": "scaleNoteRootnote",
"conditions": "is_going_to_scaleNoteRootnote",
},
{
"trigger": "advance",
"source": "chordNoteType",
"dest": "chordNote",
"conditions": "is_going_to_change_rootNote",
},
{
"trigger": "advance",
"source": "chordNoteType",
"dest": "chordNoteRootnote",
"conditions": "is_going_to_change_type",
},
{
"trigger": "advance",
"source": "scaleNoteType",
"dest": "scaleNote",
"conditions": "is_going_to_change_rootNote",
},
{
"trigger": "advance",
"source": "scaleNoteType",
"dest": "scaleNoteRootnote",
"conditions": "is_going_to_change_type",
},
{
"trigger": "advance",
"source": "fsm",
"dest": "menu",
"conditions": "is_going_to_menu",
},
{
"trigger": "advance",
"source": "menu",
"dest": "demo",
"conditions": "is_going_to_demo",
},
{
"trigger": "advance",
"source": "demo",
"dest": "menu",
"conditions": "is_going_to_menu",
},
],
"initial":"start",
"auto_transitions":False,
"show_conditions":True,
},
)
#condition
def is_going_to_menu(self, event):
text = event.message.text
return "選單" in text
def is_going_to_chord(self, event):
text = event.message.text
return "和弦" in text and "組成音" not in text
def is_going_to_chordNote(self, event):
text = event.message.text
return "和弦" in text and "組成音" in text
def is_going_to_chordResult(self, event):
print("!" + str(self.notes))
text = event.message.text
if(len(containNotes(text)) != 0): self.notes = containNotes(text)
return len(containNotes(text)) != 0
def is_going_to_chordNoteRootnote(self, event):
text = event.message.text
if(len(containNotes(text)) != 0): self.notes = containNotes(text)
return len(containNotes(text)) != 0
def is_going_to_chordNoteType(self, event):
self.chord = -1
text = event.message.text
text = text.strip()
for i in range(len(chordList)):
for j in range(len(chordList[i])):
if text == chordList[i][j]:
self.chord = i
break
else:
continue
break
if self.chord == -1:
for i in range(len(chordListAlt)):
for j in range(len(chordListAlt[i])):
if str(text).lower() == chordListAlt[i][j]:
self.chord = i
break
else:
continue
break
return self.chord != -1
def is_going_back_to_chordNote(self, event):
text = event.message.text
return "重來" in text or "再來" in text or "重查" in text
def is_going_to_scale(self, event):
text = event.message.text
return "音階" in text and "組成音" not in text
def is_going_to_scaleNote(self, event):
text = event.message.text
return "音階" in text and "組成音" in text
def is_going_to_scaleResult(self, event):
text = event.message.text
if(len(containNotes(text)) != 0): self.notes = containNotes(text)
return len(containNotes(text)) != 0
def is_going_to_scaleNoteRootnote(self, event):
text = event.message.text
if(len(containNotes(text)) != 0): self.notes = containNotes(text)
return len(containNotes(text)) != 0
def is_going_to_scaleNoteType(self, event):
self.scale = -1
text = event.message.text
text = text.strip()
for i in range(len(scaleList)):
for j in range(len(scaleList[i])):
if text == scaleList[i][j]:
self.scale = i
break
else:
continue
break
if self.scale == -1:
for i in range(len(scaleListAlt)):
for j in range(len(scaleListAlt[i])):
if str(text).lower() == scaleListAlt[i][j]:
self.scale = i
break
else:
continue
break
return self.scale != -1
def is_going_back_to_scaleNote(self, event):
text = event.message.text
return "重來" in text or "再來" in text or "重查" in text
def is_going_to_change_rootNote(self, event):
text = event.message.text
return "更改" in text and "根音" in text
def is_going_to_change_type(self, event):
text = event.message.text
return "更改" in text and "種類" in text
def is_going_to_fsm(self, event):
text = event.message.text
return "fsm" in str(text).lower()
def is_going_to_demo(self, event):
text = event.message.text
return "demo" in str(text).lower()
#on enter
def on_enter_menu(self, event):
print("I'm entering menu")
reply_token = event.reply_token
send_menu_carousel(reply_token)
def on_enter_chord(self, event):
print("I'm entering chord")
reply_token = event.reply_token
text = "請輸入數個音符(A~G,可搭配升降記號)。我會想辦法告訴你他們可以組成的和弦。"
send_text_message(reply_token, text)
def on_enter_chordNote(self, event):
print("I'm entering chordNote")
reply_token = event.reply_token
text = "請先輸入和弦的「根音」,或點選下方常用音符。"
send_text_message(reply_token, text, "note")
def on_enter_chordResult(self, event):
print("I'm entering chordResult")
reply_token = event.reply_token
print(self.notes)
root_note, whichChord = notesToChord(self.notes)
if whichChord > -1:
send_chord(reply_token, root_note, whichChord)
else:
send_not_found(reply_token)
def on_enter_chordNoteRootnote(self, event):
print("I'm entering chordNoteRootnote")
reply_token = event.reply_token
text = "請輸入和弦「種類」,或點選下方常用和弦。"
send_text_message(reply_token, text, "chord")
def on_enter_chordNoteType(self, event):
print("I'm entering chordNoteType")
reply_token = event.reply_token
print(self.notes)
text = chordToNote(self.notes, self.chord)
send_chord_note(reply_token, self.notes[0], self.chord, text)
def on_enter_scale(self, event):
print("I'm entering scale")
reply_token = event.reply_token
text = "請輸入數個音符(A~G,可搭配升降記號)。我會想辦法告訴你他們可以組成的音階。"
send_text_message(reply_token, text)
def on_enter_scaleNote(self, event):
print("I'm entering scaleNote")
reply_token = event.reply_token
text = "請先輸入音階的「根音」,或點選下方常用音符。"
send_text_message(reply_token, text, "note")
def on_enter_scaleResult(self, event):
print("I'm entering scaleResult")
reply_token = event.reply_token
print(self.notes)
root_note, whichScale = notesToScale(self.notes)
if whichScale > -1:
send_scale(reply_token, root_note, whichScale)
else:
send_not_found(reply_token)
def on_enter_scaleNoteRootnote(self, event):
print("I'm entering scaleNoteRootnote")
reply_token = event.reply_token
text = "請輸入音階「種類」,或點選下方常用音階。"
send_text_message(reply_token, text, "scale")
def on_enter_scaleNoteType(self, event):
print("I'm entering chordNoteType")
reply_token = event.reply_token
print(self.notes)
text = scaleToNote(self.notes, self.scale)
send_scale_note(reply_token, self.notes[0], self.scale, text)
def on_enter_fsm(self, event):
print("I'm entering fsm")
reply_token = event.reply_token
send_fsm(reply_token)
def on_enter_demo(self, event):
print("I'm entering demo")
reply_token = event.reply_token
send_demo(reply_token)