GameLogic_cmd.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package rezekislot
  2. import (
  3. "encoding/json"
  4. "bet24.com/log"
  5. "bet24.com/servers/games/slotcommon/admanager"
  6. )
  7. func (this *GameLogic) writeGameMsg(userId int, gameMsg RezekiSlot_Message) {
  8. data, _ := json.Marshal(gameMsg)
  9. this.slotSink.SendGameCmd(userId, GAME_MESSAGE, string(data))
  10. log.Debug("rezekislot.writeGameMsg %s", string(data))
  11. }
  12. func (this *GameLogic) sendBet(userId int, errMsg string) {
  13. var msg RezekiSlot_Message
  14. msg.MsgID = Msg_Bet
  15. data, _ := json.Marshal(RezekiSlot_Bet{ErrorMsg: errMsg})
  16. msg.Data = string(data)
  17. this.writeGameMsg(userId, msg)
  18. }
  19. func (this *GameLogic) sendResult(userId int, result RezekiSlot_Result) {
  20. var msg RezekiSlot_Message
  21. msg.MsgID = Msg_Result
  22. data, _ := json.Marshal(result)
  23. msg.Data = string(data)
  24. this.writeGameMsg(userId, msg)
  25. }
  26. func (this *GameLogic) sendJackpot(userId int) {
  27. if userId <= 0 {
  28. return
  29. }
  30. var msg RezekiSlot_Message
  31. msg.MsgID = Msg_Jackpot
  32. data, _ := json.Marshal(RezekiSlot_Jackpot{Amount: this.jackpotManager.getAmount()})
  33. msg.Data = string(data)
  34. this.writeGameMsg(userId, msg)
  35. }
  36. func (this *GameLogic) sendSlots(userId int) {
  37. var msg RezekiSlot_Message
  38. msg.MsgID = Msg_Slots
  39. data, _ := json.Marshal(this.slotManager.Slots)
  40. msg.Data = string(data)
  41. this.writeGameMsg(userId, msg)
  42. }
  43. func (this *GameLogic) sendBetLevel(userId int) {
  44. this.slotCommon.SendBetLevel(userId)
  45. }
  46. func (this *GameLogic) sendFanConfig(userId int) {
  47. var msg RezekiSlot_Message
  48. msg.MsgID = Msg_FanConfig
  49. data, _ := json.Marshal(this.fanManager.fanConfig)
  50. msg.Data = string(data)
  51. this.writeGameMsg(userId, msg)
  52. }
  53. func (this *GameLogic) onMessage(userId int, msg, data string) {
  54. var gameMsg RezekiSlot_Message
  55. e := json.Unmarshal([]byte(data), &gameMsg)
  56. if e != nil {
  57. log.Release("onMessage Unmarshal data failed %v", data)
  58. return
  59. }
  60. //defer utils.TimeCost(fmt.Sprintf("RezekiSlot.onMessage %d", gameMsg.MsgID))()
  61. switch gameMsg.MsgID {
  62. case Msg_Enter:
  63. this.slotCommon.OnUserEnter(userId)
  64. this.sendSlots(userId)
  65. this.sendJackpot(userId)
  66. this.sendFanConfig(userId)
  67. this.sendBetLevel(userId)
  68. this.sendFreeAdConfig(userId)
  69. this.sendFreeStatus(userId)
  70. case Msg_Exit:
  71. this.userExit(userId)
  72. case Msg_Bet:
  73. this.onRecvBet(userId, gameMsg.Data)
  74. case Msg_Jackpot:
  75. this.sendJackpot(userId)
  76. case Msg_GetConfig:
  77. this.sendSlots(userId)
  78. this.sendJackpot(userId)
  79. this.sendFanConfig(userId)
  80. this.sendBetLevel(userId)
  81. this.sendFreeAdConfig(userId)
  82. this.sendFreeStatus(userId)
  83. case Msg_UseAd:
  84. this.onRecvUseAd(userId)
  85. default:
  86. log.Release("onMessage unhandle msg %v", data)
  87. }
  88. }
  89. func (this *GameLogic) onTribalSlotBroadCast(data string) {
  90. var msg RezekiSlot_Message
  91. msg.MsgID = Msg_Broadcast
  92. msg.Data = data
  93. this.writeGameMsg(-1, msg)
  94. }
  95. func (this *GameLogic) onTribalSlotJackpot(data string) {
  96. var msg RezekiSlot_Message
  97. msg.MsgID = Msg_Jackpot
  98. msg.Data = data
  99. this.writeGameMsg(-1, msg)
  100. }
  101. func (this *GameLogic) onRecvBet(userId int, data string) {
  102. var bet RezekiSlot_Bet
  103. e := json.Unmarshal([]byte(data), &bet)
  104. if e != nil {
  105. log.Release("onRecvBet Unmarshal data failed %v", data)
  106. return
  107. }
  108. // 是否有免费次数
  109. isFree, lastBet, fromAd := this.useFreeSpin(userId)
  110. if isFree {
  111. log.Debug("GameLogic.onRecvBet using free spin")
  112. bet.Amount = lastBet
  113. }
  114. amount := bet.Amount
  115. _, base := this.betLevelManager.GetLevelAndBase(amount)
  116. if base <= 0 {
  117. this.sendBet(userId, "Invalid Bet!")
  118. return
  119. }
  120. if isFree {
  121. // 获取结果并写分
  122. this.handleResult(userId, amount, isFree, fromAd)
  123. } else {
  124. scoreType := GAMEID*100 + 1
  125. status := 1
  126. isSuceeded := this.slotSink.WriteMoney(userId, GAMEID, -amount, 0, status, scoreType, GAME_NAME)
  127. if !isSuceeded {
  128. log.Release("onRecvBet WriteMoney failed,UserId[%d],amount[%d]", userId, amount)
  129. // 扣金币失败
  130. this.sendBet(userId, "Not enough money!")
  131. return
  132. }
  133. // 获取结果并写分
  134. this.handleResult(userId, amount, isFree, fromAd)
  135. }
  136. }
  137. func (this *GameLogic) handleResult(userId int, amount int, isFree bool, fromAd bool) {
  138. //defer utils.TimeCost(fmt.Sprintf("rezeki.handleResult %d", userId))()
  139. result := this.getResult(userId, amount, isFree)
  140. this.slotCommon.WriteResult(userId, amount, result.WinAmount+result.JackpotAmount, isFree, fromAd, this.getResultDesc(result), GAMEID)
  141. // 发送结果给客户端
  142. this.sendResult(userId, result)
  143. // 如果有jackpot
  144. // 发送事件
  145. userInfo := this.slotSink.GetUserInfo(userId)
  146. if userInfo == nil {
  147. log.Debug("handleResult userInfo == nil")
  148. return
  149. }
  150. jackpotMoney := result.JackpotAmount
  151. if jackpotMoney > 0 {
  152. var broadCast JackPot_Broadcast
  153. broadCast.UserID = userId
  154. broadCast.JackpotLevel = result.JackpotLevel
  155. broadCast.Amount = jackpotMoney
  156. broadCast.FaceID = userInfo.GetUserFaceId()
  157. broadCast.FaceUrl = userInfo.GetUserFaceUrl()
  158. broadCast.NickName = userInfo.GetUserNickName()
  159. broadCast.Sex = userInfo.GetUserSex()
  160. broadCast.Vip = userInfo.GetUserVipLevel()
  161. data, _ := json.Marshal(broadCast)
  162. //event.DispatchEvent(EVENT_BROADCAST, string(data))
  163. this.onTribalSlotBroadCast(string(data))
  164. }
  165. }
  166. //检查免费状态
  167. func (this *GameLogic) sendFreeStatus(userId int) {
  168. // 是否有免费次数
  169. var config RezekiSlot_FreeStatus
  170. isFree, lastBet, _ := this.useFreeSpin(userId)
  171. config.IsFree = isFree
  172. if isFree {
  173. config.LastBet = lastBet
  174. config.FreeCount = this.getFreeSpinTime(userId)
  175. }
  176. var msg RezekiSlot_Message
  177. msg.MsgID = Msg_FreeStatus
  178. data, _ := json.Marshal(config)
  179. msg.Data = string(data)
  180. this.writeGameMsg(userId, msg)
  181. }
  182. func (this *GameLogic) sendFreeAdConfig(userId int) {
  183. var config RezekiSlot_FreeAdConfig
  184. config.AdCount, _ = admanager.GetSlotUserInfo(userId, GAMEID)
  185. config.TotalAdCount, config.FreeSpinCount = admanager.GetSlotConfig(GAMEID)
  186. config.Bet = this.getAdBet(config.TotalAdCount - config.AdCount)
  187. config.AdBets = this.AdBets
  188. var msg RezekiSlot_Message
  189. msg.MsgID = Msg_FreeAdConfig
  190. data, _ := json.Marshal(config)
  191. msg.Data = string(data)
  192. this.writeGameMsg(userId, msg)
  193. }
  194. func (this *GameLogic) getAdBet(adCount int) int {
  195. if len(this.AdBets) == 0 {
  196. return 0
  197. }
  198. for i := 0; i < len(this.AdBets); i++ {
  199. if adCount >= this.AdBets[i].AdCount {
  200. return this.AdBets[i].BetAmount
  201. }
  202. }
  203. return this.AdBets[len(this.AdBets)-1].BetAmount
  204. }
  205. func (this *GameLogic) onRecvUseAd(userId int) {
  206. var usedAd RezekiSlot_UsedAd
  207. usedAd.FreeSpinCount, usedAd.AdCount = admanager.UseAd(userId, GAMEID)
  208. if usedAd.FreeSpinCount > 0 {
  209. totalAdCount, _ := admanager.GetSlotConfig(GAMEID)
  210. usedAd.Bet = this.getAdBet(totalAdCount - usedAd.AdCount - 1)
  211. usedAd.NextAdBetAmount = this.getAdBet(totalAdCount - usedAd.AdCount)
  212. this.addFreeSpin(userId, usedAd.FreeSpinCount, usedAd.Bet, true)
  213. }
  214. var msg RezekiSlot_Message
  215. msg.MsgID = Msg_UseAd
  216. data, _ := json.Marshal(usedAd)
  217. msg.Data = string(data)
  218. this.writeGameMsg(userId, msg)
  219. }