service.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package gm
  2. import (
  3. "f1-game/internal/code"
  4. "f1-game/internal/data"
  5. gameFacade "f1-game/nodes/game/internal/facade"
  6. ao "f1-game/nodes/game/player/asset/origin"
  7. "strings"
  8. "time"
  9. cstring "github.com/cherry-game/cherry/extend/string"
  10. ctime "github.com/cherry-game/cherry/extend/time"
  11. clog "github.com/cherry-game/cherry/logger"
  12. cprofile "github.com/cherry-game/cherry/profile"
  13. )
  14. var srv = &service{
  15. typ: newServiceType(),
  16. }
  17. func Service() *service {
  18. return srv
  19. }
  20. type (
  21. service struct {
  22. typ serviceType // gm服务实例
  23. }
  24. )
  25. func (*service) AddAssets(playerID int64, itemID int32, itemNum int64) int32 {
  26. _, statusCode := gameFacade.Asset.Add(playerID, itemID, itemNum, ao.GM, true)
  27. return statusCode
  28. }
  29. func (*service) TimeOffset(offset int64) {
  30. if offset == 0 {
  31. return
  32. }
  33. timeOffsetLimit := int64(ctime.SecondsPerDay)
  34. clog.Debugf("Current time: %v", ctime.Now())
  35. if offset > timeOffsetLimit {
  36. offset = timeOffsetLimit
  37. } else if offset < -timeOffsetLimit {
  38. offset = -timeOffsetLimit
  39. }
  40. if offset > 0 {
  41. ctime.AddOffsetTime(time.Duration(offset) * time.Second)
  42. } else {
  43. ctime.SubOffsetTime(time.Duration(-offset) * time.Second)
  44. }
  45. clog.Debugf("After offset time: %v", ctime.Now())
  46. }
  47. // 执行gm指令
  48. func (p *service) SendGM(playerID int64, gm string) int32 {
  49. // 判断是否是debug模式 如果不是直接返回
  50. if !cprofile.Debug() || !data.NodeState.GmEnable {
  51. return code.RouteIsDisabled
  52. }
  53. cmds := p.getCmds(gm)
  54. for _, cmd := range cmds {
  55. if len(cmd) <= 0 {
  56. continue
  57. }
  58. // 切割cmd参数
  59. params := strings.Split(cmd, " ")
  60. if len(params) < 3 {
  61. clog.Warnf("gm params error, playerID:%d, gm指令:%v", playerID, cmd)
  62. continue
  63. }
  64. // 获取cmd service模块实例 如果存在 则直接调用 否则直接返回
  65. parser, errCode := p.typ.get(params[1])
  66. if code.IsFail(errCode) {
  67. clog.Warnf("gm service not found, playerID:%d, gm指令:%v", playerID, cmd)
  68. continue
  69. }
  70. errCode = parser.Handle(playerID, params)
  71. if code.IsFail(errCode) {
  72. clog.Warnf("gm handle fail, playerID:%d, gm指令:%v", playerID, cmd)
  73. }
  74. }
  75. return code.OK
  76. }
  77. // 获取gm指令
  78. func (p *service) getCmds(gm string) []string {
  79. cmds := strings.Split(gm, ";")
  80. // 不包含读配置
  81. if !strings.Contains(gm, "config") {
  82. return cmds
  83. }
  84. finalCmds := []string{}
  85. for _, cmd := range cmds {
  86. if !strings.Contains(cmd, "config") {
  87. finalCmds = append(finalCmds, cmd)
  88. continue
  89. }
  90. // 切割cmd参数
  91. params := strings.Split(cmd, " ")
  92. // 读取config gm指令 /gm player config 命令ID // 命令ID M-命令表命令ID
  93. if len(params) != 4 || params[2] != "config" {
  94. finalCmds = append(finalCmds, cmd)
  95. continue
  96. }
  97. gmID, ok := cstring.ToInt32(params[3])
  98. if !ok {
  99. clog.Warnf("[getCmds] gm config not found, gmID:%d", gmID)
  100. continue
  101. }
  102. gmRow, found := data.Gm.GetByGmID(gmID)
  103. if !found {
  104. clog.Warnf("[getCmds] gm config not found, gmID:%d", gmID)
  105. continue
  106. }
  107. curCmds := strings.Split(gmRow.Gm, ";")
  108. finalCmds = append(finalCmds, curCmds...)
  109. }
  110. return finalCmds
  111. }