Skip to content

Commit

Permalink
refactor(config): 迁移 Dice 中的设置项至独立的 struct 中 (#1070)
Browse files Browse the repository at this point in the history
Co-authored-by: Xiangze Li <[email protected]>
Co-authored-by: PaienNate <[email protected]>
Co-authored-by: Szzrain <[email protected]>
  • Loading branch information
4 people authored Oct 19, 2024
1 parent a382281 commit 249a6f5
Show file tree
Hide file tree
Showing 36 changed files with 800 additions and 773 deletions.
2 changes: 1 addition & 1 deletion api/api_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func forceStop(c echo.Context) error {

for _, i := range diceManager.Dice {
if i.IsAlreadyLoadConfig {
i.BanList.SaveChanged(i)
i.Config.BanList.SaveChanged(i)
i.AttrsManager.CheckForSave()
i.Save(true)
for _, j := range i.ExtList {
Expand Down
46 changes: 23 additions & 23 deletions api/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func banConfigGet(c echo.Context) error {
return c.JSON(http.StatusForbidden, nil)
}

return c.JSON(http.StatusOK, myDice.BanList)
return c.JSON(http.StatusOK, myDice.Config.BanList)
}

func banConfigSet(c echo.Context) error {
Expand All @@ -42,23 +42,23 @@ func banConfigSet(c echo.Context) error {
v.ThresholdBan = 200
}

myDice.BanList.BanBehaviorRefuseReply = v.BanBehaviorRefuseReply
myDice.BanList.BanBehaviorRefuseInvite = v.BanBehaviorRefuseInvite
myDice.BanList.BanBehaviorQuitLastPlace = v.BanBehaviorQuitLastPlace
myDice.BanList.BanBehaviorQuitPlaceImmediately = v.BanBehaviorQuitPlaceImmediately
myDice.BanList.BanBehaviorQuitIfAdmin = v.BanBehaviorQuitIfAdmin
myDice.BanList.ScoreReducePerMinute = v.ScoreReducePerMinute
myDice.BanList.ThresholdWarn = v.ThresholdWarn
myDice.BanList.ThresholdBan = v.ThresholdBan
myDice.BanList.ScoreGroupMuted = v.ScoreGroupMuted
myDice.BanList.ScoreGroupKicked = v.ScoreGroupKicked
myDice.BanList.ScoreTooManyCommand = v.ScoreTooManyCommand

myDice.BanList.JointScorePercentOfGroup = v.JointScorePercentOfGroup
myDice.BanList.JointScorePercentOfInviter = v.JointScorePercentOfInviter
myDice.Config.BanList.BanBehaviorRefuseReply = v.BanBehaviorRefuseReply
myDice.Config.BanList.BanBehaviorRefuseInvite = v.BanBehaviorRefuseInvite
myDice.Config.BanList.BanBehaviorQuitLastPlace = v.BanBehaviorQuitLastPlace
myDice.Config.BanList.BanBehaviorQuitPlaceImmediately = v.BanBehaviorQuitPlaceImmediately
myDice.Config.BanList.BanBehaviorQuitIfAdmin = v.BanBehaviorQuitIfAdmin
myDice.Config.BanList.ScoreReducePerMinute = v.ScoreReducePerMinute
myDice.Config.BanList.ThresholdWarn = v.ThresholdWarn
myDice.Config.BanList.ThresholdBan = v.ThresholdBan
myDice.Config.BanList.ScoreGroupMuted = v.ScoreGroupMuted
myDice.Config.BanList.ScoreGroupKicked = v.ScoreGroupKicked
myDice.Config.BanList.ScoreTooManyCommand = v.ScoreTooManyCommand

myDice.Config.BanList.JointScorePercentOfGroup = v.JointScorePercentOfGroup
myDice.Config.BanList.JointScorePercentOfInviter = v.JointScorePercentOfInviter
myDice.MarkModified()

return c.JSON(http.StatusOK, myDice.BanList)
return c.JSON(http.StatusOK, myDice.Config.BanList)
}

func banMapList(c echo.Context) error {
Expand All @@ -75,7 +75,7 @@ func banMapDeleteOne(c echo.Context) error {
if err != nil {
return c.String(430, err.Error())
}
myDice.BanList.DeleteByID(myDice, v.ID)
myDice.Config.BanList.DeleteByID(myDice, v.ID)
return c.JSON(http.StatusOK, nil)
}

Expand All @@ -92,7 +92,7 @@ func banMapAddOne(c echo.Context) error {
return c.String(430, err.Error())
}
if v.Rank == dice.BanRankBanned {
score := myDice.BanList.ThresholdBan
score := myDice.Config.BanList.ThresholdBan
reason := "骰主后台设置"
if len(v.Reasons) > 0 {
reason = v.Reasons[0]
Expand All @@ -102,7 +102,7 @@ func banMapAddOne(c echo.Context) error {
platform := strings.Replace(prefix, "-Group", "", 1)
for _, i := range myDice.ImSession.EndPoints {
if i.Platform == platform && i.Enable {
v2 := myDice.BanList.AddScoreBase(v.ID, score, "海豹后台", reason, &dice.MsgContext{Dice: myDice, EndPoint: i})
v2 := myDice.Config.BanList.AddScoreBase(v.ID, score, "海豹后台", reason, &dice.MsgContext{Dice: myDice, EndPoint: i})
if v2 != nil {
if v.Name != "" {
v2.Name = v.Name
Expand All @@ -112,7 +112,7 @@ func banMapAddOne(c echo.Context) error {
}
}
if v.Rank == dice.BanRankTrusted {
myDice.BanList.SetTrustByID(v.ID, "海豹后台", "骰主后台设置")
myDice.Config.BanList.SetTrustByID(v.ID, "海豹后台", "骰主后台设置")
}

return c.JSON(http.StatusOK, nil)
Expand All @@ -132,7 +132,7 @@ func banMapAddOne(c echo.Context) error {
// return c.String(430, err.Error())
// }
//
// myDice.BanList.LoadMapFromJSON(v.data)
// myDice.Config.BanList.LoadMapFromJSON(v.data)
// return c.JSON(http.StatusOK, nil)
//}

Expand Down Expand Up @@ -199,9 +199,9 @@ func banImport(c echo.Context) error {
}
}
item.Reasons = newReasons
myDice.BanList.Map.Store(item.ID, item)
myDice.Config.BanList.Map.Store(item.ID, item)
}
myDice.BanList.SaveChanged(myDice)
myDice.Config.BanList.SaveChanged(myDice)

return Success(&c, Response{})
}
40 changes: 20 additions & 20 deletions api/censor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func check(c echo.Context) (bool, error) {
if dm.JustForTest {
return false, Error(&c, "展示模式不支持该操作", Response{"testMode": true})
}
if !myDice.EnableCensor {
if !myDice.Config.EnableCensor {
return false, Error(&c, "未启用拦截引擎", Response{})
}
if myDice.CensorManager.IsLoading {
Expand All @@ -47,10 +47,10 @@ func censorRestart(c echo.Context) error {
}

myDice.NewCensorManager()
myDice.EnableCensor = true
myDice.Config.EnableCensor = true

return Success(&c, Response{
"enable": myDice.EnableCensor,
"enable": myDice.Config.EnableCensor,
"isLoading": myDice.CensorManager.IsLoading,
})
}
Expand All @@ -61,7 +61,7 @@ func censorStop(c echo.Context) error {
return err
}

myDice.EnableCensor = false
myDice.Config.EnableCensor = false
_ = myDice.CensorManager.DB.Close()
myDice.CensorManager = nil

Expand All @@ -74,23 +74,23 @@ func censorGetStatus(c echo.Context) error {
isLoading = myDice.CensorManager.IsLoading
}
return Success(&c, Response{
"enable": myDice.EnableCensor,
"enable": myDice.Config.EnableCensor,
"isLoading": isLoading,
})
}

func censorGetConfig(c echo.Context) error {
levelConfig := map[string]LevelConfig{
"notice": getLevelConfig(censor.Notice, myDice.CensorThresholds, myDice.CensorHandlers, myDice.CensorScores),
"caution": getLevelConfig(censor.Caution, myDice.CensorThresholds, myDice.CensorHandlers, myDice.CensorScores),
"warning": getLevelConfig(censor.Warning, myDice.CensorThresholds, myDice.CensorHandlers, myDice.CensorScores),
"danger": getLevelConfig(censor.Danger, myDice.CensorThresholds, myDice.CensorHandlers, myDice.CensorScores),
"notice": getLevelConfig(censor.Notice, myDice.Config.CensorThresholds, myDice.Config.CensorHandlers, myDice.Config.CensorScores),
"caution": getLevelConfig(censor.Caution, myDice.Config.CensorThresholds, myDice.Config.CensorHandlers, myDice.Config.CensorScores),
"warning": getLevelConfig(censor.Warning, myDice.Config.CensorThresholds, myDice.Config.CensorHandlers, myDice.Config.CensorScores),
"danger": getLevelConfig(censor.Danger, myDice.Config.CensorThresholds, myDice.Config.CensorHandlers, myDice.Config.CensorScores),
}
return Success(&c, Response{
"mode": myDice.CensorMode,
"caseSensitive": myDice.CensorCaseSensitive,
"matchPinyin": myDice.CensorMatchPinyin,
"filterRegex": myDice.CensorFilterRegexStr,
"mode": myDice.Config.CensorMode,
"caseSensitive": myDice.Config.CensorCaseSensitive,
"matchPinyin": myDice.Config.CensorMatchPinyin,
"filterRegex": myDice.Config.CensorFilterRegexStr,
"levelConfig": levelConfig,
})
}
Expand Down Expand Up @@ -160,25 +160,25 @@ func censorSetConfig(c echo.Context) error {
if err != nil {
return Error(&c, "过滤字符正则不是合法的正则表达式", Response{})
}
myDice.CensorFilterRegexStr = filterRegex
myDice.Config.CensorFilterRegexStr = filterRegex
}
}
if val, ok := jsonMap["mode"]; ok {
mode, ok := val.(float64)
if ok {
myDice.CensorMode = dice.CensorMode(mode)
myDice.Config.CensorMode = dice.CensorMode(mode)
}
}
if val, ok := jsonMap["caseSensitive"]; ok {
caseSensitive, ok := val.(bool)
if ok {
myDice.CensorCaseSensitive = caseSensitive
myDice.Config.CensorCaseSensitive = caseSensitive
}
}
if val, ok := jsonMap["matchPinyin"]; ok {
matchPinyin, ok := val.(bool)
if ok {
myDice.CensorMatchPinyin = matchPinyin
myDice.Config.CensorMatchPinyin = matchPinyin
}
}
if val, ok := jsonMap["levelConfig"]; ok { //nolint:nestif
Expand Down Expand Up @@ -212,15 +212,15 @@ func censorSetConfig(c echo.Context) error {
if ok {
if val, ok = confMap["threshold"]; ok {
threshold := val.(float64)
myDice.CensorThresholds[level] = int(threshold)
myDice.Config.CensorThresholds[level] = int(threshold)
}
if val, ok = confMap["handlers"]; ok {
handlers := stringConvert(val)
setLevelHandlers(level, handlers)
}
if val, ok = confMap["score"]; ok {
score := val.(float64)
myDice.CensorScores[level] = int(score)
myDice.Config.CensorScores[level] = int(score)
}
}
}
Expand Down Expand Up @@ -259,7 +259,7 @@ func setLevelHandlers(level censor.Level, handlers []string) {
handlerVal = newHandlerVal(handlerVal, dice.BanInviter, newHandlers)
handlerVal = newHandlerVal(handlerVal, dice.AddScore, newHandlers)

myDice.CensorHandlers[level] = handlerVal
myDice.Config.CensorHandlers[level] = handlerVal
}

func newHandlerVal(val uint8, handle dice.CensorHandler, newHandlers map[dice.CensorHandler]bool) uint8 {
Expand Down
2 changes: 1 addition & 1 deletion api/deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func deckEnable(c echo.Context) error {
}
}

return c.JSON(http.StatusOK, myDice.BanList)
return c.JSON(http.StatusOK, myDice.Config.BanList)
}

func deckDelete(c echo.Context) error {
Expand Down
Loading

0 comments on commit 249a6f5

Please sign in to comment.