GameLogic_cmd.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package wildapeslot
  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) writeGameMsg(userId int, gameMsg DragonSlot_Message) {
  10. data, _ := json.Marshal(gameMsg)
  11. this.slotSink.SendGameCmd(userId, GAME_MESSAGE, string(data))
  12. }
  13. func (this *GameLogic) sendBet(userId int, errMsg string) {
  14. var msg DragonSlot_Message
  15. msg.MsgID = Msg_Bet
  16. data, _ := json.Marshal(DragonSlot_Bet{ErrorMsg: errMsg})
  17. msg.Data = string(data)
  18. this.writeGameMsg(userId, msg)
  19. }
  20. func (this *GameLogic) sendResult(userId int, result DragonSlot_Result) {
  21. var msg DragonSlot_Message
  22. msg.MsgID = Msg_Result
  23. data, _ := json.Marshal(result)
  24. msg.Data = string(data)
  25. this.writeGameMsg(userId, msg)
  26. }
  27. func (this *GameLogic) sendSlots(userId int) {
  28. var msg DragonSlot_Message
  29. msg.MsgID = Msg_Slots
  30. data, _ := json.Marshal(this.slotManager.Slots)
  31. msg.Data = string(data)
  32. this.writeGameMsg(userId, msg)
  33. }
  34. func (this *GameLogic) sendBetLevel(userId int) {
  35. this.slotCommon.SendBetLevel(userId)
  36. }
  37. func (this *GameLogic) sendWinShapes(userId int) {
  38. var msg DragonSlot_Message
  39. msg.MsgID = Msg_WinShapes
  40. data, _ := json.Marshal(this.getWinShapes())
  41. msg.Data = string(data)
  42. this.writeGameMsg(userId, msg)
  43. }
  44. func (this *GameLogic) onMessage(userId int, msg, data string) {
  45. var gameMsg DragonSlot_Message
  46. e := json.Unmarshal([]byte(data), &gameMsg)
  47. if e != nil {
  48. log.Release("onMessage Unmarshal data failed %v", data)
  49. return
  50. }
  51. //defer utils.TimeCost(fmt.Sprintf("wildapeslot.onMessage %d", gameMsg.MsgID))()
  52. switch gameMsg.MsgID {
  53. case Msg_Enter:
  54. this.slotCommon.OnUserEnter(userId)
  55. this.sendSlots(userId)
  56. this.sendBetLevel(userId)
  57. this.sendWinShapes(userId)
  58. this.sendFreeAdConfig(userId)
  59. this.lockRemove.Lock()
  60. delete(this.removingFreespins, userId)
  61. this.lockRemove.Unlock()
  62. case Msg_Exit:
  63. this.userExit(userId)
  64. case Msg_Bet:
  65. this.onRecvBet(userId, gameMsg.Data)
  66. case Msg_GetConfig:
  67. this.sendSlots(userId)
  68. this.sendBetLevel(userId)
  69. this.sendWinShapes(userId)
  70. this.sendFreeAdConfig(userId)
  71. case Msg_UseAd:
  72. this.onRecvUseAd(userId)
  73. default:
  74. log.Release("onMessage unhandle msg %v", data)
  75. }
  76. }
  77. func (this *GameLogic) onTribalSlotBroadCast(data string) {
  78. var msg DragonSlot_Message
  79. msg.MsgID = Msg_Broadcast
  80. msg.Data = data
  81. this.writeGameMsg(-1, msg)
  82. }
  83. func (this *GameLogic) onRecvBet(userId int, data string) {
  84. var bet DragonSlot_Bet
  85. e := json.Unmarshal([]byte(data), &bet)
  86. if e != nil {
  87. log.Release("onRecvBet Unmarshal data failed %v", data)
  88. return
  89. }
  90. // 是否有免费次数
  91. isBonus, lastBet, fromAd, isFree := this.useFreeSpin(userId)
  92. log.Debug("useFreeSpin isFree=%t,isBonus=%t", isFree, isBonus)
  93. if isBonus || isFree {
  94. log.Debug("GameLogic.onRecvBet using free spin")
  95. bet.Amount = lastBet
  96. }
  97. amount := bet.Amount
  98. _, base := this.betLevelManager.GetLevelAndBase(amount)
  99. if base <= 0 {
  100. this.sendBet(userId, "Invalid Bet!")
  101. return
  102. }
  103. log.Debug("onRecvBet isbonus=%t isfree=%t", isBonus, isFree)
  104. if isBonus || isFree {
  105. // 获取结果并写分
  106. this.handleResult(userId, amount, isFree, fromAd, isBonus)
  107. } else {
  108. scoreType := GAMEID*100 + 1
  109. status := 1
  110. isSuceeded := this.slotSink.WriteMoney(userId, GAMEID, -amount, 0, status, scoreType, GAME_NAME)
  111. if !isSuceeded {
  112. log.Release("onRecvBet WriteMoney failed,UserId[%d],amount[%d]", userId, amount)
  113. // 扣金币失败
  114. this.sendBet(userId, "Not enough money!")
  115. return
  116. }
  117. // 获取结果并写分
  118. this.handleResult(userId, amount, false, false, false)
  119. }
  120. }
  121. func (this *GameLogic) handleResult(userId int, amount int, isFree bool, fromAd bool, isBonus bool) {
  122. defer utils.TimeCost(fmt.Sprintf("wildapeslot.handleResult %d", userId))()
  123. result := this.getResult(userId, amount, isFree, isBonus)
  124. this.slotCommon.WriteResult(userId, amount, result.WinAmount, isFree,
  125. fromAd, this.getResultDesc(result), GAMEID)
  126. // 发送结果给客户端
  127. this.sendResult(userId, result)
  128. userInfo := this.slotSink.GetUserInfo(userId)
  129. if userInfo == nil {
  130. log.Debug("handleResult userInfo == nil")
  131. return
  132. }
  133. }
  134. func (this *GameLogic) sendFreeAdConfig(userId int) {
  135. var config DragonSlot_FreeAdConfig
  136. config.AdCount, _ = admanager.GetSlotUserInfo(userId, GAMEID)
  137. config.TotalAdCount, config.FreeSpinCount = admanager.GetSlotConfig(GAMEID)
  138. config.Bet = this.betLevelManager.GetAmountByLevel(this.getAdBet(config.TotalAdCount - config.AdCount))
  139. config.AdBets = this.AdBets
  140. var msg DragonSlot_Message
  141. msg.MsgID = Msg_FreeAdConfig
  142. data, _ := json.Marshal(config)
  143. msg.Data = string(data)
  144. this.writeGameMsg(userId, msg)
  145. }
  146. func (this *GameLogic) getAdBet(adCount int) int {
  147. if len(this.AdBets) == 0 {
  148. return 0
  149. }
  150. for i := 0; i < len(this.AdBets); i++ {
  151. if adCount >= this.AdBets[i].AdCount {
  152. return this.AdBets[i].BetLevel
  153. }
  154. }
  155. return this.AdBets[len(this.AdBets)-1].BetLevel
  156. }
  157. func (this *GameLogic) onRecvUseAd(userId int) {
  158. var usedAd DragonSlot_UsedAd
  159. usedAd.FreeSpinCount, usedAd.AdCount = admanager.UseAd(userId, GAMEID)
  160. if usedAd.FreeSpinCount > 0 {
  161. totalAdCount, _ := admanager.GetSlotConfig(GAMEID)
  162. usedAd.Bet = this.betLevelManager.GetAmountByLevel(this.getAdBet(totalAdCount - usedAd.AdCount - 1))
  163. usedAd.NextAdBetAmount = this.betLevelManager.GetAmountByLevel(this.getAdBet(totalAdCount - usedAd.AdCount))
  164. this.addFreeSpin(userId, usedAd.FreeSpinCount, usedAd.Bet, true, false)
  165. }
  166. var msg DragonSlot_Message
  167. msg.MsgID = Msg_UseAd
  168. data, _ := json.Marshal(usedAd)
  169. msg.Data = string(data)
  170. this.writeGameMsg(userId, msg)
  171. }