Robot.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Security.Policy;
  4. using System.Xml;
  5. using Sfs2X;
  6. using Sfs2X.Core;
  7. using Sfs2X.Entities.Data;
  8. using Sfs2X.Requests;
  9. using Sfs2X.Util;
  10. using UnityEngine;
  11. public class Robot
  12. {
  13. #region Config
  14. private static bool Initialized;
  15. private static int MinChestValue;
  16. private static int MaxChestValue;
  17. private static float MinLifetime;
  18. private static float MaxLifetime;
  19. private static float CreateChestRate;
  20. private static float ColorChestRate;
  21. private static float DigitChestRate;
  22. private static float MinMoveTime;
  23. private static float MaxMoveTime;
  24. private static float MinRobotCreateTime;
  25. private static float MaxRobotCreateTime;
  26. private static string MaxRobotAmtFml;
  27. private static string ConfigName = "robot_config";
  28. private static string RootNodeName = "data";
  29. private static string DataNodeName = "item";
  30. private SmartFox SmartFox;
  31. #endregion
  32. public static void Initialize()
  33. {
  34. if (Initialized)
  35. {
  36. Initialized = true;
  37. return;
  38. }
  39. XmlDocument document = ManaData.GetXmlDocument(ConfigName);
  40. XmlNode dataNode = document.SelectSingleNode(RootNodeName).SelectSingleNode(DataNodeName);
  41. XmlAttributeCollection attributes = dataNode.Attributes;
  42. int index = 1;
  43. List<int> ints = Auxiliary.IntListParse(',', attributes[index++].Value, null);
  44. MinRobotCreateTime = ints[0];
  45. MaxRobotCreateTime = ints[1];
  46. MaxRobotAmtFml = attributes[index++].Value;
  47. DigitChestRate = float.Parse(attributes[index++].Value);
  48. ColorChestRate = float.Parse(attributes[index++].Value);
  49. ints = Auxiliary.IntListParse(',', attributes[index++].Value, null);
  50. MinMoveTime = ints[0];
  51. MaxMoveTime = ints[1];
  52. CreateChestRate = float.Parse(attributes[index++].Value);
  53. ints = Auxiliary.IntListParse(',', attributes[index++].Value, null);
  54. MinChestValue = ints[0];
  55. MaxChestValue = ints[1];
  56. ints = Auxiliary.IntListParse(',', attributes[index++].Value, null);
  57. MinLifetime = ints[0];
  58. MaxLifetime = ints[1];
  59. //Debug.Log($"RobotConfig {MinRobotCreateTime}");
  60. //Debug.Log($"RobotConfig {MaxRobotCreateTime}");
  61. //Debug.Log($"RobotConfig {MaxRobotAmtFml}");
  62. //Debug.Log($"RobotConfig {DigitChestRate}");
  63. //Debug.Log($"RobotConfig {ColorChestRate}");
  64. //Debug.Log($"RobotConfig {MinMoveTime}");
  65. //Debug.Log($"RobotConfig {MaxMoveTime}");
  66. //Debug.Log($"RobotConfig {CreateChestRate}");
  67. //Debug.Log($"RobotConfig {MinChestValue}");
  68. //Debug.Log($"RobotConfig {MaxChestValue}");
  69. //Debug.Log($"RobotConfig {MinLifetime}");
  70. //Debug.Log($"RobotConfig {MaxLifetime}");
  71. }
  72. public void Connect(int sfsRoomID, int maxRobot)
  73. {
  74. Debug.LogWarning("Robot connect");
  75. SmartFox = new SmartFox();
  76. SmartFox.AddEventListener(SFSEvent.CONNECTION, OnConnectReturn);
  77. SmartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnActivateError);
  78. SmartFox.AddEventListener(SFSEvent.LOGIN, evt => { Mark(sfsRoomID, maxRobot); });
  79. SmartFox.AddEventListener(SFSEvent.LOGIN_ERROR, OnActivateError);
  80. SmartFox.AddEventListener(SFSEvent.LOGOUT, OnActivateError);
  81. SmartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);
  82. ConfigData configData = new ConfigData
  83. {
  84. Host = BaseConnector.Host,
  85. Port = BaseConnector.TcpPort,
  86. Zone = BaseConnector.Zone,
  87. };
  88. SmartFox.Connect(configData);
  89. }
  90. private void Login()
  91. {
  92. Debug.LogWarning("Robot login");
  93. string userName = Random.Range(0f, 1000f).ToString();
  94. SmartFox.Send(new LoginRequest(userName, ""));
  95. }
  96. private void Mark(int sfsRoomID, int maxRobot)
  97. {
  98. Debug.LogWarning("Robot mark");
  99. SFSObject arg = new SFSObject();
  100. arg.PutInt(Label.CommandID, PlazaRoomReq.MarkAsRobot.GetHashCode());
  101. arg.PutInt(Label.RoomID, sfsRoomID);
  102. arg.PutInt(Label.Data, maxRobot);
  103. SendRequest(arg);
  104. }
  105. private void SendRequest(SFSObject arg)
  106. {
  107. arg = GardenSmartFox.WrapIntoArray(new List<SFSObject> { arg });
  108. SmartFox.Send(new ExtensionRequest(HandlerID.PlazaRoom.GetHashString(), arg));
  109. }
  110. private void OnConnectReturn(BaseEvent evt)
  111. {
  112. if ((bool)evt.Params["success"])
  113. {
  114. Debug.LogWarning("Robot connect succeed");
  115. Login();
  116. }
  117. else
  118. {
  119. Debug.LogWarning("Robot connect failed");
  120. OnActivateError(null);
  121. }
  122. }
  123. private void OnExtensionResponse(BaseEvent evt)
  124. {
  125. string cmd = evt.Params["cmd"].ToString();
  126. if (cmd == PlazaRoomRep.JoinRoomSucceed.GetHashString())
  127. {
  128. Activate();
  129. }
  130. else if (cmd == PlazaRoomRep.MarkRobotError.GetHashString())
  131. {
  132. OnActivateError(evt);
  133. }
  134. }
  135. public bool Update()
  136. {
  137. if (SmartFox != null)
  138. {
  139. SmartFox.ProcessEvents();
  140. }
  141. return Behaviour();
  142. }
  143. private bool Activated;
  144. private void Activate()
  145. {
  146. Debug.LogWarning("Robot Activate");
  147. Activated = true;
  148. MoveTime = Random.Range(MinMoveTime, MaxMoveTime);
  149. LifetimeTime = Random.Range(MinLifetime, MaxLifetime);
  150. CreateChestTime = Random.Range(0, LifetimeTime);
  151. }
  152. private bool Behaviour()
  153. {
  154. if (!Activated)
  155. {
  156. return false;
  157. }
  158. if (LifetimeBehaviour())
  159. {
  160. return true;
  161. }
  162. MoveBehaviour();
  163. CreateChestBehaviour();
  164. GetChestAwardBehaviour();
  165. return false;
  166. }
  167. private float MoveTime;
  168. private float MoveTimer;
  169. private void MoveBehaviour()
  170. {
  171. MoveTimer += Time.deltaTime;
  172. if (MoveTimer >= MoveTime)
  173. {
  174. MoveTimer = 0;
  175. Debug.LogWarning("Robot move");
  176. }
  177. }
  178. private float LifetimeTime;
  179. private float LifetimeTimer;
  180. private bool LifetimeBehaviour()
  181. {
  182. LifetimeTimer += Time.deltaTime;
  183. if (LifetimeTimer >= LifetimeTime)
  184. {
  185. Debug.LogWarning("Robot lifetime over");
  186. return true;
  187. }
  188. else
  189. {
  190. return false;
  191. }
  192. }
  193. private bool CreateChestLock;
  194. private float CreateChestTime;
  195. private float CreateChestTimer;
  196. private void CreateChestBehaviour()
  197. {
  198. if (CreateChestLock)
  199. {
  200. return;
  201. }
  202. CreateChestTimer += Time.deltaTime;
  203. if (CreateChestTimer >= CreateChestTime)
  204. {
  205. Debug.LogWarning("Robot create chest");
  206. CreateChestLock = true;
  207. }
  208. }
  209. private void GetChestAwardBehaviour()
  210. {
  211. //Debug.LogWarning("robot get a chest");
  212. }
  213. public void Deactivate()
  214. {
  215. Debug.LogWarning("Robot disconnect");
  216. SmartFox.Disconnect();
  217. Activated = false;
  218. }
  219. private void OnActivateError(BaseEvent evt)
  220. {
  221. Debug.LogWarning("Robot activate error");
  222. RobotManager.Robots.Remove(this);
  223. }
  224. }