chipmgr.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package handler
  2. import (
  3. "encoding/json"
  4. "sync"
  5. "time"
  6. "bet24.com/log"
  7. "bet24.com/redis"
  8. pb "bet24.com/servers/micros/money/proto"
  9. notification "bet24.com/servers/micros/notification/proto"
  10. )
  11. type chipmgr struct {
  12. lock *sync.RWMutex
  13. cfg *pb.TransferCfg
  14. }
  15. func newChipMgr() *chipmgr {
  16. mgr := new(chipmgr)
  17. mgr.lock = &sync.RWMutex{}
  18. mgr.refresh()
  19. log.Debug("chip manager running")
  20. return mgr
  21. }
  22. func (this *chipmgr) refresh() {
  23. go this.loadCfg()
  24. time.AfterFunc(time.Minute*1, this.refresh)
  25. }
  26. func (this *chipmgr) loadCfg() {
  27. info := getTransferCfg()
  28. this.lock.Lock()
  29. defer this.lock.Unlock()
  30. this.cfg = info
  31. }
  32. // 筹码日志
  33. func (this *chipmgr) chipLog(userId int, beginTime, endTime string, pageIndex, pageSize int) (int, []*pb.CashInfo) {
  34. return chipLog(userId, beginTime, endTime, pageIndex, pageSize)
  35. }
  36. // 获取筹码
  37. func (this *chipmgr) getChip(userId int) (bool, int, int) {
  38. return getChip(userId)
  39. }
  40. // 加筹码并通知客户端
  41. func (this *chipmgr) giveChip(userId, amount, logType int, sourceName, remark, ipAddress string) int {
  42. retCode := giveChip(userId, amount, logType, sourceName, remark, ipAddress)
  43. if retCode != 1 {
  44. log.Debug("chip.giveChip fail userId=%d amount=%d logType=%d sourceName=%s remark=%s",
  45. userId, amount, logType, sourceName, remark)
  46. return retCode
  47. }
  48. // 通知客户端
  49. notification.AddNotification(userId, notification.Notification_Chip, "")
  50. go this.notifyChanged(userId)
  51. return retCode
  52. }
  53. // 减筹码并通知客户端
  54. func (this *chipmgr) reduceChip(userId, amount, logType int, sourceName, remark, ipAddress string) int {
  55. retCode := reduceChip(userId, amount, logType, sourceName, remark, ipAddress)
  56. if retCode != 1 {
  57. log.Debug("chip.reduceChip fail userId=%d amount=%d logType=%d sourceName=%s remark=%s",
  58. userId, amount, logType, sourceName, remark)
  59. return retCode
  60. }
  61. // 通知客户端
  62. go notification.AddNotification(userId, notification.Notification_Chip, "")
  63. go this.notifyChanged(userId)
  64. return retCode
  65. }
  66. func (this *chipmgr) notifyChanged(userId int) {
  67. var d redis.Channel_msg
  68. d.Message = "RefreshChip"
  69. d.UserID = userId
  70. js, _ := json.Marshal(d)
  71. redis.Publish(string(js))
  72. }
  73. // 保险柜存入并通知客户端
  74. func (this *chipmgr) bankIn(userId, amount, gameId int, serverName, ipAddress string) (int, int, int, string) {
  75. retCode, cash, bank, outMsg := chipBankIn(userId, amount, gameId, serverName, ipAddress)
  76. if retCode != 1 {
  77. log.Debug("chip.BankIn fail userId=%d amount=%d gameId=%d serverName=%s ipAddress=%s",
  78. userId, amount, gameId, serverName, ipAddress)
  79. return retCode, cash, bank, outMsg
  80. }
  81. // 通知客户端
  82. go notification.AddNotification(userId, notification.Notification_Chip, "")
  83. go this.notifyChanged(userId)
  84. return retCode, cash, bank, outMsg
  85. }
  86. // 保险柜取出并通知客户端
  87. func (this *chipmgr) bankOut(userId, amount, gameId int, serverName, ipAddress string) (int, int, int, string) {
  88. retCode, cash, bank, outMsg := chipBankOut(userId, amount, gameId, serverName, ipAddress)
  89. if retCode != 1 {
  90. log.Debug("chip.BankOut fail userId=%d amount=%d serverName=%s ipAddress=%s",
  91. userId, amount, serverName, ipAddress)
  92. return retCode, cash, bank, outMsg
  93. }
  94. // 通知客户端
  95. go notification.AddNotification(userId, notification.Notification_Chip, "")
  96. go this.notifyChanged(userId)
  97. return retCode, cash, bank, outMsg
  98. }
  99. // 保险柜查询
  100. func (this *chipmgr) bankQuery(userId int) (int, int) {
  101. return chipBankQuery(userId)
  102. }
  103. // 转账(操作结果/操作描述/剩余金额/返还金额)
  104. func (this *chipmgr) bankTransfer(userId, toUserId, amount int, ipAddress string) (int, string, int, int) {
  105. retCode, stillAmount, outMsg, refund := chipBankTransfer(userId, toUserId, amount, ipAddress)
  106. if retCode != 1 {
  107. log.Debug("chip.BankTransfer fail userId=%d toUserId=%d amount=%d ipAddress=%s",
  108. userId, toUserId, amount, ipAddress)
  109. return retCode, outMsg, stillAmount, refund
  110. }
  111. go this.notifyChanged(userId)
  112. go notification.AddNotification(toUserId, notification.Notification_Chip, "")
  113. go notification.AddNotification(userId, notification.Notification_Chip, "")
  114. return retCode, outMsg, stillAmount, refund
  115. }
  116. func (this *chipmgr) getTransferCfg() *pb.TransferCfg {
  117. this.lock.RLock()
  118. defer this.lock.RUnlock()
  119. return this.cfg
  120. }
  121. // 赠送记录
  122. func (this *chipmgr) transferLog(userId int) []*pb.TransferInfo {
  123. return chipTransferLog(userId)
  124. }
  125. // 保险柜记录
  126. func (this *chipmgr) bankLog(userId int, beginTime, endTime string, pageIndex, pageSize int) (int, []*pb.BankLogInfo) {
  127. return chipBankLog(userId, beginTime, endTime, pageIndex, pageSize)
  128. }
  129. // 获取银行信息
  130. func (this *chipmgr) getBankInfo(userId int) *pb.BankInfo {
  131. return getBankInfo(userId)
  132. }
  133. // 保存银行信息
  134. func (this *chipmgr) saveBankInfo(userId int, realName, bankName, bankCode, bankCard, mobile string) bool {
  135. info := &pb.BankInfo{
  136. RealName: realName,
  137. BankName: bankName,
  138. BankCode: bankCode,
  139. BankCard: bankCard,
  140. Mobile: mobile,
  141. }
  142. return saveBankInfo(userId, info)
  143. }