slotcommon.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package slotcommon
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. _ "bet24.com/log"
  7. coreservice "bet24.com/servers/coreservice/client"
  8. "bet24.com/servers/games/slotcommon/betlevel"
  9. notification "bet24.com/servers/micros/notification/proto"
  10. task "bet24.com/servers/micros/task/proto"
  11. userservices "bet24.com/servers/micros/userservices/proto"
  12. waterpool "bet24.com/servers/micros/waterpool/proto"
  13. "bet24.com/servers/transaction"
  14. )
  15. const DELAY_REMOVE = 300 // 300秒后删除用户
  16. const DELAY_CHECK = 60 // 每分钟检查一次
  17. type userFreeSpin struct {
  18. freeSpinTime int // 免费次数
  19. lastBetAmount int
  20. isChipRoom bool
  21. }
  22. type Slotcommon struct {
  23. gameId int
  24. gameName string
  25. taxRate int
  26. userFreeSpins map[int]*userFreeSpin
  27. lock *sync.RWMutex
  28. lockRemove *sync.RWMutex
  29. removingFreespins map[int]int64
  30. levelMgr *betlevel.BetLevelManager
  31. userlist map[int]int
  32. lockUser *sync.RWMutex
  33. }
  34. func NewSlotCommon(gameId int, gameName string, taxRate int) *Slotcommon {
  35. sc := new(Slotcommon)
  36. sc.gameId = gameId
  37. sc.gameName = gameName
  38. sc.taxRate = taxRate
  39. sc.lock = &sync.RWMutex{}
  40. sc.userFreeSpins = make(map[int]*userFreeSpin)
  41. sc.removingFreespins = make(map[int]int64)
  42. sc.userlist = make(map[int]int)
  43. sc.lockRemove = &sync.RWMutex{}
  44. sc.lockUser = &sync.RWMutex{}
  45. go sc.checkRemoveUser()
  46. return sc
  47. }
  48. func (sm *Slotcommon) checkRemoveUser() {
  49. time.AfterFunc(DELAY_CHECK*time.Second, sm.checkRemoveUser)
  50. var toRemove []int
  51. latestRemoveTime := time.Now().Unix() - DELAY_REMOVE
  52. sm.lockRemove.RLock()
  53. for k, v := range sm.removingFreespins {
  54. if v < latestRemoveTime {
  55. toRemove = append(toRemove, k)
  56. }
  57. }
  58. sm.lockRemove.RUnlock()
  59. if len(toRemove) == 0 {
  60. return
  61. }
  62. sm.lockRemove.Lock()
  63. for _, v := range toRemove {
  64. delete(sm.removingFreespins, v)
  65. }
  66. sm.lockRemove.Unlock()
  67. sm.lock.Lock()
  68. for _, v := range toRemove {
  69. delete(sm.userFreeSpins, v)
  70. }
  71. sm.lock.Unlock()
  72. }
  73. func (sc *Slotcommon) AddFreeSpin(userId int, freeCount, bet int, isChipRoom bool) {
  74. sc.lock.Lock()
  75. uf, ok := sc.userFreeSpins[userId]
  76. count := freeCount
  77. if ok {
  78. count += uf.freeSpinTime
  79. }
  80. sc.userFreeSpins[userId] = &userFreeSpin{freeSpinTime: count, lastBetAmount: bet, isChipRoom: isChipRoom}
  81. sc.lock.Unlock()
  82. }
  83. func (sc *Slotcommon) UseFreeSpin(userId int) (bool, int, bool) {
  84. sc.lock.Lock()
  85. defer sc.lock.Unlock()
  86. t, ok := sc.userFreeSpins[userId]
  87. if !ok || t.freeSpinTime <= 0 {
  88. return false, 0, false
  89. }
  90. t.freeSpinTime--
  91. return true, t.lastBetAmount, t.isChipRoom
  92. }
  93. func (sc *Slotcommon) GetFreeSpinTime(userId int) int {
  94. sc.lock.RLock()
  95. defer sc.lock.RUnlock()
  96. t, ok := sc.userFreeSpins[userId]
  97. if !ok {
  98. return 0
  99. }
  100. return t.freeSpinTime
  101. }
  102. func (sc *Slotcommon) OnUserEnter(userId int, isChipRoom bool) {
  103. sc.lockUser.RLock()
  104. _, ok := sc.userlist[userId]
  105. sc.lockUser.RUnlock()
  106. if ok {
  107. return
  108. }
  109. sc.lockUser.Lock()
  110. sc.userlist[userId] = 1
  111. sc.lockUser.Unlock()
  112. go transaction.Trans_SetGameStatus(userId, sc.gameId, 1, sc.gameName, isChipRoom)
  113. go coreservice.FriendSetUserStatus(userId, 1, sc.gameName)
  114. sc.lockRemove.Lock()
  115. delete(sc.removingFreespins, userId)
  116. sc.lockRemove.Unlock()
  117. }
  118. func (sc *Slotcommon) OnUserExit(userId int, isChipRoom bool) {
  119. found := false
  120. sc.lockUser.Lock()
  121. _, found = sc.userlist[userId]
  122. if found {
  123. delete(sc.userlist, userId)
  124. }
  125. sc.lockUser.Unlock()
  126. if !found {
  127. return
  128. }
  129. go transaction.Trans_SetGameStatus(userId, sc.gameId, 0, sc.gameName, isChipRoom)
  130. go coreservice.FriendSetUserStatus(userId, 0, sc.gameName)
  131. go sc.WriteMoney(userId, 0, 0, 0, sc.gameId*100+99, false)
  132. if sc.GetFreeSpinTime(userId) > 0 {
  133. sc.lockRemove.Lock()
  134. sc.removingFreespins[userId] = time.Now().Unix()
  135. sc.lockRemove.Unlock()
  136. return
  137. }
  138. sc.lock.Lock()
  139. defer sc.lock.Unlock()
  140. delete(sc.userFreeSpins, userId)
  141. }
  142. func (sc *Slotcommon) WriteMoney(userId int, amount, tax, status, scoreType int, isChipRoom bool) bool {
  143. gameId := sc.gameId
  144. srcName := sc.gameName
  145. var ret bool
  146. if isChipRoom {
  147. ret, _, _ = transaction.WriteChipSync(userId, gameId, amount, tax, status, scoreType, srcName, "")
  148. if ret {
  149. go notification.AddNotification(userId, notification.Notification_Chip, "")
  150. }
  151. } else {
  152. ret, _, _ = transaction.WriteMoneySync(userId, gameId, amount, tax, status, scoreType, srcName, "")
  153. if ret {
  154. go notification.AddNotification(userId, notification.Notification_Gold, "")
  155. }
  156. }
  157. return true
  158. }
  159. func (sc *Slotcommon) GetControlType(userId, gameId int) int {
  160. usr := userservices.GetUserInfo(userId)
  161. if usr == nil {
  162. return 0
  163. }
  164. return waterpool.GetControlType(usr.GetUserGold(), true, gameId)
  165. }
  166. func (sc *Slotcommon) WriteResult(userId int, betAmount, winAmount int, freeSpin, isChipRoom bool, resultDesc string, gameId int) {
  167. tax := 0
  168. amount := betAmount
  169. if freeSpin {
  170. amount = 0
  171. }
  172. if sc.taxRate > 0 && (winAmount-amount) > 0 {
  173. tax = (winAmount - amount) / 100 * sc.taxRate
  174. }
  175. go func() {
  176. usr := userservices.GetUserInfo(userId)
  177. if usr == nil {
  178. return
  179. }
  180. if betAmount > 0 && !freeSpin && !isChipRoom {
  181. waterpool.AddBet(usr.GetUserGold(), betAmount, true, gameId)
  182. }
  183. if winAmount > 0 && !isChipRoom {
  184. waterpool.ReducePool(usr.GetUserGold(), winAmount, true, gameId)
  185. }
  186. }()
  187. isFree := 0
  188. if freeSpin {
  189. isFree = 1
  190. if isChipRoom {
  191. isFree = 2
  192. }
  193. }
  194. sc.WriteMoney(userId, winAmount-tax, tax, 2, 2+sc.gameId*100+isFree, isChipRoom)
  195. returnLevel := 0
  196. go func() {
  197. if isChipRoom {
  198. transaction.WriteChipBetRecordAction(userId, sc.gameId, "", amount,
  199. winAmount, tax, 2.0,
  200. sc.getBetDesc(betAmount, freeSpin, isChipRoom), resultDesc, returnLevel, "", sc.gameName)
  201. slotScore := 1
  202. if winAmount > betAmount {
  203. slotScore = 2
  204. }
  205. coreservice.AddSlotScore(userId, slotScore)
  206. } else {
  207. transaction.WriteBetRecordAction(userId, sc.gameId, "", amount,
  208. winAmount, tax, 2.0,
  209. sc.getBetDesc(betAmount, freeSpin, isChipRoom), resultDesc, returnLevel, "", sc.gameName)
  210. notification.AddNotification(userId, notification.Notification_Chip, "")
  211. if isFree == 0 {
  212. winAmount -= betAmount
  213. }
  214. }
  215. coreservice.AddUserWinScore(userId, winAmount)
  216. task.DoTaskAction(userId, task.TaskAction_playgame, 1, task.TaskScope{GameName: sc.gameName})
  217. task.DoTaskAction(userId, task.TaskAction_fire, amount, task.TaskScope{GameName: sc.gameName})
  218. // 上面已经减去betAmount
  219. realWin := winAmount
  220. if realWin > 0 {
  221. task.DoTaskAction(userId, task.TaskAction_earn, realWin, task.TaskScope{GameName: sc.gameName})
  222. task.DoTaskAction(userId, task.TaskAction_wingame, 1, task.TaskScope{GameName: sc.gameName})
  223. }
  224. if winAmount > 0 {
  225. task.DoTaskAction(userId, task.TaskAction_betWin, winAmount, task.TaskScope{GameName: sc.gameName})
  226. }
  227. }()
  228. }
  229. func (sc *Slotcommon) getBetDesc(betAmount int, isFree, isChipRoom bool) string {
  230. if isFree {
  231. nFromAd := 0
  232. if isChipRoom {
  233. nFromAd = 1
  234. }
  235. return fmt.Sprintf("%d|%d", betAmount, nFromAd)
  236. } else {
  237. return fmt.Sprintf("%d", betAmount)
  238. }
  239. }