GardenSmartFox.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 class GardenSmartFox
  22. {
  23. #region Variable
  24. public User User;
  25. public SmartFox SmartFox;
  26. public BaseLoggor Loggor;
  27. public BaseConnector Connector;
  28. public SFSEventManager EventManager;
  29. public SFSPlazaRoomManager PlazaRoomManager;
  30. public float BatchTime = 3f;
  31. public float BatchTimer = 0f;
  32. public List<string> StaleRequests = new List<string>();
  33. public List<SFSObject> BatchRequestList = new List<SFSObject>();
  34. public Dictionary<string, SFSObject> BatchOverrideRequests = new Dictionary<string, SFSObject>();
  35. #endregion
  36. public GardenSmartFox()
  37. {
  38. SmartFox = new SmartFox();
  39. Loggor = new Loggor().Init(this);
  40. Connector = new Connector().Init(this);
  41. EventManager = new SFSEventManager(this);
  42. PlazaRoomManager = new SFSPlazaRoomManager(this);
  43. }
  44. public void ExecuteAfterCheckConection(Action execute = null, Action<bool, BaseEvent> onConnectResult = null, Action<bool, BaseEvent> onLoginResult = null)
  45. {
  46. if (!SmartFox.IsConnected)
  47. {
  48. Reset();
  49. Connector.Connect();
  50. if (onConnectResult != null)
  51. {
  52. new CounterAction<bool, BaseEvent>
  53. (
  54. (succeed, baseEvent) =>
  55. {
  56. onConnectResult.Invoke(succeed, baseEvent);
  57. }
  58. ).Bind(ref Connector.onConnectResult);
  59. }
  60. if (onLoginResult != null)
  61. {
  62. new CounterAction<bool, BaseEvent>
  63. (
  64. (succeed, baseEvent) =>
  65. {
  66. onLoginResult.Invoke(succeed, baseEvent);
  67. }
  68. ).Bind(ref Loggor.onLoginResult);
  69. }
  70. new CounterAction<BaseEvent>
  71. (
  72. baseEven =>
  73. {
  74. execute.SafeInvoke();
  75. }
  76. ).Bind(ref Loggor.onLogin);
  77. }
  78. else
  79. {
  80. execute.SafeInvoke();
  81. }
  82. }
  83. public void Reset()
  84. {
  85. SmartFox = new SmartFox();
  86. Loggor.Init(this);
  87. Connector.Init(this);
  88. EventManager = new SFSEventManager(this);
  89. }
  90. public void ProcessRequest()
  91. {
  92. BatchTimer += Time.deltaTime;
  93. if (BatchTimer >= BatchTime)
  94. {
  95. BatchTimer = 0;
  96. StaleRequests = new List<string>();
  97. if (BatchRequestList.Count > 0 || BatchOverrideRequests.Count > 0)
  98. {
  99. BatchRequestList.AddRange(BatchOverrideRequests.Values.ToList());
  100. SendRequest(BatchRequestList);
  101. BatchRequestList = new List<SFSObject>();
  102. BatchOverrideRequests = new Dictionary<string, SFSObject>();
  103. }
  104. }
  105. }
  106. public void AddRequest(SFSObject sfsObject, RequestType requestType, string requestName = null, BatchOption batchOption = BatchOption.Always)
  107. {
  108. if (requestType == RequestType.Immediate)
  109. {
  110. SendRequest(sfsObject);
  111. }
  112. else if (requestType == RequestType.Batch)
  113. {
  114. if (CheckBatchOption(sfsObject, requestName, batchOption))
  115. {
  116. return;
  117. }
  118. else
  119. {
  120. BatchRequestList.Add(sfsObject);
  121. }
  122. }
  123. else if (requestType == RequestType.BatchAndOverride)
  124. {
  125. if (CheckBatchOption(sfsObject, requestName, batchOption))
  126. {
  127. return;
  128. }
  129. else
  130. {
  131. if (BatchOverrideRequests.ContainsKey(requestName))
  132. {
  133. BatchOverrideRequests[requestName] = sfsObject;
  134. }
  135. else
  136. {
  137. BatchOverrideRequests.Add(requestName, sfsObject);
  138. }
  139. }
  140. }
  141. else
  142. {
  143. throw new Exception();
  144. }
  145. }
  146. private bool CheckBatchOption(SFSObject sfsObject, string requestName, BatchOption batchOption)
  147. {
  148. if (batchOption == BatchOption.Always)
  149. {
  150. return false;
  151. }
  152. else if (batchOption == BatchOption.TryNot)
  153. {
  154. if (!StaleRequests.Contains(requestName))
  155. {
  156. SendRequest(sfsObject);
  157. StaleRequests.Add(requestName);
  158. return true;
  159. }
  160. else
  161. {
  162. return false;
  163. }
  164. }
  165. else
  166. {
  167. throw new Exception();
  168. }
  169. }
  170. private void SendRequest(SFSObject parameter)
  171. {
  172. parameter = WrapIntoArray(new List<SFSObject>() { parameter });
  173. SmartFox.Send(new ExtensionRequest(HandlerID.PlazaRoom.GetHashString(), parameter));
  174. }
  175. private void SendRequest(List<SFSObject> parameters)
  176. {
  177. SFSObject parameter = WrapIntoArray(parameters);
  178. SmartFox.Send(new ExtensionRequest(HandlerID.PlazaRoom.GetHashString(), parameter));
  179. }
  180. public static SFSObject WrapIntoArray(List<SFSObject> sfsObjects)
  181. {
  182. SFSObject obj = new SFSObject();
  183. SFSArray sfsArray = new SFSArray();
  184. foreach (var sfsObject in sfsObjects)
  185. {
  186. sfsArray.AddSFSObject(sfsObject);
  187. }
  188. obj.PutSFSArray(Label.DataArray, sfsArray);
  189. return obj;
  190. }
  191. }