Skip to content

Commit

Permalink
refactor:重构帮助文档,拆离搜索引擎逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
bugtower100 committed Dec 8, 2024
1 parent 977c46b commit d28520b
Show file tree
Hide file tree
Showing 8 changed files with 528 additions and 466 deletions.
55 changes: 36 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,30 @@ 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"]),
From: fmt.Sprintf("%v", i.Fields["from"]),

Check failure on line 359 in dice/builtin_commands.go

View workflow job for this annotation

GitHub Actions / Test & Lint

unusedwrite: unused write to field From (govet)
Title: fmt.Sprintf("%v", i.Fields["title"]),
Content: fmt.Sprintf("%v", i.Fields["content"]),

Check failure on line 361 in dice/builtin_commands.go

View workflow job for this annotation

GitHub Actions / Test & Lint

unusedwrite: unused write to field Content (govet)
PackageName: fmt.Sprintf("%v", i.Fields["package"]),
// 这俩是什么东西?!
KeyWords: "",

Check failure on line 364 in dice/builtin_commands.go

View workflow job for this annotation

GitHub Actions / Test & Lint

unusedwrite: unused write to field KeyWords (govet)
RelatedExt: nil,

Check failure on line 365 in dice/builtin_commands.go

View workflow job for this annotation

GitHub Actions / Test & Lint

unusedwrite: unused write to field RelatedExt (govet)
}
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 +481,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
Loading

0 comments on commit d28520b

Please sign in to comment.