Skip to content

Commit

Permalink
feat: 增加多个子模块 vm engine 开关 (#1086)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAnotherID authored Oct 24, 2024
1 parent 392beb4 commit 96c7e90
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 36 deletions.
3 changes: 1 addition & 2 deletions api/api_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,7 @@ func Bind(e *echo.Echo, _myDice *dice.DiceManager) {
e.GET(prefix+"/dice/cmdList", DiceAllCommand)
e.POST(prefix+"/dice/upload_to_upgrade", DiceNewVersionUpload)

e.POST(prefix+"/dice/config/vm-version-for-reply-set", vmVersionForReplySet)
e.POST(prefix+"/dice/config/vm-version-for-deck-set", vmVersionForDeckSet)
e.POST(prefix+"/dice/config/vm-version-set", vmVersionSet)

e.POST(prefix+"/signin", doSignIn)
e.GET(prefix+"/signin/salt", doSignInGetSalt)
Expand Down
45 changes: 29 additions & 16 deletions api/dice_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,37 +463,50 @@ func DiceMailTest(c echo.Context) error {
return Success(&c, Response{})
}

func vmVersionForExtSetBase(c echo.Context, callback func(val string)) error {
func vmVersionSet(c echo.Context) error {
if !doAuth(c) {
return c.JSON(http.StatusForbidden, nil)
}
if dm.JustForTest {
return Error(&c, "展示模式不支持该操作", Response{"testMode": true})
}

var data struct {
var req []struct {
Type string `json:"type"`
Value string `json:"value"`
}

err := c.Bind(&data)
err := c.Bind(&req)
if err != nil {
return Error(&c, err.Error(), nil)
}
if len(req) == 0 {
return Error(&c, "缺少设置vm版本的参数", Response{})
}

callback(data.Value)
var failTypes []string
for _, data := range req {
if data.Type == "" || data.Value == "" {
failTypes = append(failTypes, data.Type)
continue
}
switch data.Type {
case dice.VMVersionReply:
(&myDice.Config).VMVersionForReply = data.Value
case dice.VMVersionDeck:
(&myDice.Config).VMVersionForDeck = data.Value
case dice.VMVersionCustomText:
(&myDice.Config).VMVersionForCustomText = data.Value
case dice.VmVersionMsg:
(&myDice.Config).VMVersionForMsg = data.Value
default:
failTypes = append(failTypes, data.Type)
}
}
myDice.MarkModified()
myDice.Parent.Save()
return Success(&c, Response{})
}

func vmVersionForReplySet(c echo.Context) error {
return vmVersionForExtSetBase(c, func(val string) {
(&myDice.Config).VMVersionForReply = val
})
}
myDice.Save(false)

func vmVersionForDeckSet(c echo.Context) error {
return vmVersionForExtSetBase(c, func(val string) {
(&myDice.Config).VMVersionForDeck = val
return Success(&c, Response{
"failTypes": failTypes,
})
}
6 changes: 4 additions & 2 deletions dice/dice_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,10 @@ type BaseConfig struct {
ReplyDebugMode bool `yaml:"replyDebugMode" json:"replyDebugMode"` // 回复调试
PlayerNameWrapEnable bool `yaml:"playerNameWrapEnable" json:"playerNameWrapEnable"` // 启用玩家名称外框

VMVersionForReply string `json:"VMVersionForReply" yaml:"VMVersionForReply"` // 自定义回复使用的vm版本
VMVersionForDeck string `json:"VMVersionForDeck" yaml:"VMVersionForDeck"` // 牌堆使用的vm版本
VMVersionForReply string `json:"VMVersionForReply" yaml:"VMVersionForReply"` // 自定义回复使用的vm版本
VMVersionForDeck string `json:"VMVersionForDeck" yaml:"VMVersionForDeck"` // 牌堆使用的vm版本
VMVersionForCustomText string `json:"VMVersionForCustomText" yaml:"VMVersionForCustomText"` // 自定义文案使用的vm版本
VMVersionForMsg string `json:"VMVersionForMsg" yaml:"VMVersionForMsg"` // 消息里使用的vm版本,也包括可能的一些边角类型的执行选择

// TODO: 历史遗留问题,由于不输出DICE日志效果过差,已经抹除日志输出选项,剩余两个选项,私以为可以想办法也抹除掉。
Name string `yaml:"name"` // 名称,默认为default
Expand Down
4 changes: 3 additions & 1 deletion dice/dice_config_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ var DefaultConfig = Config{
AliveNoticeValue: "@every 3h",
ReplyDebugMode: false,
PlayerNameWrapEnable: true,
VMVersionForDeck: "v2",
VMVersionForReply: "v1",
VMVersionForDeck: "v2",
VMVersionForCustomText: "v2",
VMVersionForMsg: "v2",
Name: "default",
DataDir: "data/default",
},
Expand Down
2 changes: 1 addition & 1 deletion dice/ext_deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ func deckStringFormat(ctx *MsgContext, deckInfo *DeckInfo, s string) (string, er
s = ImageRewrite(s, imgSolve)

s = strings.ReplaceAll(s, "\n", `\n`)
if ctx.Dice.Config.VMVersionForDeck == "v1" {
if ctx.Dice.getTargetVmEngineVersion(VMVersionDeck) == "v1" {
return DiceFormatV1(ctx, s)
} else {
return DiceFormatV2(ctx, s)
Expand Down
6 changes: 3 additions & 3 deletions dice/ext_reply_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (m *ReplyConditionExprTrue) Check(ctx *MsgContext, _ *Message, _ *CmdArgs,
// r := ctx.Eval(m.Value, ds.RollConfig{})
flags := RollExtraFlags{
V2Only: true,
V1Only: ctx.Dice.Config.VMVersionForReply == "v1",
V1Only: ctx.Dice.getTargetVmEngineVersion(VMVersionReply) == "v1",
}

r, _, err := DiceExprEvalBase(ctx, m.Value, flags)
Expand Down Expand Up @@ -182,7 +182,7 @@ func formatExprForReply(ctx *MsgContext, expr string) string {
var text string
var err error

if ctx.Dice.Config.VMVersionForReply == "v1" {
if ctx.Dice.getTargetVmEngineVersion(VMVersionReply) == "v1" {
text, err = DiceFormatV1(ctx, expr)
if err != nil {
// text = fmt.Sprintf("执行出错V1: %s", err.Error())
Expand Down Expand Up @@ -260,7 +260,7 @@ func (m *ReplyResultRunText) Execute(ctx *MsgContext, _ *Message, _ *CmdArgs) {
time.Sleep(time.Duration(m.Delay * float64(time.Second)))
flags := RollExtraFlags{
V2Only: true,
V1Only: ctx.Dice.Config.VMVersionForReply == "v1",
V1Only: ctx.Dice.getTargetVmEngineVersion(VMVersionReply) == "v1",
}
_, _, _ = DiceExprTextBase(ctx, m.Message, flags)
}
Expand Down
5 changes: 1 addition & 4 deletions dice/im_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1350,10 +1350,7 @@ func (s *IMSession) OnGroupMemberJoined(ctx *MsgContext, msg *Message) {
stdID := msg.Sender.UserID
VarSetValueStr(ctx, "$t帐号ID", stdID)
VarSetValueStr(ctx, "$t账号ID", stdID)
text, err := DiceFormatV2(ctx, groupInfo.GroupWelcomeMessage)
if err != nil {
text = fmt.Sprintf("执行出错V2: %s", err.Error())
}
text := DiceFormat(ctx, groupInfo.GroupWelcomeMessage)
for _, i := range ctx.SplitText(text) {
doSleepQQ(ctx)
ReplyGroup(ctx, msg, strings.TrimSpace(i))
Expand Down
20 changes: 13 additions & 7 deletions dice/rollvm_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,19 @@ func DiceFormatV1(ctx *MsgContext, s string) (string, error) { //nolint:revive
}

func DiceFormat(ctx *MsgContext, s string) string {
ret, err := DiceFormatV2(ctx, s)
if err != nil {
// 遇到异常,尝试一下V1
ret, _ = DiceFormatV1(ctx, s)
engineVersion := ctx.Dice.getTargetVmEngineVersion(VmVersionMsg)
if engineVersion == "v2" {
ret, err := DiceFormatV2(ctx, s)
if err != nil {
// 遇到异常,尝试一下V1
ret, _ = DiceFormatV1(ctx, s)
return ret
}
return ret
} else {
ret, _ := DiceFormatV1(ctx, s)
return ret
}
return ret
}

func DiceFormatTmpl(ctx *MsgContext, s string) string {
Expand All @@ -98,7 +104,7 @@ func DiceFormatTmpl(ctx *MsgContext, s string) string {
text = ctx.Dice.TextMap[s].PickSource(randSourceDrawAndTmplSelect).(string)

// 找出其兼容情况,以决定使用什么版本的引擎
engineVersion := "v2"
engineVersion := ctx.Dice.getTargetVmEngineVersion(VMVersionCustomText)
if items, exists := ctx.Dice.TextMapCompatible.Load(s); exists {
if info, exists := items.Load(text); exists {
if info.Version == "v1" {
Expand All @@ -110,7 +116,7 @@ func DiceFormatTmpl(ctx *MsgContext, s string) string {
if engineVersion == "v2" {
ret, _ := DiceFormatV2(ctx, text)
return ret
} else if engineVersion == "v1" {
} else {
ret, _ := DiceFormatV1(ctx, text)
return ret
}
Expand Down
22 changes: 22 additions & 0 deletions dice/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,25 @@ func UnpackGroupUserId(id string) (groupIdPart, userIdPart string, ok bool) {

return "", "", false
}

const (
VMVersionReply = "reply"
VMVersionDeck = "deck"
VMVersionCustomText = "custom-text"
VmVersionMsg = "msg"
)

func (d *Dice) getTargetVmEngineVersion(targetType string) string {
switch targetType {
case VMVersionReply:
return d.Config.VMVersionForReply
case VMVersionDeck:
return d.Config.VMVersionForDeck
case VMVersionCustomText:
return d.Config.VMVersionForCustomText
case VmVersionMsg:
return d.Config.VMVersionForMsg
default:
return "v2"
}
}

0 comments on commit 96c7e90

Please sign in to comment.