bulletcfg.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package bullet
  2. import (
  3. "encoding/json"
  4. "os"
  5. "time"
  6. "bet24.com/log"
  7. platformconfig "bet24.com/servers/micros/platformconfig/proto"
  8. )
  9. const config_key = "fishbullet_config"
  10. type BulletInfo struct {
  11. BulletID int // 子弹ID
  12. Odds int // 倍数,即对应消耗的金币
  13. Upgrade int // 升级需消耗道具数量
  14. }
  15. type BulletCfg struct {
  16. Bullets []BulletInfo
  17. lastConfigString string
  18. }
  19. func new_bulletcfg() *BulletCfg {
  20. ret := new(BulletCfg)
  21. ret.initData()
  22. return ret
  23. }
  24. func (this *BulletCfg) initData() {
  25. time.AfterFunc(10*time.Minute, this.initData)
  26. configString := platformconfig.GetConfig(config_key)
  27. if configString == "" {
  28. data, err := os.ReadFile("fish/BulletConfig.json")
  29. if err != nil {
  30. log.Release("BulletCfg.initData.loadData read json failed")
  31. return
  32. }
  33. configString = string(data)
  34. platformconfig.SetConfig(config_key, configString)
  35. }
  36. if configString == this.lastConfigString {
  37. return
  38. }
  39. this.lastConfigString = configString
  40. err := json.Unmarshal([]byte(configString), &this)
  41. if err != nil {
  42. log.Release("BulletCfg.initData Unmarshal config [%s] err:%v", configString, err)
  43. return
  44. }
  45. log.Debug("BulletConfig initData ok %v", this)
  46. }
  47. func (this *BulletCfg) getBullet(bulletID int) *BulletInfo {
  48. for _, v := range this.Bullets {
  49. if v.BulletID == bulletID {
  50. return &v
  51. }
  52. }
  53. return nil
  54. }
  55. func (this *BulletCfg) getConfig() string {
  56. d, _ := json.Marshal(this.Bullets)
  57. return string(d)
  58. }
  59. func (this *BulletCfg) getUpgradeCount(curId int) int {
  60. for _, v := range this.Bullets {
  61. if v.BulletID == curId+1 {
  62. return v.Upgrade
  63. }
  64. }
  65. return 0
  66. }