user_room.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package user
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/redis"
  5. "bet24.com/servers/common"
  6. "bet24.com/servers/micros/audioroom/handler/config"
  7. IRoomMgr "bet24.com/servers/micros/audioroom/handler/interface"
  8. pb "bet24.com/servers/micros/audioroom/proto"
  9. "bet24.com/servers/micros/audioroom/transaction/database"
  10. money "bet24.com/servers/micros/money/proto"
  11. "encoding/json"
  12. "fmt"
  13. "strconv"
  14. "sync"
  15. "time"
  16. )
  17. const user_Seconds = 600 // 过期时长(秒)
  18. type UserRoom struct {
  19. roomMgr IRoomMgr.RoomMgr
  20. userId int // 用户Id
  21. roomId int // 在线房间
  22. joinList []pb.UserRoomInfo // 加入列表(房间id)
  23. attentionList []int // 关注列表(房间id)
  24. browseList []int // 浏览列表(房间id)
  25. timeStamp int // 时间戳(用于清理过期数据)
  26. contributeList []*pb.UserContribute // 用户贡献
  27. taskList map[int][]*pb.UserRoomTask // 房间任务
  28. lock *sync.RWMutex
  29. }
  30. func NewUserRoom(userId int, mgr IRoomMgr.RoomMgr) *UserRoom {
  31. u := new(UserRoom)
  32. u.userId = userId
  33. u.roomMgr = mgr
  34. u.lock = &sync.RWMutex{}
  35. u.taskList = make(map[int][]*pb.UserRoomTask)
  36. u.timeStamp = common.GetTimeStamp() + user_Seconds
  37. log.Debug("room.newUserRoom userId=%d", userId)
  38. go func() {
  39. // 获取加入房间列表
  40. u.loadJoinList()
  41. // 获取浏览房间列表
  42. u.loadBrowseList()
  43. // 获取关注房间列表
  44. u.loadAttentionList()
  45. // 获取贡献列表
  46. u.loadContribute()
  47. }()
  48. return u
  49. }
  50. // 判断是否过期
  51. func (this *UserRoom) IsExpire() bool {
  52. if this.timeStamp >= common.GetTimeStamp() {
  53. //log.Debug("user_room.checkExpire userId=%d 有效期时间 %s", this.userId, time.Unix(int64(this.timeStamp), 0).Format(common.Layout))
  54. return false
  55. }
  56. // 判断是否在房间内
  57. if this.GetOnlineRoom() > 0 {
  58. //log.Debug("user_room.checkExpire userId=%d 已失效,房间(roomId=%d)内活动,有效期时间 %s",
  59. // this.userId, this.roomId, time.Unix(int64(this.timeStamp), 0).Format(common.Layout))
  60. return false
  61. }
  62. log.Debug("user_room.checkExpire userId=%d 过期用户清理", this.userId)
  63. return true
  64. }
  65. // 更新时间戳
  66. func (this *UserRoom) UpdateTimeStamp() {
  67. this.timeStamp = common.GetTimeStamp() + user_Seconds
  68. return
  69. }
  70. // 获取在线房间
  71. func (this *UserRoom) GetOnlineRoom() int {
  72. return this.roomId
  73. }
  74. // 设置在线房间
  75. func (this *UserRoom) SetOnlineRoom(roomId int) {
  76. this.roomId = roomId
  77. return
  78. }
  79. // 加载加入列表
  80. func (this *UserRoom) loadJoinList() {
  81. // 数据库取数据
  82. this.joinList = database.GetUserJoin(this.userId)
  83. }
  84. // 加载浏览房间列表
  85. func (this *UserRoom) loadBrowseList() {
  86. // 数据库取数据
  87. this.browseList = database.GetUserBrowse(this.userId)
  88. }
  89. // 加载关注房间列表
  90. func (this *UserRoom) loadAttentionList() {
  91. // 数据库取数据
  92. this.attentionList = database.GetUserAttention(this.userId)
  93. }
  94. // 获取加入房间列表
  95. func (this *UserRoom) GetJoinList() []int {
  96. var list []int
  97. for _, v := range this.joinList {
  98. list = append(list, v.RoomId)
  99. }
  100. return list
  101. }
  102. // 获取房间信息
  103. func (this *UserRoom) UserRoomInfo(roomId int) *pb.UserRoomInfo {
  104. for i, v := range this.joinList {
  105. if v.RoomId == roomId {
  106. return &this.joinList[i]
  107. }
  108. }
  109. return nil
  110. }
  111. // 获取关注房间列表
  112. func (this *UserRoom) GetAttentionList() []int {
  113. return this.attentionList
  114. }
  115. // 获取浏览房间列表
  116. func (this *UserRoom) GetBrowseList() []int {
  117. return this.browseList
  118. }
  119. // 加入
  120. func (this *UserRoom) AddJoin(roomId, roleId int, isFree bool, ipAddress string) int {
  121. // 判断是否已加入
  122. for _, v := range this.joinList {
  123. if v.RoomId == roomId {
  124. return 13
  125. }
  126. }
  127. // 不是免费,需要扣除钻石
  128. if !isFree {
  129. room := this.roomMgr.GetRoomInfo(roomId)
  130. if room == nil {
  131. log.Debug("user_room.addJoin roomId=%d roleId=%d isFree=%v ipAddress=%s room is not exist",
  132. roomId, roleId, isFree, ipAddress)
  133. return 11
  134. }
  135. // 有会费
  136. if room.JoinFee > 0 {
  137. // 上交入会费
  138. ret := money.ReduceChip(this.userId, room.JoinFee, common.LOGTYPE_AUDIOROOM_JOIN_DEL, "语音房", "会费扣减钻石", ipAddress)
  139. if ret != 1 {
  140. return 12
  141. }
  142. cfg := config.Mgr.GetRoomConfig()
  143. // 计算会费收益
  144. send := int(float64(room.JoinFee) * (1.00 - float64(cfg.JoinFee.Tax)/100.00))
  145. money.GiveChip(room.UserId, send, common.LOGTYPE_AUDIOROOM_JOIN_SEND, "语音房", "收益加钻石", ipAddress)
  146. // 收集点数
  147. go this.roomMgr.AddCollect(this.userId, roomId, room.JoinFee)
  148. }
  149. }
  150. info := pb.UserRoomInfo{
  151. UserId: this.userId,
  152. RoomId: roomId,
  153. RoleId: roleId,
  154. Level: 1,
  155. Exps: 0,
  156. Crdate: common.GetNowTimeStr(),
  157. }
  158. this.joinList = append(this.joinList, info)
  159. // DB: 存入数据库
  160. go database.SaveUserRoom(this.userId, info.RoomId, info.RoleId, info.Crdate)
  161. // 删除关注
  162. this.DelAttention(info.RoomId)
  163. return 1
  164. }
  165. // 取消加入
  166. func (this *UserRoom) DelJoin(roomId int) int {
  167. log.Debug("user_room userId=%d roomId=%d", this.userId, roomId)
  168. for i := 0; i < len(this.joinList); i++ {
  169. if this.joinList[i].RoomId != roomId {
  170. continue
  171. }
  172. // 删除
  173. this.joinList = append(this.joinList[:i], this.joinList[i+1:]...)
  174. // 删除任务
  175. this.DelTask(roomId)
  176. // 删除贡献
  177. this.DelContribute(roomId)
  178. // DB: 存入数据库
  179. go func(userId, roomId int) {
  180. // 取消加入
  181. database.DelUserRoom(userId, roomId)
  182. // 添加关注
  183. this.AddAttention(roomId)
  184. }(this.userId, roomId)
  185. return 1
  186. }
  187. return 11
  188. }
  189. // 关注
  190. func (this *UserRoom) AddAttention(roomId int) int {
  191. // 判断是否已关注
  192. for _, v := range this.attentionList {
  193. if v == roomId {
  194. return 11
  195. }
  196. }
  197. this.attentionList = append(this.attentionList, roomId)
  198. // DB: 存入数据库
  199. buf, _ := json.Marshal(this.attentionList)
  200. go database.SaveUserAttention(this.userId, string(buf))
  201. return 1
  202. }
  203. // 取消关注
  204. func (this *UserRoom) DelAttention(roomId int) int {
  205. for i := 0; i < len(this.attentionList); i++ {
  206. if this.attentionList[i] != roomId {
  207. continue
  208. }
  209. // 删除
  210. this.attentionList = append(this.attentionList[:i], this.attentionList[i+1:]...)
  211. // DB: 存入数据库
  212. buf, _ := json.Marshal(this.attentionList)
  213. go database.SaveUserAttention(this.userId, string(buf))
  214. break
  215. }
  216. return 1
  217. }
  218. // 是否关注房间
  219. func (this *UserRoom) IsAttention(roomId int) int {
  220. for _, v := range this.attentionList {
  221. if v == roomId {
  222. return 1
  223. }
  224. }
  225. return 11
  226. }
  227. // 加入浏览列表
  228. func (this *UserRoom) AddBrowse(roomId int) {
  229. // log.Debug("user_room.addBrowse userId=%d roomId=%d", this.userId, roomId)
  230. if len(this.browseList) >= 50 {
  231. this.browseList = this.browseList[1:]
  232. }
  233. for i := 0; i < len(this.browseList); i++ {
  234. if this.browseList[i] != roomId {
  235. continue
  236. }
  237. // 删除
  238. this.browseList = append(this.browseList[:i], this.browseList[i+1:]...)
  239. break
  240. }
  241. this.browseList = append(this.browseList, roomId)
  242. // DB:写入数据库
  243. buf, _ := json.Marshal(this.browseList)
  244. go database.SaveUserBrowse(this.userId, string(buf))
  245. return
  246. }
  247. // redis key
  248. func (this *UserRoom) getRedisKey(userId int, code string) string {
  249. return fmt.Sprintf("%s:join:%d:%s", "audioroom", userId, code)
  250. }
  251. // 发送邀请加入
  252. func (this *UserRoom) InviteJoin(toUserId, roomId int) int {
  253. code := strconv.Itoa(common.GetTimeStamp())
  254. key := this.getRedisKey(toUserId, code)
  255. value, _ := json.Marshal(struct {
  256. RoomId int
  257. RoleId int
  258. }{
  259. RoomId: roomId,
  260. RoleId: pb.Role_Member,
  261. })
  262. redis.String_SetEx(key, string(value), user_Seconds)
  263. // 发送邀请通知
  264. go this.inviteJoinNotify(toUserId, roomId, code)
  265. return 1
  266. }
  267. // 接受邀请加入
  268. func (this *UserRoom) AcceptJoin(code, ipAddress string) (int, int) {
  269. key := this.getRedisKey(this.userId, code)
  270. value, ok := redis.String_Get(key)
  271. if !ok {
  272. return -1, 0
  273. }
  274. var v struct {
  275. RoomId int
  276. RoleId int
  277. }
  278. err := json.Unmarshal([]byte(value), &v)
  279. if err != nil {
  280. log.Error("acceptJoin json unmarshal userId=%d code=%s err ==> %v", this.userId, code, err)
  281. return -2, 0
  282. }
  283. // 加入语音房
  284. if retCode := this.AddJoin(v.RoomId, v.RoleId, true, ipAddress); retCode != 1 {
  285. return -3, 0
  286. }
  287. return v.RoomId, v.RoleId
  288. }
  289. // 打印用户
  290. func (this *UserRoom) DumpUser() {
  291. log.Debug("用户[%d]信息,在线房间[%d]过期时间[%s]:", this.userId, this.roomId, time.Unix(int64(this.timeStamp), 0).Format(common.Layout))
  292. log.Debug("加入列表打印(%d)个:", len(this.joinList))
  293. for _, info := range this.joinList {
  294. log.Debug(" %+v", info)
  295. }
  296. log.Debug("++++++++++++++++++++++++++++++++++++++++++++++")
  297. log.Debug("关注列表打印(%d)个:", len(this.attentionList))
  298. for _, info := range this.attentionList {
  299. log.Debug(" %+v", info)
  300. }
  301. log.Debug("**********************************************")
  302. log.Debug("浏览列表打印(%d)个:", len(this.browseList))
  303. for _, info := range this.browseList {
  304. log.Debug(" %+v", info)
  305. }
  306. log.Debug("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  307. log.Debug("用户贡献列表打印(%d)个:", len(this.contributeList))
  308. for _, info := range this.contributeList {
  309. log.Debug(" %+v", info)
  310. }
  311. log.Debug("################################################")
  312. }