BAServer.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Sfs2X.Entities.Data;
  5. public class BAServer
  6. {
  7. public enum ConnectionType
  8. {
  9. Local,
  10. Remote,
  11. }
  12. private MessageManager.MessageCallBackDelegate msgManCallBack;
  13. private BARoom currentRoom;
  14. private List<Message> messageStack;
  15. private ResponseWaiter resWaiter;
  16. private IConnection connection;
  17. private IConnection localConnection;
  18. private IConnection remoteConnection;
  19. public BAServer ()
  20. {
  21. remoteConnection = new SFSConnection();
  22. localConnection = new LocalConnection();
  23. messageStack = new List<Message>();
  24. resWaiter = new ResponseWaiter();
  25. SetConnection (ConnectionType.Remote);
  26. }
  27. public void SetMsgManCallBack(MessageManager.MessageCallBackDelegate callBack)
  28. {
  29. this.msgManCallBack = callBack;
  30. }
  31. public void SetConnection(ConnectionType type)
  32. {
  33. IConnection newConnection = null;
  34. switch(type)
  35. {
  36. case ConnectionType.Local:
  37. newConnection = localConnection;
  38. break;
  39. case ConnectionType.Remote:
  40. newConnection = remoteConnection;
  41. break;
  42. }
  43. if (connection != null) {
  44. connection.SetResponseMessageCallBack (null);
  45. // if(connection.IsConnected && connection != newConnection)
  46. // {
  47. // connection.Disconnect();
  48. // }
  49. }
  50. connection = newConnection;
  51. connection.SetResponseMessageCallBack (OnRemoteCallBack);
  52. }
  53. public void Connect()
  54. {
  55. if(!connection.IsConnected)
  56. connection.Connect ();
  57. }
  58. public bool IsConnected()
  59. {
  60. return connection.IsConnected;
  61. }
  62. public void EnterBase()
  63. {
  64. resWaiter.StartWait();
  65. // SetConnection (ConnectionType.Local);
  66. Message msg = new Message(Command.EnterBase, new SFSObject());
  67. Debuger.Log ("Send : ["+msg.cmd+"]"+msg.data.GetDump());
  68. // connection.SendMessage(msg);
  69. DealEnterBase(msg);
  70. }
  71. private void DealEnterBase(Message msg)
  72. {
  73. string mapName = MapData.MapID.Base.ToString ();
  74. ISFSArray arr = new SFSArray ();
  75. ISFSObject myObj = new SFSObject ();
  76. myObj.PutUtfString (UserSFSObjectLabel.ID, Session.myUserId.ToString());
  77. myObj.PutInt (UserSFSObjectLabel.TEAM_ID, TeamUtil.Team.Blue.GetHashCode());
  78. myObj.PutUtfString (UserSFSObjectLabel.NICK, WWW.EscapeURL(Session.GetInstance().myUserData.nick));
  79. arr.AddSFSObject(myObj);
  80. // ISFSObject oppObj = new SFSObject ();
  81. // oppObj.PutInt ("u", -1);
  82. // oppObj.PutInt ("t", TeamUtil.Team.Red.GetHashCode());
  83. // oppObj.PutUtfString ("i", "-1");
  84. // oppObj.PutUtfString ("n", RandomNPCNick());
  85. // arr.AddSFSObject (oppObj);
  86. ISFSObject data = new SFSObject();
  87. data.PutUtfString("m", mapName);
  88. data.PutUtfString ("c", Session.myUserId.ToString());
  89. data.PutBool ("s", true);
  90. data.PutSFSArray("a", arr);
  91. Message callBackMsg = new Message(Command.SearchRoom, data);
  92. OnRemoteCallBack(callBackMsg.GetMessagePackage());
  93. }
  94. public void SearchRoom()
  95. {
  96. resWaiter.StartWait();
  97. // SetConnection (ConnectionType.Local);
  98. ISFSObject data = new SFSObject();
  99. Message msg = new Message(Command.SearchRoom, data);
  100. Debuger.Log ("Send : ["+msg.cmd+"]"+msg.data.GetDump());
  101. connection.SendMessage(msg);
  102. }
  103. private void HandleReplay()
  104. {
  105. ISFSObject msg = ReplayManager.GetInstance ().GetRecord ();
  106. if (msg != null)
  107. RoomHandleResponse (msg);
  108. }
  109. private void SetRoom(MapData.MapID mapId, ISFSArray arr, int myUserId)
  110. {
  111. if (currentRoom != null)
  112. currentRoom.Dispose ();
  113. switch(mapId)
  114. {
  115. case MapData.MapID.Base:
  116. currentRoom = new BaseRoom(this);
  117. break;
  118. case MapData.MapID.Challenge:
  119. currentRoom = new ChallengeRoom(this);
  120. break;
  121. case MapData.MapID.Defense:
  122. currentRoom = new DefenceRoom (this);
  123. break;
  124. case MapData.MapID.Flag:
  125. currentRoom = new FlagRoom (this);
  126. break;
  127. }
  128. if(currentRoom == null)
  129. throw new System.Exception("Illegal Map id["+mapId.ToString()+"]");
  130. currentRoom.AddUser(arr);
  131. currentRoom.handlerCallBack = RoomHandleResponse;
  132. if (currentRoom.isRecord)
  133. ReplayManager.GetInstance ().StartRecord ();
  134. }
  135. private void UpdateRoom()
  136. {
  137. if(currentRoom != null)
  138. currentRoom.Update();
  139. }
  140. public void RoomHandleResponse(ISFSObject msg)
  141. {
  142. if (currentRoom != null && currentRoom.isRecord) {
  143. //Did not ignore zone message yet
  144. ReplayManager.GetInstance ().CheckRecord (msg);
  145. }
  146. Debuger.Log ("Recieve : "+msg.GetDump());
  147. if(msgManCallBack != null)
  148. msgManCallBack(msg);
  149. }
  150. public void Send(ISFSObject msg)
  151. {
  152. Message message = new Message (msg);
  153. Send (message);
  154. }
  155. public void Send(Message msg)
  156. {
  157. if(currentRoom == null)
  158. return;
  159. //TODO add to message trace
  160. Debuger.Log ("Send : ["+msg.cmd+"]"+msg.data.GetDump());
  161. for(int i=0; i<messageStack.Count; i++)
  162. {
  163. Message existMsg = messageStack[i];
  164. if(existMsg.cmd == msg.cmd && existMsg.AttempOverride(msg))
  165. {
  166. return;
  167. }
  168. }
  169. if(messageStack.Count < 50)
  170. messageStack.Add(msg);
  171. }
  172. private void CheckMessageStack()
  173. {
  174. if(resWaiter.isWaiting || messageStack.Count == 0)
  175. return;
  176. resWaiter.StartWait();
  177. int unknowMsg = 0;
  178. int zoneMsg = 1;
  179. int roomMsg = 2;
  180. int msgType = unknowMsg;
  181. ISFSArray arr = new SFSArray();
  182. for(int i=0; i<messageStack.Count; i++)
  183. {
  184. Message m = messageStack[i];
  185. int tempMsgType = Command.IsRoomExtensionCommand (m.cmd) ? roomMsg : zoneMsg;
  186. if (msgType == unknowMsg)
  187. {
  188. msgType = tempMsgType;
  189. }
  190. else if(msgType != tempMsgType)
  191. {
  192. continue;
  193. }
  194. ISFSObject obj = new SFSObject();
  195. obj.PutUtfString(Message.LABEL_COMMAND, m.cmd);
  196. obj.PutSFSObject(Message.LABEL_DATA, m.data);
  197. arr.AddSFSObject (obj);
  198. messageStack.RemoveAt (i);
  199. i--;
  200. }
  201. ISFSObject data = new SFSObject();
  202. data.PutSFSArray(Message.LABEL_DATA_ARRAY, arr);
  203. Message msg = new Message(Command.MessageArray, data);
  204. msg.isRoomMsg = (msgType == roomMsg);
  205. connection.SendMessage(msg);
  206. }
  207. private void OnRemoteCallBack(ISFSObject msg)
  208. {
  209. string cmd = msg.GetUtfString(Message.LABEL_COMMAND);
  210. ISFSObject data = msg.GetSFSObject(Message.LABEL_DATA);
  211. if(cmd == Command.MessageArray)
  212. {
  213. int sender = StringUtil.ToInt(data.GetUtfString(Message.LABEL_SENDER));
  214. int timeStamp = data.GetInt (Message.LABEL_TIMESTAMP);
  215. if(currentRoom.IsMyUserId(sender))
  216. resWaiter.StopWait();
  217. if (sender == currentRoom.aiTaker)
  218. currentRoom.AITakerMessageGot ();
  219. ISFSArray arr = data.GetSFSArray(Message.LABEL_DATA_ARRAY);
  220. for(int i=0; i<arr.Count; i++)
  221. {
  222. ISFSObject m = arr.GetSFSObject(i);
  223. currentRoom.HandleMassage(m, sender, timeStamp);
  224. }
  225. }
  226. else if(cmd == Command.SearchRoom)
  227. {
  228. resWaiter.StopWait();
  229. string mapId = data.GetUtfString("m");
  230. int aiTaker = StringUtil.ToInt(data.GetUtfString ("c"));
  231. bool isStarted = data.GetBool ("s");
  232. ISFSArray arr = data.GetSFSArray("a");
  233. SetRoom(MapData.GetMapIdByName(mapId), arr, connection.myUserId);
  234. currentRoom.aiTaker = aiTaker;
  235. RoomHandleResponse(msg);
  236. }
  237. }
  238. public void Update()
  239. {
  240. connection.ProcessEvents();
  241. if(ReplayManager.GetInstance().IsReplaying())
  242. {
  243. HandleReplay ();
  244. }
  245. else
  246. {
  247. UpdateRoom();
  248. CheckMessageStack();
  249. }
  250. }
  251. public void Disconnect()
  252. {
  253. if(connection != null)
  254. connection.Disconnect();
  255. }
  256. public bool IsWaiting()
  257. {
  258. return resWaiter.isWaiting;
  259. }
  260. public class ResponseWaiter
  261. {
  262. private bool _isWaiting;
  263. private float startTime;
  264. public void StartWait()
  265. {
  266. _isWaiting = true;
  267. startTime = GameTime.realtimeSinceStartup;
  268. }
  269. public void StopWait()
  270. {
  271. _isWaiting = false;
  272. }
  273. public bool isWaiting
  274. {
  275. get{
  276. return _isWaiting;
  277. }
  278. }
  279. public float timeWaited
  280. {
  281. get{
  282. return _isWaiting ? GameTime.realtimeSinceStartup - startTime : 0;
  283. }
  284. }
  285. }
  286. }