tablesink_cmd.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package gamelogic
  2. import (
  3. "encoding/json"
  4. "bet24.com/log"
  5. "bet24.com/servers/games/greedy/common"
  6. "bet24.com/servers/insecureframe/message"
  7. "bet24.com/servers/transaction"
  8. "bet24.com/servers/user"
  9. )
  10. func (ts *tablesink) OnGameMessage(userIndex int32, msg, data string) bool {
  11. /*
  12. if !ok {
  13. log.Debug("tablesink.OnGameMessage user not exist %d", userIndex)
  14. return false
  15. }
  16. */
  17. log.Debug("tablesink.OnGameMessage Receive Msg %d,%v,%v", userIndex, msg, data)
  18. usr, _ := ts.table.GetUser(userIndex)
  19. if usr == nil {
  20. log.Debug("tablesink.OnGameMessage user not exist")
  21. return false
  22. }
  23. userId := usr.GetUserId()
  24. switch msg {
  25. case History:
  26. ts.sendHistory(userIndex)
  27. case Option:
  28. ts.sendGameOption(userIndex)
  29. case Bet:
  30. ts.handleBet(userIndex, userId, data)
  31. case BatchBet:
  32. ts.handleBatchBet(userIndex, userId, msg, data)
  33. case BetClear:
  34. ts.onBetClear(userIndex, userId, msg, data)
  35. case BetRecord:
  36. ts.onGetBetRecord(userIndex, userId, msg, data)
  37. case Popularity:
  38. ts.onGetPopularity(userIndex, userId, msg, data)
  39. case WinRank:
  40. ts.onGetWinRank(userIndex, userId, msg, data)
  41. default:
  42. log.Debug("tablesink.OnGameMessage unhandled message %v data %v", msg, data)
  43. }
  44. return true
  45. }
  46. func (ts *tablesink) sendBetFailed(userIndex int32, index int, errMsg string) {
  47. var bet_failed struct {
  48. ErrorMsg string
  49. Index int
  50. }
  51. bet_failed.Index = index
  52. bet_failed.ErrorMsg = errMsg
  53. d, _ := json.Marshal(bet_failed)
  54. ts.table.SendGameData(userIndex, Betfailed, string(d))
  55. }
  56. func (ts *tablesink) sendBetOK(userIndex int32, data string) {
  57. ts.table.SendGameData(userIndex, Bet, data)
  58. }
  59. func (ts *tablesink) sendHistory(userIndex int32) {
  60. ts.table.SendGameData(userIndex, History, ts.getHistory())
  61. }
  62. func (ts *tablesink) sendGameScene(userIndex int32) {
  63. ts.table.SendGameData(userIndex, message.Table_GameScene, ts.getStateData())
  64. }
  65. func (ts *tablesink) sendDayWin(userIndex int32, userId int) {
  66. _, _, winAmount := ts.winRank.getRankList(userId, -1)
  67. d, _ := json.Marshal(winAmount)
  68. ts.table.SendGameData(userIndex, DayWin, string(d))
  69. }
  70. // 处理筹码下注
  71. func (ts *tablesink) handleBet(userIndex int32, userId int, data string) {
  72. usr, _ := ts.table.GetUser(userIndex)
  73. if usr == nil {
  74. log.Debug("tablesink.handleBet user not exist")
  75. return
  76. }
  77. // log.Debug("handleBet %v", data)
  78. var bet common.Bet
  79. e := json.Unmarshal([]byte(data), &bet)
  80. if e != nil {
  81. log.Release("handleBet Unmarshal data failed %v |e=%v:", data, e)
  82. return
  83. }
  84. amount := bet.Amount
  85. bet.UserId = userId
  86. if amount <= 0 {
  87. ts.sendBetFailed(userIndex, bet.Index, "Invalid Bet!")
  88. return
  89. }
  90. // 看下能否下注
  91. ok, errMsg := ts.isCanBet(bet)
  92. if !ok {
  93. ts.sendBetFailed(userIndex, bet.Index, errMsg)
  94. return
  95. }
  96. ret, _ := ts.table.WriteUserMoney(userId, -amount, 0, 1, common.GAMEID*100+1, ts.roomInfo.RoomName)
  97. if !ret {
  98. // 扣金币失败
  99. ts.sendBetFailed(userIndex, bet.Index, "Not enough cash!")
  100. return
  101. }
  102. ok, err := ts.addBet(bet)
  103. if !ok {
  104. ts.table.WriteUserMoney(userId, amount, 0, 3, common.GAMEID*100+3, ts.roomInfo.RoomName)
  105. ts.sendBetFailed(userIndex, bet.Index, err)
  106. } else {
  107. ts.handleBetSuccess(userId, bet, usr)
  108. d, _ := json.Marshal(bet)
  109. ts.sendBetOK(-1, string(d))
  110. }
  111. }
  112. // 处理批量下注
  113. func (ts *tablesink) handleBatchBet(userIndex int32, userId int, msg, data string) {
  114. usr, _ := ts.table.GetUser(userIndex)
  115. if usr == nil {
  116. log.Debug("tablesink.handleBatchBet user not exist")
  117. return
  118. }
  119. var batchBet common.Batch_Bet
  120. e := json.Unmarshal([]byte(data), &batchBet)
  121. if e != nil {
  122. log.Release("handleBatchBet Unmarshal data failed %v", data)
  123. return
  124. }
  125. // 先判断所有区域是否能下
  126. totalAmount := 0
  127. for _, v := range batchBet.Bets {
  128. var bet common.Bet
  129. bet.BetBase = v
  130. bet.UserId = userId
  131. ok, errMsg := ts.isCanBet(bet)
  132. if !ok {
  133. log.Debug("handleBatchBet failed to add bet %v,%v", bet, errMsg)
  134. ts.sendBetFailed(userIndex, bet.Index, errMsg)
  135. return
  136. }
  137. totalAmount += bet.Amount
  138. }
  139. ret, _ := ts.table.WriteUserMoney(userId, -totalAmount, 0, 1, common.GAMEID*100+1, ts.roomInfo.RoomName)
  140. if !ret {
  141. // 扣金币失败
  142. ts.sendBetFailed(userIndex, 0, "Not enough cash!")
  143. return
  144. }
  145. for _, v := range batchBet.Bets {
  146. var bet common.Bet
  147. bet.BetBase = v
  148. bet.UserId = userId
  149. ok, _ := ts.addBet(bet)
  150. if !ok {
  151. go ts.table.WriteUserMoney(userId, bet.Amount, 0, 3, common.GAMEID*100+3, ts.roomInfo.RoomName)
  152. } else {
  153. ts.handleBetSuccess(userId, bet, usr)
  154. }
  155. }
  156. ts.table.SendGameData(-1, msg, data)
  157. }
  158. // 成功下注后处理handleBetSuccess
  159. func (ts *tablesink) handleBetSuccess(userId int, bet common.Bet, usr *user.UserInfo) {
  160. ts.lock.Lock()
  161. if !usr.IsRobot() {
  162. amount := bet.Amount
  163. switch bet.BetId {
  164. case int(common.HotDog):
  165. ts.hotDogAmount += amount
  166. case int(common.Kebab):
  167. ts.kebabAmount += amount
  168. case int(common.ChickenLeg):
  169. ts.chickenLegAmount += amount
  170. case int(common.MeatSlice):
  171. ts.meatSliceAmount += amount
  172. ts.drawMeatSliceWinAmount += amount
  173. case int(common.Radish):
  174. ts.radishAmount += amount
  175. case int(common.Corn):
  176. ts.cornAmount += amount
  177. case int(common.Spinach):
  178. ts.spinachAmount += amount
  179. case int(common.Tomato):
  180. ts.tomatoAmount += amount
  181. }
  182. }
  183. betCount := ts.areaPopular.BetCount[bet.BetId]
  184. //必须达到热区最低要求才更新
  185. if betCount <= 20 {
  186. ts.lock.Unlock()
  187. return
  188. }
  189. //是否推送
  190. isPush := false
  191. // 根据下注人次更新钻石热度
  192. if ts.areaPopular.MostPopular != -1 {
  193. // 更新最受欢迎的区域
  194. maxCount := ts.areaPopular.BetCount[ts.areaPopular.MostPopular]
  195. if betCount > maxCount {
  196. ts.areaPopular.MostPopular = bet.BetId
  197. isPush = true
  198. }
  199. } else {
  200. ts.areaPopular.MostPopular = bet.BetId
  201. isPush = true
  202. }
  203. // 当前钻石数量
  204. diamondHotOld := ts.areaPopular.DiamondHot[bet.BetId]
  205. diamondHotNew := diamondHotOld
  206. if betCount <= 50 {
  207. diamondHotNew = 1
  208. } else if betCount > 50 && betCount <= 100 {
  209. diamondHotNew = 2
  210. } else if betCount > 100 {
  211. diamondHotNew = 3
  212. }
  213. // 更新钻石数量
  214. ts.areaPopular.DiamondHot[bet.BetId] = diamondHotNew
  215. // 如果钻石数量发生了改变,触发推送
  216. if diamondHotNew != diamondHotOld {
  217. isPush = true
  218. }
  219. if isPush {
  220. d, _ := json.Marshal(ts.areaPopular)
  221. ts.table.SendGameData(-1, Popularity, string(d))
  222. }
  223. ts.lock.Unlock()
  224. }
  225. func (ts *tablesink) onBetClear(userIndex int32, userId int, msg, data string) {
  226. ts.lock.Lock()
  227. betList, ok := ts.userBetList[userId]
  228. if !ok {
  229. ts.lock.Unlock()
  230. return
  231. }
  232. amount := 0
  233. for _, v := range betList {
  234. ts.roomInfo.TotalBet -= v.Amount
  235. amount += v.Amount
  236. }
  237. delete(ts.userBetList, userId)
  238. ts.lock.Unlock()
  239. ts.table.WriteUserMoney(userId, amount, 0, 3, common.GAMEID*100+3, ts.roomInfo.RoomName)
  240. betClear := common.UserClear{UserId: userId}
  241. d, _ := json.Marshal(betClear)
  242. ts.table.SendGameData(userIndex, msg, string(d))
  243. ts.userList.clearBet(userId, amount)
  244. }
  245. func (ts *tablesink) onGetBetRecord(userIndex int32, userId int, msg, data string) {
  246. //log.Debug("tablesink.onGetBetRecord %v", data)
  247. var getRecord struct {
  248. RecordCount int
  249. }
  250. e := json.Unmarshal([]byte(data), &getRecord)
  251. if e != nil {
  252. log.Release("tablesink.onGetBetRecord Unmarshal data failed %v", data)
  253. return
  254. }
  255. records := transaction.GetGameRecord(userId, common.GAMEID, ts.roomInfo.RoomName, getRecord.RecordCount)
  256. retData, _ := json.Marshal(records)
  257. ts.table.SendGameData(userIndex, msg, string(retData))
  258. }
  259. func (ts *tablesink) onGetPopularity(userIndex int32, userId int, msg, data string) {
  260. d, _ := json.Marshal(ts.areaPopular)
  261. ts.table.SendGameData(userIndex, msg, string(d))
  262. }
  263. func (ts *tablesink) sendGameOption(userIndex int32) {
  264. d, _ := json.Marshal(ts.roomInfo.RoomInfoBase)
  265. ts.table.SendGameData(userIndex, Option, string(d))
  266. }
  267. func (ts *tablesink) onGetWinRank(userIndex int32, userId int, msg, data string) {
  268. //获取winRank前10名
  269. rankList := ts.GetWinRank(0)
  270. log.Debug("tablesink.onGetWinRank %v,userId:%d,rankList:%v", data, userId, rankList)
  271. //不为空
  272. if len(rankList) > 0 {
  273. retData, _ := json.Marshal(rankList)
  274. ts.table.SendGameData(userIndex, msg, string(retData))
  275. } else {
  276. ts.table.SendGameData(userIndex, msg, "[]")
  277. }
  278. }