Skip to content

Commit

Permalink
Merge branch 'master' into refactor/otter_context
Browse files Browse the repository at this point in the history
  • Loading branch information
PaienNate authored Jan 17, 2025
2 parents 23ae62a + 13e3312 commit 31eeefe
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 41 deletions.
3 changes: 3 additions & 0 deletions api/dice_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"net/http"
"time"

"sealdice-core/dice"

Expand Down Expand Up @@ -41,5 +42,7 @@ func dicePublicSet(c echo.Context) error {
myDice.PublicDiceInfoRegister()
myDice.PublicDiceEndpointRefresh()
myDice.PublicDiceSetupTick()
myDice.LastUpdatedTime = time.Now().Unix()
myDice.Save(false)
return Success(&c, Response{})
}
1 change: 0 additions & 1 deletion api/helpdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func helpDocReload(c echo.Context) error {
dm.Help.Close()

dm.InitHelp()
dm.AddHelpWithDice(dm.Dice[0])
return Success(&c, Response{})
}
return Error(&c, "帮助文档正在重新装载", Response{})
Expand Down
2 changes: 0 additions & 2 deletions dice/builtin_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,6 @@ func (d *Dice) registerCoreCommands() {
dm.Help.Close()

dm.InitHelp()
dm.AddHelpWithDice(dm.Dice[0])
ReplyToSender(ctx, msg, "帮助文档已经重新装载")
} else {
ReplyToSender(ctx, msg, "帮助文档正在重新装载,请稍后...")
Expand Down Expand Up @@ -1122,7 +1121,6 @@ func (d *Dice) registerCoreCommands() {
dm.IsHelpReloading = true
dm.Help.Close()
dm.InitHelp()
dm.AddHelpWithDice(dice)
ReplyToSender(ctx, msg, "帮助文档已重载")
} else {
ReplyToSender(ctx, msg, "帮助文档正在重新装载")
Expand Down
85 changes: 61 additions & 24 deletions dice/dice_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (e HelpTextItems) Len() int {

type HelpManager struct {
CurID uint64
Parent *DiceManager
EngineType EngineType
LoadingFn string
HelpDocTree []*HelpDoc
Expand Down Expand Up @@ -122,7 +121,7 @@ func (m *HelpManager) Close() {
_ = os.RemoveAll("./_help_cache")
}

func (m *HelpManager) Load() {
func (m *HelpManager) Load(internalCmdMap CmdMapCls, extList []*ExtInfo) {
m.loadSearchEngine()

_ = m.AddItem(docengine.HelpTextItem{
Expand Down Expand Up @@ -184,7 +183,7 @@ func (m *HelpManager) Load() {
totalEntries := len(entries)
for i, entry := range entries {
progress := float64(i+1) / float64(totalEntries) * 100
log.Infof("处理帮助文档组[文件夹]: 当前帮助文档加载总进度: %s %.2f%% (%d/%d)", entry.Name(), progress, i+1, totalEntries)
log.Infof("[帮助文档] 处理用户定义帮助文档组[文件夹]: 当前帮助文档加载进度: %s %.2f%% (%d/%d)", entry.Name(), progress, i+1, totalEntries)
if strings.HasPrefix(entry.Name(), ".") {
continue
}
Expand Down Expand Up @@ -219,7 +218,29 @@ func (m *HelpManager) Load() {
})
m.HelpDocTree = append(m.HelpDocTree, &child)
}
_ = m.AddItemApply(true)
err = m.AddItemApply(false)
if err != nil {
log.Errorf("加载用户自定义帮助文档出现异常!: %v", err)
}
log.Infof("[帮助文档] 用户定义的帮助文档组已加载完成!")
log.Infof("[帮助文档] 正在处理指令相关(含插件)帮助文档组")
err = m.addInternalCmdHelp(internalCmdMap)
if err != nil {
log.Errorf("加载内置指令帮助文档出现异常: %v", err)
}
err = m.AddItemApply(false)
if err != nil {
log.Errorf("加载内置指令帮助文档出现异常: %v", err)
}
err = m.addExternalCmdHelp(extList)
if err != nil {
log.Errorf("加载插件指令帮助文档出现异常: %v", err)
}
err = m.AddItemApply(true)
if err != nil {
log.Errorf("加载插件指令帮助文档出现异常: %v", err)
}
log.Infof("[帮助文档] 指令相关(含插件)帮助文档组已加载完成!")
m.CurID = m.searchEngine.GetTotalID()
elapsed := time.Since(start) // 计算执行时间
log.Infof("帮助文档加载完毕,共耗费时间: %s 共计加载条目:%d\n", elapsed, m.CurID)
Expand Down Expand Up @@ -409,35 +430,51 @@ out:
return synonymCount, nil
}

func (dm *DiceManager) AddHelpWithDice(dice *Dice) {
m := dm.Help

addCmdMap := func(packageName string, cmdMap CmdMapCls) {
for k, v := range cmdMap {
content := v.Help
if content == "" {
content = v.ShortHelp
}
_ = m.AddItem(docengine.HelpTextItem{
Group: HelpBuiltinGroup,
Title: k,
Content: content,
PackageName: packageName,
})
func (m *HelpManager) addCmdMap(packageName string, cmdMap CmdMapCls) error {
for k, v := range cmdMap {
content := v.Help
if content == "" {
content = v.ShortHelp
}
err := m.AddItem(docengine.HelpTextItem{
Group: HelpBuiltinGroup,
Title: k,
Content: content,
PackageName: packageName,
})
if err != nil {
log.Errorf("AddCmdMapItem err:%v", err)
return err
}
}
return nil
}

func (m *HelpManager) addInternalCmdHelp(cmdMap CmdMapCls) error {
err := m.addCmdMap("核心指令", cmdMap)
if err != nil {
return err
}
return nil
}

addCmdMap("核心指令", dice.CmdMap)
for _, i := range dice.ExtList {
_ = m.AddItem(docengine.HelpTextItem{
func (m *HelpManager) addExternalCmdHelp(ext []*ExtInfo) error {
for _, i := range ext {
err := m.AddItem(docengine.HelpTextItem{
Group: HelpBuiltinGroup,
Title: i.Name,
Content: i.GetDescText(i),
PackageName: "扩展模块",
})
addCmdMap(i.Name, i.CmdMap)
if err != nil {
return err
}
err = m.addCmdMap(i.Name, i.CmdMap)
if err != nil {
return err
}
}
_ = m.AddItemApply(false)
return nil
}

func (m *HelpManager) AddItem(item docengine.HelpTextItem) error {
Expand Down
11 changes: 5 additions & 6 deletions dice/dice_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ func (dm *DiceManager) InitHelp() {
dm.IsHelpReloading = true
_ = os.MkdirAll("./data/helpdoc", 0755)
dm.Help = new(HelpManager)
dm.Help.Parent = dm
dm.Help.EngineType = EngineType(dm.HelpDocEngineType)
dm.Help.Load()
if len(dm.Dice) == 0 {
log.Fatalf("Dice实例不存在!")
return
}
dm.Help.Load(dm.Dice[0].CmdMap, dm.Dice[0].ExtList)
dm.IsHelpReloading = false
}

Expand Down Expand Up @@ -255,12 +258,8 @@ func (dm *DiceManager) InitDice() {
}
}
}()

// 加载帮助
dm.InitHelp()
if len(dm.Dice) >= 1 {
dm.AddHelpWithDice(dm.Dice[0])
}
}()

dm.ResetAutoBackup()
Expand Down
4 changes: 1 addition & 3 deletions dice/docengine/bleve.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ func (d *BleveSearchEngine) AddItem(item HelpTextItem) (string, error) {
// AddItemApply 这里认为是真正执行插入文档的逻辑
// 由于现在已经将执行函数改为了可按文件执行,所以可以按文件进行Apply,这应当不会有太大的量级。
// end代表是否是最后一次执行,一般用在所有的数据都处理完之后,关闭逻辑的时候使用,如bleve batch重复利用后最后销毁
// TODO: 似乎很奇怪,这家伙貌似不会回收内存的吗?
func (d *BleveSearchEngine) AddItemApply(end bool) error {
if d.batch != nil {
// 执行batch
Expand All @@ -130,11 +129,10 @@ func (d *BleveSearchEngine) AddItemApply(end bool) error {
}
// 如果是最后一批
if end {
// 销毁batch
d.batch.Reset()
d.batch = nil
} else {
// 否则重置batch
// 否则仅重置batch
d.batch.Reset()
}
return err
Expand Down
3 changes: 2 additions & 1 deletion dice/model/group_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ func GroupPlayerInfoGet(db *gorm.DB, groupID string, playerID string) *GroupPlay
result := db.Model(&GroupPlayerInfoBase{}).
Where("group_id = ? AND user_id = ?", groupID, playerID).
Select("name, last_command_time, auto_set_name_template, dice_side_num").
First(&ret)
Limit(1).
Find(&ret)
err := result.Error
// 如果查询发生错误,打印错误并返回 nil
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions dice/platform_adapter_lagrange_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,21 +429,21 @@ func LagrangeGetSignInfo(dice *Dice) ([]SignInfo, error) {
cachePath := filepath.Join(dice.BaseConfig.DataDir, "extra/SignInfo.cache")
signInfo, err := lagrangeGetSignInfoFromCloud(cachePath)
if err == nil && len(signInfo) > 0 {
copy(signInfoGlobal, signInfo)
signInfoGlobal = append([]SignInfo(nil), signInfo...)
return signInfo, nil
}
dice.Logger.Infof("无法从云端获取SignInfo,即将读取本地缓存数据, 原因: %s", err.Error())

signInfo, err = lagrangeGetSignInfoFromCache(cachePath)
if err == nil && len(signInfo) > 0 {
copy(signInfoGlobal, signInfo)
signInfoGlobal = append([]SignInfo(nil), signInfo...)
return signInfo, nil
}
dice.Logger.Infof("无法从本地缓存获取SignInfo,即将读取内置数据, 原因: %s", err.Error())

if err = json.Unmarshal([]byte(signInfoJson), &signInfo); err == nil {
lagrangeGetSignServerLatency(signInfo)
copy(signInfoGlobal, signInfo)
signInfoGlobal = append([]SignInfo(nil), signInfo...)
return signInfo, nil
}
dice.Logger.Infof("无法从内置数据获取SignInfo,请联系开发者上报问题, 原因: %s", err.Error())
Expand Down
2 changes: 1 addition & 1 deletion dice/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
// APP_CHANNEL 更新频道,stable/dev,在 action 构建时自动注入
APP_CHANNEL = "dev" //nolint:revive

VERSION_CODE = int64(1004100) //nolint:revive
VERSION_CODE = int64(1004101) //nolint:revive

VERSION_JSAPI_COMPATIBLE = []*semver.Version{
VERSION,
Expand Down

0 comments on commit 31eeefe

Please sign in to comment.