@@ -3,6 +3,7 @@ package wordle
3
3
4
4
import (
5
5
"errors"
6
+ "fmt"
6
7
"image/color"
7
8
"math/rand"
8
9
"sort"
@@ -40,8 +41,19 @@ var colors = [...]color.RGBA{
40
41
{219 , 219 , 219 , 255 },
41
42
}
42
43
43
- var words []string
44
- var questions []string
44
+ var classdict = map [string ]int {
45
+ "" : 5 ,
46
+ "五阶" : 5 ,
47
+ "六阶" : 6 ,
48
+ "七阶" : 7 ,
49
+ }
50
+
51
+ type dictionary map [int ]struct {
52
+ dict []string
53
+ cet4 []string
54
+ }
55
+
56
+ var words = make (dictionary )
45
57
46
58
func init () {
47
59
en := control .Register ("wordle" , order .AcquirePrio (), & control.Options {
@@ -61,47 +73,50 @@ func init() {
61
73
}),
62
74
))
63
75
go func () {
64
- questionsdata , err := file . GetLazyData ( en . DataFolder () + "questions.bin" , true , true )
65
- if err != nil {
66
- panic ( err )
67
- }
68
- questionspacks := loadwords ( questionsdata )
69
- questions = make ([] string , len ( questionspacks ) )
70
- for i := range questionspacks {
71
- questions [ i ] = questionspacks [ i ]. String ( )
72
- }
73
- wordsdata , err := file . GetLazyData ( en . DataFolder () + "words.bin" , true , true )
74
- if err != nil {
75
- panic ( err )
76
- }
77
- wordpacks := loadwords ( wordsdata )
78
- words = make ( []string , len ( wordpacks ))
79
- for i := range wordpacks {
80
- words [ i ] = wordpacks [ i ]. String ()
76
+ for i := 5 ; i <= 7 ; i ++ {
77
+ dc , err := file . GetLazyData ( fmt . Sprintf ( "%scet-4_%d.txt" , en . DataFolder (), i ), true , true )
78
+ if err != nil {
79
+ panic ( err )
80
+ }
81
+ c := strings . Split ( string ( dc ), " \n " )
82
+ sort . Strings ( c )
83
+ dd , err := file . GetLazyData ( fmt . Sprintf ( "%sdict_%d.txt" , en . DataFolder (), i ), true , true )
84
+ if err != nil {
85
+ panic ( err )
86
+ }
87
+ d := strings . Split ( string ( dd ), " \n " )
88
+ sort . Strings ( d )
89
+ words [ i ] = struct {
90
+ dict []string
91
+ cet4 [] string
92
+ }{ d , c }
81
93
}
82
- sort .Strings (words )
83
94
}()
84
- en .OnRegex (`(个人|团队)猜单词` , zero .OnlyGroup ).SetBlock (true ).Limit (ctxext .LimitByUser ).
95
+ en .OnRegex (`(个人|团队)(五阶|六阶|七阶)? 猜单词` , zero .OnlyGroup ).SetBlock (true ).Limit (ctxext .LimitByUser ).
85
96
Handle (func (ctx * zero.Ctx ) {
86
- target := questions [rand .Intn (len (questions ))]
97
+ class := classdict [ctx .State ["regex_matched" ].([]string )[2 ]]
98
+ target := words [class ].cet4 [rand .Intn (len (words [class ].cet4 ))]
87
99
game := newWordleGame (target )
88
100
_ , img , cl , _ := game ("" )
89
- typ := ctx .State ["regex_matched" ].([]string )[1 ]
90
101
ctx .Send (
91
102
message .ReplyWithMessage (ctx .Event .MessageID ,
92
103
message .ImageBytes (img ),
93
- message .Text ("你有6次机会猜出单词,单词长度为5 ,请发送单词" ),
104
+ message .Text ("你有" , class + 1 , "次机会猜出单词,单词长度为" , class , " ,请发送单词" ),
94
105
),
95
106
)
96
107
cl ()
97
108
var next * zero.FutureEvent
98
- if typ == "个人" {
99
- next = zero .NewFutureEvent ("message" , 999 , false , zero .RegexRule (`^[A-Z]{5}$|^[a-z]{5}$` ), zero .OnlyGroup , zero .CheckUser (ctx .Event .UserID ))
109
+ if ctx .State ["regex_matched" ].([]string )[1 ] == "个人" {
110
+ next = zero .NewFutureEvent ("message" , 999 , false , zero .RegexRule (fmt .Sprintf (`^([A-Z]|[a-z]){%d}$` , class )),
111
+ zero .OnlyGroup , zero .CheckUser (ctx .Event .UserID ))
100
112
} else {
101
- next = zero .NewFutureEvent ("message" , 999 , false , zero .RegexRule (`^[A-Z]{5}$|^[a-z]{5}$` ), zero .OnlyGroup , zero .CheckGroup (ctx .Event .GroupID ))
113
+ next = zero .NewFutureEvent ("message" , 999 , false , zero .RegexRule (fmt .Sprintf (`^([A-Z]|[a-z]){%d}$` , class )),
114
+ zero .OnlyGroup , zero .CheckGroup (ctx .Event .GroupID ))
102
115
}
103
116
var win bool
104
117
var err error
118
+ recv , cancel := next .Repeat ()
119
+ defer cancel ()
105
120
for {
106
121
select {
107
122
case <- time .After (time .Second * 120 ):
@@ -111,7 +126,7 @@ func init() {
111
126
),
112
127
)
113
128
return
114
- case e := <- next . Next () :
129
+ case e := <- recv :
115
130
win , img , cl , err = game (e .Message .String ())
116
131
switch {
117
132
case win :
@@ -158,6 +173,7 @@ func init() {
158
173
}
159
174
160
175
func newWordleGame (target string ) func (string ) (bool , []byte , func (), error ) {
176
+ var class = len (target )
161
177
record := make ([]string , 0 , len (target )+ 1 )
162
178
return func (s string ) (win bool , data []byte , cl func (), err error ) {
163
179
if s != "" {
@@ -169,8 +185,8 @@ func newWordleGame(target string) func(string) (bool, []byte, func(), error) {
169
185
err = errLengthNotEnough
170
186
return
171
187
}
172
- i := sort .SearchStrings (words , s )
173
- if i >= len (words ) || words [i ] != s {
188
+ i := sort .SearchStrings (words [ class ]. dict , s )
189
+ if i >= len (words [ class ]. dict ) || words [ class ]. dict [i ] != s {
174
190
err = errUnknownWord
175
191
return
176
192
}
@@ -181,13 +197,14 @@ func newWordleGame(target string) func(string) (bool, []byte, func(), error) {
181
197
}
182
198
}
183
199
var side = 20
184
- ctx := gg .NewContext ((side + 2 )* 5 + 26 , (side + 2 )* 6 + 26 )
200
+ var space = 10
201
+ ctx := gg .NewContext ((side + 4 )* class + space * 2 - 4 , (side + 4 )* (class + 1 )+ space * 2 - 4 )
185
202
ctx .SetColor (color.RGBA {255 , 255 , 255 , 255 })
186
203
ctx .Clear ()
187
- for i := 0 ; i < len ( target ) + 1 ; i ++ {
188
- for j := 0 ; j < len ( target ) ; j ++ {
204
+ for i := 0 ; i < class + 1 ; i ++ {
205
+ for j := 0 ; j < class ; j ++ {
189
206
if len (record ) > i {
190
- ctx .DrawRectangle (float64 (10 + j * (side + 4 )), float64 (10 + i * (side + 4 )), float64 (side ), float64 (side ))
207
+ ctx .DrawRectangle (float64 (space + j * (side + 4 )), float64 (space + i * (side + 4 )), float64 (side ), float64 (side ))
191
208
switch {
192
209
case record [i ][j ] == target [j ]:
193
210
ctx .SetColor (colors [match ])
0 commit comments