-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmq.p6
executable file
·190 lines (172 loc) · 6.72 KB
/
mq.p6
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
#!/usr/bin/env perl6
# Copyright © 2019 Noisytoot
# mq is a program that asks you maths questions
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>
use v6;
use Config::TOML;
use Terminal::ANSIColor;
use Readline;
constant @version = 1, 5, 2;
constant $version = "@version[0].@version[1].@version[2]";
my Bool %*SUB-MAIN-OPTS = :named-anywhere;
my Str $config-file;
my Str $config-dir;
if %*ENV<MQ_CONFIG_DIR>:exists {
$config-dir = %*ENV<MQ_CONFIG_DIR>;
} else {
$config-dir = "$*HOME/.mq"
}
if %*ENV<MQ_CONFIG>:exists {
$config-file = %*ENV<MQ_CONFIG>;
} else {
$config-file = "$config-dir/config.toml";
}
my Hash %config;
if $config-file.IO.e {
%config = from-toml($config-file.IO.slurp);
%config<mq><group> = 20 unless %config<mq><group>:exists;
%config<mq><max> = 10 unless %config<mq><max>:exists;
} else {
%config = mq => { group => 20, max => 10 };
}
my Int $config-group = %config<mq><group>;
my Int $original-group = $config-group;
my Int $score = 0;
my Str $log-file;
if %*ENV<MQ_LOG>:exists {
$log-file = %*ENV<MQ_LOG>;
} else {
$log-file = "$config-dir/log.slf";
}
mkdir $config-dir unless $config-dir.IO.e;
unless $log-file.IO.e {
spurt $log-file, "SLF 1\nMQ_LOG 1\nH TIMESTAMP LEVEL MAX ACTUAL_LENGTH ORIGINAL_LENGTH SCORE\n";
}
sub correct {
say colored("Correct", "bold green");
}
sub wrong(Int $correct) {
say colored("Wrong", "bold red");
say colored("Correct: $correct", "bold yellow");
}
sub ask(Int $n1, Str $operator, Int $n2) {
my Readline $readline = Readline.new();
return $readline.readline(colored("What is $n1 $operator $n2? ", "bold cyan")).Int;
}
sub score(Int $score, Int $group) {
say colored("Score: $score out of $group", "bold yellow");
}
sub slf-write(Str $file, DateTime $timestamp, Int $level, Int $max, Int $actual-length, Int $original-length, Int $score) {
spurt $file, "R $timestamp $level $max $actual-length $original-length $score\n", :append;
}
sub progress(Int $score, Int $group) {
my Int $left = $group - $score;
say colored("Progress: $score done, $left left", "bold yellow");
}
sub USAGE {
say "Usage: $*PROGRAM-NAME [--disable-log] [--group=<group-length>] <mode> <max>";
say "Valid modes: level1 (addition, subtraction), level2 (multiplication, division), get-group (print group length), get-max (print maximum)";
say "mq version $version\n";
say "Config dir location is set in the environment variable \$MQ_CONFIG_DIR, the default is ~/.mq, the following variables override this:";
say "Log file location is set in the environment variable \$MQ_LOG, the default is ~/.mq/log.slf";
say "Configuration file location is set in the environment variable \$MQ_CONFIG, the default is ~/.mq/config.toml\n";
say "Example configuration file:";
say colored("[", "italic cyan") ~ colored("mq", "italic yellow") ~ colored("]", "italic cyan");
say colored("group", "italic magenta") ~ " " ~ colored("=", "italic cyan") ~ " " ~ colored("20", "italic yellow");
say colored("max", "italic magenta") ~ " " ~ colored("=", "italic cyan") ~ " " ~ colored("10", "italic yellow");
}
sub MAIN(Str $mode, Int $max = %config<mq><max>, Bool :$disable-log, Int :$group is copy = $config-group) {
$original-group = $group unless $group == $original-group;
die "Maximum must be more than 2" if $max < 3;
die "Group length (number of questions) must be more than 0" if $group < 1;
my Int $level;
if $mode eq "level1" {
$level = 1;
} elsif $mode eq "level2" {
$level = 2;
}
given $mode {
say "Group length: $config-group" when "get-group";
say "Max: %config<mq><max>" when "get-max";
when "level1" {
loop (my Int $i = 0; $i < $group; $i++) {
my Str $operator;
if 2.rand.Int {
$operator = "+";
} else {
$operator = "-";
}
my Int $n1 = (1..$max).rand.Int;
my Int $n2 = (1..$max).rand.Int;
my Int $result = ask($n1, $operator, $n2);
if $operator eq "+" {
if $result == $n1 + $n2 {
correct;
$score++;
} else {
wrong $n1 + $n2;
$group++;
}
} else {
if $result == $n1 - $n2 {
correct;
$score++;
} else {
wrong $n1 - $n2;
$group++;
}
}
progress $score, $original-group;
}
score $score, $group;
slf-write $log-file, DateTime.now(), $level, $max, $group, $original-group, $score unless $disable-log;
}
when "level2" {
loop (my Int $i = 0; $i < $group; $i++) {
my Str $operator;
if 2.rand.Int {
$operator = "*";
} else {
$operator = "/";
}
my Int $n1 = (1..$max).rand.Int;
my Int $n2 = (1..$max).rand.Int;
my Int $divanswer;
if $operator eq "/" {
$divanswer = $n1;
$n1 = $divanswer * $n2
}
my Int $result = ask($n1, $operator, $n2);
if $operator eq "*" {
if $result == $n1 * $n2 {
correct;
$score++;
} else {
wrong $n1 * $n2;
$group++;
}
} else {
if $result == $divanswer {
correct;
$score++;
} else {
wrong $divanswer;
$group++;
}
}
progress $score, $original-group;
}
score $score, $group;
slf-write $log-file, DateTime.now(), $level, $max, $group, $original-group, $score unless $disable-log;
}
}
}