package gm import ( "f1-game/internal/code" "f1-game/internal/data" gameFacade "f1-game/nodes/game/internal/facade" ao "f1-game/nodes/game/player/asset/origin" "strings" "time" cstring "github.com/cherry-game/cherry/extend/string" ctime "github.com/cherry-game/cherry/extend/time" clog "github.com/cherry-game/cherry/logger" cprofile "github.com/cherry-game/cherry/profile" ) var srv = &service{ typ: newServiceType(), } func Service() *service { return srv } type ( service struct { typ serviceType // gm服务实例 } ) func (*service) AddAssets(playerID int64, itemID int32, itemNum int64) int32 { _, statusCode := gameFacade.Asset.Add(playerID, itemID, itemNum, ao.GM, true) return statusCode } func (*service) TimeOffset(offset int64) { if offset == 0 { return } timeOffsetLimit := int64(ctime.SecondsPerDay) clog.Debugf("Current time: %v", ctime.Now()) if offset > timeOffsetLimit { offset = timeOffsetLimit } else if offset < -timeOffsetLimit { offset = -timeOffsetLimit } if offset > 0 { ctime.AddOffsetTime(time.Duration(offset) * time.Second) } else { ctime.SubOffsetTime(time.Duration(-offset) * time.Second) } clog.Debugf("After offset time: %v", ctime.Now()) } // 执行gm指令 func (p *service) SendGM(playerID int64, gm string) int32 { // 判断是否是debug模式 如果不是直接返回 if !cprofile.Debug() || !data.NodeState.GmEnable { return code.RouteIsDisabled } cmds := p.getCmds(gm) for _, cmd := range cmds { if len(cmd) <= 0 { continue } // 切割cmd参数 params := strings.Split(cmd, " ") if len(params) < 3 { clog.Warnf("gm params error, playerID:%d, gm指令:%v", playerID, cmd) continue } // 获取cmd service模块实例 如果存在 则直接调用 否则直接返回 parser, errCode := p.typ.get(params[1]) if code.IsFail(errCode) { clog.Warnf("gm service not found, playerID:%d, gm指令:%v", playerID, cmd) continue } errCode = parser.Handle(playerID, params) if code.IsFail(errCode) { clog.Warnf("gm handle fail, playerID:%d, gm指令:%v", playerID, cmd) } } return code.OK } // 获取gm指令 func (p *service) getCmds(gm string) []string { cmds := strings.Split(gm, ";") // 不包含读配置 if !strings.Contains(gm, "config") { return cmds } finalCmds := []string{} for _, cmd := range cmds { if !strings.Contains(cmd, "config") { finalCmds = append(finalCmds, cmd) continue } // 切割cmd参数 params := strings.Split(cmd, " ") // 读取config gm指令 /gm player config 命令ID // 命令ID M-命令表命令ID if len(params) != 4 || params[2] != "config" { finalCmds = append(finalCmds, cmd) continue } gmID, ok := cstring.ToInt32(params[3]) if !ok { clog.Warnf("[getCmds] gm config not found, gmID:%d", gmID) continue } gmRow, found := data.Gm.GetByGmID(gmID) if !found { clog.Warnf("[getCmds] gm config not found, gmID:%d", gmID) continue } curCmds := strings.Split(gmRow.Gm, ";") finalCmds = append(finalCmds, curCmds...) } return finalCmds }