Skip to content

Commit 6b56d66

Browse files
authored
feat:解塔罗牌 (#217)
1 parent 5081aab commit 6b56d66

2 files changed

Lines changed: 86 additions & 31 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ print("run[CQ:image,file="+j["img"]+"]")
807807

808808
- [x] 抽塔罗牌
809809
- [x] 抽n张塔罗牌
810+
- [x] 解塔罗牌[牌名]
810811

811812
</details>
812813
<details>

plugin/tarot/tarot.go

Lines changed: 85 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"math/rand"
77
"strconv"
8+
"strings"
89

910
"github.com/FloatTech/zbputils/control"
1011
"github.com/FloatTech/zbputils/ctxext"
@@ -15,27 +16,29 @@ import (
1516

1617
const bed = "https://gitcode.net/shudorcl/zbp-tarot/-/raw/master/"
1718

19+
type cardInfo struct {
20+
Description string `json:"description"`
21+
ReverseDescription string `json:"reverseDescription"`
22+
ImgURL string `json:"imgUrl"`
23+
}
1824
type card struct {
19-
Name string `json:"name"`
20-
Info struct {
21-
Description string `json:"description"`
22-
ReverseDescription string `json:"reverseDescription"`
23-
ImgURL string `json:"imgUrl"`
24-
} `json:"info"`
25+
Name string `json:"name"`
26+
cardInfo `json:"info"`
2527
}
26-
type cardset = map[string]card
28+
type cardSet = map[string]card
29+
30+
var cardMap = make(cardSet, 30)
31+
var infoMap = make(map[string]cardInfo, 30)
2732

28-
var cardMap = make(cardset, 256)
29-
var reasons = [...]string{"您抽到的是~\n", "锵锵锵,塔罗牌的预言是~\n", "诶,让我看看您抽到了~\n"}
30-
var position = [...]string{"正位", "逆位"}
33+
// var cardName = make([]string, 22)
3134

3235
func init() {
3336
engine := control.Register("tarot", &control.Options{
3437
DisableOnDefault: false,
3538
Help: "塔罗牌\n" +
3639
"- 抽塔罗牌\n" +
37-
"- 抽n张塔罗牌",
38-
// TODO 抽X张塔罗牌 解塔罗牌[牌名]
40+
"- 抽n张塔罗牌\n" +
41+
"- 解塔罗牌[牌名]",
3942
PublicDataFolder: "Tarot",
4043
}).ApplySingle(ctxext.DefaultSingle)
4144

@@ -57,6 +60,9 @@ func init() {
5760
)).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
5861
match := ctx.State["regex_matched"].([]string)[1]
5962
n := 1
63+
reasons := [...]string{"您抽到的是~\n", "锵锵锵,塔罗牌的预言是~\n", "诶,让我看看您抽到了~\n"}
64+
position := [...]string{"正位", "逆位"}
65+
reverse := [...]string{"", "Reverse"}
6066
if match != "" {
6167
var err error
6268
n, err = strconv.Atoi(match[:len(match)-3])
@@ -78,34 +84,82 @@ func init() {
7884
}
7985
}
8086
if n == 1 {
81-
if id := ctx.Send(randTarot()); id.ID() == 0 {
87+
i := rand.Intn(22)
88+
p := rand.Intn(2)
89+
card := cardMap[(strconv.Itoa(i))]
90+
name := card.Name
91+
if id := ctx.SendChain(
92+
message.Text(reasons[rand.Intn(len(reasons))], position[p], " 的 ", name, "\n"),
93+
message.Image(fmt.Sprintf(bed+"MajorArcana%s/%d.png", reverse[p], i))); id.ID() == 0 {
8294
ctx.SendChain(message.Text("ERROR:可能被风控了"))
8395
}
8496
return
8597
}
8698
msg := make([]message.MessageSegment, n)
99+
randomIntMap := make(map[int]int, 30)
87100
for i := range msg {
88-
msg[i] = ctxext.FakeSenderForwardNode(ctx, randTarot()...)
101+
j := rand.Intn(22)
102+
_, ok := randomIntMap[j]
103+
for ok {
104+
j = rand.Intn(22)
105+
_, ok = randomIntMap[j]
106+
}
107+
randomIntMap[j] = 0
108+
p := rand.Intn(2)
109+
card := cardMap[(strconv.Itoa(j))]
110+
name := card.Name
111+
tarotMsg := []message.MessageSegment{
112+
message.Text(reasons[rand.Intn(len(reasons))], position[p], " 的 ", name, "\n"),
113+
message.Image(fmt.Sprintf(bed+"MajorArcana%s/%d.png", reverse[p], j))}
114+
msg[i] = ctxext.FakeSenderForwardNode(ctx, tarotMsg...)
89115
}
90116
ctx.SendGroupForwardMessage(ctx.Event.GroupID, msg)
91117
return
92118
})
93-
}
94119

95-
func randTarot() []message.MessageSegment {
96-
i := rand.Intn(22)
97-
p := rand.Intn(2)
98-
card := cardMap[(strconv.Itoa(i))]
99-
name := card.Name
100-
var info string
101-
if p == 0 {
102-
info = card.Info.Description
103-
} else {
104-
info = card.Info.ReverseDescription
105-
}
106-
return []message.MessageSegment{
107-
message.Text(reasons[rand.Intn(len(reasons))], position[p], " 的 ", name, "\n"),
108-
message.Image(fmt.Sprintf(bed+"MajorArcana/%d.png", i)),
109-
message.Text("\n其意义为: ", info),
110-
}
120+
engine.OnRegex(`^解塔罗牌\s?(.*)`, ctxext.DoOnceOnSuccess(
121+
func(ctx *zero.Ctx) bool {
122+
if len(cardMap) > 0 {
123+
for _, card := range cardMap {
124+
infoMapKey := strings.Split(card.Name, "(")[0]
125+
infoMap[infoMapKey] = card.cardInfo
126+
// 可以拿来显示大阿尔卡纳列表
127+
// cardName = append(cardName, infoMapKey)
128+
}
129+
return true
130+
}
131+
tempMap := make(cardSet, 30)
132+
data, err := engine.GetLazyData("tarots.json", true)
133+
if err != nil {
134+
ctx.SendChain(message.Text("ERROR:", err))
135+
return false
136+
}
137+
err = json.Unmarshal(data, &tempMap)
138+
if err != nil {
139+
ctx.SendChain(message.Text("ERROR:", err))
140+
return false
141+
}
142+
143+
for _, card := range tempMap {
144+
infoMapKey := strings.Split(card.Name, "(")[0]
145+
infoMap[infoMapKey] = card.cardInfo
146+
// 可以拿来显示大阿尔卡纳列表
147+
// cardName = append(cardName, infoMapKey)
148+
}
149+
return true
150+
},
151+
)).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
152+
match := ctx.State["regex_matched"].([]string)[1]
153+
info, ok := infoMap[match]
154+
if ok {
155+
ctx.SendChain(
156+
message.Image(bed+info.ImgURL),
157+
message.Text("\n", match, "的含义是~"),
158+
message.Text("\n正位:", info.Description),
159+
message.Text("\n逆位:", info.ReverseDescription))
160+
} else {
161+
ctx.SendChain(message.Text("没有找到", match, "噢~"))
162+
}
163+
return
164+
})
111165
}

0 commit comments

Comments
 (0)