competitivewaterpoolmgr.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package competitivewaterpool
  2. import (
  3. "encoding/json"
  4. "math/rand"
  5. "os"
  6. "time"
  7. "bet24.com/log"
  8. platformconfig "bet24.com/servers/micros/platformconfig/proto"
  9. pb "bet24.com/servers/micros/waterpool/proto"
  10. )
  11. const config_key = "inventory_config"
  12. var mgr *competitiveWaterPoolMgr
  13. type competitiveWaterPoolMgr struct {
  14. inventoryList []pb.InventoryInfo
  15. config string
  16. }
  17. type Room struct {
  18. RoomName string
  19. RoomType int
  20. }
  21. type InventoryConf struct {
  22. GameID int
  23. RoomList []Room
  24. }
  25. func newCompetitiveWaterPoolMgr() *competitiveWaterPoolMgr {
  26. ret := new(competitiveWaterPoolMgr)
  27. var data = trans_getInventoryList(0)
  28. if data.Count > 0 {
  29. ret.inventoryList = data.List
  30. }
  31. ret.loadConfig()
  32. return ret
  33. }
  34. func (m *competitiveWaterPoolMgr) loadConfig() {
  35. m.loadRedisConfig(false)
  36. go time.AfterFunc(5*time.Minute, m.loadConfig)
  37. }
  38. func (m *competitiveWaterPoolMgr) loadRedisConfig(isRefresh bool) {
  39. configString := platformconfig.GetConfig(config_key)
  40. if configString == "" {
  41. data, err := os.ReadFile("serviceconf/inventory_extra.json")
  42. if err != nil {
  43. log.Release("Inventory.readConf read inventory_extra failed")
  44. return
  45. }
  46. configString = string(data)
  47. if configString != "" {
  48. platformconfig.SetConfig(config_key, configString)
  49. }
  50. }
  51. if m.config == configString && !isRefresh {
  52. return
  53. }
  54. m.config = configString
  55. var conf []InventoryConf
  56. err := json.Unmarshal([]byte(configString), &conf)
  57. if err != nil {
  58. log.Release("Inventory.readConf Unmarshal failed err:%v", err)
  59. return
  60. }
  61. for i := 0; i < len(conf); i++ {
  62. if len(conf[i].RoomList) == 0 {
  63. continue
  64. }
  65. for j := 0; j < len(conf[i].RoomList); j++ {
  66. bExist := false
  67. for n := 0; n < len(m.inventoryList); n++ {
  68. if m.inventoryList[n].GameID == conf[i].GameID && m.inventoryList[n].RoomName == conf[i].RoomList[j].RoomName {
  69. if m.inventoryList[n].RoomType != conf[i].RoomList[j].RoomType {
  70. m.inventoryList[n].RoomType = conf[i].RoomList[j].RoomType
  71. }
  72. bExist = true
  73. break
  74. }
  75. }
  76. if !bExist {
  77. m.inventoryList = append(m.inventoryList, pb.InventoryInfo{
  78. GameID: conf[i].GameID,
  79. RoomName: conf[i].RoomList[j].RoomName,
  80. RoomType: conf[i].RoomList[j].RoomType,
  81. InventoryValue: 0,
  82. ControlRate: 0,
  83. MinInventoryValue: 0,
  84. MaxInventoryValue: 0,
  85. MaxControlRate: 0,
  86. })
  87. }
  88. }
  89. }
  90. for i := 0; i < len(m.inventoryList); i++ {
  91. trans_updateInventoryList(m.inventoryList[i])
  92. }
  93. }
  94. func (m *competitiveWaterPoolMgr) getInventoryType(gameId int, roomName string) int {
  95. var controlRate float64 = 0
  96. inventoryValue := 0
  97. for i := 0; i < len(m.inventoryList); i++ {
  98. if m.inventoryList[i].RoomName == roomName && m.inventoryList[i].GameID == gameId {
  99. controlRate = m.inventoryList[i].ControlRate
  100. inventoryValue = m.inventoryList[i].InventoryValue
  101. break
  102. }
  103. }
  104. if controlRate <= 0 {
  105. return pb.PoolControl_Normal
  106. }
  107. r := rand.Intn(10000)
  108. prob := int(controlRate * 100)
  109. if r >= prob {
  110. return pb.PoolControl_Normal
  111. }
  112. if inventoryValue > 0 {
  113. return pb.PoolControl_Lose
  114. }
  115. return pb.PoolControl_Win
  116. }
  117. func (m *competitiveWaterPoolMgr) getControlRate(min, max, value, maxRate int) float64 {
  118. if max <= min {
  119. return 0
  120. }
  121. inventoryValue := value
  122. if inventoryValue < 0 {
  123. inventoryValue = -inventoryValue
  124. }
  125. if inventoryValue < min {
  126. return 0
  127. }
  128. rate := float64(maxRate) * float64(inventoryValue-min) / float64(max-min)
  129. if rate > float64(maxRate) {
  130. rate = float64(maxRate)
  131. }
  132. return rate
  133. }
  134. func (m *competitiveWaterPoolMgr) reduceInventoryValue(gameId int, roomName string, reduceValue, roomType int) {
  135. if reduceValue == 0 {
  136. return
  137. }
  138. go trans_inventoryChangeRecord(gameId, roomName, -reduceValue, roomType)
  139. for i := 0; i < len(m.inventoryList); i++ {
  140. if m.inventoryList[i].RoomName == roomName && m.inventoryList[i].GameID == gameId && m.inventoryList[i].RoomType == roomType {
  141. m.inventoryList[i].InventoryValue -= reduceValue
  142. m.inventoryList[i].ControlRate = m.getControlRate(m.inventoryList[i].MinInventoryValue,
  143. m.inventoryList[i].MaxInventoryValue, m.inventoryList[i].InventoryValue, m.inventoryList[i].MaxControlRate)
  144. go trans_updateInventoryList(m.inventoryList[i])
  145. return
  146. }
  147. }
  148. var info pb.InventoryInfo
  149. info.RoomName = roomName
  150. info.RoomType = roomType
  151. info.InventoryValue = -reduceValue
  152. info.ControlRate = 0
  153. info.MinInventoryValue = 0
  154. info.MaxControlRate = 0
  155. info.MaxInventoryValue = 0
  156. m.inventoryList = append(m.inventoryList, info)
  157. go trans_updateInventoryList(info)
  158. }
  159. func (m *competitiveWaterPoolMgr) getInventoryList(gameId int) pb.InventoryList {
  160. return trans_getInventoryList(gameId)
  161. }
  162. func (m *competitiveWaterPoolMgr) updateInventoryList(gameId int, roomName string, roomType, inventoryValue, minValue,
  163. maxValue, maxRate int) pb.InventoryList {
  164. bFound := false
  165. for i := 0; i < len(m.inventoryList); i++ {
  166. if m.inventoryList[i].RoomName == roomName && m.inventoryList[i].GameID == gameId && roomType == m.inventoryList[i].RoomType {
  167. bFound = true
  168. m.inventoryList[i].InventoryValue = inventoryValue
  169. m.inventoryList[i].MinInventoryValue = minValue
  170. m.inventoryList[i].MaxInventoryValue = maxValue
  171. m.inventoryList[i].MaxControlRate = maxRate
  172. rate := m.getControlRate(minValue, maxValue, inventoryValue, maxRate)
  173. m.inventoryList[i].ControlRate = rate
  174. go trans_updateInventoryList(m.inventoryList[i])
  175. break
  176. }
  177. }
  178. if !bFound {
  179. var info pb.InventoryInfo
  180. info.GameID = gameId
  181. info.RoomName = roomName
  182. info.RoomType = roomType
  183. info.InventoryValue = inventoryValue
  184. info.MinInventoryValue = minValue
  185. info.MaxControlRate = maxRate
  186. info.MaxInventoryValue = maxValue
  187. rate := m.getControlRate(minValue, maxValue, inventoryValue, maxRate)
  188. info.ControlRate = rate
  189. m.inventoryList = append(m.inventoryList, info)
  190. go trans_updateInventoryList(info)
  191. }
  192. var list pb.InventoryList
  193. list.Count = len(m.inventoryList)
  194. list.List = append(list.List, m.inventoryList...)
  195. return list
  196. }
  197. func (m *competitiveWaterPoolMgr) getInventoryValue(gameId, roomType int, roomName string) int {
  198. for i := 0; i < len(m.inventoryList); i++ {
  199. if m.inventoryList[i].GameID == gameId && roomType == m.inventoryList[i].RoomType &&
  200. roomName == m.inventoryList[i].RoomName {
  201. return m.inventoryList[i].InventoryValue
  202. }
  203. }
  204. return 0
  205. }
  206. func (m *competitiveWaterPoolMgr) dump() {
  207. for i := 0; i < len(m.inventoryList); i++ {
  208. log.Release("GameID:%d, RoomName:%s, RoomType:%d, InventoryValue:%d, ControlRate:%g, MinInventoryValue:%d,MaxInventoryValue:%d,MaxControlRate:%d",
  209. m.inventoryList[i].GameID, m.inventoryList[i].RoomName, m.inventoryList[i].RoomType, m.inventoryList[i].InventoryValue,
  210. m.inventoryList[i].ControlRate, m.inventoryList[i].MinInventoryValue, m.inventoryList[i].MaxInventoryValue,
  211. m.inventoryList[i].MaxControlRate)
  212. }
  213. }