sink.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package gatesink
  2. import (
  3. "encoding/json"
  4. "sync"
  5. _ "fmt"
  6. "strconv"
  7. "bet24.com/log"
  8. "bet24.com/redis"
  9. coreservice "bet24.com/servers/coreservice/client"
  10. "bet24.com/servers/fishhall/config"
  11. "bet24.com/servers/insecureframe/gate"
  12. notification "bet24.com/servers/micros/notification/proto"
  13. "bet24.com/servers/transaction"
  14. commonuser "bet24.com/servers/user"
  15. )
  16. var Sink *GateSink
  17. func Run() {
  18. Sink = new(GateSink)
  19. Sink.ctor()
  20. }
  21. type GateSink struct {
  22. userlist map[int32]*user
  23. lock *sync.RWMutex
  24. pollNotification bool // 是否需要主动轮询通知
  25. }
  26. func (this *GateSink) ctor() {
  27. this.userlist = make(map[int32]*user)
  28. this.lock = &sync.RWMutex{}
  29. }
  30. func (this *GateSink) SetPollNotification(active bool) {
  31. this.pollNotification = active
  32. }
  33. func (this *GateSink) GetServerPort() int {
  34. return config.Server.ServerPort
  35. }
  36. func (this *GateSink) OnUserEnter(userIndex int32) {
  37. this.lock.RLock()
  38. if _, ok := this.userlist[userIndex]; ok {
  39. log.Release("GateSink.OnUserEnter user already exist %d", userIndex)
  40. this.lock.RUnlock()
  41. return
  42. }
  43. this.lock.RUnlock()
  44. pUser := newUser(userIndex)
  45. this.lock.Lock()
  46. this.userlist[userIndex] = pUser
  47. this.lock.Unlock()
  48. }
  49. func (this *GateSink) OnUserLogined(userIndex int32) {
  50. this.lock.RLock()
  51. u, ok := this.userlist[userIndex]
  52. this.lock.RUnlock()
  53. if !ok {
  54. log.Debug("GateSink.OnUserLogined user not exist %d", userIndex)
  55. return
  56. }
  57. go u.onLogined()
  58. }
  59. func (this *GateSink) OnUserExit(userIndex int32) {
  60. this.lock.Lock()
  61. defer this.lock.Unlock()
  62. if user, ok := this.userlist[userIndex]; ok {
  63. user.clear()
  64. }
  65. delete(this.userlist, userIndex)
  66. }
  67. func (this *GateSink) OnGameMessage(userindex int32, userid int, msg, data string) bool {
  68. this.lock.RLock()
  69. user, ok := this.userlist[userindex]
  70. this.lock.RUnlock()
  71. if !ok {
  72. log.Release("GateSink.OnGameMessage user not found %d", userindex)
  73. return false
  74. }
  75. return user.onGameMessage(msg, data)
  76. }
  77. func (this *GateSink) GetGameID() int {
  78. return 0
  79. }
  80. func (this *GateSink) GetGameName() string {
  81. if config.Server.IsChipRoom < 0 {
  82. return "广告大厅"
  83. }
  84. return "游戏大厅"
  85. }
  86. func (this *GateSink) GetRoomName() string {
  87. if config.Server.IsChipRoom < 0 {
  88. return "广告大厅"
  89. }
  90. return "游戏大厅"
  91. }
  92. func (this *GateSink) GetRobotCount() int {
  93. return 0
  94. }
  95. func (this *GateSink) GetRobotGoldLimit() (min, max int) {
  96. return 0, 0
  97. }
  98. func (this *GateSink) GetRobotOnlineSec() int {
  99. return 0
  100. }
  101. func (this *GateSink) OnUserInfoChanged(userIndex int32) {
  102. }
  103. func (this *GateSink) GetCertFile() string {
  104. return config.Server.CertFile
  105. }
  106. func (this *GateSink) GetKeyFile() string {
  107. return config.Server.KeyFile
  108. }
  109. func (this *GateSink) IsChipRoom() bool {
  110. return config.Server.IsChipRoom > 0
  111. }
  112. func (this *GateSink) GetVersionID() int {
  113. return config.Server.VersionID
  114. }
  115. func (this *GateSink) IsLadderRoom() bool {
  116. return false
  117. }
  118. func (this *GateSink) GetChipRoom() int {
  119. return config.Server.IsChipRoom
  120. }
  121. func (this *GateSink) OnPlatformConfig(key string) {
  122. }
  123. func (this *GateSink) IsPrivateRoom() bool {
  124. return false
  125. }
  126. func (this *GateSink) onUserNotification(userId int) {
  127. if userId == -1 {
  128. this.lock.RLock()
  129. defer this.lock.RUnlock()
  130. for _, v := range this.userlist {
  131. go v.getNotifications(true)
  132. }
  133. return
  134. }
  135. u := this.getUserByUserId(userId)
  136. if u == nil {
  137. return
  138. }
  139. u.getNotifications(true)
  140. }
  141. func (this *GateSink) getUserByUserId(userId int) *user {
  142. this.lock.Lock()
  143. defer this.lock.Unlock()
  144. for _, v := range this.userlist {
  145. if v.getUserId() == userId {
  146. return v
  147. }
  148. }
  149. return nil
  150. }
  151. func (this *GateSink) SendGameCmd(userId int, msg, data string) {
  152. // 广播
  153. if userId == -1 {
  154. this.lock.Lock()
  155. defer this.lock.Unlock()
  156. for _, v := range this.userlist {
  157. go func(u *user) {
  158. u.WriteMsg(msg, data)
  159. }(v)
  160. }
  161. return
  162. }
  163. usr := this.getUserByUserId(userId)
  164. if usr == nil {
  165. log.Debug("GateSink.SendGameCmd userId[%d] not exist", userId)
  166. return
  167. }
  168. usr.WriteMsg(msg, data)
  169. }
  170. func (this *GateSink) GetUserInfo(userId int) *commonuser.UserInfo {
  171. return gate.GetUserByUserId(userId)
  172. }
  173. func (this *GateSink) WriteMoney(userId int, gameId int, amount int, tax int, status int, scoreType int, srcName string) bool {
  174. usr := this.getUserByUserId(userId)
  175. userIp := ""
  176. userName := strconv.Itoa(userId)
  177. if usr != nil {
  178. userIp = usr.GetIP()
  179. userName = usr.getNickName()
  180. }
  181. var ret bool
  182. if this.IsChipRoom() {
  183. ret, _, _ = transaction.WriteChipSync(userId, gameId, amount, tax, status, scoreType, srcName, userIp)
  184. if ret {
  185. go notification.AddNotification(userId, notification.Notification_Chip, "")
  186. if amount >= 5000000 && gameId > 0 {
  187. this.sendBroadcast(userId, userName, amount, gameId, srcName)
  188. }
  189. }
  190. } else {
  191. ret, _, _ = transaction.WriteMoneySync(userId, gameId, amount, tax, status, scoreType, srcName, userIp)
  192. if ret {
  193. go notification.AddNotification(userId, notification.Notification_Gold, "")
  194. if amount >= 5000000 && gameId > 0 {
  195. this.sendBroadcast(userId, userName, amount, gameId, srcName)
  196. }
  197. }
  198. }
  199. return ret
  200. }
  201. func (this *GateSink) WriteMoneyWithModifyAmount(userId int, gameId int, amount int, tax int, status int, scoreType int, srcName string, userIp string) int {
  202. if this.IsChipRoom() {
  203. return 0
  204. }
  205. _, ret := transaction.WriteMoneySyncWithModifyAmount(userId, gameId, amount, tax, status, scoreType, srcName, userIp)
  206. return ret
  207. }
  208. func (this *GateSink) sendBroadcast(userId int, userName string, score int, gameId int, gameName string) {
  209. go coreservice.SendGameWinBroadcast(userId, userName, score, gameId, gameName)
  210. }
  211. func RecvChannelData(data string) {
  212. //log.Debug("RecvChannelData data = %+v", data)
  213. var msg redis.Channel_msg
  214. err := json.Unmarshal([]byte(data), &msg)
  215. if err != nil {
  216. log.Release("RecvChannelData Unmarshal data failed %v", data)
  217. return
  218. }
  219. if msg.Message == "UsreNotification" {
  220. Sink.onUserNotification(msg.UserID)
  221. }
  222. }
  223. func (this *GateSink) OnNotification(userId int, data string) {
  224. var nt notification.Notification
  225. if err := json.Unmarshal([]byte(data), &nt); err != nil {
  226. log.Debug("GateSink.OnNotification unmarshal fail %v", err)
  227. return
  228. }
  229. notifications := []*notification.Notification{&nt}
  230. if userId == -1 {
  231. this.lock.RLock()
  232. defer this.lock.RUnlock()
  233. for _, v := range this.userlist {
  234. go func(u *user) {
  235. u.onNotification(notifications)
  236. }(v)
  237. }
  238. return
  239. }
  240. usr := this.getUserByUserId(userId)
  241. if usr == nil {
  242. return
  243. }
  244. usr.onNotification(notifications)
  245. }