roomconfig.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/rand"
  6. "os"
  7. "sync"
  8. "bet24.com/log"
  9. coreservice "bet24.com/servers/coreservice/client"
  10. )
  11. const config_key = "ludo_rooms"
  12. type RobotParam struct {
  13. WinAmount int // 输赢金额
  14. Param1 int // 换牌解闷概率
  15. Param2 int // 换牌闷玩家概率
  16. }
  17. type RobotConfig struct {
  18. MinRobotGold int
  19. MaxRobotGold int
  20. RobotOnlineSec int
  21. RobotCount int
  22. MinRoom int
  23. Params []RobotParam
  24. winAmount int
  25. }
  26. func (rc *RobotConfig) getParam1() int {
  27. biggest := 0
  28. for _, v := range rc.Params {
  29. if rc.winAmount >= v.WinAmount {
  30. return v.Param1
  31. }
  32. if v.Param1 > biggest {
  33. biggest = v.Param1
  34. }
  35. }
  36. return biggest
  37. }
  38. func (rc *RobotConfig) addWinAmount(amount int) {
  39. rc.winAmount += amount
  40. }
  41. type DiceCount struct {
  42. Point int //点数
  43. Count int //概率
  44. }
  45. type DiceRound struct {
  46. StartRound int //开始轮次
  47. EndRound int //截止轮次
  48. pool []int
  49. DiceCount []int
  50. }
  51. type DiceConfig struct {
  52. Rounds []*DiceRound
  53. }
  54. func (dr *DiceRound) makePool() {
  55. dr.pool = []int{}
  56. for k, v := range dr.DiceCount {
  57. for i := 0; i < v; i++ {
  58. dr.pool = append(dr.pool, k+1)
  59. }
  60. }
  61. //logs.Debug("DiceRound.makePool %d-%d[%v", dr.StartRound, dr.EndRound, dr.pool)
  62. }
  63. /*
  64. 根据轮次获取骰子点数
  65. */
  66. func (dc *DiceConfig) getDicePointByRound(round int) int {
  67. var point = -1
  68. for _, v := range dc.Rounds {
  69. if round >= v.StartRound && round <= v.EndRound {
  70. point = v.pool[rand.Intn(len(v.pool))]
  71. return point
  72. }
  73. }
  74. if point < 1 {
  75. last := len(dc.Rounds) - 1
  76. point = dc.Rounds[last].pool[rand.Intn(len(dc.Rounds[last].pool))]
  77. }
  78. return point
  79. }
  80. type RoomInfoBase struct {
  81. RoomID int //场次ID, 唯一
  82. RoomName string
  83. RoomDesc string
  84. MinGold int
  85. MaxGold int
  86. BaseScore int
  87. ServerIP string
  88. ServerPort int
  89. DiceSec int //掷骰子计时 4000 客户端显示少一秒
  90. MoveSec int //移动飞机计时10000
  91. IsDual int // 是否2人桌
  92. TaxRate int // 税收
  93. }
  94. type RoomInfo struct {
  95. RoomInfoBase
  96. RobotConfig
  97. DiceConfig
  98. }
  99. type RoomList struct {
  100. Rooms []RoomInfo
  101. lock *sync.RWMutex
  102. }
  103. var Rooms *RoomList
  104. func (r *RoomList) AddWinAmount(roomId int, amount int) {
  105. r.lock.Lock()
  106. defer r.lock.Unlock()
  107. for i := 0; i < len(r.Rooms); i++ {
  108. if r.Rooms[i].RoomID == roomId {
  109. r.Rooms[i].addWinAmount(amount)
  110. return
  111. }
  112. }
  113. }
  114. func (r *RoomList) GetParam1(roomId int) int {
  115. r.lock.RLock()
  116. defer r.lock.RUnlock()
  117. for i := 0; i < len(r.Rooms); i++ {
  118. if r.Rooms[i].RoomID == roomId {
  119. return r.Rooms[i].getParam1()
  120. }
  121. }
  122. return 0
  123. }
  124. func (r *RoomList) GetDicePointByRound(roomId, round int) int {
  125. r.lock.RLock()
  126. defer r.lock.RUnlock()
  127. for i := 0; i < len(r.Rooms); i++ {
  128. if r.Rooms[i].RoomID == roomId {
  129. return r.Rooms[i].getDicePointByRound(round)
  130. }
  131. }
  132. return -1
  133. }
  134. func (r *RoomList) Dump() string {
  135. r.lock.RLock()
  136. defer r.lock.RUnlock()
  137. var ret string
  138. for _, v := range r.Rooms {
  139. ret = fmt.Sprintf("%s Room:%s winAmount:%d\n", ret, v.RoomName, v.winAmount)
  140. }
  141. return ret
  142. }
  143. func loadRedisConfig() bool {
  144. data := coreservice.GetPlatformConfig(config_key)
  145. if data == "" {
  146. log.Release(" config msg is null")
  147. return false
  148. }
  149. return marshalData([]byte(data))
  150. }
  151. func loadRoomConfig() {
  152. if loadRedisConfig() {
  153. log.Release("using remote config ignored rooms.json !!!")
  154. return
  155. }
  156. // 保存
  157. // 加载房间表
  158. data, err := os.ReadFile("ludo/rooms.json")
  159. //fmt.Println(string(data))
  160. if err != nil {
  161. log.Release("read rooms failed ludo/rooms.json")
  162. return
  163. }
  164. if marshalData(data) {
  165. // 写入redis
  166. coreservice.SetPlatformConfig(config_key, string(data))
  167. }
  168. }
  169. func marshalData(data []byte) bool {
  170. Rooms.lock.Lock()
  171. defer Rooms.lock.Unlock()
  172. err := json.Unmarshal(data, &Rooms)
  173. if err != nil {
  174. log.Release("Unmarshal rooms failed err:%v", err)
  175. return false
  176. }
  177. if len(Rooms.Rooms) <= 0 {
  178. log.Release("Rooms.Rooms == 0")
  179. return false
  180. }
  181. //写入掷骰子轮次概率数据
  182. for _, v := range Rooms.Rooms {
  183. if len(v.Rounds) <= 0 {
  184. log.Release("v.Dice.Rounds == 0")
  185. return false
  186. }
  187. for i := 0; i < len(v.Rounds); i++ {
  188. v.Rounds[i].makePool()
  189. }
  190. }
  191. if len(Server.ServerIP) > 0 {
  192. ws := "ws"
  193. if len(Server.CertFile) > 0 {
  194. ws = "wss"
  195. }
  196. for i := 0; i < len(Rooms.Rooms); i++ {
  197. Rooms.Rooms[i].ServerIP = fmt.Sprintf("%s://%s:%d", ws, Server.ServerIP, Server.ServerPort)
  198. }
  199. }
  200. return true
  201. }
  202. func OnConfigChanged(key string) {
  203. if key != config_key {
  204. return
  205. }
  206. log.Release("正在刷新配置信息")
  207. loadRedisConfig()
  208. }