GardenSmartFox.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Sfs2X;
  5. using Sfs2X.Core;
  6. using Sfs2X.Entities;
  7. using Sfs2X.Entities.Data;
  8. using Sfs2X.Requests;
  9. using UnityEngine;
  10. public enum BatchOption
  11. {
  12. Always,
  13. TryNot,
  14. }
  15. public enum RequestType
  16. {
  17. Batch,
  18. BatchAndOverride,
  19. Immediate,
  20. }
  21. public enum RequestStatus
  22. {
  23. Failed,
  24. Succeed,
  25. Pending,
  26. }
  27. public class GardenSmartFox
  28. {
  29. #region Variable
  30. public const string CommandArrayLabel = "A";
  31. public const string CommandLabel = "C";
  32. public const string RoomIDLabel = "I";
  33. public const string ParameterLabel = "P";
  34. public const string TargetUserLabel = "U";
  35. public User User;
  36. public SmartFox SmartFox;
  37. public BaseLoggor Loggor;
  38. public BaseConnector Connector;
  39. public DatabaseManager DatabaseManager;
  40. public SFSExtensionManager ExtensionManager;
  41. public SFSPlazaRoomManager SFSPlazaRoomManager;
  42. public float RequestTime = 3f;
  43. public float RequestTimer = 0f;
  44. public List<SFSObject> BatchRequestList = new List<SFSObject>();
  45. public List<string> StaleRequests = new List<string>();
  46. public Dictionary<string, SFSObject> BatchAndOverrideRequestDictionary = new Dictionary<string, SFSObject>();
  47. #endregion
  48. public GardenSmartFox(BaseLoggor baseLoggor, BaseConnector baseConnector)
  49. {
  50. SmartFox = new SmartFox();
  51. Loggor = baseLoggor.Init(this);
  52. Connector = baseConnector.Init(this);
  53. ExtensionManager = new SFSExtensionManager(this);
  54. DatabaseManager = new DatabaseManager(this);
  55. SFSPlazaRoomManager = new SFSPlazaRoomManager(this);
  56. }
  57. public void ExecuteAfterCheckConection(Action execute, Action<bool, BaseEvent> onConnectResult = null, Action<bool, BaseEvent> onLoginResult = null)
  58. {
  59. if (!SmartFox.IsConnected)
  60. {
  61. Reset();
  62. Connector.Connect();
  63. if (onConnectResult != null)
  64. {
  65. new CounterAction<bool, BaseEvent>
  66. (
  67. (succeed, baseEvent) =>
  68. {
  69. onConnectResult.Invoke(succeed, baseEvent);
  70. }
  71. ).Bind(ref Connector.onConnectResult);
  72. }
  73. if (onLoginResult != null)
  74. {
  75. new CounterAction<bool, BaseEvent>
  76. (
  77. (succeed, baseEvent) =>
  78. {
  79. onLoginResult.Invoke(succeed, baseEvent);
  80. }
  81. ).Bind(ref Loggor.onLoginResult);
  82. }
  83. new CounterAction<BaseEvent>
  84. (
  85. baseEven =>
  86. {
  87. execute.Invoke();
  88. }
  89. ).Bind(ref Loggor.onLogin);
  90. }
  91. else
  92. {
  93. execute.Invoke();
  94. }
  95. }
  96. public void Reset()
  97. {
  98. SmartFox.RemoveAllEventListeners();
  99. SmartFox = new SmartFox();
  100. Loggor.Init(this);
  101. Connector.Init(this);
  102. ExtensionManager.Init(this);
  103. }
  104. public void ProcessRequest()
  105. {
  106. RequestTimer += Time.deltaTime;
  107. if (RequestTimer >= RequestTime)
  108. {
  109. RequestTimer = 0;
  110. StaleRequests = new List<string>();
  111. if (BatchRequestList.Count > 0 || BatchAndOverrideRequestDictionary.Count > 0)
  112. {
  113. BatchRequestList.AddRange(BatchAndOverrideRequestDictionary.Values.ToList());
  114. SendRequest(BatchRequestList);
  115. BatchRequestList = new List<SFSObject>();
  116. BatchAndOverrideRequestDictionary = new Dictionary<string, SFSObject>();
  117. }
  118. }
  119. }
  120. public void AddRequest(SFSObject sfsObject, RequestType requestType, string requestName = null, BatchOption batchOption = BatchOption.Always)
  121. {
  122. if (requestType == RequestType.Immediate)
  123. {
  124. SendRequest(sfsObject);
  125. //Debug.Log("Send request immediately");
  126. }
  127. else if (requestType == RequestType.Batch)
  128. {
  129. if (CheckBatchOption(sfsObject, requestName, batchOption))
  130. {
  131. return;
  132. }
  133. else
  134. {
  135. BatchRequestList.Add(sfsObject);
  136. }
  137. //Debug.Log("Add batch request");
  138. }
  139. else if (requestType == RequestType.BatchAndOverride)
  140. {
  141. //Debug.Log("Override batch request");
  142. if (CheckBatchOption(sfsObject, requestName, batchOption))
  143. {
  144. return;
  145. }
  146. else
  147. {
  148. if (BatchAndOverrideRequestDictionary.ContainsKey(requestName))
  149. {
  150. BatchAndOverrideRequestDictionary[requestName] = sfsObject;
  151. }
  152. else
  153. {
  154. BatchAndOverrideRequestDictionary.Add(requestName, sfsObject);
  155. }
  156. }
  157. }
  158. else
  159. {
  160. throw new Exception();
  161. }
  162. }
  163. public bool CheckBatchOption(SFSObject sfsObject, string requestName, BatchOption batchOption)
  164. {
  165. if (batchOption == BatchOption.Always)
  166. {
  167. return false;
  168. }
  169. else if (batchOption == BatchOption.TryNot)
  170. {
  171. if (!StaleRequests.Contains(requestName))
  172. {
  173. SendRequest(sfsObject);
  174. StaleRequests.Add(requestName);
  175. return true;
  176. }
  177. else
  178. {
  179. return false;
  180. }
  181. }
  182. else
  183. {
  184. throw new Exception();
  185. }
  186. }
  187. public void SendRequest(SFSObject parameter)
  188. {
  189. parameter = ConstructRequestParameter(new List<SFSObject>() { parameter });
  190. SmartFox.Send(new ExtensionRequest(RequestID.Command.GetHashCode().ToString(), parameter));
  191. }
  192. public void SendRequest(List<SFSObject> parameters)
  193. {
  194. SFSObject parameter = ConstructRequestParameter(parameters);
  195. SmartFox.Send(new ExtensionRequest(RequestID.Command.GetHashCode().ToString(), parameter));
  196. }
  197. public static SFSObject ConstructRequestParameter(List<SFSObject> sfsObjects)
  198. {
  199. SFSObject parameter = new SFSObject();
  200. SFSArray sfsArray = new SFSArray();
  201. foreach (var sfsObject in sfsObjects)
  202. {
  203. sfsArray.AddSFSObject(sfsObject);
  204. }
  205. parameter.PutSFSArray(CommandArrayLabel, sfsArray);
  206. return parameter;
  207. }
  208. public static SFSObject ConstructCommandParameter(int commandID, SFSObject sfsObject, int targetUserID, int roomID)
  209. {
  210. SFSObject parameter = new SFSObject();
  211. parameter.PutInt(CommandLabel, commandID);
  212. parameter.PutSFSObject(ParameterLabel, sfsObject);
  213. parameter.PutInt(TargetUserLabel, targetUserID);
  214. parameter.PutInt(RoomIDLabel, roomID);
  215. return parameter;
  216. }
  217. }