main.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package gate
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. utils2 "bet24.com/servers/insecureframe/gate/Utils"
  6. "bet24.com/log"
  7. "bet24.com/redis"
  8. "bet24.com/servers/insecureframe/message"
  9. notification "bet24.com/servers/micros/notification/proto"
  10. "bet24.com/servers/transaction"
  11. "bet24.com/servers/user"
  12. )
  13. var stopping bool
  14. func Run(sink GateSink, logPath string) {
  15. stopping = false
  16. run(sink, logPath)
  17. }
  18. func DumpUsers() {
  19. //runtime.GC()
  20. log.Release("Totoal Connections = %d", gate.wsServer.GetConnectionCount())
  21. log.Release("Totoal UserCount = %d", len(gate.userlist))
  22. log.Release("Totoal Logined = %d", gate.getLoginedCount())
  23. for _, v := range gate.userlist {
  24. if !v.isRobot() {
  25. v.Dump()
  26. }
  27. }
  28. }
  29. func DumpRobots() {
  30. log.Release("Totoal Robot = %d", gate.getRobotCount())
  31. for _, v := range gate.userlist {
  32. if v.isRobot() {
  33. v.Dump()
  34. }
  35. }
  36. }
  37. func DumpMessageRecord(userId int) {
  38. gate.msgRecord.dump(userId)
  39. }
  40. func ClearMessageRecord(userId int) {
  41. gate.msgRecord.clear(userId)
  42. }
  43. func GetUserCount() int {
  44. return gate.getUserCount()
  45. }
  46. func GetPlayerCount() int {
  47. return gate.getPlayerCount()
  48. }
  49. func GetLoginedCount() int {
  50. return gate.getLoginedCount()
  51. }
  52. func GetUserInfo(userIndex int32) *user.UserInfo {
  53. return gate.getUserInfo(userIndex)
  54. }
  55. func GetUserList() []*user.UserInfo {
  56. return gate.getUserList()
  57. }
  58. func GetUserByUserId(userId int) *user.UserInfo {
  59. u := gate.getUserByUserID(userId)
  60. if u == nil {
  61. return nil
  62. }
  63. return u.userInfo
  64. }
  65. func SendMessage(userIndex int32, msg, data string) bool {
  66. u := gate.getUser(userIndex)
  67. if u == nil {
  68. log.Debug("gate.SendMessage failed %d,%s", userIndex, msg)
  69. sink.OnUserExit(userIndex)
  70. return false
  71. }
  72. if u.userInfo.IsRobot() {
  73. return true
  74. }
  75. m := message.BaseMsg{Data: data, Msg: msg}
  76. u.WriteMsg(msg, m)
  77. return true
  78. }
  79. func SendRawMessage(userIndex int32, rawData string) {
  80. u := gate.getUser(userIndex)
  81. if u == nil {
  82. log.Debug("gate.SendRawMessage failed %d,%s", userIndex, rawData)
  83. return
  84. }
  85. u.writeRawData(rawData)
  86. }
  87. func GetEventChannel(userIndex int32) chan interface{} {
  88. u := gate.getUser(userIndex)
  89. if u == nil {
  90. log.Debug("gate.GetEventChannel failed %d", userIndex)
  91. return nil
  92. }
  93. return u.GetEventChannel()
  94. }
  95. func GetUserIP(userIndex int32) string {
  96. u := gate.getUser(userIndex)
  97. if u == nil {
  98. log.Debug("gate.GetUserIP failed %d", userIndex)
  99. return ""
  100. }
  101. return u.IP()
  102. }
  103. func GetUserIPByUserID(userID int) string {
  104. u := gate.getUserByUserID(userID)
  105. if u == nil {
  106. log.Debug("gate.GetUserIPByUserID failed %d", userID)
  107. return ""
  108. }
  109. return u.IP()
  110. }
  111. func WriteUserMoneyWithModifyAmount(userId int, amount, tax int, status, scoreType int, sourceName string, isChip bool) int {
  112. if isChip {
  113. log.Release("frame.WriteUserMoneyWithModifyAmount not support chip room")
  114. return 0
  115. }
  116. u := gate.getUserByUserID(userId)
  117. if u == nil {
  118. _, ret := transaction.WriteMoneySyncWithModifyAmount(userId, sink.GetGameID(), amount, tax, status, scoreType, sourceName, "")
  119. return ret
  120. }
  121. // 机器人不写分
  122. if u.userInfo.IsRobot() {
  123. gold := u.userInfo.GetUserGold()
  124. if gold+amount < 0 {
  125. amount = -gold
  126. }
  127. u.userInfo.SetUserGold(gold + amount)
  128. return amount
  129. }
  130. balance, ret := transaction.WriteMoneySyncWithModifyAmount(userId, sink.GetGameID(), amount, tax, status,
  131. scoreType, sourceName, u.IP())
  132. if amount != 0 {
  133. go notification.AddNotification(userId, notification.Notification_Gold, "")
  134. }
  135. u.userInfo.SetUserGold(balance)
  136. u.userInfo.YyfGold=balance
  137. // 游戏才需要广播
  138. if sink.GetGameID() != 0 {
  139. fmt.Printf("WriteUserMoney===WriteUserMoneyWithModifyAmount")
  140. d, _ := json.Marshal(message.UserScoreChange{UserId: userId, Gold: u.userInfo.GetUserGold(), Chip: u.userInfo.GetChip(),YyfGold:u.userInfo.YyfGold})
  141. gate.broadcastData(message.Frame_UserScoreChange, string(d))
  142. }
  143. return ret
  144. }
  145. type UserInfoReq struct {
  146. GameType int `json:"gameType"` //游戏类型: 1.ludo 2.其他
  147. UserWallets []UserInfo `json:"userWallets"` //结算时的游戏排行
  148. }
  149. type UserInfo struct {
  150. GoldCoins int `json:"goldCoins"` //数量
  151. Type int `json:"type"` //类型: 1.加 2.减
  152. UserId int `json:"userId"` // ⽤户ID
  153. }
  154. func WriteUserMoney(userId int, amount, tax int, status, scoreType int, sourceName string, isChip bool) (bool, int) {
  155. u := gate.getUserByUserID(userId)
  156. if u.userInfo.YyfUid!=0{
  157. var userInfo = UserInfo{
  158. UserId: u.userInfo.YyfUid,
  159. //GoldCoins: math.Abs(amount),
  160. // Type: 1,
  161. }
  162. var amount2 = amount
  163. if amount2 >= 0 {
  164. amount2 = amount2 - tax
  165. userInfo.Type = 1
  166. } else {
  167. amount2 = -amount2
  168. userInfo.Type = 2
  169. }
  170. userInfo.GoldCoins = amount2
  171. var userInfos = []UserInfo{}
  172. userInfos = append(userInfos, userInfo)
  173. var userInfoReq = UserInfoReq{GameType: 1, UserWallets: userInfos}
  174. jsonData2, _ := json.Marshal(userInfoReq)
  175. fmt.Print("扣减金币=====", userInfoReq, amount2)
  176. utils2.PostData(jsonData2)
  177. }
  178. //var userInfoReq = UserInfoReq{GameType: 1, UserWallets: userInfos}
  179. if u == nil {
  180. // 离线写分
  181. if isChip {
  182. go transaction.WriteChipSync(userId, sink.GetGameID(), amount, tax,
  183. status, scoreType, sourceName, "127.0.0.1")
  184. if amount != 0 {
  185. go notification.AddNotification(userId, notification.Notification_Chip, "")
  186. }
  187. } else {
  188. go transaction.WriteMoneySync(userId, sink.GetGameID(), amount, tax,
  189. status, scoreType, sourceName, "127.0.0.1")
  190. if amount != 0 {
  191. go notification.AddNotification(userId, notification.Notification_Gold, "")
  192. }
  193. }
  194. if amount != 0 {
  195. var d redis.Channel_msg
  196. d.Message = "RefreshGold"
  197. d.UserID = userId
  198. js, _ := json.Marshal(d)
  199. redis.Publish(string(js))
  200. }
  201. return true, 0
  202. }
  203. balance := 0
  204. // 机器人不写分
  205. if u.userInfo.IsRobot() {
  206. gold := u.userInfo.GetUserGold()
  207. if isChip {
  208. gold = u.userInfo.GetChip()
  209. }
  210. if gold+amount < 0 {
  211. amount = -gold
  212. }
  213. if isChip {
  214. //u.userInfo.YyfGold=gold + amount
  215. u.userInfo.SetChip(gold + amount)
  216. } else {
  217. u.userInfo.YyfGold=gold + amount
  218. u.userInfo.SetUserGold(gold + amount)
  219. }
  220. balance = gold + amount
  221. } else {
  222. var isSuceeded bool
  223. var retCode int
  224. var blc int
  225. if isChip {
  226. isSuceeded, retCode, blc = transaction.WriteChipSync(userId, sink.GetGameID(), amount, tax,
  227. status, scoreType, sourceName, u.IP())
  228. } else {
  229. isSuceeded, retCode, blc = transaction.WriteMoneySync(userId, sink.GetGameID(), amount, tax,
  230. status, scoreType, sourceName, u.IP())
  231. }
  232. if !isSuceeded {
  233. log.Release("WriteUserMoney failed,UserId[%d],amount[%d],retCode[%d]", userId, amount, retCode)
  234. return false, balance
  235. }
  236. balance = blc
  237. if isChip {
  238. if amount != 0 {
  239. go notification.AddNotification(userId, notification.Notification_Chip, "")
  240. }
  241. u.userInfo.SetChip(balance)
  242. } else {
  243. if amount != 0 {
  244. go notification.AddNotification(userId, notification.Notification_Gold, "")
  245. }
  246. u.userInfo.YyfGold=balance
  247. u.userInfo.SetUserGold(balance)
  248. }
  249. }
  250. // 游戏才需要广播
  251. if sink.GetGameID() != 0 {
  252. fmt.Printf("WriteUserMoney===WriteUserMoney")
  253. d, _ := json.Marshal(message.UserScoreChange{UserId: userId, Gold: u.userInfo.GetUserGold(), Chip: u.userInfo.GetChip(),YyfGold:u.userInfo.YyfGold})
  254. gate.broadcastData(message.Frame_UserScoreChange, string(d))
  255. }
  256. return true, balance
  257. }
  258. func KickUser(userIndex int32) bool {
  259. return gate.kickUser(userIndex)
  260. }
  261. func BroadcastData(msg, data string) {
  262. gate.broadcastData(msg, data)
  263. }
  264. func RefreshGold(userIndex int32) {
  265. gate.refreshGold(userIndex)
  266. }
  267. type GateSink interface {
  268. GetServerPort() int
  269. OnUserEnter(userIndex int32)
  270. OnUserLogined(userIndex int32)
  271. OnUserExit(userIndex int32)
  272. OnGameMessage(userIndex int32, userID int, msg, data string) bool
  273. GetGameID() int
  274. GetGameName() string
  275. GetRoomName() string
  276. GetCertFile() string
  277. GetKeyFile() string
  278. //机器人配置
  279. GetRobotCount() int
  280. GetRobotGoldLimit() (min, max int)
  281. GetRobotOnlineSec() int
  282. IsChipRoom() bool
  283. GetChipRoom() int
  284. OnPlatformConfig(key string)
  285. IsPrivateRoom() bool
  286. IsLadderRoom() bool
  287. GetVersionID() int
  288. }
  289. func StopServer() {
  290. stopping = true
  291. gate.removeUser(-1)
  292. }
  293. func LoginAUser(userId int) *user.UserInfo {
  294. index := gate.GenUserIndex()
  295. a := NewClient(index, nil, gate, false)
  296. go a.Run()
  297. a.forceLogin(userId)
  298. return GetUserInfo(index)
  299. }
  300. func SilentRemoveUser(userId int) {
  301. gate.silentRemoveUser(userId)
  302. }