game_debug.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package data
  2. import (
  3. "f1-game/internal/types"
  4. cprofile "github.com/cherry-game/cherry/profile"
  5. )
  6. var GameDebug = &gameDebugConfig{}
  7. func init() {
  8. _allList = append(_allList, GameDebug)
  9. }
  10. type (
  11. GameDebugRow struct {
  12. Env string `json:"Env"` // 环境
  13. Debug bool `json:"Debug"` // 是否开启调试
  14. Values types.Map[string, int64] `json:"Values"` // 调试值
  15. }
  16. gameDebugConfig struct {
  17. baseConfig
  18. list []*GameDebugRow // all list
  19. maps map[string]*GameDebugRow // key:env
  20. }
  21. )
  22. func (p *gameDebugConfig) Name() string {
  23. return "game_debug"
  24. }
  25. func (p *gameDebugConfig) Init() {
  26. p.maps = map[string]*GameDebugRow{}
  27. }
  28. func (p *gameDebugConfig) OnLoad(maps interface{}, reload bool) (int, error) {
  29. if reload && p.denyLoad {
  30. return 0, nil
  31. }
  32. var list []*GameDebugRow
  33. if err := DecodeData(maps, &list); err != nil {
  34. return 0, err
  35. }
  36. p.list = list
  37. m := map[string]*GameDebugRow{}
  38. for _, row := range list {
  39. m[row.Env] = row
  40. }
  41. p.maps = m
  42. return len(list), nil
  43. }
  44. // func (p *chapterRewardConfig) OnAfterLoad(_ bool) {
  45. // }
  46. func (p *gameDebugConfig) List() []*GameDebugRow {
  47. return p.list
  48. }
  49. func (p *gameDebugConfig) GetByEnv(env string) (*GameDebugRow, bool) {
  50. mapItem, found := p.maps[env]
  51. if !found {
  52. return nil, false
  53. }
  54. return mapItem, found
  55. }
  56. func (p *gameDebugConfig) GetDebugValue(key string) (int64, bool) {
  57. row, ok := p.GetByEnv(cprofile.Env())
  58. if !ok || !row.Debug {
  59. return 0, false
  60. }
  61. return row.Values.Get(key)
  62. }