This commit is contained in:
cdle
2023-06-30 13:58:31 +08:00
parent 9c09a5588b
commit 6f1f9f0dc9
5 changed files with 68 additions and 7 deletions
+56
View File
@@ -0,0 +1,56 @@
package core
import (
"encoding/json"
"time"
"github.com/cdle/sillyplus/utils"
)
type PMsg struct {
Class string `json:"class"`
Unix int `json:"unix"`
Content string `json:"content"`
}
var plugin_messages = MakeBucket("plugin_messages")
func WritePluginMessage(uuid string, class string, content string) {
if uuid == "" {
return
}
var data = plugin_messages.GetBytes(uuid)
pmsgs := []PMsg{}
json.Unmarshal(data, &pmsgs)
s := &Strings{}
ok := false
new := PMsg{
Class: class,
Unix: int(time.Now().Unix()),
Content: content,
}
for i := range pmsgs {
if pmsgs[i].Class != class {
continue
}
if content == "" {
continue
}
if s.Similarity(pmsgs[i].Content, content) > 0.9 {
pmsgs[i] = new
ok = true
break
}
}
if !ok {
pmsgs = append(pmsgs, new)
}
plugin_messages.Set(uuid, utils.JsonMarshal(pmsgs))
}
func GetPluginMessage(uuid string) []PMsg {
var data = plugin_messages.GetBytes(uuid)
pmsgs := []PMsg{}
json.Unmarshal(data, &pmsgs)
return pmsgs
}