-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel_controller.py
More file actions
198 lines (177 loc) · 5.95 KB
/
level_controller.py
File metadata and controls
198 lines (177 loc) · 5.95 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
import log
from transition_aggregator import TransitionAggregator
from statistics import mean
from generators import (
sanitize,
probabilities,
increase_letter_probability,
) # TODO move to correct module?
from game_state import save_level_info, load_level_info
min_occurences = 10
min_wpm = 30
min_accuracy = 0.75
def wpm(key_time_in_seconds):
return 60 / key_time_in_seconds / 5
class LevelController:
# TODO replace with automatic gathering from dictionary
chars_allowed = [
"aioenz",
"rw",
"cy",
"st",
"mp",
"dk",
"u.",
"l,",
"jb",
"gh",
"f-",
"?v",
"!:",
"x;",
"()",
'1"',
"2q",
"0]",
"[3",
"54",
"67",
"89",
"/'",
"=\\"
# "asdflkjh",
# "ru",
# "ei",
# "wo",
# "qp",
# "ty",
# "gh",
# "vm",
# "c,",
# "x.",
# "z/",
# "bn",
# "[]",
# "'\\",
# "-=",
# "10",
# "29",
# "38",
# "47",
# "56",
]
from level_controller import wpm
def __init__(self, dictionary):
self.dictionary = dictionary
probabilities(dictionary)
self.aggregator = TransitionAggregator()
self.current_level = load_level_info(default=1)["level"]
self.current_dictionary = self.recalculate_dictionary()
def occurences(self, key_stats):
return {
k: len(key_stats[k]) if k in key_stats else 0 for k in self.current_chars()
}
def mean_wpm_for_keys(self, key_stats):
return {
k: wpm(mean(v)) for k, v in key_stats.items() if k in self.current_chars()
}
def time_accuracy(self, transitions):
return {
k: v
for k, v in self.aggregator.time_accuracy_for_keys(transitions).items()
if k in self.current_chars()
}
def advance_to_next_level_if_possible(self, transitions):
key_stats = self.aggregator.adjusted_key_stats(transitions)
all_occured = all(
[v >= min_occurences for v in self.occurences(key_stats).values()]
)
all_fast = all(
[v > min_wpm for v in self.mean_wpm_for_keys(key_stats).values()]
)
all_accurate = all(
[v > min_accuracy for v in self.time_accuracy(transitions).values()]
)
if not all_occured:
log.debug(
"Won't advance as not all yet occured {} times: {}".format(
min_occurences,
{
k: v
for k, v in self.occurences(key_stats).items()
if v < min_occurences
},
)
)
if not all_fast:
log.debug(
"Won't advance as not yet fast enough (>{}WPM): {}".format(
min_wpm,
{
k: v
for k, v in self.mean_wpm_for_keys(key_stats).items()
if v < min_wpm
},
)
)
if not all_accurate:
log.debug(
"Won't advance as not yet accurate enough (>{:0.1f}%): {}".format(
min_accuracy * 100,
{
k: v
for k, v in self.time_accuracy(transitions).items()
if v < min_accuracy
},
)
)
if all_occured and all_fast and all_accurate:
self.current_level += 1
save_level_info({"level": self.current_level})
log.info("Advancing to level {}".format(self.current_level))
self.current_dictionary = self.recalculate_dictionary()
def main_focus_for_level(self, transitions):
log.debug(
"Calculating focus for level {} (letters: {})".format(
self.current_level, self.current_chars()
)
)
key_stats = self.aggregator.adjusted_key_stats(transitions)
if len(key_stats) == 0:
log.warn("No key stats in given transitions, won't calclulate focus")
return "", "", ""
min_occurence = min(self.occurences(key_stats).items(), key=lambda x: x[1],)
worst_accuracy = min(
self.time_accuracy(transitions).items(), key=lambda x: x[1]
)
slowest = min(self.mean_wpm_for_keys(key_stats).items(), key=lambda x: x[1],)
if min_occurence[1] < min_occurences:
log.info(
"Focus on occurences: [{}]: {}".format(
min_occurence[0], min_occurence[1]
)
)
return (min_occurence[0], min_occurence[1], "COUNT")
if worst_accuracy[1] < min_accuracy:
log.info(
"Focus on accuracy: [{}]: {:0.1f}%".format(
worst_accuracy[0], worst_accuracy[1] * 100
)
)
return (worst_accuracy[0], worst_accuracy[1], "ACCURACY")
if slowest[1] < min_wpm:
log.info("Focus on speed: [{}]: {:0.1f} WPM".format(slowest[0], slowest[1]))
return (slowest[0], slowest[1], "SPEED")
# no focus needed
log.info("No focus needed as all stats are fine.")
return "", "", ""
def current_chars(self):
return "".join(self.chars_allowed[: self.current_level])
def new_chars(self):
return self.chars_allowed[self.current_level - 1]
def recalculate_dictionary(self):
return increase_letter_probability(
sanitize(self.dictionary, self.current_chars()), 0.5
)
def dictionary_for_current_level(self):
return self.current_dictionary