service.go 2.5 KB

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