tablesink.go 8.0 KB

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