MessageManager.cs 40 KB

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