uservideo.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package video
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/common"
  5. inventory "bet24.com/servers/micros/item_inventory/proto"
  6. item "bet24.com/servers/micros/item_inventory/proto"
  7. "sort"
  8. "sync"
  9. "time"
  10. )
  11. type user_video struct {
  12. userId int
  13. lock *sync.RWMutex
  14. video_list map[int]*userVideo
  15. settle_list []*settleVideoInfo
  16. }
  17. func newUserVideo(userId int) *user_video {
  18. ret := new(user_video)
  19. ret.userId = userId
  20. ret.lock = &sync.RWMutex{}
  21. ret.video_list = make(map[int]*userVideo)
  22. //加载数据
  23. go ret.loadUserVideo()
  24. go ret.loadSettleVideo()
  25. //log.Debug("newUserVideo %d", userId)
  26. return ret
  27. }
  28. func (this *user_video) loadUserVideo() {
  29. now := common.GetTimeStamp()
  30. list := getUserVideoList(this.userId)
  31. for _, v := range list {
  32. if common.IsSameDay(now, v.UpdateTime) {
  33. continue
  34. }
  35. v.PlayTimes = 0
  36. v.UpdateTime = now
  37. this.updateToDB(v, 0, "userVideo.loadUserVideo")
  38. }
  39. this.lock.Lock()
  40. this.video_list = list
  41. this.lock.Unlock()
  42. }
  43. func (this *user_video) loadSettleVideo() {
  44. list := getGameSettleVideoList(this.userId)
  45. // 按时间顺序
  46. sort.SliceStable(list, func(i, j int) bool {
  47. return list[i].Crdate < list[j].Crdate
  48. })
  49. this.lock.Lock()
  50. this.settle_list = list
  51. this.lock.Unlock()
  52. }
  53. func (this *user_video) getVideo(videoId int) *userVideo {
  54. if videoId <= 0 {
  55. return nil
  56. }
  57. this.lock.RLock()
  58. for _, v := range this.video_list {
  59. if v.VideoId == videoId {
  60. this.lock.RUnlock()
  61. return v
  62. }
  63. }
  64. this.lock.RUnlock()
  65. uv := &userVideo{
  66. VideoId: videoId,
  67. PlayTimes: 0,
  68. UpdateTime: common.GetTimeStamp(),
  69. }
  70. this.lock.Lock()
  71. this.video_list[videoId] = uv
  72. this.lock.Unlock()
  73. this.updateToDB(uv, 0, "userVideo.getVideo")
  74. return uv
  75. }
  76. func (this *user_video) updateToDB(uv *userVideo, isStat int, funcName string) {
  77. go updateUserVideo(this.userId, uv, isStat, funcName)
  78. }
  79. func (this *user_video) play(videoId int) (bool, int, []item.ItemPack) {
  80. success, v, s := this.getInfo(videoId)
  81. if !success {
  82. return false, 0, nil
  83. }
  84. if v == nil {
  85. return false, 0, nil
  86. }
  87. v.PlayTimes++
  88. v.UpdateTime = common.GetTimeStamp()
  89. this.updateToDB(v, 1, "userVideo.play")
  90. return true, v.PlayTimes, s.Awards
  91. }
  92. func (this *user_video) getInfo(videoId int) (bool, *userVideo, *video) {
  93. s := mgr.getSysVideo(videoId)
  94. if s == nil {
  95. log.Debug("uservideo.getInfo getSysVideo userId=%d videoId=%d is not exist", this.userId, videoId)
  96. return false, nil, nil
  97. }
  98. v := this.getVideo(videoId)
  99. if v == nil {
  100. log.Debug("uservideo.getInfo getVideo userId=%d videoId=%d is not exist", this.userId, videoId)
  101. return false, nil, s
  102. }
  103. now := common.GetTimeStamp()
  104. if !common.IsSameDay(now, v.UpdateTime) {
  105. v.PlayTimes = 0
  106. v.UpdateTime = now
  107. this.updateToDB(v, 0, "userVideo.getInfo")
  108. }
  109. if v.PlayTimes >= s.PlayTimes {
  110. return false, v, s
  111. }
  112. return true, v, s
  113. }
  114. func (this *user_video) getSettleInfo(gameId, settleAmount int, sysInfo *settleVideo) *settleInfo_resp {
  115. log.Debug("uservideo.getSettleInfo userId=%d gameId=%d settleAmount=%d sysInfo=%+v",
  116. this.userId, gameId, settleAmount, sysInfo)
  117. maxAmount := settleAmount * sysInfo.MaxRate / 100
  118. if maxAmount <= 0 {
  119. return &settleInfo_resp{}
  120. }
  121. // 保留最近20条
  122. max := 20
  123. this.lock.Lock()
  124. if count := len(this.settle_list); count >= max {
  125. this.settle_list = append(this.settle_list[:count-max], this.settle_list[count-max+1:]...)
  126. }
  127. now := time.Now()
  128. ts := common.GetStamp(now)
  129. info := &settleVideoInfo{
  130. SettleId: 0,
  131. GameID: gameId,
  132. LoseAmount: settleAmount,
  133. MaxTimes: sysInfo.MaxTimes,
  134. SettleTimes: 0,
  135. MaxAmount: maxAmount,
  136. SettleAmount: 0,
  137. Crdate: now.Format(common.Layout),
  138. TimeStamp: ts,
  139. }
  140. this.settle_list = append(this.settle_list, info)
  141. this.lock.Unlock()
  142. // 更新到数据库
  143. go addGameSettleVideo(this.userId, info)
  144. return &settleInfo_resp{
  145. Success: true,
  146. TimeStamp: ts,
  147. ReturnAmount: maxAmount,
  148. MaxTimes: sysInfo.MaxTimes,
  149. SettleAmount: maxAmount / sysInfo.MaxTimes,
  150. }
  151. }
  152. func (this *user_video) settle(ts int) (bool, int) {
  153. amount, info := 0, &settleVideoInfo{}
  154. this.lock.RLock()
  155. for _, v := range this.settle_list {
  156. if v.TimeStamp == ts {
  157. info = v
  158. break
  159. }
  160. }
  161. this.lock.RUnlock()
  162. // 没有找到数据
  163. if info == nil {
  164. return false, amount
  165. }
  166. log.Debug("uservideo.settle userId=%d ts=%d info=%+v", this.userId, ts, info)
  167. // 判断是否已经领完
  168. if info.SettleTimes >= info.MaxTimes || info.SettleAmount >= info.MaxAmount {
  169. return false, amount
  170. }
  171. // 最后一次全部给
  172. if info.SettleTimes == info.MaxTimes-1 {
  173. amount = info.MaxAmount - info.SettleAmount
  174. } else { // 否则均分
  175. amount = info.MaxAmount / info.MaxTimes
  176. }
  177. // 没有可领取的金币
  178. if amount <= 0 {
  179. return false, amount
  180. }
  181. // 加次数、金币
  182. info.SettleTimes++
  183. info.SettleAmount += amount
  184. log.Debug("uservideo.settle userId=%d ts=%d info=%+v", this.userId, ts, info)
  185. // 更新数据库
  186. go awardGameSettleVideo(this.userId, info)
  187. var items []item.ItemPack
  188. items = append(items, item.ItemPack{
  189. ItemId: 1,
  190. Count: amount,
  191. })
  192. // 发送道具
  193. if success := inventory.AddItems(this.userId, items, "看视频游戏结算输返还", info.GameID*100+common.LOGTYPE_VIDEO_GAME_RETURN); !success {
  194. log.Error("video.settle AddItems fail userId=%d items=%+v", this.userId, items)
  195. }
  196. return true, amount
  197. }
  198. // 游戏返还视频列表
  199. func (this *user_video) getGameSettleVideoList() []*settleVideoInfo {
  200. this.lock.RLock()
  201. defer this.lock.RUnlock()
  202. return this.settle_list
  203. }
  204. // 游戏返还视频奖励
  205. func (this *user_video) awardGameSettleVideo(settleId int) *awardRetInfo {
  206. ret, info := &awardRetInfo{}, &settleVideoInfo{}
  207. this.lock.RLock()
  208. for _, v := range this.settle_list {
  209. if v.SettleId == settleId {
  210. info = v
  211. break
  212. }
  213. }
  214. this.lock.RUnlock()
  215. // 没有找到数据
  216. if info == nil {
  217. ret.RetCode = 11
  218. return ret
  219. }
  220. log.Debug("uservideo.awardGameSettleVideo userId=%d settleId=%d info=%+v", this.userId, settleId, info)
  221. // 判断是否已经领完
  222. if info.SettleTimes >= info.MaxTimes || info.SettleAmount >= info.MaxAmount {
  223. ret.RetCode = 12
  224. return ret
  225. }
  226. // 最后一次全部给
  227. if info.SettleTimes == info.MaxTimes-1 {
  228. ret.Amount = info.MaxAmount - info.SettleAmount
  229. } else { // 否则均分
  230. ret.Amount = info.MaxAmount / info.MaxTimes
  231. }
  232. // 没有可领取的金币
  233. if ret.Amount <= 0 {
  234. ret.RetCode = 11
  235. return ret
  236. }
  237. // 加次数、金币
  238. info.SettleTimes++
  239. info.SettleAmount += ret.Amount
  240. log.Debug("uservideo.awardGameSettleVideo userId=%d settleId=%d info=%+v", this.userId, settleId, info)
  241. // 更新数据库
  242. go awardGameSettleVideo(this.userId, info)
  243. var items []item.ItemPack
  244. items = append(items, item.ItemPack{
  245. ItemId: 1,
  246. Count: ret.Amount,
  247. })
  248. // 发送道具
  249. if success := inventory.AddItems(this.userId, items, "看视频游戏结算输返还", info.GameID*100+common.LOGTYPE_VIDEO_GAME_RETURN); !success {
  250. log.Error("video.awardGameSettleVideo AddItems fail userId=%d items=%+v", this.userId, items)
  251. }
  252. ret.RetCode = 1
  253. return ret
  254. }
  255. // 检查小红点
  256. func (this *user_video) checkTip() bool {
  257. this.lock.RLock()
  258. defer this.lock.RUnlock()
  259. for _, v := range this.settle_list {
  260. if v.SettleTimes < v.MaxTimes {
  261. return true
  262. }
  263. }
  264. return false
  265. }