MessageManager.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  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. info = info.Replace("%DIRECTION%", Language.GetStr(map.id.ToString(), "station"+station.crystalBase.id));
  753. if(player.userId == myUserId)
  754. {
  755. info = info.Replace("%NAME%", Language.GetStr("GameInfo", "you"));
  756. }
  757. else
  758. {
  759. info = info.Replace("%NAME%", player.nick);
  760. }
  761. info = info.Replace("%COLOR%", colorStr);
  762. battleSession.GetBattleController().battleUI.ShowBottomTip(info);
  763. }
  764. }
  765. }
  766. private bool radioHold = false;
  767. public void Radio(int index)
  768. {
  769. if(!radioHold)
  770. {
  771. ISFSObject data = new SFSObject();
  772. data.PutInt("i", index);
  773. data.PutInt("u", myUserId);
  774. int craftId = battleSession.GetBattleController ().GetCtrlCraft() != null ? battleSession.GetBattleController ().GetCtrlCraft().id : 0;
  775. data.PutInt ("c", craftId);
  776. radioHold = true;
  777. Message msg = new Message(Command.Radio, data);
  778. EnSendQueue(msg);
  779. }
  780. battleSession.GetBattleController().battleUI.HideRadioPanel();
  781. }
  782. private void OnRadio(string cmd, ISFSObject data)
  783. {
  784. int userId = data.GetInt("u");
  785. int index = data.GetInt("i");
  786. int craftId = data.GetInt ("c");
  787. if(userId == myUserId)
  788. {
  789. radioHold = false;
  790. }
  791. battleSession.GetBattleController().battleUI.DealRadio(userId, craftId, index);
  792. }
  793. // private void ResetPlayer(Player player)
  794. // {
  795. // player.heroDeadTime = 0;
  796. // if(player.heroCraft != null)
  797. // {
  798. // battleSession.GetBattleController().DoDeath(player.heroCraft);
  799. // }
  800. // player.ClearHero();
  801. // }
  802. // public static void ParseStaticMapObject(ISFSArray arr, BattleSession battleSession)
  803. // {
  804. // ParseBuilding(arr);
  805. // for(int i=0; i<arr.Count; i++)
  806. // {
  807. // ISFSObject obj = arr.GetSFSObject(i);
  808. // MapObjectUtil.TypeId typeId = MapObjectUtil.GetTypeIdByCode(obj.GetInt("s"));
  809. // if(typeId == MapObjectUtil.TypeId.Station)
  810. // {
  811. // StationData stationData = new StationData();
  812. // stationData.id = obj.GetInt("i");
  813. // stationData.team = TeamUtil.GetTeam(obj.GetInt("t"));
  814. // stationData.crystalId = obj.GetInt("c");
  815. // Station station = battleSession.GetBattleController().GetMap().CreateStation(stationData, battleSession.GetBattleController());
  816. //
  817. // if(obj.ContainsKey("ti"))
  818. // {
  819. // int itemId = obj.GetInt("ti");
  820. // int itemTypeId = obj.GetInt("tt");
  821. // MapItemData itemData = new MapItemData();
  822. // itemData.id = itemId;
  823. // itemData.typeId = itemTypeId;
  824. // itemData.postion = station.itemPosition;
  825. // itemData.team = station.team;
  826. // battleSession.GetBattleController().GetMap().CreateMapItem(itemData);
  827. // }
  828. // }
  829. // else if(typeId == MapObjectUtil.TypeId.Door)
  830. // {
  831. //
  832. // }
  833. // }
  834. // }
  835. private void OnAddMapItem(string cmd, ISFSObject data)
  836. {
  837. Map map = battleSession.GetBattleController().GetMap();
  838. ISFSArray arr = data.GetSFSArray("l");
  839. for(int i=0; i<arr.Count; i++)
  840. {
  841. ISFSObject obj = arr.GetSFSObject(i);
  842. int id = obj.GetInt("i");
  843. int itemId = obj.GetInt("t");
  844. MapItemPos mapItemPos = map.GetMapItemPos(id);
  845. if(mapItemPos != null)
  846. {
  847. MapItemData itemData = new MapItemData();
  848. itemData.id = id;
  849. itemData.typeId = itemId;
  850. itemData.postion = mapItemPos.position;
  851. map.CreateMapItem(itemData);
  852. }
  853. }
  854. }
  855. public void GetItem(Player player, int craftId, int itemId)
  856. {
  857. ISFSObject data = new SFSObject();
  858. data.PutInt("u", player.userId);
  859. data.PutInt("c", craftId);
  860. data.PutInt("i", itemId);
  861. Message msg = new Message(Command.GetMapItem, data);
  862. EnSendQueue(msg);
  863. }
  864. private void OnGetItem(string cmd, ISFSObject data)
  865. {
  866. int itemId = data.GetInt("i");
  867. int craftId = data.GetInt ("c");
  868. int userId = data.GetInt("u");
  869. MapItem mapItem = battleSession.GetBattleController().GetMap().GetMapItem(itemId);
  870. if(mapItem != null)
  871. {
  872. BattleObject battleObj = battleSession.GetBattleController ().GetMap ().GetBattleObject (craftId);
  873. if(battleObj != null)
  874. {
  875. Buff buff = BuffManager.GetInstance().AddBuff(8, battleObj, battleObj);
  876. }
  877. mapItem.Remove();
  878. }
  879. }
  880. public static void ParseFlag(ISFSArray arr, BattleSession battleSession)
  881. {
  882. Map map = battleSession.GetBattleController().GetMap();
  883. for(int i=0; i<arr.Count; i++)
  884. {
  885. ISFSObject obj = arr.GetSFSObject(i);
  886. TeamUtil.Team team = TeamUtil.GetTeam(obj.GetInt("t"));
  887. int x = obj.GetInt("x");
  888. int y = obj.GetInt("y");
  889. int craftId = obj.GetInt("c");
  890. FlagItem flag = map.GetFlag(team);
  891. bool isNew = false;
  892. if(flag == null)
  893. {
  894. isNew = true;
  895. flag = map.CreateFlag(team);
  896. }
  897. if(craftId != -1)
  898. {
  899. PutFlagToCraft(craftId, flag, map);
  900. }
  901. else if(isNew)
  902. {
  903. flag.PutToBase(map.GetFlagBase(flag.team).position);
  904. string info = Language.GetStr("Flag", "flagPlace");
  905. info = info.Replace("%COLOR%", TeamUtil.GetTeamColorString(flag.team));
  906. info = info.Replace("%TEAM%", TeamUtil.GetTeamName(flag.team));
  907. }
  908. else if(x == 0 && y == 0)
  909. {
  910. flag.PutToBase(map.GetFlagBase(flag.team).position);
  911. }
  912. else if(flag.IsOnGround())
  913. {
  914. flag.position = new Vector3(x, 0, y);
  915. flag.PutGround(flag.position);
  916. }
  917. }
  918. }
  919. public static void PutFlagToCraft(int id, FlagItem flag, Map map)
  920. {
  921. Craft craft = map.GetBattleObject(id) as Craft;
  922. if(craft != null)
  923. {
  924. flag.LinkToCraft(craft);
  925. }
  926. else
  927. {
  928. flag.WaitToLinkId(id);
  929. }
  930. }
  931. private void OnFlagSync(string cmd, ISFSObject data)
  932. {
  933. ParseFlag(data.GetSFSArray("l"), battleSession);
  934. }
  935. private void OnAddFlag(string cmd, ISFSObject data)
  936. {
  937. ParseFlag(data.GetSFSArray("l"), battleSession);
  938. }
  939. public void GetFlag(Player player, Craft hero, FlagItem flag)
  940. {
  941. if(hero != null)
  942. {
  943. ISFSObject data = new SFSObject();
  944. data.PutInt("i", hero.id);
  945. data.PutInt("t", flag.team.GetHashCode());
  946. data.PutInt("u", player.userId);
  947. Message msg = new Message(Command.GetFlag, data);
  948. EnSendQueue(msg);
  949. }
  950. }
  951. private void OnGetFlag(string cmd, ISFSObject data)
  952. {
  953. Map map = battleSession.GetBattleController().GetMap();
  954. int craftId = data.GetInt("i");
  955. TeamUtil.Team team = TeamUtil.GetTeam(data.GetInt("t"));
  956. Craft craft = map.GetBattleObject(craftId) as Craft;
  957. if(craft != null)
  958. {
  959. FlagItem flag = map.GetFlag(team);
  960. if(flag != null)
  961. flag.LinkToCraft(craft);
  962. string info = Language.GetStr("Flag", "flagGet");
  963. info = info.Replace("%COLOR%", TeamUtil.GetTeamColorString(craft.team));
  964. info = info.Replace("%NICK%", craft.nick);
  965. info = info.Replace("%TEAM%", TeamUtil.GetTeamName(team));
  966. battleSession.GetBattleController().battleUI.ShowBottomTip(info);
  967. }
  968. }
  969. public void ReturnFlag(Player player, Craft craft, FlagItem flag)
  970. {
  971. PutFlag(player, craft, flag);
  972. }
  973. public void PutFlag(Player player, Craft craft, FlagItem flag)
  974. {
  975. ISFSObject data = new SFSObject();
  976. data.PutInt("t", flag.team.GetHashCode());
  977. data.PutInt("u", player.userId);
  978. data.PutInt ("i", craft.id);
  979. data.PutInt("x", 0);
  980. data.PutInt("y", 0);
  981. Message msg = new Message(Command.PutFlag, data);
  982. EnSendQueue(msg);
  983. }
  984. private void OnPutFlag(string cmd, ISFSObject data)
  985. {
  986. TeamUtil.Team team = TeamUtil.GetTeam(data.GetInt("t"));
  987. int x = data.GetInt("x");
  988. int y = data.GetInt("y");
  989. Map map = battleSession.GetBattleController().GetMap();
  990. FlagItem flag = map.GetFlag(team);
  991. if(flag == null)
  992. return;
  993. FlagBase flagBase = map.GetFlagBase(team);
  994. flag.PutToBase(flagBase.position);
  995. Player player = battleSession.GetPlayer(data.GetInt("u"));
  996. if(player != null)
  997. {
  998. string info = Language.GetStr("Flag", "flagReturn");
  999. info = info.Replace("%COLOR%", TeamUtil.GetTeamColorString(player.team));
  1000. info = info.Replace("%NICK%", player.nick);
  1001. info = info.Replace("%TEAM%", TeamUtil.GetTeamName(team));
  1002. battleSession.GetBattleController().battleUI.ShowBottomTip(info);
  1003. }
  1004. }
  1005. public void CaptureFlag(Player player, Craft hero)
  1006. {
  1007. Map map = battleSession.GetBattleController().GetMap();
  1008. if(hero != null)
  1009. {
  1010. FlagItem myFlag = map.GetFlag(player.team);
  1011. FlagItem oppFlag = map.GetFlag(TeamUtil.GetOpponentTeam(player.team));
  1012. if(myFlag != null && oppFlag != null && oppFlag.linkedCraftId == hero.id && myFlag.IsInBase())
  1013. {
  1014. ISFSObject data = new SFSObject();
  1015. data.PutInt("i", hero.id);
  1016. data.PutInt("t", player.team.GetHashCode());
  1017. data.PutInt("u", player.userId);
  1018. Message msg = new Message(Command.CaptureFlag, data);
  1019. EnSendQueue(msg);
  1020. }
  1021. }
  1022. }
  1023. private void OnCaptureFlag(string cmd, ISFSObject data)
  1024. {
  1025. Player player = battleSession.GetPlayer(data.GetInt("u"));
  1026. if(player != null)
  1027. {
  1028. string info = Language.GetStr("Flag", "flagCapture");
  1029. info = info.Replace("%COLOR%", TeamUtil.GetTeamColorString(player.team));
  1030. info = info.Replace("%NICK%", player.nick);
  1031. info = info.Replace("%TEAM%", TeamUtil.GetTeamName(player.team));
  1032. battleSession.GetBattleController().battleUI.ShowBottomTip(info);
  1033. }
  1034. battleSession.GetBattleController().GetMap().CleanFlag();
  1035. }
  1036. private void OnBattleInfo(string cmd, ISFSObject data)
  1037. {
  1038. if(data.ContainsKey("s"))
  1039. DealScore(data.GetSFSObject("s"));
  1040. // if(data.ContainsKey("p"))
  1041. // DealAiPlayerChange(data.GetSFSObject("p"));
  1042. // if(data.ContainsKey("a"))
  1043. // DealAiTaker(data.GetSFSObject("a"));
  1044. }
  1045. private void DealScore(ISFSObject data)
  1046. {
  1047. BattleController battleController = battleSession.GetBattleController();
  1048. if(battleController == null)
  1049. return;
  1050. if(data.ContainsKey("w"))
  1051. {
  1052. battleController.GameOver(TeamUtil.GetTeam(data.GetInt("w")));
  1053. }
  1054. battleController.blueScore = data.GetInt("b");
  1055. battleController.redScore = data.GetInt("r");
  1056. battleController.battleUI.scoreBar.updateScore(battleController.blueScore, battleController.redScore);
  1057. battleController.clock.freeTimeLeft = (float)data.GetInt("f")/1000f;
  1058. battleController.clock.timeLeft = (float)data.GetInt("t")/1000f;
  1059. if(!battleController.isBattleStart && battleController.clock.freeTimeLeft == 0)
  1060. {
  1061. battleController.isBattleStart = true;
  1062. battleController.GetMap().ClearBlocks(TeamUtil.Team.Yellow);
  1063. }
  1064. if(data.ContainsKey("fl"))
  1065. {
  1066. ParseFlag(data.GetSFSArray("fl"), battleSession);
  1067. }
  1068. }
  1069. // private void DealAiTaker(ISFSObject data)
  1070. // {
  1071. // if(!data.ContainsKey("a"))
  1072. // return;
  1073. //
  1074. // ISFSArray arr = data.GetSFSArray("a");
  1075. // for(int i=0; i<arr.Count; i++)
  1076. // {
  1077. // ISFSObject obj = arr.GetSFSObject(i);
  1078. // int userId = obj.GetInt("u");
  1079. // int aiTaker = obj.GetInt("t");
  1080. // Player player = battleSession.GetPlayer(userId);
  1081. // if(player != null && player.IsAI())
  1082. // {
  1083. // player.aiTakerId = aiTaker;
  1084. // }
  1085. // }
  1086. // }
  1087. //
  1088. // private void DealAiPlayerChange(ISFSObject data)
  1089. // {
  1090. // if(data.ContainsKey("a"))
  1091. // {
  1092. // ISFSArray arr = data.GetSFSArray("a");
  1093. // for(int i=0; i<arr.Count; i++)
  1094. // {
  1095. // ISFSObject obj = arr.GetSFSObject(i);
  1096. // battleSession.AddPlayer(obj.GetInt("u"), obj.GetUtfString("n"), TeamUtil.GetTeam(obj.GetInt("t")), -1);
  1097. // }
  1098. // }
  1099. // }
  1100. public void SimpleSync()
  1101. {
  1102. ISFSObject data = new SFSObject ();
  1103. Message msg = new SyncMessage ();
  1104. EnSendQueue (msg);
  1105. }
  1106. private void OnSimpleSync(string cmd, ISFSObject data)
  1107. {
  1108. }
  1109. private void OnAITaker(string cmd, ISFSObject data)
  1110. {
  1111. Debuger.Log ("OnAITaker "+data.GetDump());
  1112. int aiTaker = data.GetInt ("a");
  1113. battleSession.aiTakerUserId = aiTaker;
  1114. }
  1115. public void PrepareSwap(int id, int craftId)
  1116. {
  1117. ISFSObject data = new SFSObject();
  1118. data.PutInt("i", id);
  1119. data.PutInt("s", craftId);
  1120. Message msg = new Message(Command.PrepareSwap, data);
  1121. EnSendQueue(msg);
  1122. }
  1123. private void OnPrepareSwap(string cmd, ISFSObject data)
  1124. {
  1125. int id = data.GetInt ("i");
  1126. int craftId = data.GetInt ("s");
  1127. Craft craft = battleSession.GetBattleController().GetMap().GetBattleObject(id) as Craft;
  1128. if(craft != null)
  1129. craft.GetSwapManager().PrepareSwap(craftId);
  1130. }
  1131. public void RequestSwap(int id, int craftId)
  1132. {
  1133. ISFSObject data = new SFSObject();
  1134. data.PutInt("i", id);
  1135. data.PutInt("s", craftId);
  1136. Craft craft = battleSession.GetBattleController ().GetMap ().GetBattleObject (id) as Craft;
  1137. data.PutSFSObject ("eq", craft.GetEquipModifyData());
  1138. Message msg = new Message(Command.RequestSwap, data);
  1139. EnSendQueue(msg);
  1140. }
  1141. private void OnRequestSwap(string cmd, ISFSObject data)
  1142. {
  1143. int id = data.GetInt ("i");
  1144. int craftId = data.GetInt ("s");
  1145. Craft craft = battleSession.GetBattleController().GetMap().GetBattleObject(id) as Craft;
  1146. if(craft != null)
  1147. {
  1148. CraftEquipModify equipModify = new CraftEquipModify (data.GetSFSObject ("eq"));
  1149. craft.GetSwapManager().RequestedSwap();
  1150. battleSession.GetBattleController().CreateCapsule(id, craftId, equipModify);
  1151. }
  1152. }
  1153. public void ExitBattle()
  1154. {
  1155. Session.GetInstance().myUserData.vipNextRound = false;
  1156. LoadingPanel loadingPanel = LoadingPanel.Show(false);
  1157. loadingPanel.ReachTargetProgress.AddListener (CheckSaveReplay);
  1158. LoadingPanel.Increase(0.2f);
  1159. battleSession.GetBattleController().isExit = true;
  1160. // BattleController.battleType = BattleController.BattleType.Menu;
  1161. // LoadingPanel.LoadScene(Config.Scene.Battle.ToString());
  1162. }
  1163. private void CheckSaveReplay()
  1164. {
  1165. ReplayManager.GetInstance ().Save (battleSession);
  1166. LoadingPanel loadingPanel = LoadingPanel.Show();
  1167. loadingPanel.ReachTargetProgress.RemoveListener (CheckSaveReplay);
  1168. LoadingPanel.Increase(0.3f);
  1169. ISFSObject data = new SFSObject();
  1170. data.PutInt ("u", myUserId);
  1171. Message msg = new Message(Command.BattleExit, data);
  1172. EnSendQueue(msg);
  1173. }
  1174. private void OnExitBattle(string cmd, ISFSObject data)
  1175. {
  1176. int userId = data.GetInt ("u");
  1177. if(userId == myUserId)
  1178. {
  1179. ToMenu ();
  1180. }
  1181. }
  1182. public void ToMenu()
  1183. {
  1184. BattleController.battleType = BattleController.BattleType.Menu;
  1185. LoadingPanel panel = LoadingPanel.Show ();
  1186. LoadingPanel.Increase (0.1f);
  1187. panel.ReachTargetProgress.AddListener (()=>{
  1188. LoadingPanel.LoadScene(Config.Scene.Base.ToString());
  1189. });
  1190. }
  1191. public void ClanInit()
  1192. {
  1193. ISFSObject data = new SFSObject ();
  1194. Message msg = new Message (Command.ClanInit, data);
  1195. EnSendQueue (msg);
  1196. }
  1197. private void OnClanInit(string cmd, ISFSObject data)
  1198. {
  1199. }
  1200. private void OnClanOnline(string cmd, ISFSObject data)
  1201. {
  1202. if(data.ContainsKey("a"))
  1203. InviteManager.GetInstance ().SetData (data.GetSFSArray ("a"));
  1204. if (data.ContainsKey ("i")) {
  1205. InviteManager.GetInstance ().Add (StringUtil.ToInt (data.GetUtfString ("i")), InviteData.State.Online);
  1206. InviteManager.GetInstance ().Sort ();
  1207. }
  1208. }
  1209. private void OnClanOffline(string cmd, ISFSObject data)
  1210. {
  1211. InviteManager.GetInstance ().Offline (StringUtil.ToInt (data.GetUtfString("i")));
  1212. }
  1213. private void OnClanBattle(string cmd, ISFSObject data)
  1214. {
  1215. int id = StringUtil.ToInt (data.GetUtfString("i"));
  1216. InviteManager.GetInstance ().Battle (id);
  1217. }
  1218. public void ClanMessage(string content)
  1219. {
  1220. ISFSObject data = new SFSObject ();
  1221. data.PutInt ("u", myUserId);
  1222. data.PutUtfString ("m", content);
  1223. Message msg = new Message (Command.ClanMessage, data);
  1224. EnSendQueue (msg);
  1225. }
  1226. private void OnClanMessage(string cmd, ISFSObject data)
  1227. {
  1228. if (!CheckError (cmd, data)) {
  1229. int userId = data.GetInt ("u");
  1230. string content = data.GetUtfString ("m");
  1231. ChatManager.GetInstance ().AddClanMsg (userId, content);
  1232. }
  1233. }
  1234. public void ClanJoin()
  1235. {
  1236. if (Session.GetInstance ().myUserData.clanId > 0) {
  1237. LocalToRemote ();
  1238. ISFSObject data = new SFSObject ();
  1239. data.PutInt (UserSFSObjectLabel.CLAN, Session.GetInstance().myUserData.clanId);
  1240. Message msg = new Message (Command.ClanJoin, data);
  1241. EnSendQueue (msg);
  1242. }
  1243. }
  1244. private void OnClanJoin(string cmd, ISFSObject data)
  1245. {
  1246. Ready ();
  1247. }
  1248. public void ClanLeft()
  1249. {
  1250. Message msg = new Message (Command.ClanLeft, new SFSObject ());
  1251. EnSendQueue (msg);
  1252. }
  1253. private void OnClanLeft(string cmd, ISFSObject data)
  1254. {
  1255. List<Player> list = battleSession.GetPlayerList ();
  1256. for(int i=0; i<list.Count; i++)
  1257. {
  1258. Player player = list [i];
  1259. if(player.isMe || player.GetHero().GetCraft() == null)
  1260. continue;
  1261. battleSession.GetBattleController ().DealDeath (player.userId, player.GetHero().GetCraft().id, 0);
  1262. }
  1263. }
  1264. public void TeamInvite(int id)
  1265. {
  1266. ISFSObject data = new SFSObject ();
  1267. data.PutUtfString ("t", id.ToString());
  1268. Message msg = new Message (Command.TeamInvite, data);
  1269. EnSendQueue (msg);
  1270. }
  1271. private void OnTeamInvite(string cmd, ISFSObject data)
  1272. {
  1273. int id = StringUtil.ToInt(data.GetUtfString ("t"));
  1274. string nick = UserCache.GetNick (id);
  1275. string info = Language.GetStr ("Invite", "teamInviteConfirm").Replace ("%NICK%", nick);
  1276. AlertPanel.Show (info, AlertPanel.YES|AlertPanel.NO, (AlertCloseEvent evt)=>{
  1277. if(evt.detail == AlertPanel.YES)
  1278. {
  1279. TeamJoin(id);
  1280. }
  1281. });
  1282. }
  1283. public void TeamJoin(int id)
  1284. {
  1285. ISFSObject data = new SFSObject ();
  1286. data.PutUtfString ("t", id.ToString());
  1287. Message msg = new Message (Command.TeamJoin, data);
  1288. EnSendQueue (msg);
  1289. }
  1290. private void OnTeamJoin(string cmd, ISFSObject data)
  1291. {
  1292. }
  1293. public void TeamLeft()
  1294. {
  1295. ISFSObject data = new SFSObject ();
  1296. Message msg = new Message (Command.TeamLeft, data);
  1297. EnSendQueue (msg);
  1298. }
  1299. private void OnTeamLeft(string cmd, ISFSObject data)
  1300. {
  1301. }
  1302. public void TeamKick(int id)
  1303. {
  1304. ISFSObject data = new SFSObject ();
  1305. data.PutUtfString ("t", id.ToString());
  1306. Message msg = new Message (Command.TeamKick, data);
  1307. EnSendQueue (msg);
  1308. }
  1309. private void OnTeamKick(string cmd, ISFSObject data)
  1310. {
  1311. }
  1312. private void OnTeamState(string cmd, ISFSObject data)
  1313. {
  1314. int leader = StringUtil.ToInt (data.GetUtfString (UserSFSObjectLabel.TEAM_LEADER_ID));
  1315. if (data.ContainsKey (UserSFSObjectLabel.TEAM)) {
  1316. InviteManager.GetInstance().TeamState(leader, data.GetSFSArray(UserSFSObjectLabel.TEAM));
  1317. }
  1318. else{
  1319. InviteManager.GetInstance().TeamState(leader, null);
  1320. }
  1321. }
  1322. public void Dispose()
  1323. {
  1324. Disconnect();
  1325. }
  1326. }