tablesink.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package gamelogic
  2. import (
  3. "encoding/json"
  4. "os"
  5. "sync"
  6. "time"
  7. "bet24.com/log"
  8. "bet24.com/servers/games/greedy/common"
  9. "bet24.com/servers/games/greedy/config"
  10. "bet24.com/servers/insecureframe/frame"
  11. )
  12. type tablesink struct {
  13. table frame.Table
  14. LastStateTick time.Time
  15. userBetList map[int][]common.Bet
  16. timer *time.Timer
  17. lock *sync.RWMutex
  18. spinResult common.BetOption
  19. closeGame bool
  20. noPlayerCount int // 无人参与的局数数量
  21. userList *userlist
  22. score_users *common.ScoreUsers
  23. robotActions []robot_action
  24. robotStopEvent chan bool
  25. robotLock *sync.RWMutex
  26. scores []common.UserSetScore // 结算阶段,广播所有人的分数,给桌面玩家或者自己头像飘分用
  27. winRank *winPool
  28. hotDogAmount int
  29. kebabAmount int
  30. chickenLegAmount int
  31. meatSliceAmount int
  32. radishAmount int
  33. cornAmount int
  34. spinachAmount int
  35. tomatoAmount int
  36. drawMeatSliceWinAmount int //记录魔灯投注金额 用于避免重复开大奖
  37. areaPopular *common.AreaPopular
  38. privateData string
  39. roomInfo config.RoomInfo
  40. }
  41. func newTableSink(table frame.Table, data string) *tablesink {
  42. ts := new(tablesink)
  43. ts.table = table
  44. ts.privateData = data
  45. ts.lock = &sync.RWMutex{}
  46. err := json.Unmarshal([]byte(data), &ts.roomInfo)
  47. if err != nil {
  48. found := false
  49. for _, v := range config.Rooms.Rooms {
  50. if data == v.RoomName {
  51. ts.roomInfo = v
  52. found = true
  53. break
  54. }
  55. }
  56. if !found {
  57. ts.roomInfo = config.Rooms.Rooms[0]
  58. }
  59. }
  60. if ts.roomInfo.HistoryCount == 0 {
  61. ts.roomInfo.HistoryCount = 10
  62. }
  63. //记录这是今天的第几局
  64. ts.roomInfo.SerialNumber = ts.currentGameNumber()
  65. ts.winRank = newWinPool()
  66. ts.userList = newUserList()
  67. ts.areaPopular = new(common.AreaPopular)
  68. ts.score_users = new(common.ScoreUsers)
  69. ts.robotLock = &sync.RWMutex{}
  70. ts.closeGame = false
  71. ts.noPlayerCount = 0
  72. ts.startPeriod()
  73. if ts.test() {
  74. log.Debug("tablesink.setFrame test end")
  75. }
  76. table.SetTimer(common.TIMERID_CHECKROBOT, 5000)
  77. return ts
  78. }
  79. func (ts *tablesink) test() bool {
  80. if len(os.Args) < 2 {
  81. return false
  82. }
  83. common.ResetWinningOdds(ts.roomInfo.Odds)
  84. common.ResetBetProbability(ts.roomInfo.Probability)
  85. bidTypeMax := int(common.BidTypeMax)
  86. testCount := 1000000
  87. total := float64(-testCount * int(common.BidTypeMax))
  88. returns := make([]int, bidTypeMax)
  89. for i := 0; i < testCount; i++ {
  90. spinResult := common.Spin()
  91. for j := 0; j < bidTypeMax; j++ {
  92. odds := common.GetResultOdds(j, spinResult)
  93. if odds > 0 {
  94. returns[j]++
  95. total += odds
  96. }
  97. }
  98. }
  99. testResult := make([]float64, bidTypeMax)
  100. for j := 0; j < bidTypeMax; j++ {
  101. testResult[j] = float64(returns[j]) / float64(testCount) * common.GetResultOdds(j, -1)
  102. }
  103. log.Debug("testCount [%d] result %v total = %f", testCount, testResult, total)
  104. for j := 0; j < bidTypeMax; j++ {
  105. ts.testOne(j)
  106. }
  107. return true
  108. }
  109. func (ts *tablesink) testOne(betType int) {
  110. testCount := 10000
  111. returns := float64(0)
  112. for i := 0; i < testCount; i++ {
  113. spinResult := common.Spin()
  114. odds := common.GetResultOdds(betType, spinResult)
  115. if odds > 0 {
  116. returns += odds
  117. }
  118. }
  119. log.Debug("[%s] testCount [%d] returns %v rate = %f", common.GetBetDesc(betType), testCount, returns, float64(returns)/float64(testCount))
  120. }
  121. func (ts *tablesink) Destroy() {
  122. ts.table.LogWithTableId("------tablesink:Destroy-------")
  123. //close(ts.stopChan)
  124. }
  125. func (ts *tablesink) OnUserEnterTable(userIndex int32, chairId int) {
  126. u, _ := ts.table.GetUser(userIndex)
  127. if u == nil {
  128. log.Debug("tablesink.OnUserEnterTable %d not exist", userIndex)
  129. return
  130. }
  131. ts.winRank.addUser(u.GetUserId())
  132. if !u.IsRobot() {
  133. log.Debug("gamesink.OnUserEnter %d[%s]", userIndex, u.GetUserNickName())
  134. // 发送配置信息
  135. ts.sendGameOption(userIndex)
  136. // 发送场景
  137. ts.sendGameScene(userIndex)
  138. ts.sendDayWin(userIndex, u.GetUserId())
  139. }
  140. ts.userList.addUser(u.GetUserId(), u.GetUserNickName(), u.GetUserFaceId(), u.GetUserFaceUrl(), u.GetUserVipLevel(), u.GetUserVipExpire(), u.GetDecorations())
  141. }
  142. func (ts *tablesink) OnUserExitTable(userIndex int32, chairId int) {
  143. //判断一下用户是否有下注
  144. usr, _ := ts.table.GetUser(userIndex)
  145. if usr == nil {
  146. log.Debug("tablesink.OnUserExit %d not exist", userIndex)
  147. return
  148. }
  149. userId := usr.GetUserId()
  150. ts.lock.RLock()
  151. _, isBet := ts.userBetList[userId]
  152. ts.lock.RUnlock()
  153. if usr.IsRobot() {
  154. if !ts.winRank.isRank(userId) {
  155. ts.winRank.removeUser(userId)
  156. }
  157. }
  158. if !isBet {
  159. ts.userList.removeUser(userId)
  160. return
  161. }
  162. ts.userList.removeUser(userId)
  163. }
  164. func (ts *tablesink) OnUserOffline(chairId int) {
  165. }
  166. func (ts *tablesink) OnUserReplay(chairId int) {
  167. }
  168. func (ts *tablesink) OnUserReady(userIndex int32, chairId int) {
  169. }
  170. func (ts *tablesink) OnUserCancelReady(userIndex int32, chairId int) {
  171. }
  172. func (ts *tablesink) OnGetChairScene(chairId int, isPlayer bool) string {
  173. return ts.getStateData()
  174. }
  175. func (ts *tablesink) OnGetPrivateRoomScene(chairId int) string {
  176. return ts.getStateData()
  177. }
  178. func (ts *tablesink) OnGetChairCount() int {
  179. return 1
  180. }
  181. func (ts *tablesink) OnTimer(timerId int) {
  182. switch timerId {
  183. case common.TIMERID_CHECKROBOT:
  184. ts.checkRobot()
  185. ts.table.SetTimer(timerId, 5000)
  186. default:
  187. ts.table.LogWithTableId("tablesink.OnTimer unhandled timer[%d]", timerId)
  188. }
  189. }
  190. func (ts *tablesink) DumpScene() {
  191. }
  192. func (ts *tablesink) GetGoldLimit() (min, max int) {
  193. return ts.roomInfo.MinBet, ts.roomInfo.MaxBet
  194. }
  195. func (ts *tablesink) IsDual() bool {
  196. return false
  197. }
  198. func (ts *tablesink) OnBaseScoreChanged(baseScore int) {
  199. }
  200. func (ts *tablesink) SetPrivateRoomParam(param int, value string) {
  201. ts.table.LogWithTableId("tablesink.SetPrivateRoomParam %d:%s", param, value)
  202. }
  203. func (ts *tablesink) OnPrivateRoomStatusChanged(oldStatus, newStatus int) {
  204. ts.table.LogWithTableId("OnPrivateRoomStatusChanged %d->%d", oldStatus, newStatus)
  205. }
  206. func (ts *tablesink) OnPrivateRoomDismissed() {
  207. ts.table.LogWithTableId("OnPrivateRoomDismissed ")
  208. log.Debug("tablesink.OnPrivateRoomDismissed")
  209. ts.closeGame = true
  210. }
  211. func (ts *tablesink) IsAllRobot() bool {
  212. return false
  213. }