MessageManager.cs 40 KB

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