GameLogic_cmd.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package slotpanda
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "bet24.com/log"
  6. "bet24.com/servers/games/slotcommon/admanager"
  7. "bet24.com/utils"
  8. )
  9. func (this *GameLogic) writeSlotGameMsg(userId int, gameMsg Slot_Message) {
  10. data, _ := json.Marshal(gameMsg)
  11. this.slotSink.SendGameCmd(userId, GAME_MESSAGE, string(data))
  12. }
  13. func (this *GameLogic) sendSlotBet(userId int, errMsg string, errCode, amount int) {
  14. var msg Slot_Message
  15. msg.MsgID = Msg_Bet
  16. data, _ := json.Marshal(SlotPanda_Bet{Amount: amount, ErrorMsg: errMsg, ErrCode: errCode})
  17. msg.Data = string(data)
  18. this.writeSlotGameMsg(userId, msg)
  19. }
  20. func (this *GameLogic) sendSlotResult(userId int, result SlotPanda_Result) {
  21. var msg Slot_Message
  22. msg.MsgID = Msg_Result
  23. data, _ := json.Marshal(result)
  24. msg.Data = string(data)
  25. this.writeSlotGameMsg(userId, msg)
  26. }
  27. func (this *GameLogic) sendFreeSlotResult(userId int, result FreeSpinResult) {
  28. var msg Slot_Message
  29. msg.MsgID = Msg_FreeResult
  30. data, _ := json.Marshal(result)
  31. msg.Data = string(data)
  32. this.writeSlotGameMsg(userId, msg)
  33. }
  34. func (this *GameLogic) sendSlots(userId int) {
  35. var msg Slot_Message
  36. msg.MsgID = Msg_Slots
  37. data, _ := json.Marshal(this.getSlots())
  38. msg.Data = string(data)
  39. this.writeSlotGameMsg(userId, msg)
  40. }
  41. func (this *GameLogic) sendWinShapes(userId int) {
  42. var msg Slot_Message
  43. msg.MsgID = Msg_WinShapes
  44. data, _ := json.Marshal(this.getWinShapes())
  45. msg.Data = string(data)
  46. this.writeSlotGameMsg(userId, msg)
  47. }
  48. func (this *GameLogic) onSlotMessage(userId int, msg, data string) {
  49. var gameMsg Slot_Message
  50. defer utils.TimeCost(fmt.Sprintf("slotpanda.onSlotMessage %d,%s", userId, data))()
  51. e := json.Unmarshal([]byte(data), &gameMsg)
  52. if e != nil {
  53. log.Release("onSlotPandaMessage Unmarshal data failed %v", data)
  54. return
  55. }
  56. switch gameMsg.MsgID {
  57. case Msg_Enter:
  58. this.slotCommon.OnUserEnter(userId)
  59. this.sendSlots(userId)
  60. this.sendWinShapes(userId)
  61. this.sendBetLevel(userId)
  62. this.sendJackpot(userId)
  63. this.sendFreeAdConfig(userId)
  64. this.lock.Lock()
  65. _, ok := this.userFreeSpins[userId]
  66. if !ok {
  67. this.userFreeSpins[userId] = newFreeSpinInfo(userId, this.freeSlotCount, this.WinShpes, this.Wins, this.slotmgr, this.FreeSlotChanged)
  68. }
  69. this.lock.Unlock()
  70. this.lockRemove.Lock()
  71. delete(this.removingFreespins, userId)
  72. this.lockRemove.Unlock()
  73. case Msg_Exit:
  74. this.userExit(userId)
  75. case Msg_Bet:
  76. this.handleSlotBet(userId, gameMsg.Data)
  77. case Msg_GetConfig:
  78. this.sendSlots(userId)
  79. this.sendWinShapes(userId)
  80. this.sendFreeAdConfig(userId)
  81. this.sendJackpot(userId)
  82. this.sendBetLevel(userId)
  83. case Msg_UseAd:
  84. this.onRecvUseAd(userId)
  85. default:
  86. log.Release("onSlotPandaMessage unhandle msg %v", data)
  87. }
  88. }
  89. func (this *GameLogic) onSlotBroadCast(data string) {
  90. var msg Slot_Message
  91. msg.MsgID = Msg_Broadcast
  92. msg.Data = data
  93. this.writeSlotGameMsg(-1, msg)
  94. }
  95. func (this *GameLogic) sendBetLevel(userId int) {
  96. this.slotCommon.SendBetLevel(userId)
  97. }
  98. func (this *GameLogic) handleSlotBet(userId int, data string) {
  99. //defer utils.TimeCost(fmt.Sprintf("slotpanda.handleSlotBet %d,%s", userId, data))()
  100. var bet SlotPanda_Bet
  101. e := json.Unmarshal([]byte(data), &bet)
  102. if e != nil {
  103. log.Release("handleSlotBet Unmarshal data failed %v", data)
  104. return
  105. }
  106. // 是否有免费次数
  107. free, freeSpinResult, fromAd := this.useFreeSpin(userId)
  108. if free {
  109. this.handleFreeResult(userId, freeSpinResult, fromAd)
  110. return
  111. }
  112. amount := bet.Amount
  113. if amount <= 0 {
  114. this.sendSlotBet(userId, "Invalid Bet!", ErrCode_InvalidBet, amount)
  115. return
  116. }
  117. scoreType := GAMEID*100 + 1
  118. status := 1
  119. isSuceeded := this.slotSink.WriteMoney(userId, GAMEID, -amount, 0, status, scoreType, GAME_NAME)
  120. if !isSuceeded {
  121. log.Release("handleSlotBet WriteMoney failed,UserId[%d],amount[%d]", userId, amount)
  122. // 扣金币失败
  123. this.sendSlotBet(userId, "Not enough money!", ErrCode_NotEnoughMoney, amount)
  124. return
  125. }
  126. // 获取结果并写分
  127. this.handleSlotResult(userId, amount)
  128. }
  129. func (this *GameLogic) handleFreeResult(userId int, result FreeSpinResult, fromAd bool) {
  130. // 写分
  131. amount := result.getWinAmount()
  132. this.slotCommon.WriteResult(userId, result.BetAmount, amount, true, fromAd, result.getDesc(), GAMEID)
  133. // 发送结果给客户端
  134. this.sendFreeSlotResult(userId, result)
  135. }
  136. func (this *GameLogic) handleSlotResult(userId int, amount int) {
  137. //defer utils.TimeCost(fmt.Sprintf("slotpanda.handleSlotResult %d", userId))()
  138. controlType := this.slotCommon.GetControlType(userId)
  139. result, ok := this.getResult(userId, amount, controlType)
  140. if !ok {
  141. this.sendSlotBet(userId, "User Not Exist", ErrCode_NotExist, amount)
  142. return
  143. }
  144. bonusAmount := 0
  145. if result.Bonus != nil {
  146. bonusAmount = result.Bonus.BonusResult + result.Bonus.JackpotResult
  147. }
  148. this.slotCommon.WriteResult(userId, amount, result.WinAmount+bonusAmount, false, false, this.getResultDesc(result), GAMEID)
  149. // 发送结果给客户端
  150. this.sendSlotResult(userId, result)
  151. }
  152. func (this *GameLogic) sendFreeAdConfig(userId int) {
  153. var config SlotPanda_FreeAdConfig
  154. config.AdCount, _ = admanager.GetSlotUserInfo(userId, GAMEID)
  155. config.TotalAdCount, config.FreeSpinCount = admanager.GetSlotConfig(GAMEID)
  156. config.Bet = this.getAdBet(config.TotalAdCount - config.AdCount)
  157. config.AdBets = this.AdBets
  158. var msg Slot_Message
  159. msg.MsgID = Msg_FreeAdConfig
  160. data, _ := json.Marshal(config)
  161. msg.Data = string(data)
  162. this.writeSlotGameMsg(userId, msg)
  163. }
  164. func (this *GameLogic) getAdBet(adCount int) int {
  165. if len(this.AdBets) == 0 {
  166. return 0
  167. }
  168. for i := 0; i < len(this.AdBets); i++ {
  169. if adCount >= this.AdBets[i].AdCount {
  170. return this.AdBets[i].BetAmount
  171. }
  172. }
  173. return this.AdBets[len(this.AdBets)-1].BetAmount
  174. }
  175. func (this *GameLogic) onRecvUseAd(userId int) {
  176. var usedAd SlotPanda_UsedAd
  177. usedAd.FreeSpinCount, usedAd.AdCount = admanager.UseAd(userId, GAMEID)
  178. if usedAd.FreeSpinCount > 0 {
  179. totalAdCount, _ := admanager.GetSlotConfig(GAMEID)
  180. usedAd.Bet = this.getAdBet(totalAdCount - usedAd.AdCount - 1)
  181. usedAd.NextAdBetAmount = this.getAdBet(totalAdCount - usedAd.AdCount)
  182. this.addFreeSpin(userId, usedAd.FreeSpinCount, usedAd.Bet, true)
  183. }
  184. var msg Slot_Message
  185. msg.MsgID = Msg_UseAd
  186. data, _ := json.Marshal(usedAd)
  187. msg.Data = string(data)
  188. this.writeSlotGameMsg(userId, msg)
  189. }
  190. func (this *GameLogic) sendJackpot(userId int) {
  191. if userId <= 10 {
  192. return
  193. }
  194. var msg Slot_Message
  195. msg.MsgID = Msg_Jackpot
  196. data, _ := json.Marshal(SlotPanda_Jackpot{Amount: this.jackpotManager.getAmount()})
  197. msg.Data = string(data)
  198. this.writeSlotGameMsg(userId, msg)
  199. }