-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquizstuff.py
More file actions
911 lines (820 loc) · 30.9 KB
/
quizstuff.py
File metadata and controls
911 lines (820 loc) · 30.9 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
import time
import UserInfo
import actions
import botutils
import messagestore
import scheduled_events
import scores
from actions import TriggeredAction
from telegram import Message as TGMessage
from botstate import BotState
from edit_sessions import EditSession
class Question:
"""Represents a single Quiz question"""
@classmethod
def fetch(cls, quizid:str,index:int):
"""Fetches a specific question from a quiz
@param quizid: ID of the quiz
@param index: number of the question
"""
res = BotState.DBLink.execute("""
SELECT question,options,correct_option,extraid
FROM quiz_questions
WHERE quiz_name = ?
AND ordinal = ?""", (quizid, index))
row = res.fetchone()
if not row:
return
text, options, correct, extra_id = row
return cls(quizid,index,text,options.split("|"),correct,extra_id)
def __init__(self, quiz_id:str, index:int, text:str, choices:list[str], correct:int, attachment:str):
self.parent_quiz = quiz_id
"""Quiz this belongs to"""
self.index = index
"""Number of the question"""
self.text = text
"""Question text"""
self.choices = choices
"""Question answers"""
self.correct_answer = correct
"""Number of the correct option"""
self.attachment = attachment
"""Saved Message ID attached to the question"""
self.attachment_emoji = ""
"""Attachment type emoji, set by the quiz when loaded."""
def attach_media(self, mediaid:str):
"""Attaches a specific Saved Message to this question"""
BotState.DBLink.execute("""
UPDATE quiz_questions
SET extraid = ?
WHERE quiz_name = ?
AND ordinal = ?""", (mediaid, self.parent_quiz, self.index))
BotState.write()
self.attachment = mediaid
class Quiz:
"""Represents a Quiz"""
def __init__(self, quiz_id:str, owner_id:int,created:float,title:str,question_time:float,questions:list[Question]):
self.id:str = quiz_id
"""This Quiz's ID."""
self.owner_id:int = owner_id
"""Quiz creator UID"""
self.creation_time:float = created
"""Quiz creation timestamp"""
self.title:str = title
"""Title of the quiz"""
self.question_time:float = question_time
"""Time to answer a question, in seconds"""
self.questions:list[Question] = questions
"""Questions belonging to the quiz"""
self.count = len(questions)
"""amount of questions"""
self.questions_in_mv2 = []
"""Questions in a nice to display list with MarkDownV2 escaping."""
for question in self.questions:
index = question.index + 1
text = botutils.MD(question.text)
emoji = question.attachment_emoji
self.questions_in_mv2.append((index, emoji, text))
@classmethod
def load(cls, quizid:str):
res = BotState.DBLink.execute("""
SELECT ownerid,created,title,question_time
FROM quizzes
WHERE name = ?
""", (quizid,))
row = res.fetchone()
if not row:
return
owner_id = int(row[0])
creation_time = float(row[1])
title = row[2]
question_time = float(row[3])
# fetch the questions
res = BotState.DBLink.execute("""
SELECT quiz_name, ordinal, question, options, correct_option, extraid
FROM quiz_questions
WHERE quiz_name = ?
ORDER BY ordinal ASC
""", (quizid,))
rows = res.fetchall()
qlist = []
if rows:
ms = messagestore.MessageStore(owner_id, owner_id)
for row in rows:
qdata = list(row)
qdata[1] = int(qdata[1])
qdata[3] = qdata[3].split("|")
q = Question(*qdata)
q.attachment_emoji = ms.get_type_emoji(q.attachment)
qlist.append(q)
return cls(quizid,owner_id,creation_time,title,question_time,qlist)
@classmethod
def create(cls, creator:int, quiz_id:str, title:str, timeout:int):
"""
The quiz will be created.
@param creator:
@param quiz_id:
@param title:
@param timeout:
@return:
"""
quiz = cls.load(quiz_id)
if quiz:
return None
BotState.DBLink.execute("""
INSERT INTO quizzes
VALUES (?,?,?,?,?)
""", (creator, time.time(), title, timeout, quiz_id))
BotState.write()
return cls.load(quiz_id)
@staticmethod
def find_by_owner(owner_id:int) -> list[str]:
"""
Finds all Quizes owned by a user.
@param owner_id: User to check.
@return: List of Quiz IDs found.
"""
res = BotState.DBLink.execute("""
SELECT title,name
FROM quizzes
WHERE ownerid = ?
""", (owner_id,))
rows = res.fetchall()
if not rows:
return []
return [row[0] for row in rows]
def rename(self, newname: str):
"""Renames the Quiz"""
BotState.DBLink.execute("""
UPDATE quizzes
SET title = ?
WHERE name = ?""", (newname, self.id))
BotState.write()
self.title = newname
def set_time(self, newtime:int):
"""Sets the question timer on the Quiz"""
BotState.DBLink.execute("""
UPDATE quizzes
SET question_time = ?
WHERE name = ?""", (newtime, self.id))
BotState.write()
self.question_time = newtime
def add_question(self, question:Question, index:int = -1) -> int:
"""Adds a Question to the Quiz
@param question: The Question to be added.
@param index: Optional index to insert the question at. This will renumber the questions after it.
"""
# easy case with adding on the end
if index == -1 or index >= len(self.questions):
BotState.DBLink.execute("""
INSERT INTO quiz_questions
VALUES (?,?,?,?,?,?)
""", (self.id, question.text,
len(self.questions), "|".join(question.choices),
question.correct_answer, question.attachment))
BotState.write()
self.questions.append(question)
return len(self.questions) - 1
# update the question indices in the DB
BotState.DBLink.execute("""
UPDATE quiz_questions
SET ordinal = ordinal + 1
WHERE quiz_name = ?
AND ordinal >= ?
""",(self.id, index))
BotState.write()
# insert the question
BotState.DBLink.execute("""
INSERT INTO quiz_questions
VALUES (?,?,?,?,?,?)
""", (self.id, question.text,
index, "|".join(question.choices),
question.correct_answer, question.attachment))
# update this object
self.questions.insert(index,question)
# update question indices in this object
for ordinal, question in enumerate(self.questions):
question.index = ordinal
return index
def replace_question(self, question:Question, index:int) -> bool:
"""
Replaces a specific question.
@param question: Replacement
@param index: Index to replace
@return: True on success, False on failure
"""
if not 0 <= index < len(self.questions):
return False
BotState.DBLink.execute("""
DELETE FROM quiz_questions
WHERE quiz_name = ?
AND ordinal = ?""", (self.id, index))
BotState.DBLink.execute("""
INSERT INTO quiz_questions
VALUES (?,?,?,?,?,?)
""", (self.id, question.text,
index, "|".join(question.choices),
question.correct_answer, question.attachment))
self.questions[index] = question
return True
def remove_question(self, index:int):
"""
Removes a question at the specific index.
@param index: Number of the question to remove
@return:
"""
# -1 removes last question
if index == -1:
index = self.count - 1
# erase question from DB
BotState.DBLink.execute("""
DELETE FROM quiz_questions
WHERE quiz_name = ?
AND ordinal = ?""",(self.id, index))
# update indices in DB
BotState.DBLink.execute("""
UPDATE quiz_questions
SET ordinal = ordinal - 1
WHERE quiz_name = ?
AND ordinal > ?
""", (self.id, index))
# update this object
self.questions.pop(index)
# update question indices in this object
for ordinal, question in enumerate(self.questions):
question.index = ordinal
class QuizMedalInfo:
"""Represents a set of medals."""
def __init__(self,golds=0, silvers=0,bronzes=0,others=0,participations=0):
self.gold = golds
self.silver = silvers
self.bronze = bronzes
self.other = others
self.participation = participations
class QuizPlaySession:
"""Represents a single run of a Quiz."""
START_MESSAGE_ANIMATION_TIMER: float = 3
MEDAL_EMOJI: list[str] = ["🥇", "🥈", "🥉"]
"""Time delay used between edits"""
@classmethod
def load(cls, session_id: str):
"""
Loads a session from the database
@param session_id: The session to load
@return:
"""
res = BotState.DBLink.execute("""
SELECT quiz_session_id,chatid,quiz_id,start_message_id,ended
FROM quiz_sessions
WHERE quiz_session_id = ?""", (session_id,))
row = res.fetchone()
if not row:
return None
return cls(*row)
@classmethod
def start(cls, quiz_id: str, chat_id: int, start_message: int):
now = time.time()
# create session ID
session_id = str(chat_id) + quiz_id + str(now)
# write to DB
BotState.DBLink.execute("""
INSERT INTO quiz_sessions
VALUES (?,?,?,?,0)""", (session_id, chat_id, quiz_id, start_message))
BotState.write()
# return session object
return cls(session_id, chat_id, quiz_id, start_message, False)
@staticmethod
def check_ongoing(chat_id: int) -> bool:
"""
Checks if a chat has an unfinished session.
@param chat_id: Chat ID to check
@return: A bool indicating whether an unfinished session was found.
"""
res = BotState.DBLink.execute("""
SELECT ended
FROM quiz_sessions
WHERE chatid = ?
AND ended = 0""", (chat_id,))
rows = res.fetchall()
if rows:
return True
return False
@staticmethod
def submit_answer(poll_id:int, user_id:int, option_picked:int):
"""
Processes a poll asnwer
@param poll_id: PollID the answer comes from
@param user_id: User that sent the answer
@param option_picked: The answer option picked
@return:
"""
# put down time
now = time.time()
# find the corresponding tracker
res = BotState.DBLink.execute("""
SELECT quiz_session_id, quiz_name,ordinal,time,msgid
FROM quiz_replytracker
WHERE pollid = ?""", (str(poll_id),))
row = res.fetchone()
# exit if not found
if not row:
return
session_id, quiz_id, question_number, poll_time, poll_msgid = row
session = QuizPlaySession.load(session_id)
# fetch the question
question = Question.fetch(quiz_id, question_number)
# exit if not found
if not question:
return
# on a correct answer, record this
if option_picked == question.correct_answer:
diff = now - poll_time
session.award_correct_answer(user_id, diff)
# on a single player session, advance immediately to the next question
if session.singleplayer:
next_question = question_number + 1
botutils.schedule_kill(session.chat_id, poll_msgid, 0)
scheduled_events.ScheduledEvent.advance_event("quiz_next",session.chat_id, [(0, session.id),(2,next_question)])
@staticmethod
def get_user_medals(user_id:int, chat_id:int = 0):
sh = scores.ScoreHelper(userid=user_id,chatid=chat_id)
golds = sh.get_scope("quiz_medals_0","all")
silvers = sh.get_scope("quiz_medals_1","all")
bronzes = sh.get_scope("quiz_medals_2","all")
others = sh.get_scope("quiz_medals_other","all")
participtions = sh.get_scope("quiz_participations","all")
return golds, silvers, bronzes, others, participtions
def __init__(self, session_id: str, chat_id: int, quiz_id: str, starting_message: int, ended: bool):
self.id = session_id
"""Session ID"""
self.chat_id = chat_id
"""Chat running the session"""
self.singleplayer = chat_id > 0
"""Determines whether this session is run in a private chat with one person or in a chat with multiple people."""
self.quiz_id = quiz_id
"""The Quiz used in the session"""
self.start_message = starting_message
"""MessageID of the message launching the session"""
self.ended = ended
"""Determines if the session has ended."""
self.time = float(self.id[len(self.quiz_id)+len(str(self.chat_id)):])
"""Time when this session started."""
def write_plan(self, question_count: int, question_timer: float, frame_count: int):
"""Writes out the commands to follow for the quiz runner
@param question_count: amount of questions
@param question_timer: time given per question
@param frame_count: amount of animation frames
"""
# set starting point
now = time.time()
# this will hold the items to be written to the DB
plan = []
# plan a message edit per frame
for i in range(-frame_count, 0, 1):
now += QuizPlaySession.START_MESSAGE_ANIMATION_TIMER
plan.append((now, i))
now += QuizPlaySession.START_MESSAGE_ANIMATION_TIMER
# the questions
for index in range(question_count):
plan.append((now,index))
now += question_timer
# final item to finish the quiz
plan.append((now, question_count))
# write to DB
for entry in plan:
moment, command = entry
scheduled_events.ScheduledEvent.schedule_event("quiz_next", self.chat_id, moment, self.id, self.quiz_id, command)
def award_correct_answer(self, user_id:int, seconds:float):
"""
Scores a correct answer in this session.
@param user_id: User to receive score
@param seconds: Seconds to add to user's time score
@return:
"""
res = BotState.DBLink.execute("""
SELECT seconds,answers
FROM quiz_scores
WHERE quiz_session_id = ?
AND quiz_name = ?
AND userid = ?""", (self.id, self.quiz_id, user_id))
row = res.fetchone()
if row:
secs, score = row
secs += seconds
score += 1
BotState.DBLink.execute("""
UPDATE quiz_scores
SET seconds = ?,answers = ?
WHERE quiz_session_id = ?
AND quiz_name = ?
AND userid = ?""", (secs, score, self.id, self.quiz_id, user_id))
else:
BotState.DBLink.execute("""
INSERT INTO quiz_scores
VALUES (?,?,?,?,?)""", (self.id, self.quiz_id, user_id, seconds, 1,))
BotState.write()
def get_results(self) -> list:
"""
Fetches results of this session.
@return: list of (medal, uid, seconds, correct answers)
"""
res = BotState.DBLink.execute("""
SELECT userid,seconds,answers
FROM quiz_scores
WHERE quiz_session_id = ?
ORDER BY answers DESC,seconds""", (self.id,))
db_results = res.fetchall()
results = []
for i, result in enumerate(db_results):
userid, seconds, answers = result
seconds = int(seconds)
usr = UserInfo.User(userid, self.chat_id)
uname = usr.current_nick
if i < len(QuizPlaySession.MEDAL_EMOJI):
results.append((QuizPlaySession.MEDAL_EMOJI[i],uname, seconds, answers,userid))
else:
results.append((" ",uname, seconds, answers,userid))
return results
def give_awards(self):
"""
Processes the session's results and awards medal and other scores.
@return: Returns the results that were processed.
"""
results = self.get_results()
for i,result in enumerate(results):
sh = scores.ScoreHelper(result[4],self.chat_id)
if i >= len(QuizPlaySession.MEDAL_EMOJI):
sh.add("quiz_medals_other")
sh.add("quiz_medals_" + str(i))
sh.add("quiz_participations")
return results
def end(self):
"""
Ends the session.
@return:
"""
BotState.DBLink.execute("""
UPDATE quiz_sessions
SET ended = ?
WHERE quiz_session_id = ?""", (1, self.id))
botutils.schedule_kill(self.chat_id, self.start_message, 0)
##############################
# Running
##############################
class TryStartQuiz(TriggeredAction, action_name="quiz_check_clear"):
"""Checks if a quiz may be launched
param 0: quiz id
param 1: out result
"""
async def run_action(self, message: TGMessage) -> str:
quiz_id = self.read_param(0)
chat_id = message.chat_id
if QuizPlaySession.check_ongoing(chat_id):
self.write_param(1, "quiz_ongoing")
return ""
quiz = Quiz.load(quiz_id)
if not quiz:
self.write_param(1, "quiz_not_found")
return ""
self.write_param(1, "ok")
return ""
class RunQuiz(TriggeredAction, action_name="quiz_begin"):
"""Begins a quiz.
param 0: quiz id
param 1: starting message ID
"""
async def run_action(self, message: TGMessage) -> str:
quiz_id = self.read_param(0)
starting_message = self.read_int(1)
chat_id = message.chat_id
quiz = Quiz.load(quiz_id)
session = QuizPlaySession.start(quiz_id, chat_id, starting_message)
frame_count = len(actions.TriggeredSequence.running_sequences[self.sequence].strings["start_animation"]) - 1
session.write_plan(len(quiz.questions), quiz.question_time, frame_count)
return ""
class FetchEvents(TriggeredAction, action_name="quiz_get_plan"):
"""Fetches quiz runner commands.
param 0: variable to store the events in.
"""
async def run_action(self, message: TGMessage) -> str:
now = time.time()
res = BotState.DBLink.execute("""
SELECT quiz_session_id,chatid,time,quiz_name,ordinal
FROM quiz_next
WHERE time < ?""", (now,))
events = res.fetchall()
self.write_param(0,events)
BotState.DBLink.execute("""
DELETE
FROM quiz_next
WHERE time < ?""", (now,))
return ""
class RegisterPoll(TriggeredAction, action_name="quiz_register_poll"):
"""
Associates a specific Poll with a specific Question in a Session.
param 0: session ID
param 1: poll ID
param 2: question number
"""
async def run_action(self, message: TGMessage) -> str:
sid = self.read_param(0)
poll_id = self.read_param(1)
idx = self.read_param(2)
session = QuizPlaySession.load(sid)
BotState.DBLink.execute("""
INSERT INTO quiz_replytracker
VALUES (?,?,?,?,?,?)""", (sid, poll_id, session.quiz_id, idx, time.time(), self.varstore["__last_msg"]))
BotState.write()
return ""
class ProcessEvent(TriggeredAction, action_name="quiz_do_plan"):
"""Processes a single event.
param 0: variable storing the event.
param 1: variable to store the command type
param 2: variable to store modified event
param 3: variable to store session
"""
async def run_action(self, message: TGMessage) -> str:
event:scheduled_events.ScheduledEvent = self.varstore[self.read_param(0)]
if not event:
return ""
sid = event.event_data[0]
quiz_id = event.event_data[1]
cmd = event.event_data[2]
# store the session
self.write_param(3,QuizPlaySession.load(sid))
# negative numbers animate the starting message
if cmd < 0:
self.write_param(1,"start_animation")
self.write_param(2,int(cmd) * -1)
return ""
# numbers past the last question end the quiz
quiz = Quiz.load(quiz_id)
if cmd >= len(quiz.questions):
self.write_param(1,"quiz_finish")
self.write_param(2,0)
return ""
# any others should post the question
self.write_param(1,"question")
self.write_param(2,cmd)
return ""
class FinishQuiz(TriggeredAction, action_name="quiz_finish"):
"""
Finishes up a quiz session.
param 0: session ID
param 1: variable to store the results
"""
async def run_action(self, message: TGMessage) -> str:
sid = self.read_param(0)
# load the session
session = QuizPlaySession.load(sid)
# fetch the results and award medals
quiz_results = session.give_awards()
self.write_param(1,quiz_results)
session.end()
return ""
##################################
# Access methods
##################################
class FetchQuestion(TriggeredAction, action_name="quiz_fetch_question"):
"""
Fetches a single question.
param 0: Quiz ID
param 1: Question number
param 2: Variable to store the retrieved question
"""
async def run_action(self, message: TGMessage) -> str:
qid = self.read_param(0)
idx = self.read_param(1)
# get the question
question = Question.fetch(qid,idx)
self.write_param(2,question)
return ""
class FetchQuiz(TriggeredAction, action_name="quiz_fetch_quiz"):
"""
Fetches a Quiz
param 0: Quiz ID
param 1: Variable to store the fetched Quiz in
"""
async def run_action(self, message: TGMessage) -> str:
qid = self.read_param(0)
# get the quiz
quiz = Quiz.load(qid)
self.write_param(1,quiz)
return ""
#######################################
# Operator commands
#######################################
#######################################
# Sessions
#######################################
class BeginQuizEditSession(TriggeredAction, action_name="quiz_begin_edit"):
"""
Tries to start an edit session for a quiz, stores results.
param 0: quiz_id to edit
param 1: variable to store success/fail
"""
async def run_action(self, message: TGMessage) -> str:
quiz_id = self.read_string(0)
uid = UserInfo.User.extract_uid(message)
EditSession.clear_old_sessions()
self.write_param(1,EditSession.begin("quiz_edit", uid, quiz_id))
return ""
class EndQuizEditSession(TriggeredAction, action_name="quiz_finish_edit"):
"""
Tries to end an editing session for a quiz, stores results.
param 0: quiz_id to edit
param 1: variable to store success/fail
"""
async def run_action(self, message: TGMessage) -> str:
uid = UserInfo.User.extract_uid(message)
quiz_id = self.read_string(0)
EditSession.clear_old_sessions()
self.write_param(1,EditSession.end("quiz_edit", uid, quiz_id))
return ""
class CheckQuizEditSession(TriggeredAction, action_name="quiz_check_sessions"):
"""
Checks session state and stores "none" if no ongoing sessions,
"active" if session matching quiz ID exists,
"busy" if there are ongoing sessions but noen match this ID.
param 0: quiz_id to edit
param 1: variable to store result
"""
async def run_action(self, message: TGMessage) -> str:
quiz_id = self.read_string(0)
uid = UserInfo.User.extract_uid(message)
EditSession.clear_old_sessions()
sessions = EditSession.find_sessions("quiz_edit", uid)
if len(sessions) == 0:
result = "none"
elif quiz_id in sessions:
EditSession.refresh("quiz_edit",uid, quiz_id)
result = "active"
else:
result = "busy"
self.write_param(1,result)
return ""
class FindQuizEditSession(TriggeredAction, action_name="quiz_find_session"):
"""
Checks session state and stores num
param 0: variable to store result
param 1: variable to store quiz id if found
"""
async def run_action(self, message: TGMessage) -> str:
uid = UserInfo.User.extract_uid(message)
EditSession.clear_old_sessions()
# find any sessions by user
sessions = EditSession.find_sessions("quiz_edit", uid)
# if exactly one found, return it
if len(sessions) == 1:
EditSession.refresh("quiz_edit",uid, sessions[0])
self.write_param(1,sessions[0])
self.write_param(0,len(sessions))
return ""
#######################################
# Quiz manipulation
#######################################
class CreateQuiz(TriggeredAction, action_name="quiz_create"):
"""
param 0: quiz ID
param 1: variable to store results
"""
async def run_action(self, message: TGMessage) -> str:
quiz_id = self.read_string(0)
uid = message.from_user.id
quiz = Quiz.create(uid,quiz_id,"(Без названия)",45)
self.write_param(1, quiz)
if quiz is not None:
EditSession.begin("quiz_edit",uid,quiz.id)
return ""
class RenameQuiz(TriggeredAction, action_name="quiz_rename"):
"""
Renames a given quiz.
param 0: quiz_id
param 1: new name
"""
async def run_action(self, message: TGMessage) -> str:
newname = self.read_string(1)
quiz_id = self.read_string(0)
# more error checking not needed at this point
Quiz.load(quiz_id).rename(newname)
return ""
class SetDefaultQuestionTime(TriggeredAction, action_name="quiz_set_default_time"):
"""
"""
async def run_action(self, message: TGMessage) -> str:
pass
##########################################
# Question editing
##########################################
class AddQuestion(TriggeredAction, action_name="quiz_add_question"):
"""
param 0: quiz ID to add to
param 1: variable to store function outcome
param 2: in, out variable to store question number,
in: -1 to add the question at the end, else the question is added under that number
out: -1 if the question was not added, else the number actually assigned to the question
param 3: true if the question replaces an existing one, false if it is inserted.
"""
async def run_action(self, message: TGMessage) -> str:
quiz_id = self.read_string(0)
replace = self.read_param(3)
# get desired index - user facing side indexes from 1
question_index = self.varstore[self.read_string(2)]
if question_index != -1:
question_index -= 1
# pre-load failure
self.write_param(2, -1)
if message.reply_to_message is None:
self.write_param(1,"must_reply")
return ""
if message.reply_to_message.poll is None:
self.write_param(1,"no_poll")
return ""
poll = message.reply_to_message.poll
if poll.correct_option_id is None:
self.write_param(1,"wrong_poll")
return ""
quiz = Quiz.load(quiz_id)
if quiz is None:
self.write_param(1,"quiz_not_found")
return ""
# prepare the question
q = Question(quiz_id, -1, poll.question, [option.text for option in poll.options], poll.correct_option_id,"")
# if set to replace try to do it
if replace:
if quiz.replace_question(q, question_index):
self.write_param(2, question_index + 1)
self.write_param(1,"ok")
return ""
# write error on fail
self.write_param(1, "replace_out_of_range")
return ""
# else insert question as needed and get the resulting number
question_index = quiz.add_question(q, question_index)
self.write_param(2,question_index + 1)
self.write_param(1,"ok")
return ""
class RemoveQuestion(TriggeredAction, action_name="quiz_delete_question"):
"""
param 0: quiz ID to delete from
param 1: question index - 1-indexed. pass -1 to delete the last question.
param 2: out result, of (ok, quiz_not_found, invalid_question)
"""
async def run_action(self, message: TGMessage) -> str:
quiz_id = self.read_string(0)
index = self.read_int(1)
quiz = Quiz.load(quiz_id)
if quiz is None:
self.write_param(2,"quiz_not_found")
return ""
if index != -1:
index -= 1
if -1 > index >= quiz.count:
self.write_param(2,"invalid_question")
return ""
quiz.remove_question(index)
self.write_param(2,"ok")
return ""
class SetSpecificQuestionTime(TriggeredAction, action_name="quiz_question_time"):
"""
"""
async def run_action(self, message: TGMessage) -> str:
pass
class QuestionAttach(TriggeredAction, action_name="quiz_question_attach"):
"""
Attaches media to a question.
param 0: quiz_id
param 1: question number, or -1 to attach to last question
param 2: attachment ID
param 3: out result
"""
async def run_action(self, message: TGMessage) -> str:
quiz_id = self.read_string(0)
question_index = self.read_int(1)
msgname = self.read_string(2)
quiz = Quiz.load(quiz_id)
if quiz is None:
self.write_param(3,"quiz_not_found")
return ""
if question_index == -1:
question_index = len(quiz.questions)
question_index -= 1
if not 0 < question_index < len(quiz.questions):
self.write_param(3,"question_not_found")
quiz.questions[question_index].attach_media(msgname)
self.write_param(3, "ok")
return ""
class GetQuizMedals(TriggeredAction, action_name="quiz_get_medals"):
"""
Gets user's medals
param 0: uid
param 1: chatid, 0 for all
param 2: outvar
"""
async def run_action(self, message: TGMessage) -> str:
uid = self.read_int(0)
chatid = self.read_int(1)
usermedals = QuizPlaySession.get_user_medals(uid,chatid)
medals = QuizMedalInfo(*usermedals)
self.write_param(2,medals)
return ""