roommgr.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. package manager
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/common"
  5. "bet24.com/servers/micros/audioroom/handler/config"
  6. "bet24.com/servers/micros/audioroom/handler/game"
  7. "bet24.com/servers/micros/audioroom/handler/income"
  8. "bet24.com/servers/micros/audioroom/handler/room"
  9. "bet24.com/servers/micros/audioroom/handler/user"
  10. pb "bet24.com/servers/micros/audioroom/proto"
  11. "bet24.com/servers/micros/audioroom/transaction/database"
  12. item "bet24.com/servers/micros/item_inventory/proto"
  13. notification "bet24.com/servers/micros/notification/proto"
  14. task "bet24.com/servers/micros/task/proto"
  15. userservices "bet24.com/servers/micros/userservices/proto"
  16. "encoding/json"
  17. "sort"
  18. "strconv"
  19. "sync"
  20. "time"
  21. )
  22. const (
  23. errorCode_user = 101
  24. errorCode_room = 102
  25. )
  26. var mgr *roommgr
  27. type roommgr struct {
  28. room_list map[int]*room.Room // 房间列表
  29. roomLock *sync.RWMutex // 房间锁
  30. user_list map[int]*user.UserRoom // 用户列表
  31. userLock *sync.RWMutex // 用户锁
  32. preview_list map[int]string // 预览房间图片
  33. }
  34. func getRoomManager() *roommgr {
  35. if mgr == nil {
  36. mgr = new(roommgr)
  37. mgr.room_list = make(map[int]*room.Room)
  38. mgr.roomLock = &sync.RWMutex{}
  39. mgr.user_list = make(map[int]*user.UserRoom)
  40. mgr.userLock = &sync.RWMutex{}
  41. mgr.preview_list = make(map[int]string)
  42. mgr.doCheck()
  43. game.SubscribeRoomEnd(mgr.onRoomEnd)
  44. game.SubscribeRoomInfo(mgr.GetRoomInfo)
  45. income.SubscribeRoomInfo(mgr.GetRoomInfo)
  46. }
  47. return mgr
  48. }
  49. // 轮询
  50. func (this *roommgr) doCheck() {
  51. ticker := time.NewTicker(60 * time.Second)
  52. go func(t *time.Ticker) {
  53. for {
  54. select {
  55. case <-t.C:
  56. go this.checkRoomExpire()
  57. go this.checkUserExpire()
  58. go this.refreshRoomFromDB()
  59. }
  60. }
  61. }(ticker)
  62. }
  63. // 检查过期房间
  64. func (this *roommgr) checkRoomExpire() {
  65. this.roomLock.Lock()
  66. defer this.roomLock.Unlock()
  67. // 遍历所有房间
  68. for _, v := range this.room_list {
  69. // 判断是否过期
  70. if !v.IsExpire() {
  71. continue
  72. }
  73. // 删除
  74. delete(this.room_list, v.RoomId)
  75. log.Debug("roommgr.checkRoomExpire roomId=%d 已完成清理", v.RoomId)
  76. }
  77. return
  78. }
  79. // 检查过期用户
  80. func (this *roommgr) checkUserExpire() {
  81. this.userLock.Lock()
  82. defer this.userLock.Unlock()
  83. // 遍历所有用户
  84. for uid, v := range this.user_list {
  85. // 判断是否过期
  86. if !v.IsExpire() {
  87. continue
  88. }
  89. // 删除
  90. delete(this.user_list, uid)
  91. log.Debug("roommgr.checkUserExpire userId=%d 已完成清理", uid)
  92. }
  93. return
  94. }
  95. // 拉房间数据
  96. func (this *roommgr) refreshRoomFromDB() {
  97. previousIds := make(map[int]bool)
  98. for _, id := range config.Mgr.TopRooms {
  99. info := this.GetRoomInfo(id)
  100. if info == nil {
  101. continue
  102. }
  103. this.roomLock.Lock()
  104. info.TopFlag = true
  105. this.roomLock.Unlock()
  106. previousIds[id] = true
  107. }
  108. if len(this.room_list) > config.Mgr.MemoryRooms {
  109. return
  110. }
  111. // 直接从数据库查询
  112. rooms := database.RecommendRoom()
  113. for _, id := range rooms {
  114. this.GetRoomInfo(id)
  115. }
  116. log.Debug("roomgr.refreshRoomFromDB 当前在线 %d 个房间", len(this.room_list))
  117. if len(previousIds) == 0 {
  118. return
  119. }
  120. this.roomLock.Lock()
  121. for roomId, info := range this.room_list {
  122. if !previousIds[roomId] {
  123. info.TopFlag = false
  124. }
  125. }
  126. this.roomLock.Unlock()
  127. return
  128. }
  129. // 获取房间
  130. func (this *roommgr) getRoom(roomId int, isFromDB bool) *room.Room {
  131. if roomId <= 0 {
  132. log.Release("roommgr.getRoom roomId=%d is invalid", roomId)
  133. return nil
  134. }
  135. this.roomLock.RLock()
  136. r, ok := this.room_list[roomId]
  137. this.roomLock.RUnlock()
  138. if ok {
  139. r.UpdateTimeStamp()
  140. return r
  141. }
  142. // 不需要从数据库取
  143. if !isFromDB {
  144. return nil
  145. }
  146. //log.Debug("roommgr.getRoom.newRoom 执行前 roomId=%d ts=%d", roomId, common.GetTimeStamp())
  147. r = room.NewRoom(roomId, this)
  148. //log.Debug("roommgr.getRoom.newRoom 执行完 roomId=%d ts=%d", roomId, common.GetTimeStamp())
  149. // 无效房间
  150. if !r.IsValid() {
  151. return nil
  152. }
  153. this.roomLock.Lock()
  154. this.room_list[roomId] = r
  155. this.roomLock.Unlock()
  156. return r
  157. }
  158. // 获取用户信息
  159. func (this *roommgr) getUser(userId int, isFromDB bool) *user.UserRoom {
  160. this.userLock.RLock()
  161. u, ok := this.user_list[userId]
  162. this.userLock.RUnlock()
  163. if ok {
  164. u.UpdateTimeStamp()
  165. return u
  166. }
  167. // 不需要从数据库获取
  168. if !isFromDB {
  169. return nil
  170. }
  171. // 创建一个新用户房间数据
  172. u = user.NewUserRoom(userId, this)
  173. this.userLock.Lock()
  174. this.user_list[userId] = u
  175. this.userLock.Unlock()
  176. return u
  177. }
  178. // 根据房间名称查找房间
  179. func (this *roommgr) searchRoom(roomName string) []pb.RoomInfo {
  180. return database.SearchRoom(roomName)
  181. }
  182. // 获取房间基本信息
  183. func (this *roommgr) GetRoomInfo(roomId int) *pb.RoomInfo {
  184. r := this.getRoom(roomId, true)
  185. if r == nil {
  186. //log.Debug("roommgr.getRoomInfo roomId=%d not exist", roomId)
  187. return nil
  188. }
  189. return &r.RoomInfo
  190. }
  191. // 创建房间
  192. func (this *roommgr) createRoom(userId int, roomName, country, ipAddress, roomImg, announce, tag string) *pb.RoomInfo {
  193. u := this.getUser(userId, true)
  194. if u == nil {
  195. log.Debug("roommgr.createRoom userId=%d not exist", userId)
  196. return nil
  197. }
  198. r := this.getRoom(userId, true)
  199. if r != nil {
  200. log.Debug("roommgr.createRoom is exist userId=%d roomId=%d roomName=%s country=%s ipAddress=%s",
  201. userId, userId, roomName, country, ipAddress)
  202. return nil
  203. }
  204. // 获取默认语言
  205. language := config.Mgr.GetDefaultLanguage()
  206. // 新创建一个
  207. r = room.NewRoom(userId, this)
  208. // 创建房间
  209. r.CreateRoom(userId, roomName, country, language, ipAddress, roomImg, announce, tag)
  210. // 加入房间列表进行管理
  211. this.roomLock.Lock()
  212. this.room_list[r.RoomId] = r
  213. delete(this.preview_list, userId)
  214. this.roomLock.Unlock()
  215. // 加入关注
  216. u.AddJoin(r.RoomId, pb.Role_Administrator, true, ipAddress)
  217. return &r.RoomInfo
  218. }
  219. // 是否允许进入房间
  220. func (this *roommgr) isEnterRoom(userId, roomId int, password string) int {
  221. // 房间信息
  222. r := this.getRoom(roomId, true)
  223. if r == nil {
  224. log.Debug("roommgr.isEnterRoom userId=%d roomId=%d not exist", userId, roomId)
  225. return errorCode_room
  226. }
  227. return r.IsEnterRoom(userId, password)
  228. }
  229. // 进入房间
  230. func (this *roommgr) enterRoom(userId, roomId int) *pb.RoomInfo {
  231. // 用户信息
  232. u := this.getUser(userId, true)
  233. if u == nil {
  234. log.Debug("roommgr.enterRoom userId=%d not exist", userId)
  235. return nil
  236. }
  237. // 房间信息
  238. r := this.getRoom(roomId, true)
  239. if r == nil {
  240. log.Debug("roommgr.enterRoom userId=%d roomId=%d not exist", userId, roomId)
  241. return nil
  242. }
  243. // 进入房间
  244. if ok := r.EnterRoom(userId); !ok {
  245. return nil
  246. }
  247. // 用户房间处理
  248. go func() {
  249. // 设置用户在线房间ID
  250. u.SetOnlineRoom(roomId)
  251. // 记录浏览历史
  252. u.AddBrowse(roomId)
  253. }()
  254. // 任务:进入语聊房
  255. task.DoTaskAction(userId, task.TaskAction_enter_room, 1, task.TaskScope{})
  256. return &r.RoomInfo
  257. }
  258. // 退出房间
  259. func (this *roommgr) exitRoom(userId, roomId int) int {
  260. r := this.getRoom(roomId, false)
  261. if r == nil {
  262. log.Debug("roommgr.exitRoom userId=%d roomId=%d not exist", userId, roomId)
  263. return 1
  264. }
  265. // 退出房间
  266. r.ExitRoom(userId)
  267. u := this.getUser(userId, false)
  268. if u == nil {
  269. log.Debug("roommgr.exitRoom userId=%d not exist", userId)
  270. return 1
  271. }
  272. // 设置在线房间空闲
  273. u.SetOnlineRoom(0)
  274. return 1
  275. }
  276. // 修改房间信息
  277. func (this *roommgr) updateRoom(roomId int, rInfo *pb.RoomInfo) int {
  278. r := this.getRoom(rInfo.RoomId, true)
  279. if r == nil {
  280. log.Error("roommgr.updateRoom userId=%d roomInfo=%+v", roomId, r)
  281. return errorCode_room
  282. }
  283. return r.UpdateRoom(roomId, rInfo)
  284. }
  285. // 获取房间封面预览
  286. func (this *roommgr) getRoomImg(roomId int) string {
  287. this.roomLock.RLock()
  288. defer this.roomLock.RUnlock()
  289. if roomImg, ok := this.preview_list[roomId]; ok {
  290. return roomImg
  291. }
  292. return ""
  293. }
  294. // 修改房间封面
  295. func (this *roommgr) updateRoomImg(userId, roomId int, roomImg string) {
  296. r := this.getRoom(roomId, true)
  297. if r == nil {
  298. // log.Error("roommgr.updateRoomImg userId=%d roomInfo=%+v", roomId, r)
  299. this.roomLock.Lock()
  300. this.preview_list[roomId] = roomImg
  301. this.roomLock.Unlock()
  302. // 发送通知
  303. buf, _ := json.Marshal(notification.NotificationAudioRoom{
  304. NotifyId: pb.Notify_Action_Refresh_RoomImg,
  305. RoomId: roomId,
  306. Data: roomImg,
  307. })
  308. log.Debug("UpdateRoomImg userId=%d roomId=%d roomImg=%s ==> %s", userId, roomId, roomImg, string(buf))
  309. go notification.AddNotification(userId, notification.Notification_AudioRoom, string(buf))
  310. return
  311. }
  312. r.UpdateRoomImg(roomImg)
  313. }
  314. // 登录服务器
  315. func (this *roommgr) loginServer(userId int) (bool, *pb.RoomInfo) {
  316. u := this.getUser(userId, true)
  317. if u == nil {
  318. log.Debug("roommgr.loginServer userId=%d not exist", userId)
  319. return false, nil
  320. }
  321. // 房间信息
  322. r := this.getRoom(userId, true)
  323. if r == nil {
  324. // log.Debug("roommgr.loginServer userId=%d roomId=%d not exist", userId, userId)
  325. return true, nil
  326. }
  327. return true, r.GetRoomInfo()
  328. }
  329. // 退出服务器
  330. func (this *roommgr) logoutServer(userId int) {
  331. u := this.getUser(userId, false)
  332. if u == nil {
  333. log.Debug("roommgr.logoutServer userId=%d not exist", userId)
  334. return
  335. }
  336. this.userLock.Lock()
  337. defer this.userLock.Unlock()
  338. delete(this.user_list, userId)
  339. }
  340. // 获取用户在线房间
  341. func (this *roommgr) getOnlineRoom(userId int) int {
  342. u := this.getUser(userId, false)
  343. if u == nil {
  344. log.Debug("roommgr.getOnlineRoom userId=%d not exist", userId)
  345. return errorCode_user
  346. }
  347. return u.GetOnlineRoom()
  348. }
  349. // 获取探索列表
  350. func (this *roommgr) getExploreList() []*pb.RoomInfo {
  351. this.roomLock.RLock()
  352. defer this.roomLock.RUnlock()
  353. var list []*pb.RoomInfo
  354. for k := range this.room_list {
  355. v := this.room_list[k]
  356. list = append(list, &v.RoomInfo)
  357. }
  358. // 排序规则优先级:在线人数 --> 经验值;
  359. sort.SliceStable(list, func(i, j int) bool {
  360. if list[i].OnlineCount > list[j].OnlineCount {
  361. return true
  362. } else if list[i].OnlineCount < list[j].OnlineCount {
  363. return false
  364. }
  365. return list[i].Exps > list[j].Exps
  366. })
  367. // 排序规则优先级:置顶房间
  368. sort.SliceStable(list, func(i, j int) bool {
  369. return list[i].TopFlag
  370. })
  371. return list
  372. }
  373. // 获取加入房间列表
  374. func (this *roommgr) getJoinList(userId int) []*pb.RoomInfo {
  375. u := this.getUser(userId, true)
  376. if u == nil {
  377. log.Debug("roommgr.getJoinList userId=%d not exist", userId)
  378. return nil
  379. }
  380. join_List := u.GetJoinList()
  381. return this.getRoomList(userId, join_List)
  382. }
  383. // 获取关注房间列表
  384. func (this *roommgr) getAttentionList(userId int) []*pb.RoomInfo {
  385. u := this.getUser(userId, true)
  386. if u == nil {
  387. log.Debug("roommgr.getAttentionList userId=%d not exist", userId)
  388. return nil
  389. }
  390. attention_List := u.GetAttentionList()
  391. return this.getRoomList(userId, attention_List)
  392. }
  393. // 获取浏览房间列表
  394. func (this *roommgr) getBrowseList(userId int) []*pb.RoomInfo {
  395. u := this.getUser(userId, true)
  396. if u == nil {
  397. log.Debug("roommgr.getBrowseList userId=%d not exist", userId)
  398. return nil
  399. }
  400. browse_list := u.GetBrowseList()
  401. return this.getRoomList(userId, browse_list)
  402. }
  403. // 获取房间列表信息
  404. func (this *roommgr) getRoomList(userId int, list []int) []*pb.RoomInfo {
  405. var ret []*pb.RoomInfo
  406. for _, roomId := range list {
  407. // 剔除自己创建的房间
  408. if userId == roomId {
  409. continue
  410. }
  411. r := this.getRoom(roomId, true)
  412. if r == nil {
  413. log.Debug("roommgr.getRoomList roomId=%d not exist", roomId)
  414. continue
  415. }
  416. // 房间在线人数为空,不显示在列表中
  417. //if r.OnlineCount <= 0 {
  418. // continue
  419. //}
  420. ret = append(ret, r.GetRoomInfo())
  421. }
  422. // 排序规则优先级:在线人数 --> 经验值;
  423. sort.SliceStable(ret, func(i, j int) bool {
  424. if ret[i].OnlineCount > ret[j].OnlineCount {
  425. return true
  426. } else if ret[i].OnlineCount < ret[j].OnlineCount {
  427. return false
  428. }
  429. return ret[i].Exps > ret[j].Exps
  430. })
  431. // 排序规则优先级:置顶房间
  432. sort.SliceStable(ret, func(i, j int) bool {
  433. return ret[i].TopFlag
  434. })
  435. return ret
  436. }
  437. // 加入(扣费)
  438. func (this *roommgr) addJoin(userId, roomId, roleId int, ipAddress string) int {
  439. u := this.getUser(userId, true)
  440. if u == nil {
  441. log.Debug("roommgr.addJoin userId=%d not exist", userId)
  442. return errorCode_user
  443. }
  444. retCode := u.AddJoin(roomId, roleId, false, ipAddress)
  445. if retCode != 1 {
  446. return retCode
  447. }
  448. r := this.getRoom(roomId, true)
  449. if r == nil {
  450. log.Debug("roommgr.addJoin roomId=%d not exist", roomId)
  451. return errorCode_room
  452. }
  453. return r.AddJoin(userId, roleId)
  454. }
  455. // 取消加入
  456. func (this *roommgr) delJoin(userId, roomId int) int {
  457. r := this.getRoom(roomId, true)
  458. if r == nil {
  459. log.Debug("roommgr.delJoin roomId=%d not exist", roomId)
  460. return errorCode_room
  461. }
  462. retCode := r.DelJoin(userId)
  463. if retCode != 1 {
  464. return retCode
  465. }
  466. u := this.getUser(userId, true)
  467. if u == nil {
  468. log.Debug("roommgr.delJoin userId=%d not exist", userId)
  469. return errorCode_user
  470. }
  471. u.DelJoin(roomId)
  472. return retCode
  473. }
  474. // 关注
  475. func (this *roommgr) addAttention(userId, roomId int) int {
  476. u := this.getUser(userId, true)
  477. if u == nil {
  478. log.Debug("roommgr.addAttention userId=%d not exist", userId)
  479. return errorCode_user
  480. }
  481. return u.AddAttention(roomId)
  482. }
  483. // 取消关注
  484. func (this *roommgr) delAttention(userId, roomId int) int {
  485. u := this.getUser(userId, true)
  486. if u == nil {
  487. log.Debug("roommgr.delAttention userId=%d not exist", userId)
  488. return errorCode_user
  489. }
  490. return u.DelAttention(roomId)
  491. }
  492. // 发出成员邀请加入
  493. func (this *roommgr) inviteJoin(userId, toUserId, roomId int) int {
  494. u := this.getUser(userId, true)
  495. if u == nil {
  496. log.Debug("roommgr.inviteJoin userId=%d not exist", userId)
  497. return errorCode_user
  498. }
  499. return u.InviteJoin(toUserId, roomId)
  500. }
  501. // 接受成员邀请
  502. func (this *roommgr) acceptJoin(userId int, code, ipAddress string) int {
  503. u := this.getUser(userId, true)
  504. if u == nil {
  505. log.Debug("roommgr.acceptJoin userId=%d not exist", userId)
  506. return errorCode_user
  507. }
  508. roomId, roleId := u.AcceptJoin(code, ipAddress)
  509. if roomId <= 0 {
  510. return 11
  511. }
  512. r := this.getRoom(roomId, true)
  513. if r == nil {
  514. log.Debug("roommgr.acceptJoin roomId=%d not exist", roomId)
  515. return errorCode_room
  516. }
  517. return r.AddJoin(userId, roleId)
  518. }
  519. // 在线用户列表
  520. func (this *roommgr) GetOnlineUsers(roomId int) []pb.UserInfo {
  521. var list []pb.UserInfo
  522. r := this.getRoom(roomId, true)
  523. if r == nil {
  524. log.Debug("roommgr.getOnlineUsers roomId=%d not exist", roomId)
  525. return nil
  526. }
  527. users := r.GetOnlineUsers()
  528. for _, uid := range users {
  529. var userInfo pb.UserInfo
  530. userInfo.UserHotInfo = userservices.GetUserInfo(uid)
  531. // 是否麦位黑名单
  532. if r.IsBlack(uid, pb.BlackType_Mic) {
  533. userInfo.IsMicBlack = true
  534. }
  535. // 成员
  536. member := r.GetMemberInfo(uid)
  537. // 角色ID
  538. userInfo.RoleId = member.RoleId
  539. userInfo.Level = member.Level
  540. list = append(list, userInfo)
  541. }
  542. return list
  543. }
  544. // 查询房间在线用户信息
  545. func (this *roommgr) searchUser(userId, toUserId, roomId int) pb.UserInfo {
  546. var ret pb.UserInfo
  547. r := this.getRoom(roomId, false)
  548. if r == nil {
  549. log.Debug("roommgr.searchUser roomId=%d not exist", roomId)
  550. return ret
  551. }
  552. s := r.SearchUser(toUserId)
  553. ret.UserHotInfo = userservices.GetUserInfo(toUserId)
  554. ret.RoleId = s.RoleId
  555. ret.Level = s.Level
  556. // 判断是否麦位黑名单
  557. if r.IsBlack(toUserId, pb.BlackType_Mic) {
  558. ret.IsMicBlack = true
  559. }
  560. // 判断是否在线
  561. if u := this.getUser(toUserId, false); u != nil {
  562. if u.GetOnlineRoom() == roomId {
  563. ret.IsOnline = true
  564. }
  565. }
  566. return ret
  567. }
  568. // 成员列表
  569. func (this *roommgr) getMembers(roomId int) []pb.UserInfo {
  570. var list []pb.UserInfo
  571. r := this.getRoom(roomId, true)
  572. if r == nil {
  573. log.Debug("roommgr.getMembers roomId=%d not exist", roomId)
  574. return nil
  575. }
  576. members := r.GetMembers()
  577. for _, member := range members {
  578. var userInfo pb.UserInfo
  579. userInfo.UserHotInfo = userservices.GetUserInfo(member.UserId)
  580. // 是否麦位黑名单
  581. if r.IsBlack(member.UserId, pb.BlackType_Mic) {
  582. userInfo.IsMicBlack = true
  583. }
  584. // 角色ID
  585. userInfo.RoleId = member.RoleId
  586. userInfo.Level = member.Level
  587. userInfo.Exps = member.Exps
  588. list = append(list, userInfo)
  589. }
  590. sort.SliceStable(list, func(i, j int) bool {
  591. return list[i].Exps > list[j].Exps
  592. })
  593. return list
  594. }
  595. // 修改会员等级(只改缓存数据)
  596. func (this *roommgr) UpdateMemberLevel(userId, roomId, level, exps int) {
  597. r := this.getRoom(roomId, false)
  598. if r == nil {
  599. log.Debug("roommgr.updateMemberLevel roomId=%d not exist", roomId)
  600. return
  601. }
  602. r.UpdateMemberLevel(userId, level, exps)
  603. return
  604. }
  605. // 推荐用户
  606. func (this *roommgr) recommendUser() []*userservices.UserHotInfo {
  607. var list []*userservices.UserHotInfo
  608. // 直接从数据库查询
  609. users := database.RecommendUser()
  610. for _, id := range users {
  611. u := userservices.GetUserInfo(id)
  612. list = append(list, u)
  613. }
  614. return list
  615. }
  616. // 推荐房间
  617. func (this *roommgr) recommendRoom() []*pb.RoomInfo {
  618. var list []*pb.RoomInfo
  619. // 直接从数据库查询
  620. rooms := database.RecommendRoom()
  621. for _, id := range rooms {
  622. info := this.GetRoomInfo(id)
  623. list = append(list, info)
  624. }
  625. return list
  626. }
  627. // 根据标签获取房间列表
  628. func (this *roommgr) roomListByTag(tag string) []*pb.RoomInfo {
  629. var list []*pb.RoomInfo
  630. this.roomLock.RLock()
  631. defer this.roomLock.RUnlock()
  632. for k := range this.room_list {
  633. v := this.room_list[k]
  634. if v.Tag != tag {
  635. continue
  636. }
  637. list = append(list, &v.RoomInfo)
  638. }
  639. return list
  640. }
  641. // 上麦
  642. func (this *roommgr) onTheMic(userId, roomId, sn int) int {
  643. log.Debug("roommgr.onTheMic userId=%d roomId=%d sn=%d", userId, roomId, sn)
  644. u := this.getUser(userId, true)
  645. if u == nil {
  646. log.Debug("roommgr.onTheMic userId=%d not exist", userId)
  647. return errorCode_user
  648. }
  649. r := this.getRoom(roomId, true)
  650. if r == nil {
  651. log.Debug("roommgr.onTheMic roomId=%d not exist", roomId)
  652. return errorCode_room
  653. }
  654. return r.OnTheMic(userId, sn)
  655. }
  656. // 邀请上麦
  657. func (this *roommgr) inviteOnMic(userId, roomId, toUserId int) int {
  658. u := this.getUser(userId, true)
  659. if u == nil {
  660. log.Debug("roommgr.inviteOnMic userId=%d not exist", userId)
  661. return errorCode_user
  662. }
  663. r := this.getRoom(roomId, true)
  664. if r == nil {
  665. log.Debug("roommgr.inviteOnMic roomId=%d not exist", roomId)
  666. return errorCode_room
  667. }
  668. return r.InviteOnMic(userId, toUserId)
  669. }
  670. // 踢麦(强制下麦)
  671. func (this *roommgr) kickMic(userId, roomId, toUserId int) int {
  672. u := this.getUser(userId, true)
  673. if u == nil {
  674. log.Debug("roommgr.kickMic userId=%d not exist", userId)
  675. return errorCode_user
  676. }
  677. r := this.getRoom(roomId, true)
  678. if r == nil {
  679. log.Debug("roommgr.kickMic roomId=%d not exist", roomId)
  680. return errorCode_room
  681. }
  682. return r.KickMic(userId, toUserId)
  683. }
  684. // 上麦回调通知
  685. func (this *roommgr) onTheMicNotify(userId int, roomId int, streamId string) int {
  686. log.Debug("roommgr.onTheMicNotify userId=%d roomId=%d streamId=%s", userId, roomId, streamId)
  687. u := this.getUser(userId, true)
  688. if u == nil {
  689. log.Debug("roommgr.onTheMicNotify userId=%d not exist", userId)
  690. return errorCode_user
  691. }
  692. r := this.getRoom(roomId, true)
  693. if r == nil {
  694. log.Debug("roommgr.onTheMic_callback roomId=%d not exist", roomId)
  695. return errorCode_room
  696. }
  697. return r.OnTheMicNotify(userId, streamId)
  698. }
  699. // 下麦回调通知
  700. func (this *roommgr) offTheMicNotify(userId, roomId int, streamId string) int {
  701. log.Debug("roommgr.offTheMic userId=%d roomId=%d streamId=%s", userId, roomId, streamId)
  702. u := this.getUser(userId, true)
  703. if u == nil {
  704. log.Debug("roommgr.offTheMic userId=%d not exist", userId)
  705. return errorCode_user
  706. }
  707. r := this.getRoom(roomId, true)
  708. if r == nil {
  709. log.Debug("roommgr.offTheMic roomId=%d not exist", roomId)
  710. return errorCode_room
  711. }
  712. return r.OffTheMicNotify(userId, streamId)
  713. }
  714. // 加解锁麦(status -1=上锁 0=解锁)
  715. func (this *roommgr) setMic(userId, roomId, sn, status int) int {
  716. u := this.getUser(userId, true)
  717. if u == nil {
  718. log.Debug("roommgr.setMic userId=%d not exist", userId)
  719. return errorCode_user
  720. }
  721. r := this.getRoom(roomId, true)
  722. if r == nil {
  723. log.Debug("roommgr.setMic roomId=%d not exist", roomId)
  724. return errorCode_room
  725. }
  726. return r.SetMic(userId, sn, status)
  727. }
  728. // 获取麦列表
  729. func (this *roommgr) getMicList(userId, roomId int) []pb.UserInfo {
  730. r := this.getRoom(roomId, true)
  731. if r == nil {
  732. log.Debug("roommgr.getMicList roomId=%d not exist", roomId)
  733. return nil
  734. }
  735. var list []pb.UserInfo
  736. micList := r.GetMicList()
  737. for _, uid := range micList {
  738. var userInfo pb.UserInfo
  739. // 用户不存在
  740. if uid <= 0 {
  741. userInfo.UserHotInfo = &userservices.UserHotInfo{UserId: uid}
  742. list = append(list, userInfo)
  743. continue
  744. }
  745. userInfo.UserHotInfo = userservices.GetUserInfo(uid)
  746. // 是否麦位黑名单
  747. if r.IsBlack(uid, pb.BlackType_Mic) {
  748. userInfo.IsMicBlack = true
  749. }
  750. // 成员
  751. member := r.GetMemberInfo(uid)
  752. // 角色ID
  753. userInfo.RoleId = member.RoleId
  754. userInfo.Level = member.Level
  755. list = append(list, userInfo)
  756. }
  757. return list
  758. }
  759. // 获取权限
  760. func (this *roommgr) getPermission(userId, roomId int) []pb.PermissionInfo {
  761. u := this.getUser(userId, true)
  762. if u == nil {
  763. log.Debug("roommgr.getPermission userId=%d not exist", userId)
  764. return nil
  765. }
  766. r := this.getRoom(roomId, true)
  767. if r == nil {
  768. log.Debug("roommgr.getPermission roomId=%d not exist", roomId)
  769. return nil
  770. }
  771. return r.GetPermissionList()
  772. }
  773. // 设置权限
  774. func (this *roommgr) setPermission(userId, roomId, permissionType, enabled int) int {
  775. u := this.getUser(userId, true)
  776. if u == nil {
  777. log.Debug("roommgr.setPermission userId=%d not exist", userId)
  778. return errorCode_user
  779. }
  780. r := this.getRoom(roomId, true)
  781. if r == nil {
  782. log.Debug("roommgr.setPermission roomId=%d not exist", roomId)
  783. return errorCode_room
  784. }
  785. return r.SetPermission(permissionType, enabled)
  786. }
  787. // 获取进入房间条件
  788. func (this *roommgr) getEnterCondition(userId, roomId int) pb.EnterCondition {
  789. u := this.getUser(userId, true)
  790. if u == nil {
  791. log.Debug("roommgr.getEnterCondition userId=%d not exist", userId)
  792. return pb.EnterCondition{}
  793. }
  794. r := this.getRoom(roomId, true)
  795. if r == nil {
  796. log.Debug("roommgr.getEnterCondition roomId=%d not exist", roomId)
  797. return pb.EnterCondition{}
  798. }
  799. return r.GetEnterCondition(userId)
  800. }
  801. // 设置进入房间条件
  802. func (this *roommgr) setEnterCondition(userId, roomId int, inviteOnly bool, password string, isHide bool) int {
  803. u := this.getUser(userId, true)
  804. if u == nil {
  805. log.Debug("roommgr.setEnterCondition userId=%d not exist", userId)
  806. return errorCode_user
  807. }
  808. r := this.getRoom(roomId, true)
  809. if r == nil {
  810. log.Debug("roommgr.setEnterCondition roomId=%d not exist", roomId)
  811. return errorCode_room
  812. }
  813. return r.SetEnterCondition(userId, inviteOnly, password, config.Mgr.PwdLockSeconds, isHide)
  814. }
  815. // 获取黑名单用户列表
  816. func (this *roommgr) getBlackList(userId, roomId, blackType int) []pb.BlackUser {
  817. u := this.getUser(userId, true)
  818. if u == nil {
  819. log.Debug("roommgr.getBlackList userId=%d not exist", userId)
  820. return nil
  821. }
  822. r := this.getRoom(roomId, true)
  823. if r == nil {
  824. log.Debug("roommgr.getBlackList roomId=%d not exist", roomId)
  825. return nil
  826. }
  827. var list []pb.BlackUser
  828. for _, v := range r.GetBlackList() {
  829. for _, b := range v.BlackTypes {
  830. if b.BlackType != blackType {
  831. continue
  832. }
  833. // 计算剩余时间
  834. stillSeconds := b.ExpireTimeStamp - common.GetTimeStamp()
  835. if stillSeconds <= 0 {
  836. continue
  837. }
  838. // 无效用户
  839. u := userservices.GetUserInfo(v.UserId)
  840. if u == nil {
  841. continue
  842. }
  843. // 追加数据
  844. list = append(list, pb.BlackUser{
  845. UserHotInfo: u,
  846. StillSeconds: stillSeconds,
  847. })
  848. }
  849. }
  850. return list
  851. }
  852. // 设置黑名单
  853. func (this *roommgr) addBlack(userId, roomId, toUserId, blackType, seconds int) int {
  854. u := this.getUser(userId, true)
  855. if u == nil {
  856. log.Debug("roommgr.addBlack userId=%d not exist", userId)
  857. return errorCode_user
  858. }
  859. r := this.getRoom(roomId, true)
  860. if r == nil {
  861. log.Debug("roommgr.addBlack roomId=%d not exist", roomId)
  862. return errorCode_room
  863. }
  864. retCode := r.AddBlack(userId, toUserId, blackType, seconds)
  865. if retCode == 1 && blackType == pb.BlackType_Room {
  866. this.delJoin(toUserId, r.RoomId)
  867. }
  868. return retCode
  869. }
  870. // 移除黑名单
  871. func (this *roommgr) removeBlack(userId, roomId, toUserId, blackType int) int {
  872. u := this.getUser(userId, true)
  873. if u == nil {
  874. log.Debug("roommgr.removeBlack userId=%d not exist", userId)
  875. return errorCode_user
  876. }
  877. r := this.getRoom(roomId, true)
  878. if r == nil {
  879. log.Debug("roommgr.removeBlack roomId=%d not exist", roomId)
  880. return errorCode_room
  881. }
  882. retCode := r.RemoveBlack(userId, toUserId, blackType)
  883. log.Debug("roommgr.removeBlack userId=%d roomId=%d toUserId=%d blackType=%d retCode=%d",
  884. userId, roomId, toUserId, blackType, retCode)
  885. return retCode
  886. }
  887. // 操作记录
  888. func (this *roommgr) getOperateLog(userId, roomId, toUserId, operateType, pageIndex, pageSize int) (int, []pb.OperateInfo) {
  889. r := this.getRoom(roomId, false)
  890. if r == nil {
  891. log.Debug("roommgr.getOperateLog roomId=%d not exist", roomId)
  892. return 0, nil
  893. }
  894. var list []pb.OperateInfo
  895. recordCount, logList := r.GetOperateLog(toUserId, operateType, pageIndex, pageSize)
  896. for _, v := range logList {
  897. var info pb.OperateInfo
  898. info.UserId = v.UserId
  899. info.UserBaseInfo = userservices.GetUserInfo(v.UserId)
  900. info.ToUserId = v.ToUserId
  901. info.ToUserBaseInfo = userservices.GetUserInfo(v.ToUserId)
  902. info.OperateType = v.OperateType
  903. info.Crdate = v.Crdate
  904. list = append(list, info)
  905. }
  906. return recordCount, list
  907. }
  908. // 添加管理员
  909. func (this *roommgr) addAdmin(userId, roomId, toUserId int) int {
  910. u := this.getUser(userId, true)
  911. if u == nil {
  912. log.Debug("roommgr.addAdmin userId=%d not exist", userId)
  913. return errorCode_user
  914. }
  915. r := this.getRoom(roomId, true)
  916. if r == nil {
  917. log.Debug("roommgr.addAdmin roomId=%d not exist", roomId)
  918. return errorCode_room
  919. }
  920. return r.AddAdmin(userId, toUserId)
  921. }
  922. // 取消管理员
  923. func (this *roommgr) delAdmin(userId, roomId, toUserId int) int {
  924. toU := this.getUser(toUserId, true)
  925. if toU == nil {
  926. log.Debug("roommgr.delAdmin userId=%d not exist", toUserId)
  927. return errorCode_user
  928. }
  929. r := this.getRoom(roomId, true)
  930. if r == nil {
  931. log.Debug("roommgr.delAdmin roomId=%d not exist", roomId)
  932. return errorCode_room
  933. }
  934. // 取消房间管理员
  935. retCode := r.DelAdmin(userId, toUserId)
  936. // 取消管理员为游客
  937. if retCode == 1 {
  938. // 取消加入
  939. go toU.DelJoin(roomId)
  940. }
  941. return retCode
  942. }
  943. // 删除会员
  944. func (this *roommgr) delMember(userId, roomId, toUserId int) int {
  945. toU := this.getUser(toUserId, true)
  946. if toU == nil {
  947. log.Debug("roommgr.delMember userId=%d not exist", toUserId)
  948. return errorCode_user
  949. }
  950. r := this.getRoom(roomId, true)
  951. if r == nil {
  952. log.Debug("roommgr.delMember roomId=%d not exist", roomId)
  953. return errorCode_room
  954. }
  955. // 删除会员
  956. retCode := r.DelMember(userId, toUserId, false)
  957. log.Debug("roommgr.delMember userId=%d roomId=%d toUserId=%d retCode=%d", userId, roomId, toUserId, retCode)
  958. // 取消管理员为游客
  959. if retCode == 1 {
  960. // 取消加入
  961. go toU.DelJoin(roomId)
  962. }
  963. return retCode
  964. }
  965. // 是否有关注
  966. func (this *roommgr) isAttention(userId, roomId int) int {
  967. u := this.getUser(userId, true)
  968. if u == nil {
  969. log.Debug("roommgr.isAttention userId=%d not exist", userId)
  970. return -1
  971. }
  972. return u.IsAttention(roomId)
  973. }
  974. // 发送礼物
  975. func (this *roommgr) sendGiving(userId, roomId, toUserId, giftId, num int) (retCode int, message string) {
  976. u := this.getUser(userId, true)
  977. if u == nil {
  978. log.Debug("roommgr.sendGiving userId=%d not exist", userId)
  979. return errorCode_user, "user is not exist"
  980. }
  981. r := this.getRoom(roomId, true)
  982. if r == nil {
  983. log.Debug("roommgr.SendGiving roomId=%d not exist", roomId)
  984. return errorCode_room, "room is not exist"
  985. }
  986. return u.SendGiving(roomId, r.UserId, toUserId, giftId, num)
  987. }
  988. // 获取房间任务列表
  989. func (this *roommgr) getTaskList(userId, roomId int) []*pb.RoomTask {
  990. r := this.getRoom(roomId, true)
  991. if r == nil {
  992. log.Debug("roommgr.getTaskList roomId=%d not exist", roomId)
  993. return nil
  994. }
  995. return r.GetTaskList()
  996. }
  997. // 触发房间任务
  998. func (this *roommgr) DoRoomTaskAction(userId, roomId, action, num int) {
  999. u := this.getUser(userId, true)
  1000. if u == nil {
  1001. log.Debug("roommgr.doTaskAction userId=%d not exist", userId)
  1002. return
  1003. }
  1004. r := this.getRoom(roomId, true)
  1005. if r == nil {
  1006. log.Debug("roommgr.doTaskAction roomId=%d not exist", roomId)
  1007. return
  1008. }
  1009. // 获取用户贡献余额
  1010. ok, stillExps := u.GetContribute(roomId, action)
  1011. // 判断是否允许贡献
  1012. if !ok {
  1013. return
  1014. }
  1015. // 触发房间任务
  1016. ok, exps := r.DoTaskAction(userId, action, num, stillExps)
  1017. if !ok {
  1018. return
  1019. }
  1020. // 添加用户贡献
  1021. u.AddContribute(roomId, action, exps)
  1022. }
  1023. // 添加收集点数
  1024. func (this *roommgr) AddCollect(userId, roomId, point int) {
  1025. u := this.getUser(userId, true)
  1026. if u == nil {
  1027. log.Debug("roommgr.addCollect userId=%d not exist", userId)
  1028. return
  1029. }
  1030. r := this.getRoom(roomId, true)
  1031. if r == nil {
  1032. log.Debug("roommgr.addCollect roomId=%d not exist", roomId)
  1033. return
  1034. }
  1035. r.AddCollect(point)
  1036. return
  1037. }
  1038. // 获取昨天收集信息
  1039. func (this *roommgr) getCollect(userId, roomId int) *pb.RoomCollect {
  1040. u := this.getUser(userId, true)
  1041. if u == nil {
  1042. log.Debug("roommgr.getCollect userId=%d not exist", userId)
  1043. return nil
  1044. }
  1045. r := this.getRoom(roomId, true)
  1046. if r == nil {
  1047. log.Debug("roommgr.getCollect roomId=%d not exist", roomId)
  1048. return nil
  1049. }
  1050. return r.GetCollect()
  1051. }
  1052. // 领取收集奖励
  1053. func (this *roommgr) giftCollect(userId, roomId int) (retCode int, message string, items []item.ItemPack) {
  1054. u := this.getUser(userId, true)
  1055. if u == nil {
  1056. log.Debug("roommgr.giftCollect userId=%d not exist", userId)
  1057. return
  1058. }
  1059. r := this.getRoom(roomId, true)
  1060. if r == nil {
  1061. log.Debug("roommgr.giftCollect roomId=%d not exist", roomId)
  1062. return
  1063. }
  1064. return r.GiftCollect(userId)
  1065. }
  1066. // 获取扩展信息
  1067. func (this *roommgr) getRoomExtInfo(roomId int) *pb.RoomExtInfo {
  1068. r := this.getRoom(roomId, true)
  1069. if r == nil {
  1070. log.Debug("roommgr.giftCollect roomId=%d not exist", roomId)
  1071. return nil
  1072. }
  1073. return r.GetExtInfo()
  1074. }
  1075. // 获取用户房间任务列表
  1076. func (this *roommgr) getUserTaskList(userId, roomId int) []*pb.UserRoomTask {
  1077. u := this.getUser(userId, true)
  1078. if u == nil {
  1079. log.Debug("roommgr.getUserTaskList userId=%d not exist", userId)
  1080. return nil
  1081. }
  1082. r := this.getRoom(roomId, true)
  1083. if r == nil {
  1084. log.Debug("roommgr.getUserTaskList roomId=%d not exist", roomId)
  1085. return nil
  1086. }
  1087. return u.GetTaskList(r.RoomId)
  1088. }
  1089. // 用户分享
  1090. func (this *roommgr) share(userId, roomId int) (retCode int, errorMsg string) {
  1091. retCode, errorMsg = 1, "success"
  1092. // 触发用户任务
  1093. go this.DoUserTaskAction(userId, roomId, pb.UserTask_Action_ShareRoom, 1, 0)
  1094. return
  1095. }
  1096. // 房间内发送信息
  1097. func (this *roommgr) sendMessage(userId, roomId int) (retCode int, errorMsg string) {
  1098. retCode, errorMsg = 1, "success"
  1099. // 触发用户任务
  1100. go this.DoUserTaskAction(userId, roomId, pb.UserTask_Action_SendMessage, 1, 0)
  1101. // 触发大厅任务
  1102. go task.DoTaskAction(userId, task.TaskAction_roomChat, 1, task.TaskScope{})
  1103. return
  1104. }
  1105. // 获取用户房间信息
  1106. func (this *roommgr) getUserRoomInfo(userId, roomId int) *pb.UserRoomInfo {
  1107. u := this.getUser(userId, true)
  1108. if u == nil {
  1109. log.Debug("roommgr.getUserRoomInfo userId=%d not exist", userId)
  1110. return nil
  1111. }
  1112. r := this.getRoom(roomId, true)
  1113. if r == nil {
  1114. log.Debug("roommgr.getUserRoomInfo roomId=%d not exist", roomId)
  1115. return nil
  1116. }
  1117. return u.UserRoomInfo(r.RoomId)
  1118. }
  1119. // 触发用户房间任务
  1120. func (this *roommgr) DoUserTaskAction(userId, roomId, action, num, ext int) {
  1121. u := this.getUser(userId, true)
  1122. if u == nil {
  1123. log.Debug("roommgr.doUserTaskAction userId=%d not exist", userId)
  1124. return
  1125. }
  1126. r := this.getRoom(roomId, true)
  1127. if r == nil {
  1128. log.Debug("roommgr.doUserTaskAction roomId=%d not exist", roomId)
  1129. return
  1130. }
  1131. // 触发用户房间任务
  1132. u.DoTaskAction(r.RoomId, action, num, ext)
  1133. }
  1134. // 获取用户任务统计
  1135. func (this *roommgr) getUserRoomTaskStat(userId int) []pb.UserRoomTaskStat {
  1136. u := this.getUser(userId, true)
  1137. if u == nil {
  1138. log.Debug("roommgr.getUserRoomTaskStat userId=%d not exist", userId)
  1139. return nil
  1140. }
  1141. r := this.getRoom(userId, true)
  1142. if r == nil {
  1143. log.Debug("roommgr.getUserRoomTaskStat roomId=%d not exist", userId)
  1144. return nil
  1145. }
  1146. return r.GetUserRoomTaskStat(userId)
  1147. }
  1148. // 通知房间
  1149. func (this *roommgr) notifyRoom(userId int) {
  1150. r := this.getRoom(userId, false)
  1151. if r == nil {
  1152. log.Debug("roommgr.NotifyRoom roomId=%d not exist", userId)
  1153. return
  1154. }
  1155. r.NotifyRoom()
  1156. }
  1157. // 设置屏幕锁(1=锁定,0=解锁)
  1158. func (this *roommgr) setScreenLock(userId, roomId, screenLock int) int {
  1159. r := this.getRoom(roomId, true)
  1160. if r == nil {
  1161. log.Debug("roommgr.SetScreenLock roomId=%d not exist", roomId)
  1162. return errorCode_room
  1163. }
  1164. return r.SetScreenLock(userId, screenLock)
  1165. }
  1166. // 是否禁止发言(true:禁止)
  1167. func (this *roommgr) isBannedSpeak(userId, roomId int) bool {
  1168. r := this.getRoom(roomId, true)
  1169. if r == nil {
  1170. log.Debug("roommgr.SetScreenLock roomId=%d not exist", roomId)
  1171. return true
  1172. }
  1173. return r.IsBannedSpeak(userId)
  1174. }
  1175. // 麦位申请列表
  1176. func (this *roommgr) getOnMicApplyList(roomId int) []pb.UserInfo {
  1177. var list []pb.UserInfo
  1178. r := this.getRoom(roomId, true)
  1179. if r == nil {
  1180. log.Debug("roommgr.GetOnMicApplyList roomId=%d not exist", roomId)
  1181. return list
  1182. }
  1183. users := r.GetOnMicApplyList()
  1184. for _, uid := range users {
  1185. var userInfo pb.UserInfo
  1186. userInfo.UserHotInfo = userservices.GetUserInfo(uid)
  1187. // 是否麦位黑名单
  1188. if r.IsBlack(uid, pb.BlackType_Mic) {
  1189. userInfo.IsMicBlack = true
  1190. }
  1191. // 成员
  1192. member := r.GetMemberInfo(uid)
  1193. // 角色ID
  1194. userInfo.RoleId = member.RoleId
  1195. userInfo.Level = member.Level
  1196. list = append(list, userInfo)
  1197. }
  1198. return list
  1199. }
  1200. // 申请上麦
  1201. func (this *roommgr) applyOnMic(userId, roomId int) pb.RetMsg {
  1202. var retMsg pb.RetMsg
  1203. r := this.getRoom(roomId, true)
  1204. if r == nil {
  1205. log.Debug("roommgr.ApplyOnMic roomId=%d not exist", roomId)
  1206. retMsg.RetCode = errorCode_room
  1207. return retMsg
  1208. }
  1209. return r.ApplyOnMic(userId)
  1210. }
  1211. // 取消申请上麦
  1212. func (this *roommgr) cancelApplyOnMic(userId, roomId int) pb.RetMsg {
  1213. var retMsg pb.RetMsg
  1214. r := this.getRoom(roomId, true)
  1215. if r == nil {
  1216. log.Debug("roommgr.CancelApplyOnMic roomId=%d not exist", roomId)
  1217. retMsg.RetCode = errorCode_room
  1218. return retMsg
  1219. }
  1220. return r.CancelApplyOnMic(userId)
  1221. }
  1222. // 处理申请上麦(1=同意 2=拒绝)
  1223. func (this *roommgr) dealApplyOnMic(userId, roomId, toUserId, status int) pb.RetMsg {
  1224. var retMsg pb.RetMsg
  1225. r := this.getRoom(roomId, true)
  1226. if r == nil {
  1227. log.Debug("roommgr.DealApplyOnMic roomId=%d not exist", roomId)
  1228. retMsg.RetCode = errorCode_room
  1229. return retMsg
  1230. }
  1231. return r.DealApplyOnMic(userId, toUserId, status)
  1232. }
  1233. // 创建游戏房间
  1234. func (this *roommgr) createGameRoom(userId, roomId, gameId int, ruleName string) (pb.RetMsg, *pb.GameRoomInfo) {
  1235. var retMsg pb.RetMsg
  1236. r := this.getRoom(roomId, false)
  1237. if r == nil {
  1238. log.Debug("roommgr.CreateGameRoom roomId=%d not exist", roomId)
  1239. retMsg.RetCode = errorCode_room
  1240. return retMsg, nil
  1241. }
  1242. return r.CreateGameRoom(userId, gameId, ruleName)
  1243. }
  1244. // 关闭游戏房间
  1245. func (this *roommgr) closeGameRoom(userId, roomId, gameId, roomNo int) pb.RetMsg {
  1246. var retMsg pb.RetMsg
  1247. r := this.getRoom(roomId, false)
  1248. if r == nil {
  1249. log.Debug("roommgr.CloseGameRoom roomId=%d not exist", roomId)
  1250. retMsg.RetCode = errorCode_room
  1251. return retMsg
  1252. }
  1253. return r.CloseGameRoom(userId, gameId, roomNo)
  1254. }
  1255. // 房间结束处理
  1256. func (this *roommgr) onRoomEnd(roomId, roomNo int) {
  1257. r := this.getRoom(roomId, false)
  1258. if r == nil {
  1259. log.Debug("roommgr.onRoomEnd roomId=%d not exist", roomId)
  1260. return
  1261. }
  1262. r.OnRoomEnd(roomNo)
  1263. }
  1264. // 获取游戏列表
  1265. func (this *roommgr) getGameRoomList(roomId, gameId int) []*pb.GameRoomInfo {
  1266. r := this.getRoom(roomId, false)
  1267. if r == nil {
  1268. log.Debug("roommgr.GetGameRoomList roomId=%d not exist", roomId)
  1269. return nil
  1270. }
  1271. return r.GetGameRoomList(gameId)
  1272. }
  1273. // 是否有游戏权限
  1274. func (this *roommgr) isGamePermission(userId, roomId, gameId int) bool {
  1275. r := this.getRoom(roomId, false)
  1276. if r == nil {
  1277. log.Debug("roommgr.IsPermission roomId=%d not exist", roomId)
  1278. return false
  1279. }
  1280. return r.IsGamePermission(userId, gameId)
  1281. }
  1282. // 清理房间麦位数据
  1283. func (this *roommgr) clearRoomMic() {
  1284. for _, v := range this.room_list {
  1285. v.AddOffMicLog(0, "")
  1286. }
  1287. return
  1288. }
  1289. // 打印房间
  1290. func (this *roommgr) dumpRoom(param1 string) {
  1291. roomId := 0
  1292. if param1 != "" {
  1293. roomId, _ = strconv.Atoi(param1)
  1294. }
  1295. log.Debug(" ^_^ 开始打印房间数据,房间(%d)个", len(this.room_list))
  1296. this.roomLock.RLock()
  1297. for k, v := range this.room_list {
  1298. if roomId > 0 && k != roomId {
  1299. continue
  1300. }
  1301. v.DumpRoom()
  1302. }
  1303. this.roomLock.RUnlock()
  1304. log.Debug("完成房间数据打印 ^_^")
  1305. }
  1306. // 打印用户
  1307. func (this *roommgr) dumpUser(param1 string) {
  1308. userId := 0
  1309. if param1 != "" {
  1310. userId, _ = strconv.Atoi(param1)
  1311. }
  1312. log.Debug(" ^_^ 开始打印用户数据,用户(%d)人", len(this.user_list))
  1313. this.userLock.RLock()
  1314. for uid, v := range this.user_list {
  1315. if userId > 0 && userId != uid {
  1316. continue
  1317. }
  1318. v.DumpUser()
  1319. }
  1320. this.userLock.RUnlock()
  1321. log.Debug("完成用户数据打印 ^_^")
  1322. }