room_game.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package room
  2. import (
  3. "bet24.com/servers/micros/audioroom/handler/game"
  4. pb "bet24.com/servers/micros/audioroom/proto"
  5. )
  6. // 创建游戏房间
  7. func (this *Room) CreateGameRoom(userId, gameId int, ruleName string) (retMsg pb.RetMsg, gameRoomInfo *pb.GameRoomInfo) {
  8. // 判断是否有游戏权限
  9. if !this.IsGamePermission(userId, gameId) {
  10. retMsg.RetCode, retMsg.Message = 11, "No permission to create rooms"
  11. return
  12. }
  13. // 创建房间,如果返回nil表示创建失败
  14. _, gameRoomInfo = game.CreateGameRoom(this.RoomId, gameId, ruleName)
  15. //判断是否成功
  16. if gameRoomInfo == nil {
  17. retMsg.RetCode, retMsg.Message = 13, "Failed to create room"
  18. return
  19. }
  20. // 添加房间
  21. this.gameRoomList = append(this.gameRoomList, gameRoomInfo)
  22. // 给所有用户发送通知
  23. go this.notify(pb.Notify_Action_Refresh_Game, pb.ReasonData{
  24. Reason: pb.Notify_Reason_Game_CreateRoom,
  25. GameId: gameId,
  26. RuleName: ruleName,
  27. })
  28. // 创建房间成功
  29. retMsg.RetCode, retMsg.Message = 1, "Create room successfully"
  30. return
  31. }
  32. // 关闭游戏房间
  33. func (this *Room) CloseGameRoom(userId, gameId, roomNo int) (retMsg pb.RetMsg) {
  34. // 判断是否有游戏权限
  35. if !this.IsGamePermission(userId, gameId) {
  36. retMsg.RetCode, retMsg.Message = 11, "No permission to close the room"
  37. return
  38. }
  39. for i := 0; i < len(this.gameRoomList); i++ {
  40. if this.gameRoomList[i].RoomNo != roomNo {
  41. continue
  42. }
  43. // 关闭房间
  44. ok := game.CloseGameRoom(this.RoomId, roomNo)
  45. if !ok {
  46. retMsg.RetCode, retMsg.Message = 12, "Failed to close room"
  47. return
  48. }
  49. // 删除
  50. this.gameRoomList = append(this.gameRoomList[:i], this.gameRoomList[i+1:]...)
  51. // 关闭房间成功
  52. retMsg.RetCode, retMsg.Message = 1, "Close the room successfully"
  53. break
  54. }
  55. return
  56. }
  57. // 房间结束处理
  58. func (this *Room) OnRoomEnd(roomNo int) {
  59. for i := 0; i < len(this.gameRoomList); i++ {
  60. if this.gameRoomList[i].RoomNo != roomNo {
  61. continue
  62. }
  63. // 删除
  64. this.gameRoomList = append(this.gameRoomList[:i], this.gameRoomList[i+1:]...)
  65. break
  66. }
  67. return
  68. }
  69. // 获取游戏房间列表
  70. func (this *Room) GetGameRoomList(gameId int) []*pb.GameRoomInfo {
  71. var list []*pb.GameRoomInfo
  72. for _, v := range this.gameRoomList {
  73. if gameId > 0 && v.GameId != gameId {
  74. continue
  75. }
  76. list = append(list, v)
  77. }
  78. return list
  79. }
  80. // 获取强弹游戏房间
  81. func (this *Room) StrongBombGame() (gameId int, ruleName string) {
  82. if len(this.gameRoomList) <= 0 {
  83. return
  84. }
  85. gameId = this.gameRoomList[0].GameId
  86. ruleName = this.gameRoomList[0].RuleName
  87. return
  88. }