From 65f9203597da3d9f1f61a70ea47102213c4c4b49 Mon Sep 17 00:00:00 2001 From: fumiama Date: Tue, 14 Dec 2021 13:51:15 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20=20drop=20pb=20in=20fortun?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin_fortune/cfg.pb.go | 5 ---- plugin_fortune/data.go | 57 --------------------------------------- plugin_fortune/fortune.go | 32 ++++++++++++---------- 3 files changed, 18 insertions(+), 76 deletions(-) delete mode 100644 plugin_fortune/cfg.pb.go delete mode 100644 plugin_fortune/data.go diff --git a/plugin_fortune/cfg.pb.go b/plugin_fortune/cfg.pb.go deleted file mode 100644 index 96e6500f..00000000 --- a/plugin_fortune/cfg.pb.go +++ /dev/null @@ -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"` -} diff --git a/plugin_fortune/data.go b/plugin_fortune/data.go deleted file mode 100644 index ee9e6ebf..00000000 --- a/plugin_fortune/data.go +++ /dev/null @@ -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 -} diff --git a/plugin_fortune/fortune.go b/plugin_fortune/fortune.go index 894a8403..1e874ce3 100644 --- a/plugin_fortune/fortune.go +++ b/plugin_fortune/fortune.go @@ -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