MessageManager.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  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.Base.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 (mapId);
  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.PutFloat("ex", craft.moveDestination.x);
  422. obj.PutFloat("ey", craft.moveDestination.z);
  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.GetFloat("ex"), obj.GetFloat("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. Move (craft, new Vector3(Map.GetCenterX(col), 0, Map.GetCenterZ(row)));
  571. }
  572. public void Move(Craft craft, Vector3 pos)
  573. {
  574. Message msg = new MoveMessage(craft.id, pos);
  575. EnSendQueue(msg);
  576. // if(craft.aiType == AI.AIType.Manual)
  577. // {
  578. // craft.MoveTo(col, row);
  579. // }
  580. }
  581. private void OnMove(string cmd, ISFSObject data)
  582. {
  583. int craftId = data.GetInt("i");
  584. float x = data.GetFloat("x");
  585. float y = data.GetFloat("y");
  586. // float sx = data.GetFloat ("sx");
  587. // float sy = data.GetFloat ("sy");
  588. // Vector3 startPos = new Vector3 (sx, 0, sy);
  589. Craft craft = battleSession.GetBattleController().GetMap().GetBattleObject(craftId) as Craft;
  590. if(craft != null)
  591. {
  592. // if(NumberUtil.distanceVector3(craft.position, startPos, true) > craftSyncDistance)
  593. // craft.position = startPos;
  594. craft.MoveTo(x, y);
  595. }
  596. else
  597. {
  598. Debuger.LogError("onReceiveMove::can not find craft with id("+craftId+")");
  599. }
  600. }
  601. public void Target(Craft craft)
  602. {
  603. ISFSObject data = new SFSObject();
  604. data.PutInt("i", craft.id);
  605. PackTarget(craft.target, data);
  606. Message msg = new Message(Command.Target, data);
  607. EnSendQueue(msg);
  608. }
  609. private void PackTarget(ITarget target, ISFSObject data)
  610. {
  611. if(target is BattleObject)
  612. {
  613. BattleObject battleObj = target as BattleObject;
  614. data.PutInt("ti", battleObj.id);
  615. data.PutInt("tt", TargetUtil.GetTargetType(battleObj).GetHashCode());
  616. }
  617. }
  618. private void OnTarget(string cmd, ISFSObject data)
  619. {
  620. Craft craft = battleSession.GetBattleController().GetMap().GetBattleObject(data.GetInt("i")) as Craft;
  621. if(craft != null && craft.userId != myUserId)
  622. {
  623. craft.target = ParseTarget(data);
  624. if(craft.target == null)
  625. {
  626. craft.StopMove();
  627. }
  628. }
  629. }
  630. private ITarget ParseTarget(ISFSObject data)
  631. {
  632. if(data.ContainsKey("ti"))
  633. {
  634. int targetId = data.GetInt("ti");
  635. TargetUtil.TargetType targetType = TargetUtil.GetTargetTypeByCode(data.GetInt("tt"));
  636. if(targetType == TargetUtil.TargetType.CrystalBase)
  637. {
  638. return battleSession.GetBattleController().GetMap().GetCrystalBase(targetId).GetStation();
  639. }
  640. else if(targetType == TargetUtil.TargetType.Craft)
  641. {
  642. return battleSession.GetBattleController().GetMap().GetBattleObject(targetId);
  643. }
  644. }
  645. return null;
  646. }
  647. public void Power(Power power, ITarget target)
  648. {
  649. ISFSObject powerData = power.PackMessage(target, battleSession.GetBattleController());
  650. Message msg = new PowerMessage(power, powerData);
  651. EnSendQueue(msg);
  652. }
  653. private void OnPower(string cmd, ISFSObject data)
  654. {
  655. int userId = data.GetInt ("u");
  656. int craftId = data.GetInt("i");
  657. int typeId = data.GetInt ("t");
  658. int powerId = data.GetInt("p");
  659. ISFSObject powerData = data.GetSFSObject("d");
  660. IPowerOwner owner = null;
  661. if(typeId == MapObjectUtil.TypeId.UAV.GetHashCode())
  662. owner = battleSession.GetBattleController().GetMap().GetUAVehicle(userId) as IPowerOwner;
  663. else
  664. owner = battleSession.GetBattleController().GetMap().GetBattleObject(craftId) as IPowerOwner;
  665. if(owner != null)
  666. {
  667. Vector3 pos = new Vector3(data.GetFloat("x"), 0, data.GetFloat("y"));
  668. if(NumberUtil.distanceVector3(owner.position, pos, true) > craftSyncDistance)
  669. owner.position = pos;
  670. owner.GetPowerManager ().ParsePower (powerId, powerData, battleSession.GetBattleController ());
  671. }
  672. else
  673. {
  674. Debuger.LogError("onReceivePower::can not find craft with id("+craftId+")");
  675. }
  676. }
  677. public void Dead(BattleObject battleObj, int killerUserId, int killerCraftId)
  678. {
  679. ISFSObject data = new SFSObject();
  680. data.PutInt("i", battleObj.id);
  681. data.PutInt("m", battleObj.team.GetHashCode());
  682. data.PutInt("c", battleObj is Craft ? (battleObj as Craft).GetCraftId() : 0);
  683. data.PutInt("t", battleObj.typeId);
  684. data.PutInt("u", battleObj.userId);
  685. data.PutInt("ku", killerUserId);
  686. data.PutInt("kc", killerCraftId);
  687. data.PutInt("x", battleObj.col);
  688. data.PutInt("y", battleObj.row);
  689. Message msg = new Message(Command.Dead, data);
  690. EnSendQueue(msg);
  691. if(battleObj.userId == myUserId)
  692. {
  693. battleSession.GetBattleController().DealDeath(battleObj.userId, battleObj.id, killerUserId);
  694. }
  695. }
  696. private void OnDead(string cmd, ISFSObject data)
  697. {
  698. int objid = data.GetInt("i");
  699. TeamUtil.Team team = TeamUtil.GetTeam (data.GetInt("m"));
  700. int killerUserId = data.GetInt("ku");
  701. Map map = battleSession.GetBattleController ().GetMap ();
  702. IBattleObject battleObj = map.GetBattleObject(objid);
  703. if (battleObj != null) {
  704. battleSession.GetBattleController().DealDeath(battleObj.userId, battleObj.id, killerUserId);
  705. }
  706. team = TeamUtil.GetOpponentTeam (team);
  707. FlagItem flag = map.GetFlag(team);
  708. if(flag != null && flag.linkedCraftId == objid)
  709. {
  710. int x = data.GetInt("x");
  711. int y = data.GetInt("y");
  712. Vector3 pos = new Vector3 (Map.GetCenterX(x), 0, Map.GetCenterZ(y));
  713. flag.PutGround(pos);
  714. Player player = battleSession.GetPlayer(data.GetInt("u"));
  715. if(player != null)
  716. {
  717. string info = Language.GetStr("Flag", "flagLose");
  718. info = info.Replace("%COLOR%", TeamUtil.GetTeamColorString(player.team));
  719. info = info.Replace("%NICK%", player.nick);
  720. info = info.Replace("%TEAM%", TeamUtil.GetTeamName(team));
  721. battleSession.GetBattleController().battleUI.ShowBottomTip(info);
  722. }
  723. }
  724. }
  725. public void OccupyStation(Craft craft, CrystalBase crystalBase)
  726. {
  727. ISFSObject data = new SFSObject();
  728. data.PutInt("c", crystalBase.id);
  729. data.PutInt("t", craft.team.GetHashCode());
  730. data.PutInt("u", craft.userId);
  731. Message msg = new Message(Command.Station, data);
  732. EnSendQueue(msg);
  733. }
  734. private void OnOccupyStation(string cmd, ISFSObject data)
  735. {
  736. if(data.ContainsKey("i"))
  737. {
  738. Map map = battleSession.GetBattleController().GetMap();
  739. int userId = data.GetInt ("u");
  740. StationData stationData = new StationData();
  741. stationData.team = TeamUtil.GetTeam(data.GetInt("t"));
  742. stationData.userId = userId;
  743. stationData.id = data.GetInt("i");
  744. stationData.crystalId = data.GetInt("c");
  745. Station station = map.CreateStation(stationData, battleSession.GetBattleController());
  746. Player player = battleSession.GetPlayer(userId);
  747. if(player != null)
  748. {
  749. player.station++;
  750. string colorStr = TeamUtil.GetTeamColorString(player.team);
  751. string info = Language.GetStr("GameInfo", "occupy");
  752. if(player.userId == myUserId)
  753. {
  754. info = info.Replace("%NAME%", Language.GetStr("GameInfo", "you"));
  755. }
  756. else
  757. {
  758. info = info.Replace("%NAME%", player.nick);
  759. }
  760. info = info.Replace("%COLOR%", colorStr);
  761. battleSession.GetBattleController().battleUI.ShowBottomTip(info);
  762. }
  763. }
  764. }
  765. private bool radioHold = false;
  766. public void Radio(int index)
  767. {
  768. if(!radioHold)
  769. {
  770. ISFSObject data = new SFSObject();
  771. data.PutInt("i", index);
  772. data.PutInt("u", myUserId);
  773. int craftId = battleSession.GetBattleController ().GetCtrlCraft() != null ? battleSession.GetBattleController ().GetCtrlCraft().id : 0;
  774. data.PutInt ("c", craftId);
  775. radioHold = true;
  776. Message msg = new Message(Command.Radio, data);
  777. EnSendQueue(msg);
  778. }
  779. battleSession.GetBattleController().battleUI.HideRadioPanel();
  780. }
  781. private void OnRadio(string cmd, ISFSObject data)
  782. {
  783. int userId = data.GetInt("u");
  784. int index = data.GetInt("i");
  785. int craftId = data.GetInt ("c");
  786. if(userId == myUserId)
  787. {
  788. radioHold = false;
  789. }
  790. battleSession.GetBattleController().battleUI.DealRadio(userId, craftId, index);
  791. }
  792. // private void ResetPlayer(Player player)
  793. // {
  794. // player.heroDeadTime = 0;
  795. // if(player.heroCraft != null)
  796. // {
  797. // battleSession.GetBattleController().DoDeath(player.heroCraft);
  798. // }
  799. // player.ClearHero();
  800. // }
  801. // public static void ParseStaticMapObject(ISFSArray arr, BattleSession battleSession)
  802. // {
  803. // ParseBuilding(arr);
  804. // for(int i=0; i<arr.Count; i++)
  805. // {
  806. // ISFSObject obj = arr.GetSFSObject(i);
  807. // MapObjectUtil.TypeId typeId = MapObjectUtil.GetTypeIdByCode(obj.GetInt("s"));
  808. // if(typeId == MapObjectUtil.TypeId.Station)
  809. // {
  810. // StationData stationData = new StationData();
  811. // stationData.id = obj.GetInt("i");
  812. // stationData.team = TeamUtil.GetTeam(obj.GetInt("t"));
  813. // stationData.crystalId = obj.GetInt("c");
  814. // Station station = battleSession.GetBattleController().GetMap().CreateStation(stationData, battleSession.GetBattleController());
  815. //
  816. // if(obj.ContainsKey("ti"))
  817. // {
  818. // int itemId = obj.GetInt("ti");
  819. // int itemTypeId = obj.GetInt("tt");
  820. // MapItemData itemData = new MapItemData();
  821. // itemData.id = itemId;
  822. // itemData.typeId = itemTypeId;
  823. // itemData.postion = station.itemPosition;
  824. // itemData.team = station.team;
  825. // battleSession.GetBattleController().GetMap().CreateMapItem(itemData);
  826. // }
  827. // }
  828. // else if(typeId == MapObjectUtil.TypeId.Door)
  829. // {
  830. //
  831. // }
  832. // }
  833. // }
  834. private void OnAddMapItem(string cmd, ISFSObject data)
  835. {
  836. Map map = battleSession.GetBattleController().GetMap();
  837. ISFSArray arr = data.GetSFSArray("l");
  838. for(int i=0; i<arr.Count; i++)
  839. {
  840. ISFSObject obj = arr.GetSFSObject(i);
  841. int id = obj.GetInt("i");
  842. int itemId = obj.GetInt("t");
  843. MapItemPos mapItemPos = map.GetMapItemPos(id);
  844. if(mapItemPos != null)
  845. {
  846. MapItemData itemData = new MapItemData();
  847. itemData.id = id;
  848. itemData.typeId = itemId;
  849. itemData.postion = mapItemPos.position;
  850. map.CreateMapItem(itemData);
  851. }
  852. }
  853. }
  854. public void GetItem(Player player, int craftId, int itemId)
  855. {
  856. ISFSObject data = new SFSObject();
  857. data.PutInt("u", player.userId);
  858. data.PutInt("c", craftId);
  859. data.PutInt("i", itemId);
  860. Message msg = new Message(Command.GetMapItem, data);
  861. EnSendQueue(msg);
  862. }
  863. private void OnGetItem(string cmd, ISFSObject data)
  864. {
  865. int itemId = data.GetInt("i");
  866. int craftId = data.GetInt ("c");
  867. int userId = data.GetInt("u");
  868. MapItem mapItem = battleSession.GetBattleController().GetMap().GetMapItem(itemId);
  869. if(mapItem != null)
  870. {
  871. BattleObject battleObj = battleSession.GetBattleController ().GetMap ().GetBattleObject (craftId);
  872. if(battleObj != null)
  873. {
  874. Buff buff = BuffManager.GetInstance().AddBuff(8, battleObj, battleObj);
  875. }
  876. mapItem.Remove();
  877. }
  878. }
  879. public static void ParseFlag(ISFSArray arr, BattleSession battleSession)
  880. {
  881. Map map = battleSession.GetBattleController().GetMap();
  882. for(int i=0; i<arr.Count; i++)
  883. {
  884. ISFSObject obj = arr.GetSFSObject(i);
  885. TeamUtil.Team team = TeamUtil.GetTeam(obj.GetInt("t"));
  886. int x = obj.GetInt("x");
  887. int y = obj.GetInt("y");
  888. int craftId = obj.GetInt("c");
  889. FlagItem flag = map.GetFlag(team);
  890. bool isNew = false;
  891. if(flag == null)
  892. {
  893. isNew = true;
  894. flag = map.CreateFlag(team);
  895. }
  896. if(craftId != -1)
  897. {
  898. PutFlagToCraft(craftId, flag, map);
  899. }
  900. else if(isNew)
  901. {
  902. flag.PutToBase(map.GetFlagBase(flag.team).position);
  903. string info = Language.GetStr("Flag", "flagPlace");
  904. info = info.Replace("%COLOR%", TeamUtil.GetTeamColorString(flag.team));
  905. info = info.Replace("%TEAM%", TeamUtil.GetTeamName(flag.team));
  906. }
  907. else if(x == 0 && y == 0)
  908. {
  909. flag.PutToBase(map.GetFlagBase(flag.team).position);
  910. }
  911. else if(flag.IsOnGround())
  912. {
  913. flag.position = new Vector3(x, 0, y);
  914. flag.PutGround(flag.position);
  915. }
  916. }
  917. }
  918. public static void PutFlagToCraft(int id, FlagItem flag, Map map)
  919. {
  920. Craft craft = map.GetBattleObject(id) as Craft;
  921. if(craft != null)
  922. {
  923. flag.LinkToCraft(craft);
  924. }
  925. else
  926. {
  927. flag.WaitToLinkId(id);
  928. }
  929. }
  930. private void OnFlagSync(string cmd, ISFSObject data)
  931. {
  932. ParseFlag(data.GetSFSArray("l"), battleSession);
  933. }
  934. private void OnAddFlag(string cmd, ISFSObject data)
  935. {
  936. ParseFlag(data.GetSFSArray("l"), battleSession);
  937. }
  938. public void GetFlag(Player player, Craft hero, FlagItem flag)
  939. {
  940. if(hero != null)
  941. {
  942. ISFSObject data = new SFSObject();
  943. data.PutInt("i", hero.id);
  944. data.PutInt("t", flag.team.GetHashCode());
  945. data.PutInt("u", player.userId);
  946. Message msg = new Message(Command.GetFlag, data);
  947. EnSendQueue(msg);
  948. }
  949. }
  950. private void OnGetFlag(string cmd, ISFSObject data)
  951. {
  952. Map map = battleSession.GetBattleController().GetMap();
  953. int craftId = data.GetInt("i");
  954. TeamUtil.Team team = TeamUtil.GetTeam(data.GetInt("t"));
  955. Craft craft = map.GetBattleObject(craftId) as Craft;
  956. if(craft != null)
  957. {
  958. FlagItem flag = map.GetFlag(team);
  959. if(flag != null)
  960. flag.LinkToCraft(craft);
  961. string info = Language.GetStr("Flag", "flagGet");
  962. info = info.Replace("%COLOR%", TeamUtil.GetTeamColorString(craft.team));
  963. info = info.Replace("%NICK%", craft.nick);
  964. info = info.Replace("%TEAM%", TeamUtil.GetTeamName(team));
  965. battleSession.GetBattleController().battleUI.ShowBottomTip(info);
  966. }
  967. }
  968. public void ReturnFlag(Player player, Craft craft, FlagItem flag)
  969. {
  970. PutFlag(player, craft, flag);
  971. }
  972. public void PutFlag(Player player, Craft craft, FlagItem flag)
  973. {
  974. ISFSObject data = new SFSObject();
  975. data.PutInt("t", flag.team.GetHashCode());
  976. data.PutInt("u", player.userId);
  977. data.PutInt ("i", craft.id);
  978. data.PutInt("x", 0);
  979. data.PutInt("y", 0);
  980. Message msg = new Message(Command.PutFlag, data);
  981. EnSendQueue(msg);
  982. }
  983. private void OnPutFlag(string cmd, ISFSObject data)
  984. {
  985. TeamUtil.Team team = TeamUtil.GetTeam(data.GetInt("t"));
  986. int x = data.GetInt("x");
  987. int y = data.GetInt("y");
  988. Map map = battleSession.GetBattleController().GetMap();
  989. FlagItem flag = map.GetFlag(team);
  990. if(flag == null)
  991. return;
  992. FlagBase flagBase = map.GetFlagBase(team);
  993. flag.PutToBase(flagBase.position);
  994. Player player = battleSession.GetPlayer(data.GetInt("u"));
  995. if(player != null)
  996. {
  997. string info = Language.GetStr("Flag", "flagReturn");
  998. info = info.Replace("%COLOR%", TeamUtil.GetTeamColorString(player.team));
  999. info = info.Replace("%NICK%", player.nick);
  1000. info = info.Replace("%TEAM%", TeamUtil.GetTeamName(team));
  1001. battleSession.GetBattleController().battleUI.ShowBottomTip(info);
  1002. }
  1003. }
  1004. public void CaptureFlag(Player player, Craft hero)
  1005. {
  1006. Map map = battleSession.GetBattleController().GetMap();
  1007. if(hero != null)
  1008. {
  1009. FlagItem myFlag = map.GetFlag(player.team);
  1010. FlagItem oppFlag = map.GetFlag(TeamUtil.GetOpponentTeam(player.team));
  1011. if(myFlag != null && oppFlag != null && oppFlag.linkedCraftId == hero.id && myFlag.IsInBase())
  1012. {
  1013. ISFSObject data = new SFSObject();
  1014. data.PutInt("i", hero.id);
  1015. data.PutInt("t", player.team.GetHashCode());
  1016. data.PutInt("u", player.userId);
  1017. Message msg = new Message(Command.CaptureFlag, data);
  1018. EnSendQueue(msg);
  1019. }
  1020. }
  1021. }
  1022. private void OnCaptureFlag(string cmd, ISFSObject data)
  1023. {
  1024. Player player = battleSession.GetPlayer(data.GetInt("u"));
  1025. if(player != null)
  1026. {
  1027. string info = Language.GetStr("Flag", "flagCapture");
  1028. info = info.Replace("%COLOR%", TeamUtil.GetTeamColorString(player.team));
  1029. info = info.Replace("%NICK%", player.nick);
  1030. info = info.Replace("%TEAM%", TeamUtil.GetTeamName(player.team));
  1031. battleSession.GetBattleController().battleUI.ShowBottomTip(info);
  1032. }
  1033. battleSession.GetBattleController().GetMap().CleanFlag();
  1034. }
  1035. private void OnBattleInfo(string cmd, ISFSObject data)
  1036. {
  1037. if(data.ContainsKey("s"))
  1038. DealScore(data.GetSFSObject("s"));
  1039. // if(data.ContainsKey("p"))
  1040. // DealAiPlayerChange(data.GetSFSObject("p"));
  1041. // if(data.ContainsKey("a"))
  1042. // DealAiTaker(data.GetSFSObject("a"));
  1043. }
  1044. private void DealScore(ISFSObject data)
  1045. {
  1046. BattleController battleController = battleSession.GetBattleController();
  1047. if(battleController == null)
  1048. return;
  1049. if(data.ContainsKey("w"))
  1050. {
  1051. battleController.GameOver(TeamUtil.GetTeam(data.GetInt("w")));
  1052. }
  1053. battleController.blueScore = data.GetInt("b");
  1054. battleController.redScore = data.GetInt("r");
  1055. battleController.battleUI.scoreBar.updateScore(battleController.blueScore, battleController.redScore);
  1056. battleController.clock.freeTimeLeft = (float)data.GetInt("f")/1000f;
  1057. battleController.clock.timeLeft = (float)data.GetInt("t")/1000f;
  1058. if(!battleController.isBattleStart && battleController.clock.freeTimeLeft == 0)
  1059. {
  1060. battleController.isBattleStart = true;
  1061. battleController.GetMap().ClearBlocks(TeamUtil.Team.Yellow);
  1062. }
  1063. if(data.ContainsKey("fl"))
  1064. {
  1065. ParseFlag(data.GetSFSArray("fl"), battleSession);
  1066. }
  1067. }
  1068. // private void DealAiTaker(ISFSObject data)
  1069. // {
  1070. // if(!data.ContainsKey("a"))
  1071. // return;
  1072. //
  1073. // ISFSArray arr = data.GetSFSArray("a");
  1074. // for(int i=0; i<arr.Count; i++)
  1075. // {
  1076. // ISFSObject obj = arr.GetSFSObject(i);
  1077. // int userId = obj.GetInt("u");
  1078. // int aiTaker = obj.GetInt("t");
  1079. // Player player = battleSession.GetPlayer(userId);
  1080. // if(player != null && player.IsAI())
  1081. // {
  1082. // player.aiTakerId = aiTaker;
  1083. // }
  1084. // }
  1085. // }
  1086. //
  1087. // private void DealAiPlayerChange(ISFSObject data)
  1088. // {
  1089. // if(data.ContainsKey("a"))
  1090. // {
  1091. // ISFSArray arr = data.GetSFSArray("a");
  1092. // for(int i=0; i<arr.Count; i++)
  1093. // {
  1094. // ISFSObject obj = arr.GetSFSObject(i);
  1095. // battleSession.AddPlayer(obj.GetInt("u"), obj.GetUtfString("n"), TeamUtil.GetTeam(obj.GetInt("t")), -1);
  1096. // }
  1097. // }
  1098. // }
  1099. public void SimpleSync()
  1100. {
  1101. ISFSObject data = new SFSObject ();
  1102. Message msg = new SyncMessage ();
  1103. EnSendQueue (msg);
  1104. }
  1105. private void OnSimpleSync(string cmd, ISFSObject data)
  1106. {
  1107. }
  1108. private void OnAITaker(string cmd, ISFSObject data)
  1109. {
  1110. Debuger.Log ("OnAITaker "+data.GetDump());
  1111. int aiTaker = data.GetInt ("a");
  1112. battleSession.aiTakerUserId = aiTaker;
  1113. }
  1114. public void PrepareSwap(int id, int craftId)
  1115. {
  1116. ISFSObject data = new SFSObject();
  1117. data.PutInt("i", id);
  1118. data.PutInt("s", craftId);
  1119. Message msg = new Message(Command.PrepareSwap, data);
  1120. EnSendQueue(msg);
  1121. }
  1122. private void OnPrepareSwap(string cmd, ISFSObject data)
  1123. {
  1124. int id = data.GetInt ("i");
  1125. int craftId = data.GetInt ("s");
  1126. Craft craft = battleSession.GetBattleController().GetMap().GetBattleObject(id) as Craft;
  1127. if(craft != null)
  1128. craft.GetSwapManager().PrepareSwap(craftId);
  1129. }
  1130. public void RequestSwap(int id, int craftId)
  1131. {
  1132. ISFSObject data = new SFSObject();
  1133. data.PutInt("i", id);
  1134. data.PutInt("s", craftId);
  1135. Craft craft = battleSession.GetBattleController ().GetMap ().GetBattleObject (id) as Craft;
  1136. data.PutSFSObject ("eq", craft.GetEquipModifyData());
  1137. Message msg = new Message(Command.RequestSwap, data);
  1138. EnSendQueue(msg);
  1139. }
  1140. private void OnRequestSwap(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. {
  1147. CraftEquipModify equipModify = new CraftEquipModify (data.GetSFSObject ("eq"));
  1148. craft.GetSwapManager().RequestedSwap();
  1149. battleSession.GetBattleController().CreateCapsule(id, craftId, equipModify);
  1150. }
  1151. }
  1152. public void ExitBattle()
  1153. {
  1154. Session.GetInstance().myUserData.vipNextRound = false;
  1155. LoadingPanel loadingPanel = LoadingPanel.Show(false);
  1156. loadingPanel.ReachTargetProgress.AddListener (CheckSaveReplay);
  1157. LoadingPanel.Increase(0.2f);
  1158. battleSession.GetBattleController().isExit = true;
  1159. // BattleController.battleType = BattleController.BattleType.Menu;
  1160. // LoadingPanel.LoadScene(Config.Scene.Battle.ToString());
  1161. }
  1162. private void CheckSaveReplay()
  1163. {
  1164. ReplayManager.GetInstance ().Save (battleSession);
  1165. LoadingPanel loadingPanel = LoadingPanel.Show();
  1166. loadingPanel.ReachTargetProgress.RemoveListener (CheckSaveReplay);
  1167. LoadingPanel.Increase(0.3f);
  1168. ISFSObject data = new SFSObject();
  1169. data.PutInt ("u", myUserId);
  1170. Message msg = new Message(Command.BattleExit, data);
  1171. EnSendQueue(msg);
  1172. }
  1173. private void OnExitBattle(string cmd, ISFSObject data)
  1174. {
  1175. int userId = data.GetInt ("u");
  1176. if(userId == myUserId)
  1177. {
  1178. ToMenu ();
  1179. }
  1180. }
  1181. public void ToMenu()
  1182. {
  1183. BattleController.battleType = BattleController.BattleType.Menu;
  1184. LoadingPanel panel = LoadingPanel.Show ();
  1185. LoadingPanel.Increase (0.1f);
  1186. panel.ReachTargetProgress.AddListener (()=>{
  1187. LoadingPanel.LoadScene(Config.Scene.Base.ToString());
  1188. });
  1189. }
  1190. public void ClanInit()
  1191. {
  1192. ISFSObject data = new SFSObject ();
  1193. Message msg = new Message (Command.ClanInit, data);
  1194. EnSendQueue (msg);
  1195. }
  1196. private void OnClanInit(string cmd, ISFSObject data)
  1197. {
  1198. }
  1199. private void OnClanOnline(string cmd, ISFSObject data)
  1200. {
  1201. if(data.ContainsKey("a"))
  1202. InviteManager.GetInstance ().SetData (data.GetSFSArray ("a"));
  1203. if (data.ContainsKey ("i")) {
  1204. InviteManager.GetInstance ().Add (StringUtil.ToInt (data.GetUtfString ("i")), InviteData.State.Online);
  1205. InviteManager.GetInstance ().Sort ();
  1206. }
  1207. }
  1208. private void OnClanOffline(string cmd, ISFSObject data)
  1209. {
  1210. InviteManager.GetInstance ().Offline (StringUtil.ToInt (data.GetUtfString("i")));
  1211. }
  1212. private void OnClanBattle(string cmd, ISFSObject data)
  1213. {
  1214. int id = StringUtil.ToInt (data.GetUtfString("i"));
  1215. InviteManager.GetInstance ().Battle (id);
  1216. }
  1217. public void ClanMessage(string content)
  1218. {
  1219. ISFSObject data = new SFSObject ();
  1220. data.PutInt ("u", myUserId);
  1221. data.PutUtfString ("m", content);
  1222. Message msg = new Message (Command.ClanMessage, data);
  1223. EnSendQueue (msg);
  1224. }
  1225. private void OnClanMessage(string cmd, ISFSObject data)
  1226. {
  1227. if (!CheckError (cmd, data)) {
  1228. int userId = data.GetInt ("u");
  1229. string content = data.GetUtfString ("m");
  1230. ChatManager.GetInstance ().AddClanMsg (userId, content);
  1231. }
  1232. }
  1233. public void ClanJoin()
  1234. {
  1235. if (Session.GetInstance ().myUserData.clanId > 0) {
  1236. LocalToRemote ();
  1237. ISFSObject data = new SFSObject ();
  1238. data.PutInt (UserSFSObjectLabel.CLAN, Session.GetInstance().myUserData.clanId);
  1239. Message msg = new Message (Command.ClanJoin, data);
  1240. EnSendQueue (msg);
  1241. }
  1242. }
  1243. private void OnClanJoin(string cmd, ISFSObject data)
  1244. {
  1245. Ready ();
  1246. }
  1247. public void ClanLeft()
  1248. {
  1249. Message msg = new Message (Command.ClanLeft, new SFSObject ());
  1250. EnSendQueue (msg);
  1251. }
  1252. private void OnClanLeft(string cmd, ISFSObject data)
  1253. {
  1254. List<Player> list = battleSession.GetPlayerList ();
  1255. for(int i=0; i<list.Count; i++)
  1256. {
  1257. Player player = list [i];
  1258. if(player.isMe || player.GetHero().GetCraft() == null)
  1259. continue;
  1260. battleSession.GetBattleController ().DealDeath (player.userId, player.GetHero().GetCraft().id, 0);
  1261. }
  1262. }
  1263. public void TeamInvite(int id)
  1264. {
  1265. ISFSObject data = new SFSObject ();
  1266. data.PutUtfString ("t", id.ToString());
  1267. Message msg = new Message (Command.TeamInvite, data);
  1268. EnSendQueue (msg);
  1269. }
  1270. private void OnTeamInvite(string cmd, ISFSObject data)
  1271. {
  1272. int id = StringUtil.ToInt(data.GetUtfString ("t"));
  1273. string nick = UserCache.GetNick (id);
  1274. string info = Language.GetStr ("Invite", "teamInviteConfirm").Replace ("%NICK%", nick);
  1275. AlertPanel.Show (info, AlertPanel.YES|AlertPanel.NO, (AlertCloseEvent evt)=>{
  1276. if(evt.detail == AlertPanel.YES)
  1277. {
  1278. TeamJoin(id);
  1279. }
  1280. });
  1281. }
  1282. public void TeamJoin(int id)
  1283. {
  1284. ISFSObject data = new SFSObject ();
  1285. data.PutUtfString ("t", id.ToString());
  1286. Message msg = new Message (Command.TeamJoin, data);
  1287. EnSendQueue (msg);
  1288. }
  1289. private void OnTeamJoin(string cmd, ISFSObject data)
  1290. {
  1291. }
  1292. public void TeamLeft()
  1293. {
  1294. ISFSObject data = new SFSObject ();
  1295. Message msg = new Message (Command.TeamLeft, data);
  1296. EnSendQueue (msg);
  1297. }
  1298. private void OnTeamLeft(string cmd, ISFSObject data)
  1299. {
  1300. }
  1301. public void TeamKick(int id)
  1302. {
  1303. ISFSObject data = new SFSObject ();
  1304. data.PutUtfString ("t", id.ToString());
  1305. Message msg = new Message (Command.TeamKick, data);
  1306. EnSendQueue (msg);
  1307. }
  1308. private void OnTeamKick(string cmd, ISFSObject data)
  1309. {
  1310. }
  1311. private void OnTeamState(string cmd, ISFSObject data)
  1312. {
  1313. int leader = StringUtil.ToInt (data.GetUtfString (UserSFSObjectLabel.TEAM_LEADER_ID));
  1314. if (data.ContainsKey (UserSFSObjectLabel.TEAM)) {
  1315. InviteManager.GetInstance().TeamState(leader, data.GetSFSArray(UserSFSObjectLabel.TEAM));
  1316. }
  1317. else{
  1318. InviteManager.GetInstance().TeamState(leader, null);
  1319. }
  1320. }
  1321. public void Dispose()
  1322. {
  1323. Disconnect();
  1324. }
  1325. }