Skip to content

Commit

Permalink
refactor(helpdoc): 抽离帮助文档引擎并修正Arm下无法使用Bleve的问题 (#1158)
Browse files Browse the repository at this point in the history
Co-authored-by: bugtower100 <[email protected]>
  • Loading branch information
PaienNate and bugtower100 authored Dec 25, 2024
1 parent b299d71 commit 034058b
Show file tree
Hide file tree
Showing 9 changed files with 633 additions and 385 deletions.
50 changes: 31 additions & 19 deletions dice/builtin_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
"time"

"github.com/golang-module/carbon"
"github.com/samber/lo"

"github.com/juliangruber/go-intersect"
cp "github.com/otiai10/copy"
"github.com/samber/lo"
ds "github.com/sealdice/dicescript"

"sealdice-core/dice/docengine"
)

/** 这几条指令不能移除 */
Expand Down Expand Up @@ -250,6 +251,7 @@ func (d *Dice) registerCoreCommands() {

var id string
if cmdArgs.GetKwarg("rand") != nil || cmdArgs.GetKwarg("随机") != nil {
// FIXME: byd WHAT IS THAT
_id := rand.Uint64()%d.Parent.Help.CurID + 1
id = strconv.FormatUint(_id, 10)
}
Expand All @@ -270,8 +272,8 @@ func (d *Dice) registerCoreCommands() {
}

if id != "" {
text, exists := d.Parent.Help.TextMap.Load(id)
if exists {
text, err := d.Parent.Help.searchEngine.GetItemByID(id)
if err == nil {
content := d.Parent.Help.GetContent(text, 0)
ReplyToSender(ctx, msg, fmt.Sprintf("词条: %s:%s\n%s", text.PackageName, text.Title, content))
} else {
Expand Down Expand Up @@ -321,6 +323,7 @@ func (d *Dice) registerCoreCommands() {
// 未指定搜索分组时,取当前群指定的分组
group = ctx.Group.DefaultHelpGroup
}
// 进行结果搜索
search, total, pgStart, pgEnd, err := d.Parent.Help.Search(ctx, text, false, numLimit, page, group)
if err != nil {
ReplyToSender(ctx, msg, groupStr+"搜索故障: "+err.Error())
Expand All @@ -336,20 +339,25 @@ func (d *Dice) registerCoreCommands() {
}

hasSecond := len(search.Hits) >= 2
best, ok := d.Parent.Help.TextMap.Load(search.Hits[0].ID)
if !ok {
d.Logger.Errorf("加载d.Parent.Help.TextMap.Load(search.Hits[0].ID)->(%s)的数据出现错误!", search.Hits[0].ID)
ReplyToSender(ctx, msg, "未找到搜索结果,出现数据加载错误!")
return CmdExecuteResult{Matched: true, Solved: true}
// 准备接下来读取这里面的Fields
bestRaw := search.Hits[0].Fields
best := &docengine.HelpTextItem{
Group: fmt.Sprintf("%v", bestRaw["group"]),
From: fmt.Sprintf("%v", bestRaw["from"]),
Title: fmt.Sprintf("%v", bestRaw["title"]),
Content: fmt.Sprintf("%v", bestRaw["content"]),
PackageName: fmt.Sprintf("%v", bestRaw["package"]),
// 这俩是什么东西?!
// KeyWords: "",
// RelatedExt: nil,
}
others := ""

for _, i := range search.Hits {
t, ok := d.Parent.Help.TextMap.Load(i.ID)
if !ok {
d.Logger.Errorf("加载d.Parent.Help.TextMap.Load(search.Hits[0].ID)->(%s)的数据出现错误!", search.Hits[0].ID)
ReplyToSender(ctx, msg, "未找到搜索结果,出现数据加载错误!")
return CmdExecuteResult{Matched: true, Solved: true}
t := &docengine.HelpTextItem{
Group: fmt.Sprintf("%v", i.Fields["group"]),
Title: fmt.Sprintf("%v", i.Fields["title"]),
PackageName: fmt.Sprintf("%v", i.Fields["package"]),
}
if t.Group != "" && t.Group != HelpBuiltinGroup {
others += fmt.Sprintf("[%s][%s]【%s:%s】 匹配度%.2f\n", i.ID, t.Group, t.PackageName, t.Title, i.Score)
Expand Down Expand Up @@ -468,11 +476,15 @@ func (d *Dice) registerCoreCommands() {
search, _, _, _, err := d.Parent.Help.Search(ctx, cmdArgs.CleanArgs, true, 1, 1, "")
if err == nil {
if len(search.Hits) > 0 {
a, ok := d.Parent.Help.TextMap.Load(search.Hits[0].ID)
if !ok {
d.Logger.Error("HELPDOC:读取ID对应的信息出现问题")
ReplyToSender(ctx, msg, "HELPDOC:读取ID对应的信息出现问题")
return CmdExecuteResult{Matched: true, Solved: true}
a := &docengine.HelpTextItem{
Group: fmt.Sprintf("%v", search.Hits[0].Fields["group"]),
From: fmt.Sprintf("%v", search.Hits[0].Fields["from"]),
Title: fmt.Sprintf("%v", search.Hits[0].Fields["title"]),
Content: fmt.Sprintf("%v", search.Hits[0].Fields["content"]),
PackageName: fmt.Sprintf("%v", search.Hits[0].Fields["package"]),
// 这俩是什么东西?!
KeyWords: "",
RelatedExt: nil,
}
content := d.Parent.Help.GetContent(a, 0)
ReplyToSender(ctx, msg, fmt.Sprintf("%s:%s\n%s", a.PackageName, a.Title, content))
Expand Down
35 changes: 30 additions & 5 deletions dice/dice_attrs_manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dice

import (
"context"
"errors"
"fmt"
"time"
Expand All @@ -15,9 +16,15 @@ import (
type AttrsManager struct {
db *gorm.DB
logger *log.Helper
cancel context.CancelFunc
m SyncMap[string, *AttributesItem]
}

func (am *AttrsManager) Stop() {
log.Info("结束数据库保存程序...")
am.cancel()
}

// LoadByCtx 获取当前角色,如有绑定,则获取绑定的角色,若无绑定,获取群内默认卡
func (am *AttrsManager) LoadByCtx(ctx *MsgContext) (*AttributesItem, error) {
return am.Load(ctx.Group.GroupID, ctx.Player.UserID)
Expand Down Expand Up @@ -148,14 +155,25 @@ func (am *AttrsManager) LoadById(id string) (*AttributesItem, error) {
func (am *AttrsManager) Init(d *Dice) {
am.db = d.DBData
am.logger = d.Logger
// 创建一个 context 用于取消 goroutine
ctx, cancel := context.WithCancel(context.Background())
// 确保程序退出时取消上下文
go func() {
// NOTE(Xiangze Li): 这种不退出的goroutine不利于平稳结束程序
for {
am.CheckForSave()
am.CheckAndFreeUnused()
time.Sleep(15 * time.Second)
select {
case <-ctx.Done():
// 检测到取消信号后退出循环
return
default:
// 正常工作
am.CheckForSave()
am.CheckAndFreeUnused()
time.Sleep(15 * time.Second)
}
}
}()
am.cancel = cancel
}

func (am *AttrsManager) CheckForSave() (int, int) {
Expand Down Expand Up @@ -200,15 +218,22 @@ func (am *AttrsManager) CheckAndFreeUnused() {

prepareToFree := map[string]int{}
currentTime := time.Now().Unix()
tx := db.Begin()
am.m.Range(func(key string, value *AttributesItem) bool {
if value.LastUsedTime-currentTime > 60*10 {
prepareToFree[key] = 1
// 直接保存
value.SaveToDB(am.db)
value.SaveToDB(tx)
}
return true
})

err := tx.Commit().Error
if err != nil {
if am.logger != nil {
am.logger.Errorf("定期清理无用用户数据出错(提交事务): %v", err)
}
_ = tx.Rollback()
}
for key := range prepareToFree {
am.m.Delete(key)
}
Expand Down
Loading

0 comments on commit 034058b

Please sign in to comment.