MessageManager.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using Sfs2X;
  6. using Sfs2X.Entities.Data;
  7. public class MessageManager
  8. {
  9. public BAServer server;
  10. public delegate void MessageCallBackDelegate(ISFSObject data);
  11. public delegate void MessageDealDelegate(string cmd, ISFSObject data);
  12. private Dictionary<string, MessageDealDelegate> messageDealDict;
  13. private List<ISFSObject> receiveQueue;
  14. private BattleSession battleSession;
  15. private float craftSyncDistance = 10f;
  16. public MessageManager()
  17. {
  18. server = new BAServer();
  19. server.SetMsgManCallBack(EnRecieveQueue);
  20. receiveQueue = new List<ISFSObject> ();
  21. //remove old message queue if exist
  22. MessageQueueUpdate mqu = GameObject.FindObjectOfType<MessageQueueUpdate>();
  23. if(mqu != null)
  24. GameObject.Destroy(mqu.gameObject);
  25. //create new message queue
  26. GameObject mquObj = new GameObject("MessageQueueUpdate");
  27. mqu = mquObj.AddComponent<MessageQueueUpdate>();
  28. mqu.messageManager = this;
  29. messageDealDict = new Dictionary<string, MessageDealDelegate>();
  30. messageDealDict.Add(Command.ConnectionError, OnConnectionError);
  31. messageDealDict.Add(Command.SearchRoom, OnSearchBattle);
  32. messageDealDict.Add(Command.BattleInfo, OnBattleInfo);
  33. messageDealDict.Add (Command.BattleExit, OnExitBattle);
  34. messageDealDict.Add (Command.AITaker, OnAITaker);
  35. messageDealDict.Add (Command.PlayerJoin, OnPlayerJoin);
  36. messageDealDict.Add(Command.PlayerLeft, OnPlayerLeft);
  37. messageDealDict.Add(Command.Ready, OnReady);
  38. messageDealDict.Add (Command.State, OnState);
  39. messageDealDict.Add(Command.CraftSelection, OnSelectCraft);
  40. messageDealDict.Add(Command.Move, OnMove);
  41. messageDealDict.Add(Command.Attack, OnPower);
  42. messageDealDict.Add(Command.Dead, OnDead);
  43. messageDealDict.Add (Command.PrepareSwap, OnPrepareSwap);
  44. messageDealDict.Add (Command.RequestSwap, OnRequestSwap);
  45. messageDealDict.Add(Command.Radio, OnRadio);
  46. messageDealDict.Add(Command.Station, OnOccupyStation);
  47. messageDealDict.Add(Command.AddMapItem, OnAddMapItem);
  48. messageDealDict.Add(Command.GetMapItem, OnGetItem);
  49. messageDealDict.Add(Command.AddFlag, OnAddFlag);
  50. messageDealDict.Add(Command.GetFlag, OnGetFlag);
  51. messageDealDict.Add(Command.PutFlag, OnPutFlag);
  52. messageDealDict.Add(Command.CaptureFlag, OnCaptureFlag);
  53. messageDealDict.Add(Command.SimpleSync, OnSimpleSync);
  54. //clan state
  55. messageDealDict.Add (Command.ClanInit, OnClanInit);
  56. messageDealDict.Add (Command.ClanOnline, OnClanOnline);
  57. messageDealDict.Add (Command.ClanOffline, OnClanOffline);
  58. messageDealDict.Add (Command.ClanBattle, OnClanBattle);
  59. messageDealDict.Add (Command.ClanMessage, OnClanMessage);
  60. messageDealDict.Add (Command.ClanJoin, OnClanJoin);
  61. messageDealDict.Add (Command.ClanLeft, OnClanLeft);
  62. //team
  63. messageDealDict.Add (Command.TeamInvite, OnTeamInvite);
  64. messageDealDict.Add (Command.TeamJoin, OnTeamJoin);
  65. messageDealDict.Add (Command.TeamLeft, OnTeamLeft);
  66. messageDealDict.Add (Command.TeamState, OnTeamState);
  67. messageDealDict.Add (Command.TeamKick, OnTeamKick);
  68. }
  69. public void SetBattleSession(BattleSession battleSession)
  70. {
  71. this.battleSession = battleSession;
  72. }
  73. public int myUserId
  74. {
  75. get{
  76. return Session.myUserId;
  77. }
  78. }
  79. private void EnSendQueue(Message msg)
  80. {
  81. server.Send(msg);
  82. }
  83. private void EnRecieveQueue(ISFSObject msg)
  84. {
  85. receiveQueue.Add (msg);
  86. }
  87. public void DeQueue()
  88. {
  89. if (BattleController.battleType == BattleController.BattleType.Edit)
  90. return;
  91. if (battleSession.IsReady ()) {
  92. for (int i = 0; i < receiveQueue.Count; i++) {
  93. ParseMessage (receiveQueue [i]);
  94. }
  95. receiveQueue.Clear ();
  96. }
  97. // else if(receiveQueue.Count > 0) {
  98. // ISFSObject msg = receiveQueue [0];
  99. // receiveQueue.RemoveAt (0);
  100. // ParseMessage (msg);
  101. // }
  102. server.Update();
  103. }
  104. private void ParseMessage(ISFSObject msg)
  105. {
  106. string cmd = msg.GetUtfString(Message.LABEL_COMMAND);
  107. ISFSObject data = msg.GetSFSObject(Message.LABEL_DATA);
  108. if(messageDealDict.ContainsKey(cmd))
  109. {
  110. messageDealDict[cmd](cmd, data);
  111. }
  112. else
  113. {
  114. Debuger.LogError("Can not parse command["+cmd+"]");
  115. }
  116. }
  117. private bool CheckError(string cmd, ISFSObject data)
  118. {
  119. if(data.ContainsKey("error"))
  120. {
  121. int error = data.GetInt ("error");
  122. Debuger.LogError (data.GetUtfString("errormsg"));
  123. return true;
  124. }
  125. return false;
  126. }
  127. public void Connect()
  128. {
  129. server.Connect ();
  130. }
  131. private void OnConnectionError(string cmd, ISFSObject data)
  132. {
  133. ProgressPanel.Hide();
  134. if(data.ContainsKey("success"))
  135. {
  136. if(!data.GetBool("success") && data.ContainsKey("error"))
  137. {
  138. int errorCode = data.GetInt("error");
  139. string errorInfo = Language.GetStr("Connection", "connectFail")+"["+errorCode+"]";
  140. // if(errorCode == SFSConnection.ERROR_CODE.LoginFailed.GetHashCode())
  141. // {
  142. // errorInfo = Language.GetStr("Connection", "loginFail");
  143. // }
  144. // else if(errorCode == SFSConnection.ERROR_CODE.ConnectLost.GetHashCode())
  145. // {
  146. // errorInfo = Language.GetStr("Connection", "connectLost");
  147. // }
  148. AlertPanel.Show(null, errorInfo, AlertPanel.OK, ConnectFail);
  149. }
  150. }
  151. }
  152. private void ConnectFail(AlertCloseEvent evt)
  153. {
  154. BattleController.battleType = BattleController.BattleType.Menu;
  155. Application.LoadLevel(Config.Scene.Battle.ToString());
  156. }
  157. public void Disconnect()
  158. {
  159. server.Disconnect();
  160. }
  161. public void EnterBase()
  162. {
  163. server.EnterBase ();
  164. }
  165. public void SearchBattle(MapData.Mode mode)
  166. {
  167. LocalToRemote ();
  168. battleSession.mode = mode;
  169. LoadingPanel panel = LoadingPanel.Show ();
  170. LoadingPanel.Increase (0.1f);
  171. panel.ReachTargetProgress.AddListener (()=>{
  172. server.SearchRoom ();
  173. });
  174. }
  175. private void OnSearchBattle(string cmd, ISFSObject data)
  176. {
  177. if(!CheckError(cmd, data))
  178. {
  179. string mapId = data.GetUtfString("m");
  180. battleSession.mapId = MapData.GetMapIdByName(mapId);
  181. if (battleSession.mapId != MapData.MapID.Base) {
  182. BattleController.battleType = BattleController.BattleType.Battle;
  183. battleSession.ResetBattle ();
  184. } else {
  185. BattleController.battleType = BattleController.BattleType.Menu;
  186. }
  187. int aiTakerUserId = StringUtil.ToInt(data.GetUtfString ("c"));
  188. battleSession.aiTakerUserId = aiTakerUserId;
  189. //init player
  190. ParsePlayer(data.GetSFSArray("a"));
  191. if (battleSession.mapId != MapData.MapID.Base)
  192. LoadingPanel.LoadScene (Config.Scene.Battle.ToString ());
  193. else {
  194. if (Session.GetInstance ().myUserData.isLogin) {
  195. CheckConnection ();
  196. } else {
  197. LoadingPanel.Increase (0.2f);
  198. Session.GetInstance ().GetNetworkManager ().Login (() => {
  199. EquipManager.GetInstance().RequestInventory();
  200. battleSession.GetBattleController ().menuUI.clanChatBar.Refresh ();
  201. battleSession.SetMyPlayerId(Session.myUserId);
  202. CheckConnection ();
  203. if (LocalSaver.GetInstance ().noAutoBinding == 0)
  204. Session.GetInstance ().GetNetworkManager ().Binding ();
  205. });
  206. }
  207. }
  208. }
  209. }
  210. private void ParsePlayer(ISFSArray arr)
  211. {
  212. for(int i=0; i<arr.Count; i++)
  213. {
  214. ISFSObject obj = arr.GetSFSObject (i);
  215. int id = StringUtil.ToInt(obj.GetUtfString (UserSFSObjectLabel.ID));
  216. string nick = obj.GetUtfString (UserSFSObjectLabel.NICK);
  217. int team = obj.GetInt (UserSFSObjectLabel.TEAM_ID);
  218. int playerId = obj.GetInt (UserSFSObjectLabel.PLAYER_ID);
  219. Player player = battleSession.AddPlayer(id, nick, TeamUtil.GetTeam(team), playerId);
  220. }
  221. }
  222. private void OnPlayerJoin(string cmd, ISFSObject data)
  223. {
  224. ParsePlayer(data.GetSFSArray("a"));
  225. }
  226. private void ParseBuilding(ISFSArray arr)
  227. {
  228. Map map = battleSession.GetBattleController().GetMap();
  229. for(int i=0; i<arr.Count; i++)
  230. {
  231. ISFSObject obj = arr.GetSFSObject(i);
  232. MapObjectUtil.TypeId typeId = MapObjectUtil.GetTypeIdByCode(obj.GetInt("s"));
  233. if(typeId == MapObjectUtil.TypeId.Station)
  234. {
  235. StationData sd = new StationData();
  236. sd.team = TeamUtil.GetTeam(obj.GetInt("t"));
  237. sd.userId = obj.GetInt ("u");
  238. sd.id = obj.GetInt("i");
  239. sd.crystalId = obj.GetInt("c");
  240. Station station = map.CreateStation(sd, battleSession.GetBattleController());
  241. station.hp = station.maxHp;
  242. }
  243. else if(typeId == MapObjectUtil.TypeId.Door)
  244. {
  245. Door door = map.GetDoorByIndex(obj.GetInt("c"));
  246. CraftData craftData = new CraftData(1002);
  247. craftData.userId = obj.GetInt("u");
  248. craftData.id = obj.GetInt("i");
  249. craftData.team = TeamUtil.GetTeam(obj.GetInt("t"));
  250. craftData.position = door.transform.position;
  251. craftData.isHero = true;
  252. craftData.aiType = AI.AIType.Door;
  253. door.Init(map, craftData, new CraftEquipModify());
  254. DoorAI ai = door.gameObject.AddComponent<DoorAI>();
  255. ai.init(battleSession.GetBattleController());
  256. Player player = battleSession.GetPlayer(craftData.userId);
  257. ai.SetPlayer(player);
  258. battleSession.GetBattleController().battleUI.miniMap.CreateCraft(door);
  259. map.AddBattleObject(door);
  260. }
  261. else if(typeId == MapObjectUtil.TypeId.MapItemPos)
  262. {
  263. int id = obj.GetInt("i");
  264. int itemId = obj.GetInt("t");
  265. if(itemId != -1)
  266. {
  267. MapItemData itemData = new MapItemData();
  268. itemData.id = id;
  269. itemData.typeId = itemId;
  270. MapItemPos mapItemPos = battleSession.GetBattleController().GetMap().GetMapItemPos(id);
  271. itemData.postion = mapItemPos.position;
  272. MapItem mapItem = battleSession.GetBattleController().GetMap().CreateMapItem(itemData);
  273. mapItemPos.item = mapItem;
  274. }
  275. }
  276. else if(typeId == MapObjectUtil.TypeId.Flag)
  277. {
  278. int id = obj.GetInt("i");
  279. int itemId = obj.GetInt("t");
  280. if(itemId != -1)
  281. {
  282. MapItemData itemData = new MapItemData();
  283. itemData.id = id;
  284. itemData.typeId = itemId;
  285. MapItemPos mapItemPos = battleSession.GetBattleController().GetMap().GetMapItemPos(id);
  286. itemData.postion = mapItemPos.position;
  287. MapItem mapItem = battleSession.GetBattleController().GetMap().CreateMapItem(itemData);
  288. mapItemPos.item = mapItem;
  289. }
  290. }
  291. }
  292. map.RemoveNoIdDoor();
  293. }
  294. private void OnPlayerLeft(string cmd, ISFSObject data)
  295. {
  296. if(!battleSession.IsReady() || battleSession.IsOver())
  297. return;
  298. int userId = StringUtil.ToInt(data.GetUtfString("u"));
  299. battleSession.PlayerLeft (userId);
  300. }
  301. private void RemoteToLocal()
  302. {
  303. server.SetConnection (BAServer.ConnectionType.Local);
  304. }
  305. private void LocalToRemote()
  306. {
  307. server.SetConnection (BAServer.ConnectionType.Remote);
  308. }
  309. public void Ready()
  310. {
  311. ISFSObject data = new SFSObject();
  312. Message msg = new Message(Command.Ready, data);
  313. EnSendQueue(msg);
  314. }
  315. private void OnReady(string cmd, ISFSObject data)
  316. {
  317. if (data != null) {
  318. int receiver = data.GetInt ("r");
  319. if(receiver != myUserId)
  320. {
  321. SendState (receiver);
  322. return;
  323. }
  324. int freeTime = data.GetInt ("f");
  325. int time = data.GetInt ("t");
  326. battleSession.GetBattleController ().clock.freeTimeLeft = (float)freeTime / 1000f;
  327. battleSession.GetBattleController ().clock.timeLeft = (float)time / 1000f;
  328. ParseBuilding (data.GetSFSArray ("l"));
  329. if (data.ContainsKey ("a"))
  330. ParsePlayer (data.GetSFSArray ("a"));
  331. }
  332. WaitLoadComplete ();
  333. }
  334. private void CheckConnection()
  335. {
  336. if (Session.GetInstance ().myUserData.clanId > 0) {
  337. LocalToRemote ();
  338. battleSession.GetMessageManager ().Ready ();
  339. } else {
  340. // battleSession.GetMessageManager ().Connect ();
  341. RemoteToLocal ();
  342. OnReady (Command.Ready.ToString(), null);
  343. }
  344. }
  345. private void WaitLoadComplete()
  346. {
  347. if(LoadingPanel.GetCurrentPanel() != null)
  348. LoadingPanel.Complete (OnLoadBattleComplete);
  349. }
  350. private void OnLoadBattleComplete()
  351. {
  352. battleSession.GetBattleController().ReadyToFight();
  353. }
  354. private void SendState(int target)
  355. {
  356. ISFSArray arr = new SFSArray ();
  357. PlayerCraftObjToArr (battleSession.myPlayer, arr);
  358. if (battleSession.aiTakerUserId == myUserId) {
  359. List<Player> list = battleSession.GetPlayerList ();
  360. for(int i=0; i<list.Count; i++)
  361. {
  362. Player player = list [i];
  363. if(player.IsAI())
  364. {
  365. PlayerCraftObjToArr (player, arr);
  366. }
  367. }
  368. }
  369. if (arr.Size () == 0)
  370. return;
  371. ISFSObject data = new SFSObject ();
  372. data.PutInt ("t", target);
  373. data.PutSFSArray ("a", arr);
  374. Message msg = new Message (Command.State, data);
  375. EnSendQueue (msg);
  376. }
  377. private void OnState(string cmd, ISFSObject data)
  378. {
  379. int target = data.GetInt ("t");
  380. ISFSArray arr = data.GetSFSArray ("a");
  381. if (target != myUserId)
  382. return;
  383. for(int i=0; i<arr.Size(); i++)
  384. {
  385. ParseStateObject (arr.GetSFSObject (i));
  386. }
  387. }
  388. private void PlayerCraftObjToArr(Player player, ISFSArray arr)
  389. {
  390. Player.Hero hero = player.GetHero ();
  391. ISFSObject heroObj = null;
  392. if(hero.GetCraft() != null)
  393. heroObj = GetStateCraftObj(hero.GetCraft());
  394. else if(hero.craftLandingData != null)
  395. heroObj = GetStateCraftObj (hero.craftLandingData, hero.equipModify);
  396. if (heroObj != null) {
  397. AddUAVObj (heroObj, player.uavItem);
  398. }
  399. Craft[] soldierArr = hero.soldierArr;
  400. for(int i=0; i<soldierArr.Length; i++)
  401. {
  402. Craft craft = soldierArr [i];
  403. if(craft != null)
  404. {
  405. arr.AddSFSObject (GetStateCraftObj(craft));
  406. }
  407. }
  408. }
  409. private ISFSObject GetStateCraftObj(Craft craft)
  410. {
  411. ISFSObject obj = new SFSObject();
  412. obj.PutInt("u", craft.userId);
  413. obj.PutInt("i", craft.id);
  414. obj.PutInt("c", craft.GetCraftId());
  415. obj.PutInt("t", craft.team.GetHashCode());
  416. obj.PutFloat("sx", craft.position.x);
  417. obj.PutFloat("sy", craft.position.z);
  418. obj.PutBool("h", craft.IsHero());
  419. if(craft.isMoving)
  420. {
  421. obj.PutInt("ex", craft.targetMovePosition.x);
  422. obj.PutInt("ey", craft.targetMovePosition.y);
  423. }
  424. obj.PutFloat("hp", craft.hp);
  425. obj.PutSFSObject ("eq", craft.GetEquipModifyData());
  426. return obj;
  427. }
  428. private ISFSObject GetStateCraftObj(CraftData craft, CraftEquipModify equipModify)
  429. {
  430. ISFSObject obj = new SFSObject();
  431. obj.PutInt("u", craft.userId);
  432. obj.PutInt("i", craft.id);
  433. obj.PutInt("c", craft.GetCraftId());
  434. obj.PutInt("t", craft.team.GetHashCode());
  435. obj.PutFloat("sx", craft.position.x);
  436. obj.PutFloat("sy", craft.position.z);
  437. obj.PutBool("h", craft.isHero);
  438. obj.PutFloat("hp", craft.GetMaxHp());
  439. obj.PutSFSObject ("eq", equipModify.GetData());
  440. return obj;
  441. }
  442. private void AddUAVObj(ISFSObject heroObj, UAVItem uavItem)
  443. {
  444. heroObj.PutSFSObject ("uav", UAVManager.GetUAVSFSData (uavItem));
  445. }
  446. private void ParseStateObject(ISFSObject obj)
  447. {
  448. int userId = obj.GetInt ("u");
  449. Player player = battleSession.GetPlayer (userId);
  450. if (player == null)
  451. return;
  452. if (obj.ContainsKey ("uav")) {
  453. player.uavItem = UAVManager.ParseUAVItem (obj.GetSFSObject("uav"));
  454. }
  455. CraftData craftData = new CraftData(obj.GetInt("c"));
  456. craftData.id = obj.GetInt("i");
  457. craftData.userId = player.userId;
  458. craftData.nick = player.nick;
  459. craftData.team = TeamUtil.GetTeam(obj.GetInt("t"));
  460. craftData.position = new Vector3(obj.GetFloat("sx"), 0, obj.GetFloat("sy"));
  461. craftData.isHero = obj.GetBool("h");
  462. if (craftData.isHero) {
  463. craftData.aiType = AI.AIType.Hero;
  464. if (player.uavItem != null)
  465. craftData.extraPowerIds = player.uavItem.GetData ().powers;
  466. } else {
  467. craftData.aiType = AI.AIType.Soldier;
  468. }
  469. ISFSObject equipData = obj.GetSFSObject ("eq");
  470. CraftEquipModify equipModify = new CraftEquipModify ();
  471. equipModify.SetByData (equipData);
  472. Craft craft = battleSession.GetBattleController().CreateCraft(craftData, equipModify);
  473. if(obj.ContainsKey("p"))
  474. {
  475. // TODO craft power
  476. }
  477. if(craft.IsHero())
  478. player.GetHero().SetCraft(craft);
  479. else
  480. player.GetHero().SetSoldier(craft);
  481. craft.PositionTo(new Vector3(obj.GetFloat("sx"), 0, obj.GetFloat("sy")));
  482. craft.hp = obj.GetFloat("hp");
  483. if(obj.ContainsKey("ex"))
  484. {
  485. craft.MoveTo(obj.GetInt("ex"), obj.GetInt("ey"));
  486. }
  487. // TODO buff
  488. // TODO target
  489. }
  490. public void SelectCraft(Player player, int craftId, int col, int row)
  491. {
  492. Player.Hero hero = player.GetHero();
  493. hero.lastSelectTime = GameTime.time;
  494. ISFSObject data = new SFSObject();
  495. data.PutInt("u", player.userId);
  496. data.PutInt("c", craftId);
  497. data.PutInt("x", col);
  498. data.PutInt("y", row);
  499. if (player.isMe) {
  500. data.PutInt ("h", HaloManager.GetInstance ().GetCurrentEquipedId ());
  501. data.PutInt ("s", SkinManager.GetInstance ().GetSkinId (craftId));
  502. data.PutSFSObject ("eq", CraftEquipModify.GetMyEquipData (craftId));
  503. data.PutSFSObject ("uav", UAVManager.GetMyUAVSFSData());
  504. } else {
  505. data.PutSFSObject ("eq", CraftEquipModify.GetAIEquipData (craftId));
  506. }
  507. Message msg = new Message(Command.CraftSelection, data);
  508. EnSendQueue(msg);
  509. }
  510. private void OnSelectCraft(string cmd, ISFSObject data)
  511. {
  512. int userId = data.GetInt("u");
  513. float x = Map.GetCenterX(data.GetInt("x"));
  514. float y = Map.GetCenterZ(data.GetInt("y"));
  515. Player player = battleSession.GetPlayer(data.GetInt("u"));
  516. int haloId = 0;
  517. if(data.ContainsKey("h"))
  518. haloId = data.GetInt("h");
  519. int skinId = 0;
  520. if (data.ContainsKey ("s"))
  521. skinId = data.GetInt ("s");
  522. if(player != null)
  523. {
  524. Player.Hero hero = player.GetHero();
  525. hero.lastSelectTime = float.MinValue;
  526. if (hero.GetCraft() != null) {
  527. return;
  528. }
  529. if (data.ContainsKey ("uav")) {
  530. player.uavItem = UAVManager.ParseUAVItem (data.GetSFSObject ("uav"));
  531. }
  532. CraftData craftData = new CraftData(data.GetInt("c"));
  533. craftData.id = player.GetHeroUid();
  534. craftData.userId = player.userId;
  535. craftData.position = new Vector3(x, 0, y);
  536. craftData.team = player.team;
  537. if(userId == myUserId)
  538. {
  539. craftData.aiType = AI.AIType.Manual;
  540. }
  541. else if(userId < 0)
  542. {
  543. craftData.aiType = AI.AIType.Hero;
  544. }
  545. else
  546. {
  547. craftData.aiType = AI.AIType.Remote;
  548. }
  549. craftData.nick = player.nick;
  550. craftData.isHero = true;
  551. craftData.haloId = haloId;
  552. craftData.skinId = skinId;
  553. if (player.uavItem != null)
  554. craftData.extraPowerIds = player.uavItem.GetData ().powers;
  555. CraftEquipModify equipModify = new CraftEquipModify ();
  556. equipModify.SetByData (data.GetSFSObject("eq"));
  557. hero.craftLandingData = craftData;
  558. hero.equipModify = equipModify;
  559. player.uavItem = UAVManager.ParseUAVItem (data.GetSFSObject ("uav"));
  560. // Craft craft = battleSession.GetBattleController().CreateCraft(craftData);
  561. battleSession.GetBattleController().CreateCapsule(craftData, equipModify);
  562. }
  563. else
  564. {
  565. Debuger.LogError("OnSelectCraft Can not find Player "+userId.ToString());
  566. }
  567. }
  568. public void Move(Craft craft, int col, int row)
  569. {
  570. Message msg = new MoveMessage(craft.id, col, row);
  571. EnSendQueue(msg);
  572. // if(craft.aiType == AI.AIType.Manual)
  573. // {
  574. // craft.MoveTo(col, row);
  575. // }
  576. }
  577. private void OnMove(string cmd, ISFSObject data)
  578. {
  579. int craftId = data.GetInt("i");
  580. int x = data.GetInt("x");
  581. int y = data.GetInt("y");
  582. // float sx = data.GetFloat ("sx");
  583. // float sy = data.GetFloat ("sy");
  584. // Vector3 startPos = new Vector3 (sx, 0, sy);
  585. Craft craft = battleSession.GetBattleController().GetMap().GetBattleObject(craftId) as Craft;
  586. if(craft != null)
  587. {
  588. // if(NumberUtil.distanceVector3(craft.position, startPos, true) > craftSyncDistance)
  589. // craft.position = startPos;
  590. craft.MoveTo(x, y);
  591. }
  592. else
  593. {
  594. Debuger.LogError("onReceiveMove::can not find craft with id("+craftId+")");
  595. }
  596. }
  597. public void Target(Craft craft)
  598. {
  599. ISFSObject data = new SFSObject();
  600. data.PutInt("i", craft.id);
  601. PackTarget(craft.target, data);
  602. Message msg = new Message(Command.Target, data);
  603. EnSendQueue(msg);
  604. }
  605. private void PackTarget(ITarget target, ISFSObject data)
  606. {
  607. if(target is BattleObject)
  608. {
  609. BattleObject battleObj = target as BattleObject;
  610. data.PutInt("ti", battleObj.id);
  611. data.PutInt("tt", TargetUtil.GetTargetType(battleObj).GetHashCode());
  612. }
  613. }
  614. private void OnTarget(string cmd, ISFSObject data)
  615. {
  616. Craft craft = battleSession.GetBattleController().GetMap().GetBattleObject(data.GetInt("i")) as Craft;
  617. if(craft != null && craft.userId != myUserId)
  618. {
  619. craft.target = ParseTarget(data);
  620. if(craft.target == null)
  621. {
  622. craft.StopMove();
  623. }
  624. }
  625. }
  626. private ITarget ParseTarget(ISFSObject data)
  627. {
  628. if(data.ContainsKey("ti"))
  629. {
  630. int targetId = data.GetInt("ti");
  631. TargetUtil.TargetType targetType = TargetUtil.GetTargetTypeByCode(data.GetInt("tt"));
  632. if(targetType == TargetUtil.TargetType.CrystalBase)
  633. {
  634. return battleSession.GetBattleController().GetMap().GetCrystalBase(targetId).GetStation();
  635. }
  636. else if(targetType == TargetUtil.TargetType.Craft)
  637. {
  638. return battleSession.GetBattleController().GetMap().GetBattleObject(targetId);
  639. }
  640. }
  641. return null;
  642. }
  643. public void Power(Power power, ITarget target)
  644. {
  645. ISFSObject powerData = power.PackMessage(target, battleSession.GetBattleController());
  646. Message msg = new PowerMessage(power, powerData);
  647. EnSendQueue(msg);
  648. }
  649. private void OnPower(string cmd, ISFSObject data)
  650. {
  651. int userId = data.GetInt ("u");
  652. int craftId = data.GetInt("i");
  653. int typeId = data.GetInt ("t");
  654. int powerId = data.GetInt("p");
  655. ISFSObject powerData = data.GetSFSObject("d");
  656. IPowerOwner owner = null;
  657. if(typeId == MapObjectUtil.TypeId.UAV.GetHashCode())
  658. owner = battleSession.GetBattleController().GetMap().GetUAVehicle(userId) as IPowerOwner;
  659. else
  660. owner = battleSession.GetBattleController().GetMap().GetBattleObject(craftId) as IPowerOwner;
  661. if(owner != null)
  662. {
  663. Vector3 pos = new Vector3(data.GetFloat("x"), 0, data.GetFloat("y"));
  664. if(NumberUtil.distanceVector3(owner.position, pos, true) > craftSyncDistance)
  665. owner.position = pos;
  666. owner.GetPowerManager ().ParsePower (powerId, powerData, battleSession.GetBattleController ());
  667. }
  668. else
  669. {
  670. Debuger.LogError("onReceivePower::can not find craft with id("+craftId+")");
  671. }
  672. }
  673. public void Dead(BattleObject battleObj, int killerUserId, int killerCraftId)
  674. {
  675. ISFSObject data = new SFSObject();
  676. data.PutInt("i", battleObj.id);
  677. data.PutInt("m", battleObj.team.GetHashCode());
  678. data.PutInt("c", battleObj is Craft ? (battleObj as Craft).GetCraftId() : 0);
  679. data.PutInt("t", battleObj.typeId);
  680. data.PutInt("u", battleObj.userId);
  681. data.PutInt("ku", killerUserId);
  682. data.PutInt("kc", killerCraftId);
  683. data.PutInt("x", battleObj.col);
  684. data.PutInt("y", battleObj.row);
  685. Message msg = new Message(Command.Dead, data);
  686. EnSendQueue(msg);
  687. if(battleObj.userId == myUserId)
  688. {
  689. battleSession.GetBattleController().DealDeath(battleObj.userId, battleObj.id, killerUserId);
  690. }
  691. }
  692. private void OnDead(string cmd, ISFSObject data)
  693. {
  694. int objid = data.GetInt("i");
  695. TeamUtil.Team team = TeamUtil.GetTeam (data.GetInt("m"));
  696. int killerUserId = data.GetInt("ku");
  697. Map map = battleSession.GetBattleController ().GetMap ();
  698. IBattleObject battleObj = map.GetBattleObject(objid);
  699. if (battleObj != null) {
  700. battleSession.GetBattleController().DealDeath(battleObj.userId, battleObj.id, killerUserId);
  701. }
  702. team = TeamUtil.GetOpponentTeam (team);
  703. FlagItem flag = map.GetFlag(team);
  704. if(flag != null && flag.linkedCraftId == objid)
  705. {
  706. int x = data.GetInt("x");
  707. int y = data.GetInt("y");
  708. AStarNode node = map.GetAStarNode(x, y);
  709. if(node == null)
  710. {
  711. node = map.FindNearestEmptyAstarNode(x, y);
  712. }
  713. if(node == null)
  714. {
  715. FlagBase flagBase = map.GetFlagBase(team);
  716. flag.PutToBase(flagBase.position);
  717. }
  718. else
  719. {
  720. flag.PutGround(map.GetAstarNodePosition(node.X, node.Y));
  721. }
  722. Player player = battleSession.GetPlayer(data.GetInt("u"));
  723. if(player != null)
  724. {
  725. string info = Language.GetStr("Flag", "flagLose");
  726. info = info.Replace("%COLOR%", TeamUtil.GetTeamColorString(player.team));
  727. info = info.Replace("%NICK%", player.nick);
  728. info = info.Replace("%TEAM%", TeamUtil.GetTeamName(team));
  729. battleSession.GetBattleController().battleUI.ShowBottomTip(info);
  730. }
  731. }
  732. }
  733. public void OccupyStation(Craft craft, CrystalBase crystalBase)
  734. {
  735. ISFSObject data = new SFSObject();
  736. data.PutInt("c", crystalBase.id);
  737. data.PutInt("t", craft.team.GetHashCode());
  738. data.PutInt("u", craft.userId);
  739. Message msg = new Message(Command.Station, data);
  740. EnSendQueue(msg);
  741. }
  742. private void OnOccupyStation(string cmd, ISFSObject data)
  743. {
  744. if(data.ContainsKey("i"))
  745. {
  746. Map map = battleSession.GetBattleController().GetMap();
  747. int userId = data.GetInt ("u");
  748. StationData stationData = new StationData();
  749. stationData.team = TeamUtil.GetTeam(data.GetInt("t"));
  750. stationData.userId = userId;
  751. stationData.id = data.GetInt("i");
  752. stationData.crystalId = data.GetInt("c");
  753. Station station = map.CreateStation(stationData, battleSession.GetBattleController());
  754. Player player = battleSession.GetPlayer(userId);
  755. if(player != null)
  756. {
  757. player.station++;
  758. string colorStr = TeamUtil.GetTeamColorString(player.team);
  759. string info = Language.GetStr("GameInfo", "occupy");
  760. info = info.Replace("%DIRECTION%", Language.GetStr(map.id.ToString(), "station"+station.crystalBase.id));
  761. if(player.userId == myUserId)
  762. {
  763. info = info.Replace("%NAME%", Language.GetStr("GameInfo", "you"));
  764. }
  765. else
  766. {
  767. info = info.Replace("%NAME%", player.nick);
  768. }
  769. info = info.Replace("%COLOR%", colorStr);
  770. battleSession.GetBattleController().battleUI.ShowBottomTip(info);
  771. }
  772. }
  773. }
  774. private bool radioHold = false;
  775. public void Radio(int index)
  776. {
  777. if(!radioHold)
  778. {
  779. ISFSObject data = new SFSObject();
  780. data.PutInt("i", index);
  781. data.PutInt("u", myUserId);
  782. int craftId = battleSession.GetBattleController ().GetCtrlCraft() != null ? battleSession.GetBattleController ().GetCtrlCraft().id : 0;
  783. data.PutInt ("c", craftId);
  784. radioHold = true;
  785. Message msg = new Message(Command.Radio, data);
  786. EnSendQueue(msg);
  787. }
  788. battleSession.GetBattleController().battleUI.HideRadioPanel();
  789. }
  790. private void OnRadio(string cmd, ISFSObject data)
  791. {
  792. int userId = data.GetInt("u");
  793. int index = data.GetInt("i");
  794. int craftId = data.GetInt ("c");
  795. if(userId == myUserId)
  796. {
  797. radioHold = false;
  798. }
  799. battleSession.GetBattleController().battleUI.DealRadio(userId, craftId, index);
  800. }
  801. // private void ResetPlayer(Player player)
  802. // {
  803. // player.heroDeadTime = 0;
  804. // if(player.heroCraft != null)
  805. // {
  806. // battleSession.GetBattleController().DoDeath(player.heroCraft);
  807. // }
  808. // player.ClearHero();
  809. // }
  810. // public static void ParseStaticMapObject(ISFSArray arr, BattleSession battleSession)
  811. // {
  812. // ParseBuilding(arr);
  813. // for(int i=0; i<arr.Count; i++)
  814. // {
  815. // ISFSObject obj = arr.GetSFSObject(i);
  816. // MapObjectUtil.TypeId typeId = MapObjectUtil.GetTypeIdByCode(obj.GetInt("s"));
  817. // if(typeId == MapObjectUtil.TypeId.Station)
  818. // {
  819. // StationData stationData = new StationData();
  820. // stationData.id = obj.GetInt("i");
  821. // stationData.team = TeamUtil.GetTeam(obj.GetInt("t"));
  822. // stationData.crystalId = obj.GetInt("c");
  823. // Station station = battleSession.GetBattleController().GetMap().CreateStation(stationData, battleSession.GetBattleController());
  824. //
  825. // if(obj.ContainsKey("ti"))
  826. // {
  827. // int itemId = obj.GetInt("ti");
  828. // int itemTypeId = obj.GetInt("tt");
  829. // MapItemData itemData = new MapItemData();
  830. // itemData.id = itemId;
  831. // itemData.typeId = itemTypeId;
  832. // itemData.postion = station.itemPosition;
  833. // itemData.team = station.team;
  834. // battleSession.GetBattleController().GetMap().CreateMapItem(itemData);
  835. // }
  836. // }
  837. // else if(typeId == MapObjectUtil.TypeId.Door)
  838. // {
  839. //
  840. // }
  841. // }
  842. // }
  843. private void OnAddMapItem(string cmd, ISFSObject data)
  844. {
  845. Map map = battleSession.GetBattleController().GetMap();
  846. ISFSArray arr = data.GetSFSArray("l");
  847. for(int i=0; i<arr.Count; i++)
  848. {
  849. ISFSObject obj = arr.GetSFSObject(i);
  850. int id = obj.GetInt("i");
  851. int itemId = obj.GetInt("t");
  852. MapItemPos mapItemPos = map.GetMapItemPos(id);
  853. if(mapItemPos != null)
  854. {
  855. MapItemData itemData = new MapItemData();
  856. itemData.id = id;
  857. itemData.typeId = itemId;
  858. itemData.postion = mapItemPos.position;
  859. map.CreateMapItem(itemData);
  860. }
  861. }
  862. }
  863. public void GetItem(Player player, int craftId, int itemId)
  864. {
  865. ISFSObject data = new SFSObject();
  866. data.PutInt("u", player.userId);
  867. data.PutInt("c", craftId);
  868. data.PutInt("i", itemId);
  869. Message msg = new Message(Command.GetMapItem, data);
  870. EnSendQueue(msg);
  871. }
  872. private void OnGetItem(string cmd, ISFSObject data)
  873. {
  874. int itemId = data.GetInt("i");
  875. int craftId = data.GetInt ("c");
  876. int userId = data.GetInt("u");
  877. MapItem mapItem = battleSession.GetBattleController().GetMap().GetMapItem(itemId);
  878. if(mapItem != null)
  879. {
  880. BattleObject battleObj = battleSession.GetBattleController ().GetMap ().GetBattleObject (craftId);
  881. if(battleObj != null)
  882. {
  883. Buff buff = BuffManager.GetInstance().AddBuff(8, battleObj, battleObj);
  884. }
  885. mapItem.Remove();
  886. }
  887. }
  888. public static void ParseFlag(ISFSArray arr, BattleSession battleSession)
  889. {
  890. Map map = battleSession.GetBattleController().GetMap();
  891. for(int i=0; i<arr.Count; i++)
  892. {
  893. ISFSObject obj = arr.GetSFSObject(i);
  894. TeamUtil.Team team = TeamUtil.GetTeam(obj.GetInt("t"));
  895. int x = obj.GetInt("x");
  896. int y = obj.GetInt("y");
  897. int craftId = obj.GetInt("c");
  898. FlagItem flag = map.GetFlag(team);
  899. bool isNew = false;
  900. if(flag == null)
  901. {
  902. isNew = true;
  903. flag = map.CreateFlag(team);
  904. }
  905. if(craftId != -1)
  906. {
  907. PutFlagToCraft(craftId, flag, map);
  908. }
  909. else if(isNew)
  910. {
  911. flag.PutToBase(map.GetFlagBase(flag.team).position);
  912. string info = Language.GetStr("Flag", "flagPlace");
  913. info = info.Replace("%COLOR%", TeamUtil.GetTeamColorString(flag.team));
  914. info = info.Replace("%TEAM%", TeamUtil.GetTeamName(flag.team));
  915. }
  916. else if(x == 0 && y == 0)
  917. {
  918. flag.PutToBase(map.GetFlagBase(flag.team).position);
  919. }
  920. else if(flag.IsOnGround())
  921. {
  922. flag.position = new Vector3(x, 0, y);
  923. AStarNode astarNode = map.GetAStarNodeByPosition(flag.position);
  924. if(!astarNode.IsWalkable(null))
  925. {
  926. astarNode = map.FindNearestEmptyAstarNode(astarNode.X, astarNode.Y);
  927. if(astarNode != null)
  928. {
  929. flag.position = new Vector3(Map.GetCenterX(astarNode.X), 0, Map.GetCenterZ(astarNode.Y));
  930. }
  931. }
  932. flag.PutGround(flag.position);
  933. }
  934. }
  935. }
  936. public static void PutFlagToCraft(int id, FlagItem flag, Map map)
  937. {
  938. Craft craft = map.GetBattleObject(id) as Craft;
  939. if(craft != null)
  940. {
  941. flag.LinkToCraft(craft);
  942. }
  943. else
  944. {
  945. flag.WaitToLinkId(id);
  946. }
  947. }
  948. private void OnFlagSync(string cmd, ISFSObject data)
  949. {
  950. ParseFlag(data.GetSFSArray("l"), battleSession);
  951. }
  952. private void OnAddFlag(string cmd, ISFSObject data)
  953. {
  954. ParseFlag(data.GetSFSArray("l"), battleSession);
  955. }
  956. public void GetFlag(Player player, Craft hero, FlagItem flag)
  957. {
  958. if(hero != null)
  959. {
  960. ISFSObject data = new SFSObject();
  961. data.PutInt("i", hero.id);
  962. data.PutInt("t", flag.team.GetHashCode());
  963. data.PutInt("u", player.userId);
  964. Message msg = new Message(Command.GetFlag, data);
  965. EnSendQueue(msg);
  966. }
  967. }
  968. private void OnGetFlag(string cmd, ISFSObject data)
  969. {
  970. Map map = battleSession.GetBattleController().GetMap();
  971. int craftId = data.GetInt("i");
  972. TeamUtil.Team team = TeamUtil.GetTeam(data.GetInt("t"));
  973. Craft craft = map.GetBattleObject(craftId) as Craft;
  974. if(craft != null)
  975. {
  976. FlagItem flag = map.GetFlag(team);
  977. if(flag != null)
  978. flag.LinkToCraft(craft);
  979. string info = Language.GetStr("Flag", "flagGet");
  980. info = info.Replace("%COLOR%", TeamUtil.GetTeamColorString(craft.team));
  981. info = info.Replace("%NICK%", craft.nick);
  982. info = info.Replace("%TEAM%", TeamUtil.GetTeamName(team));
  983. battleSession.GetBattleController().battleUI.ShowBottomTip(info);
  984. }
  985. }
  986. public void ReturnFlag(Player player, Craft craft, FlagItem flag)
  987. {
  988. PutFlag(player, craft, flag);
  989. }
  990. public void PutFlag(Player player, Craft craft, FlagItem flag)
  991. {
  992. ISFSObject data = new SFSObject();
  993. data.PutInt("t", flag.team.GetHashCode());
  994. data.PutInt("u", player.userId);
  995. data.PutInt ("i", craft.id);
  996. data.PutInt("x", 0);
  997. data.PutInt("y", 0);
  998. Message msg = new Message(Command.PutFlag, data);
  999. EnSendQueue(msg);
  1000. }
  1001. private void OnPutFlag(string cmd, ISFSObject data)
  1002. {
  1003. TeamUtil.Team team = TeamUtil.GetTeam(data.GetInt("t"));
  1004. int x = data.GetInt("x");
  1005. int y = data.GetInt("y");
  1006. Map map = battleSession.GetBattleController().GetMap();
  1007. FlagItem flag = map.GetFlag(team);
  1008. if(flag == null)
  1009. return;
  1010. FlagBase flagBase = map.GetFlagBase(team);
  1011. flag.PutToBase(flagBase.position);
  1012. Player player = battleSession.GetPlayer(data.GetInt("u"));
  1013. if(player != null)
  1014. {
  1015. string info = Language.GetStr("Flag", "flagReturn");
  1016. info = info.Replace("%COLOR%", TeamUtil.GetTeamColorString(player.team));
  1017. info = info.Replace("%NICK%", player.nick);
  1018. info = info.Replace("%TEAM%", TeamUtil.GetTeamName(team));
  1019. battleSession.GetBattleController().battleUI.ShowBottomTip(info);
  1020. }
  1021. }
  1022. public void CaptureFlag(Player player, Craft hero)
  1023. {
  1024. Map map = battleSession.GetBattleController().GetMap();
  1025. if(hero != null)
  1026. {
  1027. FlagItem myFlag = map.GetFlag(player.team);
  1028. FlagItem oppFlag = map.GetFlag(TeamUtil.GetOpponentTeam(player.team));
  1029. if(myFlag != null && oppFlag != null && oppFlag.linkedCraftId == hero.id && myFlag.IsInBase())
  1030. {
  1031. ISFSObject data = new SFSObject();
  1032. data.PutInt("i", hero.id);
  1033. data.PutInt("t", player.team.GetHashCode());
  1034. data.PutInt("u", player.userId);
  1035. Message msg = new Message(Command.CaptureFlag, data);
  1036. EnSendQueue(msg);
  1037. }
  1038. }
  1039. }
  1040. private void OnCaptureFlag(string cmd, ISFSObject data)
  1041. {
  1042. Player player = battleSession.GetPlayer(data.GetInt("u"));
  1043. if(player != null)
  1044. {
  1045. string info = Language.GetStr("Flag", "flagCapture");
  1046. info = info.Replace("%COLOR%", TeamUtil.GetTeamColorString(player.team));
  1047. info = info.Replace("%NICK%", player.nick);
  1048. info = info.Replace("%TEAM%", TeamUtil.GetTeamName(player.team));
  1049. battleSession.GetBattleController().battleUI.ShowBottomTip(info);
  1050. }
  1051. battleSession.GetBattleController().GetMap().CleanFlag();
  1052. }
  1053. private void OnBattleInfo(string cmd, ISFSObject data)
  1054. {
  1055. if(data.ContainsKey("s"))
  1056. DealScore(data.GetSFSObject("s"));
  1057. // if(data.ContainsKey("p"))
  1058. // DealAiPlayerChange(data.GetSFSObject("p"));
  1059. // if(data.ContainsKey("a"))
  1060. // DealAiTaker(data.GetSFSObject("a"));
  1061. }
  1062. private void DealScore(ISFSObject data)
  1063. {
  1064. BattleController battleController = battleSession.GetBattleController();
  1065. if(battleController == null)
  1066. return;
  1067. if(data.ContainsKey("w"))
  1068. {
  1069. battleController.GameOver(TeamUtil.GetTeam(data.GetInt("w")));
  1070. }
  1071. battleController.blueScore = data.GetInt("b");
  1072. battleController.redScore = data.GetInt("r");
  1073. battleController.battleUI.scoreBar.updateScore(battleController.blueScore, battleController.redScore);
  1074. battleController.clock.freeTimeLeft = (float)data.GetInt("f")/1000f;
  1075. battleController.clock.timeLeft = (float)data.GetInt("t")/1000f;
  1076. if(!battleController.isBattleStart && battleController.clock.freeTimeLeft == 0)
  1077. {
  1078. battleController.isBattleStart = true;
  1079. battleController.GetMap().ClearBlocks(TeamUtil.Team.Yellow);
  1080. }
  1081. if(data.ContainsKey("fl"))
  1082. {
  1083. ParseFlag(data.GetSFSArray("fl"), battleSession);
  1084. }
  1085. }
  1086. // private void DealAiTaker(ISFSObject data)
  1087. // {
  1088. // if(!data.ContainsKey("a"))
  1089. // return;
  1090. //
  1091. // ISFSArray arr = data.GetSFSArray("a");
  1092. // for(int i=0; i<arr.Count; i++)
  1093. // {
  1094. // ISFSObject obj = arr.GetSFSObject(i);
  1095. // int userId = obj.GetInt("u");
  1096. // int aiTaker = obj.GetInt("t");
  1097. // Player player = battleSession.GetPlayer(userId);
  1098. // if(player != null && player.IsAI())
  1099. // {
  1100. // player.aiTakerId = aiTaker;
  1101. // }
  1102. // }
  1103. // }
  1104. //
  1105. // private void DealAiPlayerChange(ISFSObject data)
  1106. // {
  1107. // if(data.ContainsKey("a"))
  1108. // {
  1109. // ISFSArray arr = data.GetSFSArray("a");
  1110. // for(int i=0; i<arr.Count; i++)
  1111. // {
  1112. // ISFSObject obj = arr.GetSFSObject(i);
  1113. // battleSession.AddPlayer(obj.GetInt("u"), obj.GetUtfString("n"), TeamUtil.GetTeam(obj.GetInt("t")), -1);
  1114. // }
  1115. // }
  1116. // }
  1117. public void SimpleSync()
  1118. {
  1119. ISFSObject data = new SFSObject ();
  1120. Message msg = new SyncMessage ();
  1121. EnSendQueue (msg);
  1122. }
  1123. private void OnSimpleSync(string cmd, ISFSObject data)
  1124. {
  1125. }
  1126. private void OnAITaker(string cmd, ISFSObject data)
  1127. {
  1128. Debuger.Log ("OnAITaker "+data.GetDump());
  1129. int aiTaker = data.GetInt ("a");
  1130. battleSession.aiTakerUserId = aiTaker;
  1131. }
  1132. public void PrepareSwap(int id, int craftId)
  1133. {
  1134. ISFSObject data = new SFSObject();
  1135. data.PutInt("i", id);
  1136. data.PutInt("s", craftId);
  1137. Message msg = new Message(Command.PrepareSwap, data);
  1138. EnSendQueue(msg);
  1139. }
  1140. private void OnPrepareSwap(string cmd, ISFSObject data)
  1141. {
  1142. int id = data.GetInt ("i");
  1143. int craftId = data.GetInt ("s");
  1144. Craft craft = battleSession.GetBattleController().GetMap().GetBattleObject(id) as Craft;
  1145. if(craft != null)
  1146. craft.GetSwapManager().PrepareSwap(craftId);
  1147. }
  1148. public void RequestSwap(int id, int craftId)
  1149. {
  1150. ISFSObject data = new SFSObject();
  1151. data.PutInt("i", id);
  1152. data.PutInt("s", craftId);
  1153. Craft craft = battleSession.GetBattleController ().GetMap ().GetBattleObject (id) as Craft;
  1154. data.PutSFSObject ("eq", craft.GetEquipModifyData());
  1155. Message msg = new Message(Command.RequestSwap, data);
  1156. EnSendQueue(msg);
  1157. }
  1158. private void OnRequestSwap(string cmd, ISFSObject data)
  1159. {
  1160. int id = data.GetInt ("i");
  1161. int craftId = data.GetInt ("s");
  1162. Craft craft = battleSession.GetBattleController().GetMap().GetBattleObject(id) as Craft;
  1163. if(craft != null)
  1164. {
  1165. CraftEquipModify equipModify = new CraftEquipModify (data.GetSFSObject ("eq"));
  1166. craft.GetSwapManager().RequestedSwap();
  1167. battleSession.GetBattleController().CreateCapsule(id, craftId, equipModify);
  1168. }
  1169. }
  1170. public void ExitBattle()
  1171. {
  1172. Session.GetInstance().myUserData.vipNextRound = false;
  1173. LoadingPanel loadingPanel = LoadingPanel.Show(false);
  1174. loadingPanel.ReachTargetProgress.AddListener (CheckSaveReplay);
  1175. LoadingPanel.Increase(0.2f);
  1176. battleSession.GetBattleController().isExit = true;
  1177. // BattleController.battleType = BattleController.BattleType.Menu;
  1178. // LoadingPanel.LoadScene(Config.Scene.Battle.ToString());
  1179. }
  1180. private void CheckSaveReplay()
  1181. {
  1182. ReplayManager.GetInstance ().Save (battleSession);
  1183. LoadingPanel loadingPanel = LoadingPanel.Show();
  1184. loadingPanel.ReachTargetProgress.RemoveListener (CheckSaveReplay);
  1185. LoadingPanel.Increase(0.3f);
  1186. ISFSObject data = new SFSObject();
  1187. data.PutInt ("u", myUserId);
  1188. Message msg = new Message(Command.BattleExit, data);
  1189. EnSendQueue(msg);
  1190. }
  1191. private void OnExitBattle(string cmd, ISFSObject data)
  1192. {
  1193. int userId = data.GetInt ("u");
  1194. if(userId == myUserId)
  1195. {
  1196. ToMenu ();
  1197. }
  1198. }
  1199. public void ToMenu()
  1200. {
  1201. BattleController.battleType = BattleController.BattleType.Menu;
  1202. LoadingPanel panel = LoadingPanel.Show ();
  1203. LoadingPanel.Increase (0.1f);
  1204. panel.ReachTargetProgress.AddListener (()=>{
  1205. LoadingPanel.LoadScene(Config.Scene.Battle.ToString());
  1206. });
  1207. }
  1208. public void ClanInit()
  1209. {
  1210. ISFSObject data = new SFSObject ();
  1211. Message msg = new Message (Command.ClanInit, data);
  1212. EnSendQueue (msg);
  1213. }
  1214. private void OnClanInit(string cmd, ISFSObject data)
  1215. {
  1216. }
  1217. private void OnClanOnline(string cmd, ISFSObject data)
  1218. {
  1219. if(data.ContainsKey("a"))
  1220. InviteManager.GetInstance ().SetData (data.GetSFSArray ("a"));
  1221. if (data.ContainsKey ("i")) {
  1222. InviteManager.GetInstance ().Add (StringUtil.ToInt (data.GetUtfString ("i")), InviteData.State.Online);
  1223. InviteManager.GetInstance ().Sort ();
  1224. }
  1225. }
  1226. private void OnClanOffline(string cmd, ISFSObject data)
  1227. {
  1228. InviteManager.GetInstance ().Offline (StringUtil.ToInt (data.GetUtfString("i")));
  1229. }
  1230. private void OnClanBattle(string cmd, ISFSObject data)
  1231. {
  1232. int id = StringUtil.ToInt (data.GetUtfString("i"));
  1233. InviteManager.GetInstance ().Battle (id);
  1234. }
  1235. public void ClanMessage(string content)
  1236. {
  1237. ISFSObject data = new SFSObject ();
  1238. data.PutInt ("u", myUserId);
  1239. data.PutUtfString ("m", content);
  1240. Message msg = new Message (Command.ClanMessage, data);
  1241. EnSendQueue (msg);
  1242. }
  1243. private void OnClanMessage(string cmd, ISFSObject data)
  1244. {
  1245. if (!CheckError (cmd, data)) {
  1246. int userId = data.GetInt ("u");
  1247. string content = data.GetUtfString ("m");
  1248. ChatManager.GetInstance ().AddClanMsg (userId, content);
  1249. }
  1250. }
  1251. public void ClanJoin()
  1252. {
  1253. if (Session.GetInstance ().myUserData.clanId > 0) {
  1254. LocalToRemote ();
  1255. ISFSObject data = new SFSObject ();
  1256. data.PutInt (UserSFSObjectLabel.CLAN, Session.GetInstance().myUserData.clanId);
  1257. Message msg = new Message (Command.ClanJoin, data);
  1258. EnSendQueue (msg);
  1259. }
  1260. }
  1261. private void OnClanJoin(string cmd, ISFSObject data)
  1262. {
  1263. Ready ();
  1264. }
  1265. public void ClanLeft()
  1266. {
  1267. Message msg = new Message (Command.ClanLeft, new SFSObject ());
  1268. EnSendQueue (msg);
  1269. }
  1270. private void OnClanLeft(string cmd, ISFSObject data)
  1271. {
  1272. List<Player> list = battleSession.GetPlayerList ();
  1273. for(int i=0; i<list.Count; i++)
  1274. {
  1275. Player player = list [i];
  1276. if(player.isMe || player.GetHero().GetCraft() == null)
  1277. continue;
  1278. battleSession.GetBattleController ().DealDeath (player.userId, player.GetHero().GetCraft().id, 0);
  1279. }
  1280. }
  1281. public void TeamInvite(int id)
  1282. {
  1283. ISFSObject data = new SFSObject ();
  1284. data.PutUtfString ("t", id.ToString());
  1285. Message msg = new Message (Command.TeamInvite, data);
  1286. EnSendQueue (msg);
  1287. }
  1288. private void OnTeamInvite(string cmd, ISFSObject data)
  1289. {
  1290. int id = StringUtil.ToInt(data.GetUtfString ("t"));
  1291. string nick = UserCache.GetNick (id);
  1292. string info = Language.GetStr ("Invite", "teamInviteConfirm").Replace ("%NICK%", nick);
  1293. AlertPanel.Show (info, AlertPanel.YES|AlertPanel.NO, (AlertCloseEvent evt)=>{
  1294. if(evt.detail == AlertPanel.YES)
  1295. {
  1296. TeamJoin(id);
  1297. }
  1298. });
  1299. }
  1300. public void TeamJoin(int id)
  1301. {
  1302. ISFSObject data = new SFSObject ();
  1303. data.PutUtfString ("t", id.ToString());
  1304. Message msg = new Message (Command.TeamJoin, data);
  1305. EnSendQueue (msg);
  1306. }
  1307. private void OnTeamJoin(string cmd, ISFSObject data)
  1308. {
  1309. }
  1310. public void TeamLeft()
  1311. {
  1312. ISFSObject data = new SFSObject ();
  1313. Message msg = new Message (Command.TeamLeft, data);
  1314. EnSendQueue (msg);
  1315. }
  1316. private void OnTeamLeft(string cmd, ISFSObject data)
  1317. {
  1318. }
  1319. public void TeamKick(int id)
  1320. {
  1321. ISFSObject data = new SFSObject ();
  1322. data.PutUtfString ("t", id.ToString());
  1323. Message msg = new Message (Command.TeamKick, data);
  1324. EnSendQueue (msg);
  1325. }
  1326. private void OnTeamKick(string cmd, ISFSObject data)
  1327. {
  1328. }
  1329. private void OnTeamState(string cmd, ISFSObject data)
  1330. {
  1331. int leader = StringUtil.ToInt (data.GetUtfString (UserSFSObjectLabel.TEAM_LEADER_ID));
  1332. if (data.ContainsKey (UserSFSObjectLabel.TEAM)) {
  1333. InviteManager.GetInstance().TeamState(leader, data.GetSFSArray(UserSFSObjectLabel.TEAM));
  1334. }
  1335. else{
  1336. InviteManager.GetInstance().TeamState(leader, null);
  1337. }
  1338. }
  1339. public void Dispose()
  1340. {
  1341. Disconnect();
  1342. }
  1343. }