roomconfig.go 4.8 KB

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