BARoom.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Sfs2X.Entities.Data;
  5. public class BARoom
  6. {
  7. public const int MAP_USER_ID = -1000;
  8. // public const int STATION_ID_START = 100;
  9. // public const int DOOR_ID_START = 200;
  10. // public const int FLAG_ID_START = 300;
  11. public const int STATION_ID_START = 100;
  12. public const int DOOR_ID_START = 200;
  13. public const int FLAG_ID_START = 300;
  14. protected int m_MaxUsers = 6;
  15. public int roundTime = 5*60*1000;
  16. protected int freeTime = 1*30*1000;
  17. protected float startTime = -1f;
  18. public int aiTaker = -1;
  19. public int blueScore;
  20. public int redScore;
  21. protected int scoreTaskInterval = 5000;
  22. protected int lastSendScoreTime = 0;
  23. protected bool scoreTaskRunning = true;
  24. public bool isGameStart = false;
  25. public bool isGameOver = false;
  26. public bool isRecord = true;
  27. public MessageManager.MessageCallBackDelegate handlerCallBack;
  28. protected BAServer server;
  29. protected Dictionary<string, BAHandler> handlerDict;
  30. protected List<BAUser> userList;
  31. protected Dictionary<int, BAUser> userDict;
  32. public Dictionary<int, BAStation> stationDict; //key is crystalBase id
  33. public Dictionary<int, BAFlag> flagDict;
  34. public Dictionary<int, BADoor> doorDict;
  35. public Dictionary<int, BAItemPos> itemPosDict;
  36. public BARoom(BAServer server)
  37. {
  38. this.server = server;
  39. userList = new List<BAUser>();
  40. userDict = new Dictionary<int, BAUser>();
  41. stationDict = new Dictionary<int, BAStation> ();
  42. itemPosDict = new Dictionary<int, BAItemPos> ();
  43. doorDict = new Dictionary<int, BADoor> ();
  44. flagDict = new Dictionary<int, BAFlag> ();
  45. handlerDict = new Dictionary<string, BAHandler>();
  46. InitHandler ();
  47. startTime = GameTime.realtimeSinceStartup;
  48. }
  49. protected virtual void InitHandler()
  50. {
  51. handlerDict.Add (Command.Ready, new ReadyHandler(this));
  52. handlerDict.Add (Command.PlayerJoin, new PlayerJoinHandler(this));
  53. handlerDict.Add (Command.PlayerLeft, new PlayerLeftHandler(this));
  54. handlerDict.Add (Command.BattleStart, new BattleStartHandler(this));
  55. handlerDict.Add (Command.BattleInfo, new BattleInfoHandler(this));
  56. handlerDict.Add (Command.Dead, new DeadHandler (this));
  57. handlerDict.Add (Command.Station, new StationHandler(this));
  58. handlerDict.Add (Command.AddMapItem, new AddItemHandler(this));
  59. handlerDict.Add (Command.GetMapItem, new GetItemHandler(this));
  60. handlerDict.Add (Command.AddFlag, new AddFlagHandler(this));
  61. handlerDict.Add (Command.GetFlag, new GetFlagHandler(this));
  62. handlerDict.Add (Command.PutFlag, new PutFlagHandler(this));
  63. handlerDict.Add (Command.CaptureFlag, new CaptureFlagHandler(this));
  64. handlerDict.Add (Command.SimpleSync, new SyncHandler(this));
  65. handlerDict.Add (Command.AITaker, new AITakerHandler(this));
  66. handlerDict.Add (Command.AITakerRequest, new AITakeRequestrHandler(this));
  67. }
  68. public void AddUser(ISFSArray arr)
  69. {
  70. for(int i=0; i<arr.Size(); i++)
  71. {
  72. ISFSObject obj = arr.GetSFSObject (i);
  73. int id = StringUtil.ToInt(obj.GetUtfString (UserSFSObjectLabel.ID));
  74. string nick = obj.GetUtfString (UserSFSObjectLabel.NICK);
  75. int teamId = obj.GetInt (UserSFSObjectLabel.TEAM_ID);
  76. int playerId = obj.GetInt (UserSFSObjectLabel.PLAYER_ID);
  77. RemoveAIUserByPlayerId (playerId);
  78. BAUser user = GetUser (id);
  79. if (user == null) {
  80. user = new BAUser (id, nick);
  81. userList.Add(user);
  82. userDict.Add(id, user);
  83. } else {
  84. user.lost = false;
  85. }
  86. user.teamId = teamId;
  87. user.playerId = playerId;
  88. }
  89. }
  90. public void UserLeft(int id)
  91. {
  92. BAUser user = GetUser(id);
  93. if (user != null)
  94. user.lost = true;
  95. }
  96. public void RemoveUser(int id)
  97. {
  98. BAUser user = null;
  99. userDict.TryGetValue (id, out user);
  100. userDict.Remove (id);
  101. userList.Remove (user);
  102. }
  103. public void RemoveAIUserByPlayerId(int playerId)
  104. {
  105. for(int i=0; i<userList.Count; i++)
  106. {
  107. BAUser user = userList [i];
  108. if(user.playerId == playerId && user.id < 0)
  109. {
  110. RemoveUser (user.id);
  111. return;
  112. }
  113. }
  114. }
  115. public BAUser GetUser(int id)
  116. {
  117. BAUser user = null;
  118. userDict.TryGetValue (id, out user);
  119. return user;
  120. }
  121. public List<BAUser> GetUserList()
  122. {
  123. return userList;
  124. }
  125. public bool IsMyUserId(int userId)
  126. {
  127. return userId == Session.myUserId;
  128. }
  129. public bool IsHost()
  130. {
  131. return Session.myUserId == aiTaker;
  132. }
  133. public int maxUsers
  134. {
  135. get{
  136. return m_MaxUsers;
  137. }
  138. }
  139. public int GetTime()
  140. {
  141. return Mathf.FloorToInt((GameTime.realtimeSinceStartup-startTime)*1000f);
  142. }
  143. public int GetFreeTime()
  144. {
  145. int time = GetTime ();
  146. if(time <= freeTime)
  147. {
  148. return freeTime - time;
  149. }
  150. return 0;
  151. }
  152. public int GetGameTime()
  153. {
  154. int time = GetTime ();
  155. if(time <= freeTime)
  156. {
  157. return 0;
  158. }
  159. return time - freeTime;
  160. }
  161. public virtual ISFSArray GetRoomBuildingData()
  162. {
  163. ISFSArray arr = new SFSArray ();
  164. foreach(KeyValuePair<int, BAStation> kvp in stationDict)
  165. {
  166. BAStation station = kvp.Value;
  167. ISFSObject obj = new SFSObject ();
  168. obj.PutInt ("i", station.id);
  169. obj.PutInt ("t", station.teamId);
  170. obj.PutInt ("c", station.crystalId);
  171. obj.PutInt ("s", MapObjectUtil.TypeId.Station.GetHashCode());
  172. arr.AddSFSObject (obj);
  173. }
  174. foreach(KeyValuePair<int, BADoor> kvp in doorDict)
  175. {
  176. BADoor door = kvp.Value;
  177. ISFSObject obj = new SFSObject ();
  178. obj.PutInt ("u", MAP_USER_ID);
  179. obj.PutInt ("i", door.id);
  180. obj.PutInt ("t", door.teamId);
  181. obj.PutInt ("c", door.index);
  182. obj.PutInt ("s", MapObjectUtil.TypeId.Door.GetHashCode());
  183. arr.AddSFSObject (obj);
  184. }
  185. foreach(KeyValuePair<int, BAItemPos> kvp in itemPosDict)
  186. {
  187. BAItemPos itemPos = kvp.Value;
  188. ISFSObject obj = new SFSObject ();
  189. obj.PutInt ("i", itemPos.id);
  190. obj.PutInt ("t", itemPos.itemId);
  191. obj.PutInt ("s", MapObjectUtil.TypeId.MapItemPos.GetHashCode());
  192. arr.AddSFSObject (obj);
  193. }
  194. return arr;
  195. }
  196. public virtual ISFSArray GetRoomUserData()
  197. {
  198. ISFSArray arr = new SFSArray ();
  199. for(int i=0; i<userList.Count; i++)
  200. {
  201. BAUser user = userList [i];
  202. ISFSObject obj = new SFSObject ();
  203. obj.PutUtfString (UserSFSObjectLabel.ID, user.id.ToString());
  204. obj.PutUtfString (UserSFSObjectLabel.NICK, user.nick);
  205. obj.PutInt (UserSFSObjectLabel.TEAM_ID, user.teamId);
  206. obj.PutInt (UserSFSObjectLabel.PLAYER_ID, user.playerId);
  207. arr.AddSFSObject (obj);
  208. }
  209. return arr;
  210. }
  211. public int GetAvailabledPlayerId()
  212. {
  213. int aiId = -1;
  214. for(int i=0; i<maxUsers; i++)
  215. {
  216. bool found = false;
  217. for(int j=0; j<userList.Count; j++)
  218. {
  219. BAUser user = userList [j];
  220. if(user.id < 0 && aiId < 0)
  221. {
  222. aiId = user.playerId;
  223. }
  224. if(user.playerId == j)
  225. {
  226. found = true;
  227. break;
  228. }
  229. }
  230. if (found)
  231. continue;
  232. return i;
  233. }
  234. return aiId;
  235. }
  236. public List<int> GetAvailabledPlayerIds()
  237. {
  238. List<int> list = new List<int> ();
  239. for(int i=0; i<maxUsers; i++)
  240. {
  241. bool found = false;
  242. for(int j=0; j<userList.Count; j++)
  243. {
  244. BAUser user = userList [j];
  245. if(user.playerId == i)
  246. {
  247. found = true;
  248. break;
  249. }
  250. }
  251. if (found)
  252. continue;
  253. list.Add(i);
  254. }
  255. return list;
  256. }
  257. public BAStation AttempAddStation(int crystalIndex, int teamId)
  258. {
  259. if(stationDict.ContainsKey(crystalIndex))
  260. {
  261. return null;
  262. }
  263. BAStation station = new BAStation(STATION_ID_START+crystalIndex, crystalIndex, teamId);
  264. stationDict.Add(crystalIndex, station);
  265. return station;
  266. }
  267. public virtual void RemoveStation(int id)
  268. {
  269. }
  270. public void AddDoor(int index, int teamId)
  271. {
  272. if(doorDict.ContainsKey(index))
  273. {
  274. return;
  275. }
  276. BADoor door = new BADoor(DOOR_ID_START+index, index, teamId);
  277. doorDict.Add(index, door);
  278. }
  279. public void RemoveDoor(int doorId)
  280. {
  281. foreach(KeyValuePair<int, BADoor> kvp in doorDict)
  282. {
  283. BADoor door = kvp.Value;
  284. if(door.id == doorId)
  285. {
  286. doorDict.Remove(kvp.Key);
  287. return;
  288. }
  289. }
  290. }
  291. public int AttempGetItem(int posId)
  292. {
  293. if(itemPosDict.ContainsKey(posId))
  294. {
  295. BAItemPos itemPos = itemPosDict[posId];
  296. int itemId = itemPos.itemId;
  297. itemPos.itemId = -1;
  298. return itemId;
  299. }
  300. return -1;
  301. }
  302. public virtual bool AttempGetFlag(int userId, int craftId)
  303. {
  304. return false;
  305. }
  306. public virtual bool AttempPutFlag(int userId, int craftId, int flagTeamId, int x, int y)
  307. {
  308. return false;
  309. }
  310. public virtual bool AttempCaptureFlag(int userId, int craftId)
  311. {
  312. return false;
  313. }
  314. protected void SendBattleInfo(ISFSObject scoreData)
  315. {
  316. ISFSObject data = new SFSObject();
  317. if(scoreData != null)
  318. data.PutSFSObject("s", scoreData);
  319. Message msg = new Message (Command.BattleInfo, data);
  320. server.Send(msg);
  321. }
  322. protected virtual void StopScoreTask()
  323. {
  324. scoreTaskRunning = false;
  325. }
  326. protected void DealGameOver(SFSObject data, int winTeam)
  327. {
  328. StopScoreTask();
  329. isGameOver = true;
  330. data.PutInt("w", winTeam);
  331. }
  332. protected virtual void CheckGameStart()
  333. {
  334. if(!isGameStart && IsHost())
  335. {
  336. if(GetFreeTime() < 10*1000)
  337. {
  338. isGameStart = true;
  339. Message msg = new Message (Command.BattleStart, new SFSObject ());
  340. server.Send (msg);
  341. }
  342. }
  343. }
  344. public virtual void Update(){
  345. CheckGameStart ();
  346. CheckAITaker ();
  347. }
  348. public void HandleMassage(ISFSObject msg, int sender, int timeStamp)
  349. {
  350. string cmd = msg.GetUtfString(Message.LABEL_COMMAND);
  351. BAHandler handler = null;
  352. handlerDict.TryGetValue(cmd, out handler);
  353. if (handler != null) {
  354. handler.HandleRequest (server, msg, sender, timeStamp);
  355. } else {
  356. server.RoomHandleResponse (msg);
  357. // Debuger.LogError ("No such handler["+cmd+"]");
  358. }
  359. }
  360. private float aiTakerMsgWaitTime;
  361. public void AITakerMessageGot()
  362. {
  363. aiTakerMsgWaitTime = GameTime.time;
  364. aiTakerRequestId = 0;
  365. timeStamp = 0;
  366. requestTime = 0;
  367. }
  368. private bool IsAITakerTimeOut()
  369. {
  370. return GameTime.time - aiTakerMsgWaitTime >= 5f;
  371. }
  372. private int aiTakerRequestId;
  373. private int timeStamp;
  374. private float requestTime;
  375. public void AITakerRequest(int id, int timeStamp)
  376. {
  377. if (aiTakerRequestId == 0 || timeStamp < this.timeStamp || (requestTime > 0 && GameTime.time - requestTime > 5f))
  378. {
  379. aiTakerRequestId = id;
  380. this.timeStamp = timeStamp;
  381. requestTime = GameTime.time;
  382. }
  383. }
  384. public bool AITakerRequestHoldComplete()
  385. {
  386. if(IsMyUserId(aiTakerRequestId))
  387. return (GameTime.time - requestTime) > 2f;
  388. return false;
  389. }
  390. protected virtual void CheckAITaker()
  391. {
  392. if (isGameOver)
  393. return;
  394. if (IsAITakerTimeOut ()) {
  395. ISFSObject data = new SFSObject ();
  396. Message msg = new Message (Command.AITakerRequest, data);
  397. server.Send (msg);
  398. aiTakerMsgWaitTime = GameTime.time;
  399. }
  400. if(AITakerRequestHoldComplete())
  401. {
  402. ISFSObject data = new SFSObject ();
  403. Message msg = new Message (Command.AITaker, data);
  404. server.Send (msg);
  405. AITakerMessageGot ();
  406. }
  407. }
  408. public virtual void Dispose()
  409. {
  410. }
  411. }