/
check.go
131 lines (114 loc) · 2.17 KB
/
check.go
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
package main
import (
"strings"
"time"
)
func checkMsg(msgCh <-chan message, source string) (<-chan message, <-chan report) {
sum := time.Duration(0)
n := 0
procCh := make(chan message)
repCh := make(chan report)
go func() {
defer close(procCh)
defer close(repCh)
history := ""
prev := []rune{}
mistakes := 0
strokes := 0
wrongs := 0
hasMistake := false
text := ""
duration := time.Duration(0)
tokens := strings.Fields(source)
total := len(tokens)
for m := range msgCh {
duration = time.Duration(0)
if m.t != time.Duration(0) {
sum += m.t
n++
duration = sum / time.Duration(n)
}
text = ""
o := []rune(tokens[0])
hasMistake = false
if len(m.s) > len(o) {
text += mistake(string(o))
if len(m.s) > len(prev) {
mistakes++
}
hasMistake = true
} else {
for i, c := range m.s {
if i >= len(o) {
break
}
if c == o[i] {
text += fade(string(o[i]))
} else {
if i < len(prev) {
if c != prev[i] {
mistakes++
}
} else {
mistakes++
}
text += mistake(string(o[i]))
hasMistake = true
}
}
}
if len(m.s) < len(o) {
if m.submit {
text += mistake(string(o[len(m.s):]))
hasMistake = true
mistakes++
} else {
text += underline(string(o[len(m.s)]))
text += string(o[len(m.s)+1:])
}
}
if m.submit {
if hasMistake {
wrongs++
}
if len(tokens) == 1 {
break
}
history += text + " "
tokens = tokens[1:]
next := tokens[0]
text = underline(string(next[0]))
if len(next) > 1 {
text += string(next[1:])
}
}
text += " " + strings.Join(tokens[1:], " ")
if !m.correction {
strokes++
}
procCh <- message{
s: history + text,
}
repCh <- report{
mistakes: mistakes,
duration: duration,
lenght: strokes,
wrongs: wrongs,
total: total,
}
prev = []rune(m.s)
}
procCh <- message{
s: history + text,
}
repCh <- report{
mistakes: mistakes,
duration: duration,
lenght: strokes,
end: true,
wrongs: wrongs,
total: total,
}
}()
return procCh, repCh
}