MessageManager.cs 40 KB

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