generated from Tnze/CoolQ-Golang-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
229 lines (208 loc) · 5.47 KB
/
main.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"context"
"crypto/md5"
"encoding/binary"
"fmt"
"github.com/BaiMeow/SimpleBot/message"
"log"
"math/rand"
"time"
"github.com/BaiMeow/SimpleBot/bot"
"github.com/BaiMeow/SimpleBot/driver"
"github.com/miaoscraft/McAugur/conf"
"github.com/miaoscraft/McAugur/data"
"github.com/miaoscraft/McAugur/rcon"
)
var (
b *bot.Bot
config *conf.Config
AugurData *data.AugurData
clear = make(chan bool)
)
func main() {
enable()
ctx, cancel := context.WithCancel(context.TODO())
go cleanEff(ctx)
defer cancel()
b = bot.New(driver.NewWsDriver(config.Websocket, config.Token))
b.Attach(&bot.GroupMsgHandler{
Priority: 1,
F: func(MsgID int32, GroupID int64, FromQQ int64, Msg message.Msg) bool {
if len(Msg) != 1 {
return false
}
txt, ok := Msg[0].(message.Text)
if !ok || GroupID != config.Group || txt.Text != "算命" {
return false
}
if _, err := b.SendGroupMsg(GroupID, Augur(FromQQ)); err != nil {
log.Println(err)
}
return true
},
})
b.Attach(&bot.GroupMsgHandler{
Priority: 2,
F: func(MsgID int32, GroupID int64, FromQQ int64, Msg message.Msg) bool {
if len(Msg) != 1 {
return false
}
txt, ok := Msg[0].(message.Text)
if !ok || GroupID != config.Group || !isAdmin(FromQQ) {
return false
}
switch txt.Text {
case "/mcaugur reload":
enable()
case "/mcaugur clear":
clear <- true
}
return true
},
})
b.Run()
select {}
}
func isAdmin(qq int64) bool {
for _, v := range config.Admin {
if v == qq {
return true
}
}
return false
}
func enable() {
if c, err := conf.Load("conf.json"); err != nil {
log.Fatal(err.Error())
} else {
config = c
}
if d, err := data.Load("data.json"); err != nil {
log.Fatal(err)
} else {
AugurData = d
}
if err := data.Open(config.Source); err != nil {
log.Fatal(err.Error())
} else {
log.Println("成功连接数据库")
}
if err := rcon.Open(config.Server, config.PassWd); err != nil {
log.Fatal(err.Error())
} else {
log.Println("成功连接RCON")
}
}
// 恐慌之神马斯特
func must(err error) {
if err != nil {
panic(err)
}
}
// 占卜术式
func Augur(fromQQ int64) message.Msg {
//启动术式
hash := md5.New()
name, err := data.QQGetName(fromQQ)
if err != nil {
log.Println(err.Error())
return strToMsg("算命大失败")
}
if name == "" {
return strToMsg("Wait!!!")
}
//将被占卜物代入天命术式
y, m, d := time.Now().Date()
_, err = hash.Write([]byte(name)) // never returns a err
must(err)
must(binary.Write(hash, binary.BigEndian, int64(y)))
must(binary.Write(hash, binary.BigEndian, int64(m)))
must(binary.Write(hash, binary.BigEndian, int64(d)))
//用天命开始占卜
destiny := hash.Sum(nil) //获取destiny 天命
rand.Seed(int64(binary.BigEndian.Uint64(destiny)))
//占卜获得以太坐标与幸运指数
luckindex := rand.Intn(100) + 1
placeID := rand.Intn(len(AugurData.Places))
//占卜具体细节
goodevents := append(AugurData.GoodEvents, AugurData.Places[placeID].GoodEvents...)
badevents := append(AugurData.BadEvents, AugurData.Places[placeID].BadEvents...)
result := fmt.Sprintf("%s今天的仙气值(1-100)为 %d\n", name, luckindex)
switch {
case luckindex <= 15:
result += "今日 大凶\n"
result1 := badevents[rand.Intn(len(badevents))]
result2 := badevents[rand.Intn(len(badevents))]
result += "去" + AugurData.Places[placeID].Name + "不但" + result1.Name + "\n"
result += "而且" + result2.Name
runCmd(fmt.Sprintf(result1.Cmd, name))
runCmd(fmt.Sprintf(result2.Cmd, name))
case luckindex > 15 && luckindex <= 45:
result += "今日 凶\n"
result1 := badevents[rand.Intn(len(badevents))]
result2 := goodevents[rand.Intn(len(goodevents))]
result += "去" + AugurData.Places[placeID].Name + result1.Name + "\n"
result += "不过呢" + result2.Name
runCmd(fmt.Sprintf(result1.Cmd, name))
runCmd(fmt.Sprintf(result2.Cmd, name))
case luckindex > 45 && luckindex <= 55:
result += "今日 平,无特殊事件"
case luckindex >= 55 && luckindex <= 85:
result += "今日 吉\n"
result1 := goodevents[rand.Intn(len(goodevents))]
result2 := badevents[rand.Intn(len(badevents))]
result += "去" + AugurData.Places[placeID].Name + result1.Name + "\n"
result += "但是要注意" + result2.Name
runCmd(fmt.Sprintf(result1.Cmd, name))
runCmd(fmt.Sprintf(result2.Cmd, name))
case luckindex > 85:
result += "今日 大吉大利\n"
result1 := goodevents[rand.Intn(len(goodevents))]
result += "去" + AugurData.Places[placeID].Name + result1.Name + "\n"
result += "Today is your day!"
runCmd(fmt.Sprintf(result1.Cmd, name))
}
return strToMsg(result)
}
func strToMsg(str string) message.Msg {
return message.Msg{
message.Text{Text: str},
}
}
func runCmd(cmd string) {
log.Println(cmd)
resp, err := rcon.Cmd(cmd)
if err != nil {
b.SendGroupMsg(config.Group, strToMsg("连接服务器失败,算命效果可能无法实装"))
log.Fatal(err.Error())
}
log.Println(resp)
}
//清除算命效果
func cleanEff(ctx context.Context) {
now := time.Now()
clean := func() {
for _, v := range AugurData.ResetCmds {
runCmd(v)
}
b.SendGroupMsg(config.Group, strToMsg("清除已有算命效果完成"))
}
// 等待12点
select {
case <-time.After(time.Until(time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, now.Location()))):
clean()
case <-ctx.Done():
return
}
timer := time.NewTicker(time.Hour * 24)
defer timer.Stop()
for {
select {
case <-timer.C:
clean()
case <-ctx.Done():
return
}
}
}