GardenSmartFox.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 RequestType
  11. {
  12. Batch,
  13. BatchAndOverride,
  14. Immediate,
  15. }
  16. public enum RequestStatus
  17. {
  18. Failed,
  19. Succeed,
  20. Pending,
  21. }
  22. public class GardenSmartFox
  23. {
  24. #region Variable
  25. public const string CommandRequestLabel = "0";
  26. public const string CommandArrayLabel = "A";
  27. public const string CommandLabel = "C";
  28. public const string ParameterLabel = "P";
  29. public const string TargetUserLabel = "U";
  30. public User User;
  31. public SmartFox SmartFox;
  32. public BaseLoggor Loggor;
  33. public BaseConnector Connector;
  34. public SFSExtensionManager ExtensionManager;
  35. public PlazaRoomManager PlazaRoomManager;
  36. public float RequestTime = 0.5f;
  37. public float RequestTimer = 0f;
  38. public List<SFSObject> BatchRequestList = new List<SFSObject>();
  39. public Dictionary<string, SFSObject> BatchAndOverrideRequestDictionary = new Dictionary<string, SFSObject>();
  40. #endregion
  41. public GardenSmartFox(BaseLoggor baseLoggor, BaseConnector baseConnector)
  42. {
  43. SmartFox = new SmartFox();
  44. Loggor = baseLoggor.Init(this);
  45. Connector = baseConnector.Init(this);
  46. ExtensionManager = new SFSExtensionManager(this);
  47. PlazaRoomManager = new PlazaRoomManager(this);
  48. }
  49. public void ExecuteAfterCheckConection(Action execute, Action<bool, BaseEvent> onConnectResult = null, Action<bool, BaseEvent> onLoginResult = null)
  50. {
  51. if (!SmartFox.IsConnected)
  52. {
  53. Reset();
  54. Connector.Connect();
  55. if (onConnectResult != null)
  56. {
  57. new CounterAction<bool, BaseEvent>
  58. (
  59. (succeed, baseEvent) =>
  60. {
  61. onConnectResult.Invoke(succeed, baseEvent);
  62. }
  63. ).Bind(ref Connector.onConnectResult);
  64. }
  65. if (onLoginResult != null)
  66. {
  67. new CounterAction<bool, BaseEvent>
  68. (
  69. (succeed, baseEvent) =>
  70. {
  71. onLoginResult.Invoke(succeed, baseEvent);
  72. }
  73. ).Bind(ref Loggor.onLoginResult);
  74. }
  75. new CounterAction<BaseEvent>
  76. (
  77. baseEven =>
  78. {
  79. execute.Invoke();
  80. }
  81. ).Bind(ref Loggor.onLogin);
  82. }
  83. else
  84. {
  85. execute.Invoke();
  86. }
  87. }
  88. public void Reset()
  89. {
  90. SmartFox = new SmartFox();
  91. Loggor.Init(this);
  92. Connector.Init(this);
  93. ExtensionManager = new SFSExtensionManager(this);
  94. PlazaRoomManager = new PlazaRoomManager(this);
  95. }
  96. public void ProcessRequest()
  97. {
  98. RequestTimer += Time.deltaTime;
  99. if (RequestTimer >= RequestTime)
  100. {
  101. RequestTimer = 0;
  102. if (BatchRequestList.Count > 0 || BatchAndOverrideRequestDictionary.Count > 0)
  103. {
  104. BatchRequestList.AddRange(BatchAndOverrideRequestDictionary.Values.ToList());
  105. SFSObject parameter = ConstructRequestParameter(BatchRequestList);
  106. BatchRequestList = new List<SFSObject>();
  107. BatchAndOverrideRequestDictionary = new Dictionary<string, SFSObject>();
  108. SendRequest(parameter);
  109. }
  110. }
  111. }
  112. public void AddRequest(SFSObject sfsObject, RequestType requestType, string requestName = null)
  113. {
  114. if (requestType == RequestType.Immediate)
  115. {
  116. SFSObject parameter = ConstructRequestParameter(new List<SFSObject>() { sfsObject });
  117. SendRequest(parameter);
  118. //Debug.Log("Send request immediately");
  119. }
  120. else if (requestType == RequestType.Batch)
  121. {
  122. BatchRequestList.Add(sfsObject);
  123. //Debug.Log("Add batch request");
  124. }
  125. else if (requestType == RequestType.BatchAndOverride)
  126. {
  127. //Debug.Log("Override batch request");
  128. if (BatchAndOverrideRequestDictionary.ContainsKey(requestName))
  129. {
  130. BatchAndOverrideRequestDictionary[requestName] = sfsObject;
  131. }
  132. else
  133. {
  134. BatchAndOverrideRequestDictionary.Add(requestName, sfsObject);
  135. }
  136. }
  137. else
  138. {
  139. throw new Exception();
  140. }
  141. }
  142. public void SendRequest(SFSObject parameter)
  143. {
  144. SmartFox.Send(new ExtensionRequest(CommandRequestLabel, parameter));
  145. }
  146. public static SFSObject ConstructRequestParameter(List<SFSObject> sfsObjects)
  147. {
  148. SFSObject parameter = new SFSObject();
  149. SFSArray sfsArray = new SFSArray();
  150. foreach (var sfsObject in sfsObjects)
  151. {
  152. sfsArray.AddSFSObject(sfsObject);
  153. }
  154. parameter.PutSFSArray(CommandArrayLabel, sfsArray);
  155. return parameter;
  156. }
  157. public static SFSObject ConstructCommandParameter(int commandID, SFSObject sfsObject, int targetUserID)
  158. {
  159. SFSObject parameter = new SFSObject();
  160. parameter.PutInt("C", commandID);
  161. parameter.PutSFSObject("P", sfsObject);
  162. parameter.PutInt("U", targetUserID);
  163. parameter.PutSFSObject("P", sfsObject);
  164. return parameter;
  165. }
  166. }