PlazaRoomExtension.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 BaseSFSExtension BaseSFSExtension;
  20. public Room CurrentPlazaRoom;
  21. #endregion
  22. public PlazaRoomExtension(BaseSFSExtension baseSFSExtension)
  23. {
  24. baseSFSExtension.Instances.Add(this);
  25. BaseSFSExtension = baseSFSExtension;
  26. }
  27. public void ExitPlazaRoom()
  28. {
  29. BaseSFSExtension.GardenSmartFox.SmartFox.Send(new LeaveRoomRequest(CurrentPlazaRoom));
  30. if (onExitPlazaRoom != null)
  31. onExitPlazaRoom.Invoke();
  32. }
  33. public void JoinPlazaRoom()
  34. {
  35. List<Room> rooms = BaseSFSExtension.GardenSmartFox.SmartFox.GetRoomListFromGroup(PlazaRoomGroupID);
  36. foreach (var room in rooms)
  37. {
  38. if (room.UserCount < room.Capacity)
  39. {
  40. BaseSFSExtension.GardenSmartFox.SmartFox.Send(new JoinRoomRequest(room));
  41. return;
  42. }
  43. }
  44. CreateAndJoinPlazaRoom(rooms.Count);
  45. }
  46. private void CreateAndJoinPlazaRoom(int index)
  47. {
  48. string roomName = BasePlazaRoomName + index;
  49. RoomSettings roomSettings = new RoomSettings(roomName);
  50. roomSettings.GroupId = PlazaRoomGroupID;
  51. roomSettings.MaxUsers = PlazaRoomCapacity;
  52. BaseSFSExtension.GardenSmartFox.SmartFox.Send(new CreateRoomRequest(roomSettings, true));
  53. }
  54. public void SendSynchronize(Vector3 destination)
  55. {
  56. JsonData jsonData = new JsonData();
  57. jsonData[DataID.Destination.GetHashCode().ToString()] = destination.VectorToString();
  58. SFSObject sfsObject = new SFSObject();
  59. sfsObject.PutInt(DataID.SenderID.GetHashCode().ToString(), BaseSFSExtension.GardenSmartFox.User.Id);
  60. sfsObject.PutText(DataID.JsonString.GetHashCode().ToString(), jsonData.ToJson());
  61. SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.Synchronize.GetHashCode(), sfsObject, -1);
  62. BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.BatchAndOverride, CommandID.Synchronize.ToString());
  63. }
  64. public void SendPublicMessage(string message)
  65. {
  66. SFSObject sfsObject = new SFSObject();
  67. sfsObject.PutInt(DataID.SenderID.GetHashCode().ToString(), BaseSFSExtension.GardenSmartFox.User.Id);
  68. sfsObject.PutText(DataID.Message.GetHashCode().ToString(), message);
  69. SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.ReceivePublicMessage.GetHashCode(), sfsObject, -1);
  70. BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.Immediate);
  71. }
  72. public void Synchronize(BaseEvent baseEvent)
  73. {
  74. //DebugManager.PrintKeysAndValuesOfBaseEvent("Synchronize", baseEvent);
  75. SFSObject parameter = (SFSObject)baseEvent.Params["params"];
  76. int senderID = parameter.GetInt(DataID.SenderID.GetHashCode().ToString());
  77. string jsonString = parameter.GetText(DataID.JsonString.GetHashCode().ToString());
  78. User sender = CurrentPlazaRoom.GetUserById(senderID);
  79. JsonData jsonData = JsonMapper.ToObject(jsonString);
  80. BaseSFSExtension.GardenSmartFox.PlazaRoomManager.Synchronize(jsonData, sender);
  81. }
  82. public void ReceivePublicMessage(BaseEvent baseEvent)
  83. {
  84. //DebugManager.PrintKeysAndValuesOfBaseEvent("ReceivePlazaRoomPublicMessage", baseEvent);
  85. SFSObject parameter = (SFSObject)baseEvent.Params["params"];
  86. int senderID = parameter.GetInt(DataID.SenderID.GetHashCode().ToString());
  87. string message = parameter.GetText(DataID.Message.GetHashCode().ToString());
  88. User sender = CurrentPlazaRoom.GetUserById(senderID);
  89. BaseSFSExtension.GardenSmartFox.PlazaRoomManager.ReceivePublicMessage(message, sender);
  90. }
  91. public void OnJoinPlazaRoom(Room room)
  92. {
  93. //DebugManager.PrintKeysAndValuesOfBaseEvent("OnJoinPlazaRoom", baseEvent);
  94. Debug.LogWarning("Join plaza room : " + room.Name);
  95. CurrentPlazaRoom = room;
  96. if (onJoinPlazaRoom != null)
  97. onJoinPlazaRoom.Invoke(room);
  98. }
  99. public void OnUserExitPlazaRoom(Room room, User user)
  100. {
  101. if (room.Id == CurrentPlazaRoom.Id)
  102. {
  103. OnUserExitCurrentPlazaRoom(user);
  104. }
  105. }
  106. public void OnUserExitCurrentPlazaRoom(User user)
  107. {
  108. Debug.LogWarning($"User : {user.Name} left room");
  109. if (onUserExitCurrentPlazaRoom != null)
  110. onUserExitCurrentPlazaRoom.Invoke(user);
  111. }
  112. public void OnUserEnterPlazaRoom(Room room, User user)
  113. {
  114. if (room.Id == CurrentPlazaRoom.Id)
  115. {
  116. OnUserEnterCurrentPlazaRoom(user);
  117. }
  118. }
  119. public void OnUserEnterCurrentPlazaRoom(User user)
  120. {
  121. Debug.LogWarning($"User : {user.Name} joined room");
  122. if (onUserEnterCurrentPlazaRoom != null)
  123. onUserEnterCurrentPlazaRoom.Invoke(user);
  124. }
  125. }