tablesink.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package gamelogic
  2. import (
  3. "encoding/json"
  4. "time"
  5. "bet24.com/servers/games/ludo/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()
  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. usr := ts.table.GetUserByChair(chairId)
  99. if usr == nil {
  100. ts.table.LogWithTableId("tablesink.OnUserExitTable user not exist")
  101. return
  102. }
  103. ts.table.LogWithTableId("tablesink.OnUserExitTable chair[%d]: %d:%s", chairId, usr.GetUserId(), usr.GetUserNickName())
  104. //有玩家离开, 更新在线
  105. if !Stopping {
  106. go func() {
  107. frame.UpdateRoomOnline(ts.roomInfo.RoomName, ts.roomInfo.RoomID-1)
  108. }()
  109. }
  110. // 如果是中途离开,则检查是否结束
  111. if ts.gameScene.Phase == Phase_Free || ts.gameScene.Phase == Phase_End {
  112. /*
  113. -- timer
  114. ts.readyTimer[chairId].Stop()
  115. */
  116. ts.table.KillTimer(TIMER_READY_0 + chairId)
  117. /*
  118. if ts.getValidUserCount() == 1 {
  119. time.AfterFunc(3*time.Second, ts.checkIsNeedRobot)
  120. }*/
  121. ts.table.SetTimer(TIMER_ADD_ROBOT, 2000)
  122. go ts.checkAndStartGame()
  123. } else {
  124. // 没有参与游戏?
  125. if !ts.gameScene.Players[chairId].IsValid {
  126. return
  127. }
  128. if ts.gameScene.Players[chairId].Dropped {
  129. return
  130. }
  131. //游戏中离开,逃跑
  132. ts.dealDrop(chairId)
  133. }
  134. }
  135. //用户离线
  136. func (ts *tablesink) OnUserOffline(chairId int) {
  137. usr := ts.table.GetUserByChair(chairId)
  138. if usr == nil {
  139. ts.table.LogWithTableId("tablesink.OnUserOffline user not exist")
  140. return
  141. }
  142. ts.table.LogWithTableId("tablesink.OnUserOffline %d:%s", usr.GetUserId(), usr.GetUserNickName())
  143. }
  144. //用户重进
  145. func (ts *tablesink) OnUserReplay(chairId int) {
  146. usr := ts.table.GetUserByChair(chairId)
  147. if usr == nil {
  148. ts.table.LogWithTableId("tablesink.OnUserOffline user not exist")
  149. return
  150. }
  151. ts.table.LogWithTableId("tablesink.OnUserReplay %d:%s", usr.GetUserId(), usr.GetUserNickName())
  152. //下发房间配置
  153. d, _ := json.Marshal(ts.roomInfo.RoomInfoBase)
  154. ts.table.SendGameData(usr.GetUserIndex(), CMD_ROOMINFO, string(d))
  155. }
  156. //准备
  157. func (ts *tablesink) OnUserReady(userIndex int32, chairId int) {
  158. if chairId == -1 {
  159. return
  160. }
  161. usr := gate.GetUserInfo(userIndex)
  162. if usr == nil {
  163. ts.table.LogWithTableId("tablesink.OnUserReady user not exist")
  164. return
  165. }
  166. userGold := ts.table.GetUserChipOrGoldByUser(usr)
  167. if userGold < ts.roomInfo.MinGold || (ts.roomInfo.MaxGold > 0 && userGold > ts.roomInfo.MaxGold) {
  168. ts.table.LogWithTableId("tablesink.OnUserReady 玩家:%d 金币不足, T出去 ", usr.GetUserId())
  169. ts.table.KickUser(usr.GetUserIndex(), !usr.IsRobot())
  170. // if usr.IsRobot() {
  171. // ts.table.KickUser(usr.GetUserIndex(), false)
  172. // } else {
  173. // ts.table.UserWatch(usr.GetUserIndex())
  174. // }
  175. return
  176. }
  177. ts.table.LogWithTableId("tablesink.OnUserReady chair[%d]: %d:%s", chairId, usr.GetUserId(), usr.GetUserNickName())
  178. /*
  179. -- timer
  180. ts.readyTimer[chairId].Stop()
  181. */
  182. ts.table.KillTimer(TIMER_READY_0 + chairId)
  183. ts.table.SetTimer(TIMER_ADD_ROBOT, 2000)
  184. ts.checkAndStartGame()
  185. }
  186. //取消准备
  187. func (ts *tablesink) OnUserCancelReady(userIndex int32, chairId int) {
  188. usr := gate.GetUserInfo(userIndex)
  189. if usr == nil {
  190. ts.table.LogWithTableId("tablesink.OnUserCancelReady user not exist")
  191. return
  192. }
  193. ts.table.LogWithTableId("tablesink.OnUserCancelReady %d:%s", usr.GetUserId(), usr.GetUserNickName())
  194. }
  195. func (ts *tablesink) OnGetChairScene(chairId int, player bool) string {
  196. // ts.table.LogWithTableId("------------OnGetChairScene %d", chairId)
  197. now := time.Now()
  198. ts.gameScene.LeftSec = 1000 - int(now.Sub(ts.LastOpraTime).Seconds())
  199. str := ts.gameScene.getScene(chairId, player)
  200. return str
  201. }
  202. func (ts *tablesink) OnGetChairCount() int {
  203. return CHAIR_COUNT
  204. }
  205. func (ts *tablesink) OnTimer(timerId int) {
  206. switch timerId {
  207. case TIMER_READY_0:
  208. fallthrough
  209. case TIMER_READY_1:
  210. fallthrough
  211. case TIMER_READY_2:
  212. fallthrough
  213. case TIMER_READY_3:
  214. ts.checkUserReadyStatus(timerId - TIMER_READY_0)
  215. case TIMER_MATCH:
  216. ts.dealMatchTimeOut()
  217. case TIMER_GAME:
  218. //ts.table.LogWithTableId("------gameTimer-------")
  219. ts.dealPlayTimeOut()
  220. case TIMER_ROBOT:
  221. ts.onRobotChatTimer()
  222. case TIMER_ADD_ROBOT:
  223. ts.checkIsNeedRobot()
  224. case TIMER_REMOVE_ROBOT:
  225. ts.removeOneRobot()
  226. }
  227. }
  228. func (ts *tablesink) DumpScene() {
  229. ts.gameScene.dump(ts.table.GetTableID())
  230. }
  231. func (ts *tablesink) GetGoldLimit() (min, max int) {
  232. return ts.roomInfo.MinGold, ts.roomInfo.MaxGold
  233. }
  234. func (ts *tablesink) IsDual() bool {
  235. return ts.roomInfo.IsDual > 0
  236. }
  237. func (ts *tablesink) GetDiceSec() int {
  238. sec := ts.roomInfo.DiceSec
  239. if sec <= 0 {
  240. sec = 3000
  241. }
  242. sec += 1000 //加1秒操作延时
  243. return sec
  244. }
  245. func (ts *tablesink) GetMoveSec() int {
  246. sec := ts.roomInfo.MoveSec
  247. if sec <= 0 {
  248. sec = 10000
  249. }
  250. return sec
  251. }
  252. //获得骰子点数
  253. func (ts *tablesink) getRandomNumber(round int) int {
  254. num := config.Rooms.GetDicePointByRound(ts.roomInfo.RoomID, round)
  255. //ts.table.LogWithTableId("getRandomNumber %d, %d", ts.gameScene.round, num)
  256. return num
  257. }
  258. func (ts *tablesink) IsAllRobot() bool {
  259. return false
  260. }