-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
93 lines (75 loc) · 2.01 KB
/
handlers.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
package main
import (
"encoding/json"
"fmt"
tele "gopkg.in/telebot.v3"
"math/rand"
"os"
"sync"
)
type Country struct {
CountryName string `json:"country"`
Population int64 `json:"population"`
}
var (
countryList []Country
totalPopulation int64
mutex sync.Mutex
)
func initCountryList() error {
rawJson, err := os.ReadFile("countries.json")
if err != nil {
return err
}
if err = json.Unmarshal(rawJson, &countryList); err != nil {
return err
}
totalPopulation = int64(0)
for _, country := range countryList {
totalPopulation += country.Population
}
return nil
}
func getRandomCountry() Country {
// 生成随机数
randomNum := rand.Int63n(totalPopulation)
// 根据随机数获取对应的国家
index := 0
for i, country := range countryList {
if randomNum < country.Population {
index = i
break
}
randomNum -= country.Population
}
return countryList[index]
}
func CommandRemake(c tele.Context) error {
remakeData := []string{"男孩子", "女孩子", "MtF", "FtM", "MtC", "萝莉", "正太", "武装直升机", "沃尔玛购物袋", "星巴克", "太监", "无性别", "扶她"}
remakeResult := rand.Intn(len(remakeData))
randomCountry := getRandomCountry()
mutex.Lock()
_, hasKey := remakeCount[c.Sender().ID]
if !hasKey {
remakeCount[c.Sender().ID] = new(RemakeData)
}
oldGender := remakeCount[c.Sender().ID].count
remakeCount[c.Sender().ID] = &RemakeData{
country: randomCountry.CountryName,
gender: remakeData[remakeResult],
count: oldGender + 1,
}
mutex.Unlock()
text := fmt.Sprintf("转生成功!您现在是 %s 的 %s 了。", randomCountry.CountryName, remakeData[remakeResult])
return c.Reply(text)
}
func CommandRemakeData(c tele.Context) error {
var text string
userData, hasKey := remakeCount[c.Sender().ID]
if hasKey {
text = fmt.Sprintf("您现在是 %s 的 %s,共 remake 了 %d 次", userData.country, userData.gender, userData.count)
} else {
text = "您还没有 remake 过呢,快 /remake 吧"
}
return c.Reply(text)
}