PlazaRoomExtension.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. using Sfs2X.Core;
  5. using Sfs2X.Entities;
  6. using Sfs2X.Requests;
  7. using Sfs2X.Entities.Data;
  8. public class PlazaRoomExtension
  9. {
  10. #region Variable
  11. public short PlazaRoomCapacity = 10;
  12. public string BasePlazaRoomName = "PlazaRoom";
  13. public string PlazaRoomGroupID = "default";
  14. public Action onExitPlazaRoom;
  15. public Action<Room> onJoinPlazaRoom;
  16. public Action<User> onUserExitCurrentPlazaRoom;
  17. public Action<User> onUserEnterCurrentPlazaRoom;
  18. public BaseSFSExtension BaseSFSExtension;
  19. public Room CurrentPlazaRoom;
  20. #endregion
  21. public PlazaRoomExtension(BaseSFSExtension baseSFSExtension)
  22. {
  23. baseSFSExtension.Instances.Add(this);
  24. BaseSFSExtension = baseSFSExtension;
  25. }
  26. public void ExitPlazaRoom()
  27. {
  28. BaseSFSExtension.GardenSmartFox.SmartFox.Send(new LeaveRoomRequest(CurrentPlazaRoom));
  29. if (onExitPlazaRoom != null)
  30. onExitPlazaRoom.Invoke();
  31. }
  32. public void JoinPlazaRoom()
  33. {
  34. List<Room> rooms = BaseSFSExtension.GardenSmartFox.SmartFox.GetRoomListFromGroup(PlazaRoomGroupID);
  35. foreach (var room in rooms)
  36. {
  37. if (room.UserCount < room.Capacity)
  38. {
  39. BaseSFSExtension.GardenSmartFox.SmartFox.Send(new JoinRoomRequest(room));
  40. return;
  41. }
  42. }
  43. CreateAndJoinPlazaRoom(rooms.Count);
  44. }
  45. private void CreateAndJoinPlazaRoom(int index)
  46. {
  47. //Debug.Log("A");
  48. //Room room = new SFSRoom(999, "123");
  49. //BaseSFSExtension.GardenSmartFox.SmartFox.Send(new JoinRoomRequest(room));
  50. //return;
  51. string roomName = BasePlazaRoomName + index;
  52. RoomSettings roomSettings = new RoomSettings(roomName);
  53. roomSettings.GroupId = PlazaRoomGroupID;
  54. roomSettings.MaxUsers = PlazaRoomCapacity;
  55. roomSettings.MaxVariables = 30;
  56. BaseSFSExtension.GardenSmartFox.SmartFox.Send(new CreateRoomRequest(roomSettings, true));
  57. }
  58. public void SendPublicMessage(string message)
  59. {
  60. SFSObject parameter = new SFSObject();
  61. parameter.PutInt("RoomID", CurrentPlazaRoom.Id);
  62. parameter.PutText("Message", message);
  63. BaseSFSExtension.SendRequest(Request.SendPlazaRoomPublicMessage.GetHashCode(), parameter);
  64. }
  65. public void ReceivePlazaRoomPublicMessage(BaseEvent baseEvent)
  66. {
  67. //DebugManager.PrintKeysAndValuesOfBaseEvent("ReceivePlazaRoomPublicMessage", baseEvent);
  68. SFSObject parameter = (SFSObject) baseEvent.Params["params"];
  69. int senderID = parameter.GetInt("SenderID");
  70. string message = parameter.GetText("Message");
  71. User sender = CurrentPlazaRoom.GetUserById(senderID);
  72. Debug.LogWarning(sender.IsItMe + " : " + message);
  73. }
  74. public void OnJoinPlazaRoom(Room room)
  75. {
  76. //DebugManager.PrintKeysAndValuesOfBaseEvent("OnJoinPlazaRoom", baseEvent);
  77. Debug.LogWarning("Join plaza room : " + room.Name);
  78. CurrentPlazaRoom = room;
  79. if (onJoinPlazaRoom != null)
  80. onJoinPlazaRoom.Invoke(room);
  81. }
  82. public void OnUserExitPlazaRoom(Room room, User user)
  83. {
  84. if (room.Id == CurrentPlazaRoom.Id)
  85. {
  86. OnUserExitCurrentPlazaRoom(user);
  87. }
  88. }
  89. public void OnUserExitCurrentPlazaRoom(User user)
  90. {
  91. Debug.LogWarning($"User : {user.Name} left room");
  92. if (onUserExitCurrentPlazaRoom != null)
  93. onUserExitCurrentPlazaRoom.Invoke(user);
  94. }
  95. public void OnUserEnterPlazaRoom(Room room, User user)
  96. {
  97. if (room.Id == CurrentPlazaRoom.Id)
  98. {
  99. OnUserEnterCurrentPlazaRoom(user);
  100. }
  101. }
  102. public void OnUserEnterCurrentPlazaRoom(User user)
  103. {
  104. Debug.LogWarning($"User : {user.Name} joined room");
  105. if (onUserEnterCurrentPlazaRoom != null)
  106. onUserEnterCurrentPlazaRoom.Invoke(user);
  107. }
  108. }