roomconfig.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/rand"
  6. "os"
  7. "bet24.com/log"
  8. platformconfig "bet24.com/servers/micros/platformconfig/proto"
  9. "bet24.com/utils"
  10. )
  11. const config_key = "fish_rooms"
  12. const config_key_chip = "fish_chip_rooms"
  13. const config_key_private = "fish_private_rooms"
  14. type OpenHistory struct {
  15. SerialNumber int
  16. SpinResult int
  17. }
  18. type RoomInfoBase struct {
  19. RoomName string
  20. RoomDesc string
  21. RoomID int
  22. RoomMin int
  23. RoomMax int
  24. BulletMin int // 炮台限制,0表示不限制
  25. BulletMax int
  26. Demo int // 非零表示练习场
  27. ServerIP string
  28. ServerPort int
  29. }
  30. type RoomInfo struct {
  31. RoomInfoBase
  32. SpawnCfg []string
  33. Group []string
  34. OddsExtra int // 附加难度,大于0表示容易,小于0表示难,万分比
  35. RoomSecond int // 每局时间
  36. NewbieExtraList []NewbieExtra
  37. }
  38. type NewbieExtra struct {
  39. MaxBullet int
  40. Extra int
  41. }
  42. func (this *RoomInfo) GetSpawn() string {
  43. if len(this.SpawnCfg) > 1 {
  44. return this.SpawnCfg[rand.Intn(len(this.SpawnCfg))]
  45. }
  46. return this.SpawnCfg[0]
  47. }
  48. func (this *RoomInfo) GetGroup() string {
  49. if len(this.Group) > 1 {
  50. return this.Group[rand.Intn(len(this.Group))]
  51. }
  52. return this.Group[0]
  53. }
  54. func GetSpawn(roomName string) string {
  55. for _, v := range Rooms.Rooms {
  56. if v.RoomName == roomName {
  57. return v.GetSpawn()
  58. }
  59. }
  60. return ""
  61. }
  62. func GetGroup(roomName string) string {
  63. for _, v := range Rooms.Rooms {
  64. if v.RoomName == roomName {
  65. return v.GetGroup()
  66. }
  67. }
  68. return ""
  69. }
  70. func GetNewbieExtraList(roomName string) []NewbieExtra {
  71. for _, v := range Rooms.Rooms {
  72. if v.RoomName == roomName {
  73. return v.NewbieExtraList
  74. }
  75. }
  76. return nil
  77. }
  78. var Rooms struct {
  79. Rooms []RoomInfo
  80. }
  81. //var Room *RoomInfo
  82. func getConfigKey() string {
  83. // 私人场优先
  84. if Server.IsPrivateRoom {
  85. return config_key_private
  86. }
  87. if Server.IsChipRoom {
  88. return config_key_chip
  89. }
  90. return config_key
  91. }
  92. func loadRedisConfig(createRoom bool) bool {
  93. data := platformconfig.GetConfig(getConfigKey())
  94. if data == "" {
  95. log.Release(" config msg is null")
  96. return false
  97. }
  98. ok, _ := marshalData([]byte(data))
  99. return ok
  100. }
  101. func loadRoomConfig() {
  102. if loadRedisConfig(true) {
  103. log.Release("using remote config ignored rooms.json !!!")
  104. return
  105. }
  106. // 保存
  107. // 加载房间表
  108. data, err := os.ReadFile("fish/rooms.json")
  109. //fmt.Println(string(data))
  110. if err != nil {
  111. log.Release("read rooms failed fish/rooms.json")
  112. return
  113. }
  114. if ok, newConfig := marshalData(data); ok {
  115. // 写入redis
  116. platformconfig.SetConfig(getConfigKey(), newConfig)
  117. }
  118. }
  119. func marshalData(data []byte) (bool, string) {
  120. changedConfigString := string(data)
  121. err := json.Unmarshal(data, &Rooms)
  122. if err != nil {
  123. log.Release("Unmarshal rooms failed err:%v", err)
  124. return false, changedConfigString
  125. }
  126. if len(Rooms.Rooms) <= 0 {
  127. log.Release("Rooms.Rooms == 0")
  128. return false, changedConfigString
  129. }
  130. //Room = &Rooms.Rooms[0]
  131. //originPort := Room.ServerPort
  132. for i := 0; i < 100; i++ {
  133. if utils.CheckPortInUse(Server.ServerPort) {
  134. Server.ServerPort++
  135. continue
  136. }
  137. break
  138. }
  139. if len(Server.ServerIP) > 0 {
  140. ws := "ws"
  141. if len(Server.CertFile) > 0 {
  142. ws = "wss"
  143. }
  144. //Room.ServerIP = fmt.Sprintf("%s://%s:%d", ws, Server.ServerIP, Room.ServerPort)
  145. for i := 0; i < len(Rooms.Rooms); i++ {
  146. Rooms.Rooms[i].ServerIP = fmt.Sprintf("%s://%s:%d", ws, Server.ServerIP, Server.ServerPort)
  147. }
  148. }
  149. return true, changedConfigString
  150. }
  151. func OnConfigChanged(key string) {
  152. if key != getConfigKey() {
  153. return
  154. }
  155. log.Release("正在刷新配置信息")
  156. loadRedisConfig(false)
  157. }