tablesink.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package gamelogic
  2. import (
  3. "encoding/json"
  4. "time"
  5. "bet24.com/servers/games/blitzludo/config"
  6. "bet24.com/servers/insecureframe/frame"
  7. "bet24.com/servers/insecureframe/gate"
  8. )
  9. type tablesink struct {
  10. table frame.Table
  11. gameScene *GameScene
  12. roomInfo config.RoomInfo
  13. LastOpraTime time.Time //记录操作时间点
  14. privateData string
  15. }
  16. func newTableSink(table frame.Table, data string) *tablesink {
  17. ts := new(tablesink)
  18. ts.privateData = data
  19. ts.table = table
  20. ts.gameScene = newGameScene(table.GetTableID())
  21. err := json.Unmarshal([]byte(data), &ts.roomInfo)
  22. if err != nil {
  23. found := false
  24. for _, v := range config.Rooms.Rooms {
  25. if data == v.RoomName {
  26. ts.roomInfo = v
  27. found = true
  28. break
  29. }
  30. }
  31. if !found {
  32. ts.roomInfo = config.Rooms.Rooms[0]
  33. }
  34. }
  35. return ts
  36. }
  37. func (ts *tablesink) Destroy() {
  38. ts.table.LogWithTableId("------tablesink:Destroy-------")
  39. }
  40. func (ts *tablesink) OnGetMinGold() int {
  41. return ts.roomInfo.MinGold
  42. }
  43. func (ts *tablesink) OnGetMaxGold() int {
  44. return ts.roomInfo.MaxGold
  45. }
  46. func (ts *tablesink) OnGameMessage(userIndex int32, msg, data string) bool {
  47. switch msg {
  48. case CMD_ACTION:
  49. ts.recvAction(userIndex, data)
  50. case CMD_TABLECHAT:
  51. ts.recvChatMsg(userIndex, data)
  52. case CMD_CANCLE_AUTO:
  53. ts.recvCancleAutoMsg(userIndex, data)
  54. default:
  55. ts.table.LogWithTableId("tablesink.OnGameMessage %s\n%s", msg, data)
  56. return false
  57. }
  58. return true
  59. }
  60. func (ts *tablesink) OnUserEnterTable(userIndex int32, chairId int) {
  61. usr := gate.GetUserInfo(userIndex)
  62. if usr == nil {
  63. ts.table.LogWithTableId("tablesink.OnUserEnterTable user not exist")
  64. return
  65. }
  66. ts.table.LogWithTableId("tablesink.OnUserEnterTable chair[%d]: %d:%s ", chairId, usr.GetUserId(), usr.GetUserNickName())
  67. if ts.table.GetUserChipOrGoldByUser(usr) < ts.roomInfo.MinGold {
  68. ts.table.LogWithTableId("----玩家:%d 金币不足, T出去 金币数量 %d ", usr.GetUserId(), ts.table.GetUserChipOrGoldByUser(usr))
  69. //ts.table.UserWatch(userIndex)
  70. ts.table.KickUser(userIndex, true)
  71. return
  72. }
  73. //下发房间配置
  74. d, _ := json.Marshal(ts.roomInfo.RoomInfoBase)
  75. ts.table.SendGameData(userIndex, CMD_ROOMINFO, string(d))
  76. // 如果是中途进入,则不起定时器
  77. if !ts.table.IsPlaying() {
  78. /*
  79. -- timer
  80. ts.readyTimer[chairId].Stop()
  81. ts.readyTimer[chairId].Reset(time.Duration(SEC_READY) * time.Millisecond)
  82. */
  83. ts.table.SetTimer(TIMER_READY_0+chairId, SEC_READY)
  84. }
  85. //有玩家进桌, 更新在线
  86. if !Stopping {
  87. go func() {
  88. frame.UpdateRoomOnline(ts.roomInfo.RoomName, ts.roomInfo.RoomID-1)
  89. }()
  90. }
  91. // 机器人,随机300秒准备
  92. if usr.IsRobot() {
  93. // sec := rand.Intn(2*1000) + 1000
  94. ts.table.SetTimer(TIMER_READY_0+chairId, 300)
  95. }
  96. }
  97. func (ts *tablesink) OnUserExitTable(userIndex int32, chairId int) {
  98. //用户离开要判断是否为游戏玩家,不是游戏玩家不做业务逻辑处理
  99. usr := ts.table.GetPlayer(userIndex)
  100. if usr == nil {
  101. ts.table.LogWithTableId("tablesink.OnUserExitTable,usr[%d] not exist", userIndex)
  102. return
  103. }
  104. ts.table.LogWithTableId("tablesink.OnUserExitTable chair[%d]: %d:%s", chairId, usr.GetUserId(), usr.GetUserNickName())
  105. //有玩家离开, 更新在线
  106. if !Stopping {
  107. go func() {
  108. frame.UpdateRoomOnline(ts.roomInfo.RoomName, ts.roomInfo.RoomID-1)
  109. }()
  110. }
  111. // 如果是中途离开,则检查是否结束
  112. if ts.gameScene.Phase == Phase_Free || ts.gameScene.Phase == Phase_End {
  113. /*
  114. -- timer
  115. ts.readyTimer[chairId].Stop()
  116. */
  117. ts.table.KillTimer(TIMER_READY_0 + chairId)
  118. /*
  119. if ts.getValidUserCount() == 1 {
  120. time.AfterFunc(3*time.Second, ts.checkIsNeedRobot)
  121. }*/
  122. ts.table.SetTimer(TIMER_ADD_ROBOT, 2000)
  123. go ts.checkAndStartGame()
  124. } else {
  125. // 没有参与游戏?
  126. if !ts.gameScene.Players[chairId].IsValid {
  127. return
  128. }
  129. if ts.gameScene.Players[chairId].Dropped {
  130. return
  131. }
  132. //游戏中离开,逃跑
  133. ts.dealDrop(chairId)
  134. }
  135. }
  136. // 用户离线
  137. func (ts *tablesink) OnUserOffline(chairId int) {
  138. usr := ts.table.GetUserByChair(chairId)
  139. if usr == nil {
  140. ts.table.LogWithTableId("tablesink.OnUserOffline user not exist")
  141. return
  142. }
  143. ts.table.LogWithTableId("tablesink.OnUserOffline %d:%s", usr.GetUserId(), usr.GetUserNickName())
  144. }
  145. // 用户重进
  146. func (ts *tablesink) OnUserReplay(chairId int) {
  147. usr := ts.table.GetUserByChair(chairId)
  148. if usr == nil {
  149. ts.table.LogWithTableId("tablesink.OnUserOffline user not exist")
  150. return
  151. }
  152. ts.table.LogWithTableId("tablesink.OnUserReplay %d:%s", usr.GetUserId(), usr.GetUserNickName())
  153. //下发房间配置
  154. d, _ := json.Marshal(ts.roomInfo.RoomInfoBase)
  155. ts.table.SendGameData(usr.GetUserIndex(), CMD_ROOMINFO, string(d))
  156. }
  157. // 准备
  158. func (ts *tablesink) OnUserReady(userIndex int32, chairId int) {
  159. if chairId == -1 {
  160. return
  161. }
  162. usr := gate.GetUserInfo(userIndex)
  163. if usr == nil {
  164. ts.table.LogWithTableId("tablesink.OnUserReady user not exist")
  165. return
  166. }
  167. userGold := ts.table.GetUserChipOrGoldByUser(usr)
  168. if userGold < ts.roomInfo.MinGold || (ts.roomInfo.MaxGold > 0 && userGold > ts.roomInfo.MaxGold) {
  169. ts.table.LogWithTableId("tablesink.OnUserReady 玩家:%d 金币不足, T出去 ", usr.GetUserId())
  170. ts.table.KickUser(usr.GetUserIndex(), !usr.IsRobot())
  171. // if usr.IsRobot() {
  172. // ts.table.KickUser(usr.GetUserIndex(), false)
  173. // } else {
  174. // ts.table.UserWatch(usr.GetUserIndex())
  175. // }
  176. return
  177. }
  178. ts.table.LogWithTableId("tablesink.OnUserReady chair[%d]: %d:%s", chairId, usr.GetUserId(), usr.GetUserNickName())
  179. /*
  180. -- timer
  181. ts.readyTimer[chairId].Stop()
  182. */
  183. ts.table.KillTimer(TIMER_READY_0 + chairId)
  184. ts.table.SetTimer(TIMER_ADD_ROBOT, 2000)
  185. ts.checkAndStartGame()
  186. }
  187. // 取消准备
  188. func (ts *tablesink) OnUserCancelReady(userIndex int32, chairId int) {
  189. usr := gate.GetUserInfo(userIndex)
  190. if usr == nil {
  191. ts.table.LogWithTableId("tablesink.OnUserCancelReady user not exist")
  192. return
  193. }
  194. ts.table.LogWithTableId("tablesink.OnUserCancelReady %d:%s", usr.GetUserId(), usr.GetUserNickName())
  195. }
  196. func (ts *tablesink) OnGetChairScene(chairId int, player bool) string {
  197. // ts.table.LogWithTableId("------------OnGetChairScene %d", chairId)
  198. now := time.Now()
  199. ts.gameScene.LeftSec = 1000 - int(now.Sub(ts.LastOpraTime).Seconds())
  200. str := ts.gameScene.getScene(chairId, player)
  201. return str
  202. }
  203. func (ts *tablesink) OnGetChairCount() int {
  204. return CHAIR_COUNT
  205. }
  206. func (ts *tablesink) OnTimer(timerId int) {
  207. switch timerId {
  208. case TIMER_READY_0:
  209. fallthrough
  210. case TIMER_READY_1:
  211. fallthrough
  212. case TIMER_READY_2:
  213. fallthrough
  214. case TIMER_READY_3:
  215. ts.checkUserReadyStatus(timerId - TIMER_READY_0)
  216. case TIMER_MATCH:
  217. ts.dealMatchTimeOut()
  218. case TIMER_GAME:
  219. //ts.table.LogWithTableId("------gameTimer-------")
  220. ts.dealPlayTimeOut()
  221. case TIMER_ROBOT:
  222. ts.onRobotChatTimer()
  223. case TIMER_ADD_ROBOT:
  224. ts.checkIsNeedRobot()
  225. case TIMER_REMOVE_ROBOT:
  226. ts.removeOneRobot()
  227. case TIMER_QUIT_0:
  228. fallthrough
  229. case TIMER_QUIT_1:
  230. fallthrough
  231. case TIMER_QUIT_2:
  232. fallthrough
  233. case TIMER_QUIT_3:
  234. ts.checkUserQuitStatus(timerId - TIMER_QUIT_0)
  235. }
  236. }
  237. func (ts *tablesink) DumpScene() {
  238. ts.gameScene.dump(true)
  239. }
  240. func (ts *tablesink) GetGoldLimit() (min, max int) {
  241. return ts.roomInfo.MinGold, ts.roomInfo.MaxGold
  242. }
  243. func (ts *tablesink) IsDual() bool {
  244. return ts.roomInfo.IsDual > 0
  245. }
  246. func (ts *tablesink) GetDiceSec() int {
  247. sec := ts.roomInfo.DiceSec
  248. if sec <= 0 {
  249. sec = 3000
  250. }
  251. sec += 1000 //加1秒操作延时
  252. return sec
  253. }
  254. func (ts *tablesink) GetMoveSec() int {
  255. sec := ts.roomInfo.MoveSec
  256. if sec <= 0 {
  257. sec = 10000
  258. }
  259. return sec
  260. }
  261. // 获得骰子点数
  262. func (ts *tablesink) getRandomNumber(round int) int {
  263. num := config.Rooms.GetDicePointByRound(ts.roomInfo.RoomID, round)
  264. return num
  265. }
  266. func (ts *tablesink) IsAllRobot() bool {
  267. return false
  268. }