GameLogic_cmd.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package TribalSlot
  2. import (
  3. "encoding/json"
  4. "bet24.com/log"
  5. "bet24.com/servers/games/slotcommon/admanager"
  6. )
  7. func (this *GameLogic) writeTribalSlotGameMsg(userId int, gameMsg TribalSlot_Message) {
  8. data, _ := json.Marshal(gameMsg)
  9. this.slotSink.SendGameCmd(userId, GAME_MESSAGE, string(data))
  10. }
  11. func (this *GameLogic) sendTribalSlotBet(userId int, errMsg string) {
  12. var msg TribalSlot_Message
  13. msg.MsgID = Msg_Bet
  14. data, _ := json.Marshal(TribalSlot_Bet{ErrorMsg: errMsg})
  15. msg.Data = string(data)
  16. this.writeTribalSlotGameMsg(userId, msg)
  17. }
  18. func (this *GameLogic) sendTribalSlotResult(userId int, result TribalSlot_Result) {
  19. var msg TribalSlot_Message
  20. msg.MsgID = Msg_Result
  21. data, _ := json.Marshal(result)
  22. msg.Data = string(data)
  23. this.writeTribalSlotGameMsg(userId, msg)
  24. }
  25. func (this *GameLogic) sendTribalSlotJackpot(userId int) {
  26. var msg TribalSlot_Message
  27. msg.MsgID = Msg_Jackpot
  28. data, _ := json.Marshal(TribalSlot_Jackpot{Amount: this.getJackPotAmount()})
  29. msg.Data = string(data)
  30. this.writeTribalSlotGameMsg(userId, msg)
  31. }
  32. func (this *GameLogic) sendTribalSlotSlots(userId int) {
  33. var msg TribalSlot_Message
  34. msg.MsgID = Msg_Slots
  35. data, _ := json.Marshal(this.getSlots())
  36. msg.Data = string(data)
  37. this.writeTribalSlotGameMsg(userId, msg)
  38. }
  39. func (this *GameLogic) sendTribalSlotWinShapes(userId int) {
  40. var msg TribalSlot_Message
  41. msg.MsgID = Msg_WinShapes
  42. data, _ := json.Marshal(this.getWinShapes())
  43. msg.Data = string(data)
  44. this.writeTribalSlotGameMsg(userId, msg)
  45. }
  46. func (this *GameLogic) onTribalSlotMessage(userId int, msg, data string) {
  47. var gameMsg TribalSlot_Message
  48. e := json.Unmarshal([]byte(data), &gameMsg)
  49. if e != nil {
  50. log.Release("onTribalSlotMessage Unmarshal data failed %v", data)
  51. return
  52. }
  53. switch gameMsg.MsgID {
  54. case Msg_Enter:
  55. this.slotCommon.OnUserEnter(userId)
  56. this.sendTribalSlotSlots(userId)
  57. this.sendTribalSlotWinShapes(userId)
  58. this.sendTribalSlotJackpot(userId)
  59. this.sendFreeAdConfig(userId)
  60. case Msg_Exit:
  61. this.userExit(userId)
  62. case Msg_Bet:
  63. this.handleTribalSlotBet(userId, gameMsg.Data)
  64. case Msg_Jackpot:
  65. this.sendTribalSlotJackpot(userId)
  66. case Msg_GetConfig:
  67. this.sendTribalSlotSlots(userId)
  68. this.sendTribalSlotWinShapes(userId)
  69. this.sendTribalSlotJackpot(userId)
  70. this.sendFreeAdConfig(userId)
  71. case Msg_UseAd:
  72. this.onRecvUseAd(userId)
  73. default:
  74. log.Release("onTribalSlotMessage unhandle msg %v", data)
  75. }
  76. }
  77. func (this *GameLogic) onTribalSlotBroadCast(data string) {
  78. var msg TribalSlot_Message
  79. msg.MsgID = Msg_Broadcast
  80. msg.Data = data
  81. this.writeTribalSlotGameMsg(-1, msg)
  82. }
  83. func (this *GameLogic) onTribalSlotJackpot(data string) {
  84. var msg TribalSlot_Message
  85. msg.MsgID = Msg_Jackpot
  86. msg.Data = data
  87. this.writeTribalSlotGameMsg(-1, msg)
  88. }
  89. func (this *GameLogic) handleTribalSlotBet(userId int, data string) {
  90. var bet TribalSlot_Bet
  91. e := json.Unmarshal([]byte(data), &bet)
  92. if e != nil {
  93. log.Release("handleTribalSlotBet Unmarshal data failed %v", data)
  94. return
  95. }
  96. // 是否有免费次数
  97. free, lastBet, fromAd := this.useFreeSpin(userId)
  98. if free {
  99. bet.Amount = lastBet
  100. }
  101. amount := bet.Amount
  102. if amount < this.MinBet || amount > this.MaxBet {
  103. this.sendTribalSlotBet(userId, "Invalid Bet!")
  104. return
  105. }
  106. if free {
  107. // 获取结果并写分
  108. this.handleTribalSlotResult(userId, amount, true, fromAd)
  109. } else {
  110. scoreType := GAMEID*100 + 1
  111. status := 1
  112. isSuceeded := this.slotSink.WriteMoney(userId, GAMEID, -amount, 0, status, scoreType, GAME_NAME)
  113. if !isSuceeded {
  114. log.Release("handleTribalSlotBet WriteMoney failed,UserId[%d],amount[%d]", userId, amount)
  115. // 扣金币失败
  116. this.sendTribalSlotBet(userId, "Not enough money!")
  117. return
  118. }
  119. // 获取结果并写分
  120. this.handleTribalSlotResult(userId, amount, false, false)
  121. }
  122. }
  123. func (this *GameLogic) handleTribalSlotResult(userId int, amount int, isFree bool, fromAd bool) {
  124. controlType := 0
  125. if !isFree {
  126. controlType = this.slotCommon.GetControlType(userId)
  127. }
  128. result := this.getResult(userId, amount, isFree, controlType)
  129. // 如果有特殊玩法
  130. specialMoney := 0
  131. specialMultiple := result.Special.WinRate1 + result.Special.WinRate2
  132. if specialMultiple > 0 {
  133. specialMoney = specialMultiple * amount
  134. }
  135. // 是否中奖池了
  136. jackpotMoney := result.Jackpot.Amount
  137. if jackpotMoney > 0 {
  138. go this.modifyJackpot(-jackpotMoney, userId)
  139. }
  140. // 写入jackpot
  141. if result.WinAmount+specialMoney+jackpotMoney <= 0 {
  142. var jackPotAmount = this.JackPotInfo.getInJackPotAmount(amount)
  143. if jackPotAmount > 0 {
  144. go this.modifyJackpot(jackPotAmount, userId)
  145. }
  146. }
  147. // writemoney
  148. //this.writeResult(userId, result.WinAmount+specialMoney+jackpotMoney, amount)
  149. this.slotCommon.WriteResult(userId, amount, result.WinAmount, isFree, fromAd, this.getResultDesc(result), GAMEID)
  150. // 发送结果给客户端
  151. this.sendTribalSlotResult(userId, result)
  152. userInfo := this.slotSink.GetUserInfo(userId)
  153. if userInfo == nil {
  154. log.Debug("handleTribalSlotResult userInfo == nil")
  155. return
  156. }
  157. // 如果有jackpot
  158. // 发送事件
  159. if jackpotMoney > 0 {
  160. var broadCast JackPot_Broadcast
  161. broadCast.UserID = userId
  162. broadCast.Amount = jackpotMoney
  163. broadCast.FaceID = userInfo.GetUserFaceId()
  164. broadCast.FaceUrl = userInfo.GetUserFaceUrl()
  165. broadCast.NickName = userInfo.GetUserNickName()
  166. broadCast.Sex = userInfo.GetUserSex()
  167. broadCast.Vip = userInfo.GetUserVipLevel()
  168. data, _ := json.Marshal(broadCast)
  169. //event.DispatchEvent(EVENT_BROADCAST, string(data))
  170. this.onTribalSlotBroadCast(string(data))
  171. }
  172. }
  173. func (this *GameLogic) sendFreeAdConfig(userId int) {
  174. var config FafafaSlot_FreeAdConfig
  175. config.AdCount, _ = admanager.GetSlotUserInfo(userId, GAMEID)
  176. config.TotalAdCount, config.FreeSpinCount = admanager.GetSlotConfig(GAMEID)
  177. config.Bet = this.getAdBet(config.TotalAdCount - config.AdCount)
  178. config.AdBets = this.AdBets
  179. var msg TribalSlot_Message
  180. msg.MsgID = Msg_FreeAdConfig
  181. data, _ := json.Marshal(config)
  182. msg.Data = string(data)
  183. this.writeTribalSlotGameMsg(userId, msg)
  184. }
  185. func (this *GameLogic) getAdBet(adCount int) int {
  186. if len(this.AdBets) == 0 {
  187. return 0
  188. }
  189. for i := 0; i < len(this.AdBets); i++ {
  190. if adCount >= this.AdBets[i].AdCount {
  191. return this.AdBets[i].BetAmount
  192. }
  193. }
  194. return this.AdBets[len(this.AdBets)-1].BetAmount
  195. }
  196. func (this *GameLogic) onRecvUseAd(userId int) {
  197. var usedAd FafafaSlot_UsedAd
  198. usedAd.FreeSpinCount, usedAd.AdCount = admanager.UseAd(userId, GAMEID)
  199. if usedAd.FreeSpinCount > 0 {
  200. totalAdCount, _ := admanager.GetSlotConfig(GAMEID)
  201. usedAd.Bet = this.getAdBet(totalAdCount - usedAd.AdCount - 1)
  202. usedAd.NextAdBetAmount = this.getAdBet(totalAdCount - usedAd.AdCount)
  203. this.addFreeSpin(userId, usedAd.FreeSpinCount, usedAd.Bet, true)
  204. }
  205. var msg TribalSlot_Message
  206. msg.MsgID = Msg_UseAd
  207. data, _ := json.Marshal(usedAd)
  208. msg.Data = string(data)
  209. this.writeTribalSlotGameMsg(userId, msg)
  210. }