PlazaRoomExtension.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. using LitJson;
  5. using Sfs2X.Core;
  6. using Sfs2X.Entities;
  7. using Sfs2X.Requests;
  8. using Sfs2X.Entities.Data;
  9. public class PlazaRoomExtension
  10. {
  11. #region Variable
  12. public short PlazaRoomCapacity = 10;
  13. public string BasePlazaRoomName = "PlazaRoom";
  14. public string PlazaRoomGroupID = "default";
  15. public Action onExitPlazaRoom;
  16. public Action<Room> onJoinPlazaRoom;
  17. public Action<User> onUserExitCurrentPlazaRoom;
  18. public Action<User> onUserEnterCurrentPlazaRoom;
  19. public Action<User, SFSObject> onSynchronize;
  20. public Action<string, User> onReceivePublicMessage;
  21. public Action<User, SFSObject> onInstantiate;
  22. public BaseSFSExtension BaseSFSExtension;
  23. public Room CurrentPlazaRoom;
  24. #endregion
  25. public PlazaRoomExtension(BaseSFSExtension baseSFSExtension)
  26. {
  27. baseSFSExtension.Instances.Add(this);
  28. BaseSFSExtension = baseSFSExtension;
  29. }
  30. public void ExitPlazaRoom()
  31. {
  32. BaseSFSExtension.GardenSmartFox.SmartFox.Send(new LeaveRoomRequest(CurrentPlazaRoom));
  33. if (onExitPlazaRoom != null)
  34. onExitPlazaRoom.Invoke();
  35. }
  36. public void JoinPlazaRoom()
  37. {
  38. List<Room> rooms = BaseSFSExtension.GardenSmartFox.SmartFox.GetRoomListFromGroup(PlazaRoomGroupID);
  39. foreach (var room in rooms)
  40. {
  41. if (room.UserCount < room.Capacity)
  42. {
  43. BaseSFSExtension.GardenSmartFox.SmartFox.Send(new JoinRoomRequest(room));
  44. return;
  45. }
  46. }
  47. CreateAndJoinPlazaRoom(rooms.Count);
  48. }
  49. private void CreateAndJoinPlazaRoom(int index)
  50. {
  51. string roomName = BasePlazaRoomName + index;
  52. RoomSettings roomSettings = new RoomSettings(roomName);
  53. roomSettings.GroupId = PlazaRoomGroupID;
  54. roomSettings.MaxUsers = PlazaRoomCapacity;
  55. BaseSFSExtension.GardenSmartFox.SmartFox.Send(new CreateRoomRequest(roomSettings, true));
  56. }
  57. public void SendSynchronizeDestination(Vector3 destination)
  58. {
  59. SFSObject sfsObject = new SFSObject();
  60. sfsObject.PutInt(DataID.SenderID.GetHashString(), BaseSFSExtension.GardenSmartFox.User.Id);
  61. sfsObject.PutUtfString(DataID.Destination.GetHashString(), destination.VectorToString());
  62. SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.Synchronize.GetHashCode(), sfsObject, -1);
  63. BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.BatchAndOverride, RequestName.SynchronizePosition.ToString());
  64. }
  65. public void SendSynchronizeClose(List<int> closeIDs)
  66. {
  67. SFSObject sfsObject = new SFSObject();
  68. sfsObject.PutInt(DataID.SenderID.GetHashString(), BaseSFSExtension.GardenSmartFox.User.Id);
  69. sfsObject.PutIntArray(DataID.Close.GetHashString(), closeIDs.ToArray());
  70. SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.Synchronize.GetHashCode(), sfsObject, -1);
  71. BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.BatchAndOverride, RequestName.SynchronizeClose.ToString());
  72. }
  73. public void SendPublicMessage(string message)
  74. {
  75. SFSObject sfsObject = new SFSObject();
  76. sfsObject.PutInt(DataID.SenderID.GetHashString(), BaseSFSExtension.GardenSmartFox.User.Id);
  77. sfsObject.PutUtfString(DataID.Message.GetHashString(), message);
  78. SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.ReceivePublicMessage.GetHashCode(), sfsObject, -1);
  79. BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.Immediate);
  80. }
  81. public void Instantiate(BaseEvent baseEvent)
  82. {
  83. //DebugManager.PrintKeysAndValuesOfBaseEvent("Instantiate", baseEvent);
  84. SFSObject parameter = (SFSObject)baseEvent.Params["params"];
  85. int senderID = parameter.GetInt(DataID.SenderID.GetHashString());
  86. User sender = CurrentPlazaRoom.GetUserById(senderID);
  87. if (onInstantiate != null)
  88. onInstantiate.Invoke(sender, parameter);
  89. }
  90. public void Synchronize(BaseEvent baseEvent)
  91. {
  92. //DebugManager.PrintKeysAndValuesOfBaseEvent("Synchronize", baseEvent);
  93. SFSObject parameter = (SFSObject)baseEvent.Params["params"];
  94. int senderID = parameter.GetInt(DataID.SenderID.GetHashString());
  95. User sender = CurrentPlazaRoom.GetUserById(senderID);
  96. if (onSynchronize != null)
  97. onSynchronize.Invoke(sender, parameter);
  98. }
  99. public void ReceivePublicMessage(BaseEvent baseEvent)
  100. {
  101. //DebugManager.PrintKeysAndValuesOfBaseEvent("ReceivePlazaRoomPublicMessage", baseEvent);
  102. SFSObject parameter = (SFSObject)baseEvent.Params["params"];
  103. int senderID = parameter.GetInt(DataID.SenderID.GetHashString());
  104. string message = parameter.GetUtfString(DataID.Message.GetHashString());
  105. User sender = CurrentPlazaRoom.GetUserById(senderID);
  106. if (onReceivePublicMessage != null)
  107. onReceivePublicMessage.Invoke(message, sender);
  108. }
  109. public void OnJoinPlazaRoom(Room room)
  110. {
  111. //DebugManager.PrintKeysAndValuesOfBaseEvent("OnJoinPlazaRoom", baseEvent);
  112. Debug.LogWarning("Join plaza room : " + room.Name);
  113. CurrentPlazaRoom = room;
  114. if (onJoinPlazaRoom != null)
  115. onJoinPlazaRoom.Invoke(room);
  116. }
  117. public void OnUserExitPlazaRoom(Room room, User user)
  118. {
  119. if (room.Id == CurrentPlazaRoom.Id)
  120. {
  121. OnUserExitCurrentPlazaRoom(user);
  122. }
  123. }
  124. public void OnUserExitCurrentPlazaRoom(User user)
  125. {
  126. Debug.LogWarning($"User : {user.Name} left room");
  127. if (onUserExitCurrentPlazaRoom != null)
  128. onUserExitCurrentPlazaRoom.Invoke(user);
  129. }
  130. public void OnUserEnterPlazaRoom(Room room, User user)
  131. {
  132. if (room.Id == CurrentPlazaRoom.Id)
  133. {
  134. OnUserEnterCurrentPlazaRoom(user);
  135. }
  136. }
  137. public void OnUserEnterCurrentPlazaRoom(User user)
  138. {
  139. Debug.LogWarning($"User : {user.Name} joined room");
  140. if (onUserEnterCurrentPlazaRoom != null)
  141. onUserEnterCurrentPlazaRoom.Invoke(user);
  142. }
  143. }