✏️ drop pb in fortune

This commit is contained in:
fumiama 2021-12-14 13:51:15 +08:00
parent 4c1926e395
commit 65f9203597
3 changed files with 18 additions and 76 deletions

View File

@ -1,5 +0,0 @@
package fortune
type Conf struct {
Kind map[int64]uint32 `protobuf:"bytes,1,rep" protobuf_key:"varint,1,opt" protobuf_val:"varint,0,opt"`
}

View File

@ -1,57 +0,0 @@
package fortune
import (
"errors"
io "io"
"os"
"sync"
"github.com/RomiChan/protobuf/proto"
"github.com/FloatTech/ZeroBot-Plugin/utils/file"
)
var (
conf Conf
mu sync.Mutex
)
func loadcfg(name string) error {
name = base + name
if file.IsExist(name) {
f, err := os.Open(name)
if err == nil {
defer f.Close()
data, err1 := io.ReadAll(f)
if err1 == nil {
if len(data) > 0 {
return proto.Unmarshal(data, &conf)
}
}
return err1
}
} else { // 如果没有 cfg则使用空 map
conf.Kind = make(map[int64]uint32)
}
return nil
}
func savecfg(name string) error {
name = base + name
data, err := proto.Marshal(&conf)
if err == nil {
if file.IsExist(base) {
f, err1 := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err1 == nil {
mu.Lock()
_, err2 := f.Write(data)
f.Close()
mu.Unlock()
return err2
}
return err1
}
return errors.New("base dir is not exist")
}
return err
}

View File

@ -35,20 +35,16 @@ var (
// 公主连结 原神 明日方舟 碧蓝航线 碧蓝幻想 战双 阴阳师
table = [...]string{"车万", "DC4", "爱因斯坦", "星空列车", "樱云之恋", "富婆妹", "李清歌", "公主连结", "原神", "明日方舟", "碧蓝航线", "碧蓝幻想", "战双", "阴阳师"}
// 映射底图与 index
index = make(map[string]uint32)
index = make(map[string]uint8)
// 下载锁
dlmu sync.Mutex
)
func init() {
err := loadcfg("cfg.pb")
if err != nil {
panic(err)
}
for i, s := range table {
index[s] = uint32(i)
index[s] = uint8(i)
}
err = os.MkdirAll(base, 0755)
err := os.MkdirAll(base, 0755)
if err != nil {
panic(err)
}
@ -68,12 +64,16 @@ func init() {
}
i, ok := index[ctx.State["regex_matched"].([]string)[1]]
if ok {
conf.Kind[gid] = i
savecfg("cfg.pb")
ctx.SendChain(message.Text("设置成功~"))
} else {
ctx.SendChain(message.Text("没有这个底图哦~"))
c, ok := control.Lookup("fortune")
if ok {
c.SetData(gid, int64(i)&0xff)
ctx.SendChain(message.Text("设置成功~"))
return
}
ctx.SendChain(message.Text("设置失败!"))
return
}
ctx.SendChain(message.Text("没有这个底图哦~"))
})
en.OnFullMatchGroup([]string{"运势", "抽签"}).SetBlock(true).SecondPriority().
Handle(func(ctx *zero.Ctx) {
@ -115,8 +115,12 @@ func init() {
gid = -ctx.Event.UserID
}
logrus.Debugln("[fortune]gid:", ctx.Event.GroupID, "uid:", ctx.Event.UserID)
if v, ok := conf.Kind[gid]; ok {
kind = table[v]
c, ok := control.Lookup("fortune")
if ok {
v := uint8(c.GetData(gid) & 0xff)
if int(v) < len(table) {
kind = table[v]
}
}
// 检查背景图片是否存在
folder := base + kind