PlazaRoomExtension.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 = 50;
  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<int, User> onReceiveExpression;
  20. public Action<string, User> onReceivePublicMessage;
  21. public Action<User, SFSObject> onSynchronize;
  22. public Action<User, SFSObject> onInstantiate;
  23. public BaseSFSExtension BaseSFSExtension;
  24. public Room CurrentPlazaRoom;
  25. #endregion
  26. public PlazaRoomExtension(BaseSFSExtension baseSFSExtension)
  27. {
  28. baseSFSExtension.Instances.Add(this);
  29. BaseSFSExtension = baseSFSExtension;
  30. }
  31. public void ExitPlazaRoom()
  32. {
  33. BaseSFSExtension.GardenSmartFox.SmartFox.Send(new LeaveRoomRequest(CurrentPlazaRoom));
  34. if (onExitPlazaRoom != null)
  35. onExitPlazaRoom.Invoke();
  36. }
  37. public void JoinPlazaRoom()
  38. {
  39. List<Room> rooms = BaseSFSExtension.GardenSmartFox.SmartFox.GetRoomListFromGroup(PlazaRoomGroupID);
  40. foreach (var room in rooms)
  41. {
  42. if (room.UserCount < room.Capacity)
  43. {
  44. BaseSFSExtension.GardenSmartFox.SmartFox.Send(new JoinRoomRequest(room));
  45. return;
  46. }
  47. }
  48. CreateAndJoinPlazaRoom(rooms.Count);
  49. }
  50. private void CreateAndJoinPlazaRoom(int index)
  51. {
  52. string roomName = BasePlazaRoomName + index;
  53. RoomSettings roomSettings = new RoomSettings(roomName);
  54. roomSettings.GroupId = PlazaRoomGroupID;
  55. roomSettings.MaxUsers = PlazaRoomCapacity;
  56. BaseSFSExtension.GardenSmartFox.SmartFox.Send(new CreateRoomRequest(roomSettings, true));
  57. }
  58. public void SendSynchronizeDestination(Vector3 destination)
  59. {
  60. SFSObject sfsObject = new SFSObject();
  61. sfsObject.PutInt(DataID.SenderID.GetHashString(), BaseSFSExtension.GardenSmartFox.User.Id);
  62. sfsObject.PutUtfString(DataID.Destination.GetHashString(), destination.VectorToString());
  63. SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.Synchronize.GetHashCode(), sfsObject, -1, BaseSFSExtension.GardenSmartFox.SFSPlazaRoomManager.CurrentPlazaRoom.Id);
  64. BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.BatchAndOverride, RequestName.SynchronizePosition.ToString(), BatchOption.TryNot);
  65. }
  66. public void SendSynchronizeClose(List<int> closeIDs)
  67. {
  68. SFSObject sfsObject = new SFSObject();
  69. sfsObject.PutInt(DataID.SenderID.GetHashString(), BaseSFSExtension.GardenSmartFox.User.Id);
  70. sfsObject.PutIntArray(DataID.Close.GetHashString(), closeIDs.ToArray());
  71. SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.Synchronize.GetHashCode(), sfsObject, -1, BaseSFSExtension.GardenSmartFox.SFSPlazaRoomManager.CurrentPlazaRoom.Id);
  72. BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.BatchAndOverride, RequestName.SynchronizeClose.ToString());
  73. }
  74. public void SendExpression(ExpressionID expressionID)
  75. {
  76. SFSObject sfsObject = new SFSObject();
  77. sfsObject.PutInt(DataID.SenderID.GetHashString(), BaseSFSExtension.GardenSmartFox.User.Id);
  78. sfsObject.PutInt(DataID.Expression.GetHashString(), expressionID.GetHashCode());
  79. SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.ReceiveExpression.GetHashCode(), sfsObject, -1, BaseSFSExtension.GardenSmartFox.SFSPlazaRoomManager.CurrentPlazaRoom.Id);
  80. BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.Immediate);
  81. }
  82. public void SendPublicMessage(string message)
  83. {
  84. SFSObject sfsObject = new SFSObject();
  85. sfsObject.PutInt(DataID.SenderID.GetHashString(), BaseSFSExtension.GardenSmartFox.User.Id);
  86. sfsObject.PutUtfString(DataID.Message.GetHashString(), message);
  87. SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.ReceivePublicMessage.GetHashCode(), sfsObject, -1, BaseSFSExtension.GardenSmartFox.SFSPlazaRoomManager.CurrentPlazaRoom.Id);
  88. BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.Immediate);
  89. }
  90. public void Instantiate(BaseEvent baseEvent)
  91. {
  92. //DebugManager.PrintKeysAndValuesOfBaseEvent("Instantiate", baseEvent);
  93. SFSObject parameter = (SFSObject)baseEvent.Params["params"];
  94. int senderID = parameter.GetInt(DataID.SenderID.GetHashString());
  95. User sender = CurrentPlazaRoom.GetUserById(senderID);
  96. if (onInstantiate != null)
  97. onInstantiate.Invoke(sender, parameter);
  98. }
  99. public void Synchronize(BaseEvent baseEvent)
  100. {
  101. //DebugManager.PrintKeysAndValuesOfBaseEvent("Synchronize", baseEvent);
  102. SFSObject parameter = (SFSObject)baseEvent.Params["params"];
  103. int senderID = parameter.GetInt(DataID.SenderID.GetHashString());
  104. User sender = CurrentPlazaRoom.GetUserById(senderID);
  105. if (onSynchronize != null)
  106. onSynchronize.Invoke(sender, parameter);
  107. }
  108. public void ReceiveExpression(BaseEvent baseEvent)
  109. {
  110. //DebugManager.PrintKeysAndValuesOfBaseEvent("ReceivePlazaRoomPublicMessage", baseEvent);
  111. SFSObject parameter = (SFSObject)baseEvent.Params["params"];
  112. int senderID = parameter.GetInt(DataID.SenderID.GetHashString());
  113. int expressionID = parameter.GetInt(DataID.Expression.GetHashString());
  114. User sender = CurrentPlazaRoom.GetUserById(senderID);
  115. if (onReceiveExpression != null)
  116. onReceiveExpression.Invoke(expressionID, sender);
  117. }
  118. public void ReceivePublicMessage(BaseEvent baseEvent)
  119. {
  120. //DebugManager.PrintKeysAndValuesOfBaseEvent("ReceivePlazaRoomPublicMessage", baseEvent);
  121. SFSObject parameter = (SFSObject)baseEvent.Params["params"];
  122. int senderID = parameter.GetInt(DataID.SenderID.GetHashString());
  123. string message = parameter.GetUtfString(DataID.Message.GetHashString());
  124. User sender = CurrentPlazaRoom.GetUserById(senderID);
  125. if (onReceivePublicMessage != null)
  126. onReceivePublicMessage.Invoke(message, sender);
  127. }
  128. public void OnJoinPlazaRoom(Room room)
  129. {
  130. //DebugManager.PrintKeysAndValuesOfBaseEvent("OnJoinPlazaRoom", baseEvent);
  131. Debug.LogWarning("Join plaza room : " + room.Name);
  132. CurrentPlazaRoom = room;
  133. if (onJoinPlazaRoom != null)
  134. onJoinPlazaRoom.Invoke(room);
  135. }
  136. public void OnUserExitPlazaRoom(Room room, User user)
  137. {
  138. if (room.Id == CurrentPlazaRoom.Id)
  139. {
  140. OnUserExitCurrentPlazaRoom(user);
  141. }
  142. }
  143. public void OnUserExitCurrentPlazaRoom(User user)
  144. {
  145. Debug.LogWarning($"User : {user.Name} left room");
  146. if (onUserExitCurrentPlazaRoom != null)
  147. onUserExitCurrentPlazaRoom.Invoke(user);
  148. }
  149. public void OnUserEnterPlazaRoom(Room room, User user)
  150. {
  151. if (room.Id == CurrentPlazaRoom.Id)
  152. {
  153. OnUserEnterCurrentPlazaRoom(user);
  154. }
  155. }
  156. public void OnUserEnterCurrentPlazaRoom(User user)
  157. {
  158. Debug.LogWarning($"User : {user.Name} joined room");
  159. if (onUserEnterCurrentPlazaRoom != null)
  160. onUserEnterCurrentPlazaRoom.Invoke(user);
  161. }
  162. }