gamelogic.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package minislot
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/rand"
  6. "os"
  7. "time"
  8. "bet24.com/log"
  9. coreservice "bet24.com/servers/coreservice/client"
  10. "bet24.com/servers/games/slotcommon"
  11. task "bet24.com/servers/micros/task/proto"
  12. "bet24.com/servers/transaction"
  13. )
  14. type Prize struct {
  15. PrizeId int
  16. Desc string
  17. Multiple float64
  18. }
  19. type PrizeEx struct {
  20. Prize
  21. Chance int // 范围值
  22. }
  23. type GameLogic struct {
  24. BetOptions []int
  25. Prizes []PrizeEx
  26. pools []int
  27. slotSink slotcommon.SlotSink
  28. configString string
  29. lastConfigString string
  30. }
  31. const config_key = "minislot_config"
  32. const refresh_config_sec = 600
  33. func newGameLogic(slotSink slotcommon.SlotSink) *GameLogic {
  34. ret := new(GameLogic)
  35. ret.slotSink = slotSink
  36. log.Color(LogColor, "minislot GameLogic.run")
  37. rand.Seed(time.Now().UnixNano())
  38. ret.refreshData()
  39. time.AfterFunc(time.Second, ret.test)
  40. return ret
  41. }
  42. func (this *GameLogic) refreshData() {
  43. go this.initData()
  44. time.AfterFunc(time.Duration(refresh_config_sec)*time.Second, this.refreshData)
  45. }
  46. func (this *GameLogic) initData() {
  47. configString := coreservice.GetPlatformConfig(config_key)
  48. if configString == "" {
  49. data, err := os.ReadFile("fishconf/minislot.json")
  50. if err != nil {
  51. log.Release("read minislot.json failed")
  52. return
  53. }
  54. configString = string(data)
  55. coreservice.SetPlatformConfig(config_key, configString)
  56. }
  57. if this.lastConfigString == configString {
  58. return
  59. }
  60. this.lastConfigString = configString
  61. err := json.Unmarshal([]byte(configString), &this)
  62. if err != nil {
  63. log.Release("Unmarshal minislot.json failed err:%v", err)
  64. return
  65. }
  66. var config struct {
  67. BetOptions []int
  68. Prizes []Prize
  69. }
  70. config.BetOptions = this.BetOptions
  71. for i := 0; i < len(this.Prizes); i++ {
  72. config.Prizes = append(config.Prizes, this.Prizes[i].Prize)
  73. }
  74. d, _ := json.Marshal(config)
  75. this.configString = string(d)
  76. // 生成pool
  77. this.makePrizePool()
  78. }
  79. func (this *GameLogic) makePrizePool() {
  80. this.pools = []int{}
  81. for _, v := range this.Prizes {
  82. for i := 0; i < v.Chance; i++ {
  83. this.pools = append(this.pools, v.PrizeId)
  84. }
  85. }
  86. }
  87. func (this *GameLogic) onMessage(userId int, msg, data string) {
  88. switch msg {
  89. case "minislot_config":
  90. this.sendConfig(userId)
  91. case "minislot_bet":
  92. this.doBet(userId, msg, data)
  93. }
  94. }
  95. func (this *GameLogic) sendConfig(userId int) {
  96. this.slotSink.SendGameCmd(userId, "minislot_config", this.configString)
  97. }
  98. func (this *GameLogic) isValidBet(betAmount int) bool {
  99. if betAmount <= 0 {
  100. return false
  101. }
  102. if len(this.BetOptions) == 0 {
  103. return true
  104. }
  105. for _, v := range this.BetOptions {
  106. if v == betAmount {
  107. return true
  108. }
  109. }
  110. return false
  111. }
  112. func (this *GameLogic) doBet(userId int, msg, data string) {
  113. var ret CmdBetReturn
  114. var bet CmdBet
  115. err := json.Unmarshal([]byte(data), &bet)
  116. if err != nil {
  117. log.Release("Unmarshal minislot.json failed err:%v", err)
  118. ret.ErrMsg = "Unmarshal failed"
  119. this.sendBetReturn(userId, ret, msg)
  120. return
  121. }
  122. if !this.isValidBet(bet.Amount) {
  123. log.Release("Invalid bet[%d] of %v", bet.Amount, this.BetOptions)
  124. ret.ErrMsg = "Invalid bet"
  125. this.sendBetReturn(userId, ret, msg)
  126. return
  127. }
  128. ret.Amount = bet.Amount
  129. scoreType := GAMEID*100 + 1
  130. status := 1
  131. isSuceeded := this.slotSink.WriteMoney(userId, GAMEID, -bet.Amount, 0, status, scoreType, GAME_NAME)
  132. if !isSuceeded {
  133. log.Release("minislot.dobet WriteMoney failed,UserId[%d],amount[%d]", userId, bet.Amount)
  134. // 扣金币失败
  135. ret.ErrMsg = "not enough money"
  136. this.sendBetReturn(userId, ret, msg)
  137. return
  138. }
  139. result := this.getResult()
  140. ret.WinAmount = int(float64(bet.Amount) * result.Multiple)
  141. ret.PrizeId = result.PrizeId
  142. this.slotSink.WriteMoney(userId, GAMEID, ret.WinAmount, 0, 2, 2+GAMEID*100, GAME_NAME)
  143. transaction.WriteBetRecordAction(userId, GAMEID, "", bet.Amount,
  144. ret.WinAmount, 0, result.Multiple,
  145. fmt.Sprintf("bet:%d", bet.Amount), fmt.Sprintf("PrizeId:%d,WinAmount:%d", ret.PrizeId, ret.WinAmount), 0, "", GAME_NAME)
  146. task.DoTaskAction(userId, task.TaskAction_playgame, 1, task.TaskScope{GameName: GAME_NAME})
  147. task.DoTaskAction(userId, task.TaskAction_fire, bet.Amount, task.TaskScope{GameName: GAME_NAME})
  148. ret.ErrMsg = "OK"
  149. this.sendBetReturn(userId, ret, msg)
  150. }
  151. func (this *GameLogic) sendBetReturn(userId int, ret CmdBetReturn, msg string) {
  152. d, _ := json.Marshal(ret)
  153. this.slotSink.SendGameCmd(userId, msg, string(d))
  154. }
  155. func (this *GameLogic) getResult() Prize {
  156. prizeId := this.pools[rand.Intn(len(this.pools))]
  157. return this.getResultByPrizeId(prizeId)
  158. }
  159. func (this *GameLogic) getResultByPrizeId(prizeId int) Prize {
  160. for _, v := range this.Prizes {
  161. if v.PrizeId == prizeId {
  162. return v.Prize
  163. }
  164. }
  165. log.Release("GameLogic.getResultByPrizeId failed %d,%v", prizeId, this.Prizes)
  166. return Prize{}
  167. }
  168. func (this *GameLogic) test() {
  169. if len(os.Args) < 2 {
  170. return
  171. }
  172. if os.Args[1] != "minislot" {
  173. return
  174. }
  175. testCount := 10000000
  176. winAmount := 0
  177. betAmount := this.BetOptions[0]
  178. for i := 0; i < testCount; i++ {
  179. ret := this.getResult()
  180. winAmount += int(float64(betAmount) * ret.Multiple)
  181. }
  182. log.Release("minislot.test end winAmount = %d,betAmount = %d", winAmount, testCount*betAmount)
  183. }