-
Notifications
You must be signed in to change notification settings - Fork 1
/
eb_math.py
98 lines (75 loc) · 2.94 KB
/
eb_math.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
"eb_math.py by Christer Enfors (c) 2016"
from __future__ import print_function
import time
import math_engine.engine as math_engine
import eb_activity
class MathDrill(eb_activity.Activity):
"Activity for practicing math."
def __init__(self, user): # pylint: disable=super-init-not-called
self.user = user
self.name = "MathDrill"
math_user = math_engine.User(user.name)
self.drill = math_engine.MultiplicationDrill(math_user,
limit=12,
num_questions=20)
self.last_answer_time = None
self.elapsed_time = 0
self.score = None
self.started = False
def start(self, text): # pylint: disable=unused-argument
"Start the activity."
pass
def end(self):
"Called at end of drill. Returns a string to the user."
minutes = int(self.elapsed_time / 60)
if minutes is 1:
time_output = "1 minute, "
elif minutes is not 0:
time_output = "%d minutes, " % minutes
else:
time_output = ""
time_output += "%d seconds. " % (self.elapsed_time % 60)
output = "\nTime: " + time_output
self.score = self.calc_score()
record = self.user.load_data("multi_record")
output += "\nScore: %d points" % self.score
if record is not None:
record = int(record)
if self.score > record:
output += " - a new record! (previous record: %d)" % \
record
self.user.save_data("multi_record", str(self.score))
else:
output += " (record: %d)." % record
else:
self.user.save_data("multi_record", str(self.score))
output += "."
return output
def handle_text(self, text):
"""Handle incoming text from the user, return
math_engine.ActivityStatus object or None if we don't want to
repond to this text.
"""
if self.last_answer_time:
used_time = time.time() - self.last_answer_time
if used_time > 30:
used_time = 30
self.elapsed_time += used_time
self.last_answer_time = time.time()
if not self.started:
self.started = True
return self.drill.start()
else:
status = self.drill.recv_input(text)
if status.done:
end_ret = self.end()
if end_ret is not None:
status.output += end_ret
return status
def calc_score(self):
"Calculate a score for the user and the end of the drill."
avg_time = self.elapsed_time / self.drill.num_questions
score = (30 - avg_time) * (self.drill.num_questions /
self.drill.num_correct)
score *= self.drill.limit
return int(score)