threadsafe_table_unsafe.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package frame
  2. import (
  3. _ "bet24.com/log"
  4. "bet24.com/servers/user"
  5. "encoding/json"
  6. )
  7. type threadsafe_message struct {
  8. Msg string
  9. Data string
  10. Callback chan byte
  11. }
  12. type msg_AddUser struct {
  13. UserIndex int32
  14. ChairId int
  15. Replay bool
  16. ToWatch bool
  17. }
  18. type msg_RemoveUser struct {
  19. UserIndex int32
  20. ToWatch bool
  21. ChangeTable bool
  22. }
  23. type msg_UserIndex struct {
  24. UserIndex int32
  25. }
  26. type msg_TableMessage struct {
  27. UserIndex int32
  28. Msg string
  29. Data string
  30. }
  31. type msg_SetReadyStatus struct {
  32. UserIndex int32
  33. IsReady bool
  34. }
  35. type msg_UserReplay struct {
  36. OldUserIndex int32
  37. NewUserIndex int32
  38. }
  39. type msg_SetBaseScore struct {
  40. BaseScore int
  41. }
  42. type msg_statusChanged struct {
  43. OldStatus int
  44. NewStatus int
  45. }
  46. func (t *ThreadsafeTable) AddTableUser(userIndex int32, chairId int, replay bool, toWatch bool) bool {
  47. data := msg_AddUser{UserIndex: userIndex, ChairId: chairId, Replay: replay, ToWatch: toWatch}
  48. d, _ := json.Marshal(data)
  49. msg := &threadsafe_message{Msg: "AddUser", Data: string(d)}
  50. //t.messageChan <- msg
  51. go t.sendMessageToMainThread(msg)
  52. return true
  53. }
  54. func (t *ThreadsafeTable) AddTableUser_sync(userIndex int32, chairId int, replay bool, toWatch bool) bool {
  55. data := msg_AddUser{UserIndex: userIndex, ChairId: chairId, Replay: replay, ToWatch: toWatch}
  56. d, _ := json.Marshal(data)
  57. callBack := make(chan byte)
  58. msg := &threadsafe_message{Msg: "AddUser", Data: string(d), Callback: callBack}
  59. //t.messageChan <- msg
  60. go t.sendMessageToMainThread(msg)
  61. <-callBack
  62. return true
  63. }
  64. func (t *ThreadsafeTable) RemoveUser(userIndex int32, toWatch, changeTable bool) {
  65. data := msg_RemoveUser{UserIndex: userIndex, ToWatch: toWatch, ChangeTable: changeTable}
  66. d, _ := json.Marshal(data)
  67. callBack := make(chan byte)
  68. msg := &threadsafe_message{Msg: "RemoveUser", Data: string(d), Callback: callBack}
  69. //t.messageChan <- msg
  70. go t.sendMessageToMainThread(msg)
  71. <-callBack
  72. //log.Debug("ThreadsafeTable.RemoveUser msg sent")
  73. }
  74. func (t *ThreadsafeTable) dismiss() {
  75. msg := &threadsafe_message{Msg: "dismiss", Data: ""}
  76. t.messageChan <- msg
  77. }
  78. func (t *ThreadsafeTable) userReplay(userIndex int32, usr *user.UserInfo) {
  79. data := msg_UserReplay{OldUserIndex: userIndex, NewUserIndex: usr.GetUserIndex()}
  80. d, _ := json.Marshal(data)
  81. callBack := make(chan byte)
  82. msg := &threadsafe_message{Msg: "userReplay", Data: string(d), Callback: callBack}
  83. //t.messageChan <- msg
  84. go t.sendMessageToMainThread(msg)
  85. <-callBack
  86. }
  87. func (t *ThreadsafeTable) dumpScene() {
  88. msg := &threadsafe_message{Msg: "dumpScene", Data: ""}
  89. t.messageChan <- msg
  90. }
  91. func (t *ThreadsafeTable) dump() {
  92. msg := &threadsafe_message{Msg: "dump", Data: ""}
  93. //t.messageChan <- msg
  94. go t.sendMessageToMainThread(msg)
  95. }
  96. func (t *ThreadsafeTable) dumpTimers() {
  97. msg := &threadsafe_message{Msg: "dumpTimers", Data: ""}
  98. //t.messageChan <- msg
  99. go t.sendMessageToMainThread(msg)
  100. }
  101. func (t *ThreadsafeTable) onTableMessage(usr *user.UserInfo, msg, data string) bool {
  102. msgData := msg_TableMessage{UserIndex: usr.GetUserIndex(), Msg: msg, Data: data}
  103. d, _ := json.Marshal(msgData)
  104. msgInfo := &threadsafe_message{Msg: "TableMessage", Data: string(d)}
  105. //t.messageChan <- msgInfo
  106. go t.sendMessageToMainThread(msgInfo)
  107. return true
  108. }
  109. func (t *ThreadsafeTable) userWatch(userIndex int32) {
  110. data := msg_UserIndex{UserIndex: userIndex}
  111. d, _ := json.Marshal(data)
  112. msg := &threadsafe_message{Msg: "UserWatch", Data: string(d)}
  113. //t.messageChan <- msg
  114. go t.sendMessageToMainThread(msg)
  115. }
  116. func (t *ThreadsafeTable) setUserReadyStatus(userIndex int32, isReady bool) bool {
  117. data := msg_SetReadyStatus{UserIndex: userIndex, IsReady: isReady}
  118. d, _ := json.Marshal(data)
  119. msg := &threadsafe_message{Msg: "SetUserReadyStatus", Data: string(d)}
  120. //t.messageChan <- msg
  121. go t.sendMessageToMainThread(msg)
  122. return true
  123. }
  124. func (t *ThreadsafeTable) setUserReadyStatus_sync(userIndex int32, isReady bool) bool {
  125. data := msg_SetReadyStatus{UserIndex: userIndex, IsReady: isReady}
  126. d, _ := json.Marshal(data)
  127. callBack := make(chan byte)
  128. msg := &threadsafe_message{Msg: "SetUserReadyStatus", Data: string(d), Callback: callBack}
  129. //t.messageChan <- msg
  130. go t.sendMessageToMainThread(msg)
  131. <-callBack
  132. return true
  133. }
  134. func (t *ThreadsafeTable) setBaseScore(data string) bool {
  135. d, _ := json.Marshal(data)
  136. msg := &threadsafe_message{Msg: "SetBaseScore", Data: string(d)}
  137. //t.messageChan <- msg
  138. go t.sendMessageToMainThread(msg)
  139. return true
  140. }