PageRenderTime 91ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/com/lele/Manager/NetManager.as

https://gitlab.com/Treeky-NULL/LezaiNiubi
ActionScript | 333 lines | 307 code | 17 blank | 9 comment | 22 complexity | 50a24ba7786c59d741ddc53875ddbea8 MD5 | raw file
  1. package com.lele.Manager
  2. {
  3. import com.lele.LeleSocket.Param;
  4. import com.lele.Manager.Events.APIEvent;
  5. import com.lele.Manager.Interface.IPrivateNetWork;
  6. import com.lele.Plugin.GUID.Guid;
  7. import com.lele.LeleSocket.Command;
  8. import com.lele.LeleSocket.LeleSocketBasic;
  9. import com.lele.LeleSocket.EventCommandConver;
  10. import com.lele.Manager.Events.ManagerEventBase;
  11. import com.lele.Manager.Events.Net_Game_ManagerEvent;
  12. import com.lele.Manager.Events.NetData_Net_ManagerEvent;
  13. import com.lele.Manager.Interface.INetManager;
  14. import com.lele.Manager.Interface.IReport;
  15. import com.lele.Data.GloableData;
  16. import flash.display.Sprite;
  17. import flash.events.Event;
  18. import flash.filters.GradientGlowFilter;
  19. /**
  20. * ...
  21. * @author Lele
  22. */
  23. public class NetManager extends Sprite implements INetManager,IPrivateNetWork
  24. {
  25. //内部端口计数器
  26. private var _mPortCounter:int;
  27. private var _portPairs:Array;
  28. private var _leleSocket:LeleSocketBasic;
  29. private var _repoter:IReport;
  30. private var _serverIP:String;
  31. private var _serverPort:int;
  32. public static var _privateNetWork:IPrivateNetWork;
  33. public function NetManager(serverIP:String,serverPort:int,repoter:IReport)
  34. {
  35. _portPairs = new Array();
  36. _mPortCounter = 1000;
  37. _serverIP = serverIP;
  38. _serverPort = serverPort;
  39. _repoter = repoter;
  40. _leleSocket = new LeleSocketBasic(OnCommand, OnServerClose);
  41. _privateNetWork = this;
  42. }
  43. private function OnCommand(command:Command,vip:Boolean=false)
  44. {
  45. if (vip && GloableData.VipNetEnable)
  46. {
  47. var callBack:Function = GetPortPairPort((int)(command.GetValueByName("port"))).callBack;
  48. for (var a:int = 0; a < command._paramArray.length; a++ )
  49. {
  50. if ((command._paramArray[a] as Param)._name == "port")
  51. {
  52. command._paramArray.splice(a, 1);
  53. break;
  54. }
  55. }
  56. callBack(command);
  57. return;
  58. }
  59. var tempEvt:NetData_Net_ManagerEvent = EventCommandConver.ConverCommandToEvent(command);
  60. if (tempEvt == null)
  61. {
  62. trace("未定义的通讯协议");
  63. return;
  64. }
  65. var toGameManger:Net_Game_ManagerEvent;
  66. switch(tempEvt.EvtType)
  67. {
  68. case NetData_Net_ManagerEvent.DOACTION:
  69. {
  70. toGameManger= new Net_Game_ManagerEvent(Net_Game_ManagerEvent.DOACTION);
  71. toGameManger._playerID = tempEvt._playerID;//一定要
  72. toGameManger.DOACTION_name = tempEvt.DOACTION_name;
  73. toGameManger.DOACTION_direction = tempEvt.DOACTION_direction;
  74. break;
  75. }
  76. case NetData_Net_ManagerEvent.ADDNETPLAYER:
  77. {
  78. toGameManger = new Net_Game_ManagerEvent(Net_Game_ManagerEvent.ADDNETPLAYER);
  79. toGameManger.ADDNETPLAYER_color = tempEvt.ADDNETPLAYER_color;
  80. toGameManger.ADDNETPLAYER_name = tempEvt.ADDNETPLAYER_name;
  81. toGameManger._playerID = tempEvt._playerID;
  82. toGameManger.ADDNETPLAYER_spownPoint = tempEvt.ADDNETPLAYER_spownPoint;
  83. toGameManger.ADDNETPLAYER_map = tempEvt.ADDNETPLAYER_map;
  84. toGameManger.ADDNETPLAYER_dress = tempEvt.ADDNETPLAYER_dress;
  85. break;
  86. }
  87. case NetData_Net_ManagerEvent.LOGINRESULT:
  88. {
  89. toGameManger = new Net_Game_ManagerEvent(Net_Game_ManagerEvent.LOGINRESULT);
  90. toGameManger.LOGINRESULT_result = tempEvt.LOGINRESULT_result;
  91. break;
  92. }
  93. case NetData_Net_ManagerEvent.NETPLAYERMOVE:
  94. {
  95. toGameManger = new Net_Game_ManagerEvent(Net_Game_ManagerEvent.NETPLAYERMOVE);
  96. toGameManger._playerID = tempEvt._playerID;
  97. toGameManger.NETPLAYERMOVE_point = tempEvt.NETPLAYERMOVE_target;
  98. break;
  99. }
  100. case NetData_Net_ManagerEvent.REMOVENETPLAYER:
  101. {
  102. toGameManger = new Net_Game_ManagerEvent(Net_Game_ManagerEvent.REMOVENETPLAYER);
  103. toGameManger._playerID = tempEvt._playerID;
  104. break;
  105. }
  106. case NetData_Net_ManagerEvent.NETPLAYERSAY:
  107. {
  108. toGameManger = new Net_Game_ManagerEvent(Net_Game_ManagerEvent.NETCHATMSG);
  109. toGameManger._playerID = tempEvt._playerID;
  110. toGameManger.NETCHATMSG_msg = tempEvt.NETPLAYERSAY_msg;
  111. break;
  112. }
  113. case NetData_Net_ManagerEvent.CHANGEWEATHER:
  114. {
  115. toGameManger = new Net_Game_ManagerEvent(Net_Game_ManagerEvent.CHANGEWEATHER);
  116. toGameManger.CHANGEWEATHER_weather = tempEvt.CHANGEWEATHER_weather;
  117. toGameManger.CHANGEWEATHER_strength = tempEvt.CHANGEWEATHER_strength;
  118. break;
  119. }
  120. case NetData_Net_ManagerEvent.CREATACCOUNTRESULT:
  121. {
  122. toGameManger = new Net_Game_ManagerEvent(Net_Game_ManagerEvent.CREATACCOUNTRESULT);
  123. toGameManger.CREATACCOUNTRESULT_result = tempEvt.CREATACCOUNTRESULT_result;
  124. toGameManger.CREATACCOUNTRESULT_id = tempEvt.CREATACCOUNTRESULT_id;
  125. break;
  126. }
  127. case NetData_Net_ManagerEvent.MOLEBASEINFO:
  128. {
  129. toGameManger = new Net_Game_ManagerEvent(Net_Game_ManagerEvent.MOLEBASEINFO);
  130. toGameManger.MOLEBASEINFO_num = tempEvt.MOLEBASEINFO_num;
  131. toGameManger.MOLEBASEINFO_color = tempEvt.MOLEBASEINFO_color;
  132. toGameManger.MOLEBASEINFO_name = tempEvt.MOLEBASEINFO_name;
  133. toGameManger.MOLEBASEINFO_dress = tempEvt.MOLEBASEINFO_dress;
  134. break;
  135. }
  136. case NetData_Net_ManagerEvent.CREATEMOLEBACK:
  137. {
  138. toGameManger = new Net_Game_ManagerEvent(Net_Game_ManagerEvent.CREATEMOLEBACK);
  139. toGameManger.CREATEMOLEBACK_result = tempEvt.CREATEMOLEBACK_result;
  140. break;
  141. }
  142. case NetData_Net_ManagerEvent.NETTHROWITEM:
  143. {
  144. toGameManger = new Net_Game_ManagerEvent(Net_Game_ManagerEvent.NETTHROWITEM);
  145. toGameManger.NETTHROWITEM_action = tempEvt.NETTHROWITEM_action;
  146. toGameManger.NETTHROWITEM_blood = tempEvt.NETTHROWITEM_blood;
  147. toGameManger.NETTHROWITEM_dir = tempEvt.NETTHROWITEM_dir;
  148. toGameManger.NETTHROWITEM_id = tempEvt.NETTHROWITEM_id;
  149. toGameManger.NETTHROWITEM_itemStyle = tempEvt.NETTHROWITEM_itemStyle;
  150. toGameManger.NETTHROWITEM_position = tempEvt.NETTHROWITEM_position;
  151. break;
  152. }
  153. case NetData_Net_ManagerEvent.FRIENDCHATRECEIVE:
  154. {
  155. //好友信息接受
  156. GameManager.ApplicationManagerFuncer.StartApp("FriendsApp",2, tempEvt.FRIENDCHATRECEIVE_time,
  157. tempEvt.FRIENDCHATRECEIVE_from, tempEvt.FRIENDCHATRECEIVE_msg);
  158. break;
  159. }
  160. case NetData_Net_ManagerEvent.CHATLISTDATABACK:
  161. {
  162. //构建全局好友信息,简单版
  163. if (tempEvt.CHATLISTDATABACK_dataField == "0")
  164. {
  165. for (var a:int = 0; a < tempEvt.CHATLISTDATABACK_num; a++)
  166. {
  167. GloableData.AddFriend(tempEvt.CHATLISTDATABACK_ids.split("|")[a], tempEvt.CHATLISTDATABACK_colors.split("|")[a], tempEvt.CHATLISTDATABACK_names.split("|")[a], int(tempEvt.CHATLISTDATABACK_stys.split("|")[a]));
  168. }
  169. }
  170. GameManager.ApplicationManagerFuncer.StartApp("FriendsApp", 3, tempEvt.CHATLISTDATABACK_dataField,
  171. tempEvt.CHATLISTDATABACK_isAppend, tempEvt.CHATLISTDATABACK_totalNum, tempEvt.CHATLISTDATABACK_num,
  172. tempEvt.CHATLISTDATABACK_colors, tempEvt.CHATLISTDATABACK_ids, tempEvt.CHATLISTDATABACK_isOnlines,
  173. tempEvt.CHATLISTDATABACK_names, tempEvt.CHATLISTDATABACK_stys);
  174. break;
  175. }
  176. case NetData_Net_ManagerEvent.FRIENDLOGIN://好友登录
  177. {
  178. GameManager.ApplicationManagerFuncer.StartApp("FriendsApp", 4, tempEvt.FRIENDLOGIN_id);
  179. break;
  180. }
  181. case NetData_Net_ManagerEvent.FRIENDOFFLINE://好友离线
  182. {
  183. GameManager.ApplicationManagerFuncer.StartApp("FriendsApp", 5, tempEvt.FRIENDOFFLINE_id);
  184. break;
  185. }
  186. case NetData_Net_ManagerEvent.XMFRIEND://请求加好友
  187. {
  188. ApplicationManager.GetIDialog().AddNote(1, function(state:Boolean) {
  189. GloableData.AddFriend(tempEvt.XMFRIEND_id, "white", "null", 0);
  190. var str:String = "<XXMFriend;2;ans;";
  191. if (state) { str += "1"; }
  192. else { str += "0"; }
  193. str += ";aorID;" + tempEvt.XMFRIEND_id + ">";
  194. _leleSocket.Send(str);
  195. },
  196. tempEvt.XMFRIEND_name+"要和你交朋友!", "和我做朋友吧," + tempEvt.XMFRIEND_name+"(" + tempEvt.XMFRIEND_id + ")",
  197. ((new Date()).getMonth() + 1).toString(), (new Date()).getDate().toString(),
  198. (new Date()).hours.toString() + ":" + (new Date()).minutes.toString());
  199. break;
  200. }
  201. case NetData_Net_ManagerEvent.NOTEY:
  202. {
  203. ApplicationManager.GetIDialog().AddNote(0, function() {},
  204. tempEvt.NOTEY_title,tempEvt.NOTEY_content,
  205. ((new Date()).getMonth() + 1).toString(), (new Date()).getDate().toString(),
  206. (new Date()).hours.toString() + ":" + (new Date()).minutes.toString());
  207. break;
  208. }
  209. case NetData_Net_ManagerEvent.NCHANGEDRESS:
  210. {
  211. toGameManger = new Net_Game_ManagerEvent(Net_Game_ManagerEvent.NCHANGEDRESS);
  212. toGameManger.NCHANGEDRESS_HENHSC = tempEvt.NCHANGEDRESS_HENHSC;
  213. toGameManger.NCHANGEDRESS_id = tempEvt.NCHANGEDRESS_id;
  214. break;
  215. }
  216. case NetData_Net_ManagerEvent.SHOWDIALOG:
  217. {
  218. ApplicationManager.GetIDialog().ShowDialog("emoy", tempEvt.SHOWDIALOG_mood, tempEvt.SHOWDIALOG_msg, null);
  219. break;
  220. }
  221. case NetData_Net_ManagerEvent.BLOOD:
  222. {
  223. var apiEvt:APIEvent = new APIEvent(APIEvent.OnSetBlood);
  224. var obj:Object = new Object();
  225. apiEvt.data = tempEvt.BLOOD_id+";"+tempEvt.BLOOD_num;
  226. GameManager.GetEventDispatcher().dispatchEvent(apiEvt);
  227. }
  228. }
  229. _repoter.OnReport(toGameManger);
  230. }
  231. private function OnServerClose()
  232. {
  233. //ApplicationManager.GetIDialog().ShowDialog("emoy", "sad", "服务器似乎挂了!", null);
  234. }
  235. public function Connect()
  236. {
  237. _leleSocket.Connect(_serverIP, _serverPort);
  238. }
  239. public function DisConnect()
  240. {
  241. _leleSocket.DisConnect();
  242. }
  243. public function OnReceive(evt:Event)
  244. {
  245. var str:String = EventCommandConver.ConverEventToCommand(evt as ManagerEventBase);
  246. _leleSocket.Send(str);
  247. }
  248. //提供独立端口给子app使用
  249. public function GetPrivateNetPort(callBack:Function):String
  250. {
  251. var temp:PortPair = new PortPair();
  252. temp.id = Guid.create();
  253. temp.port = _mPortCounter;
  254. temp.callBack = callBack;
  255. _mPortCounter++;
  256. _portPairs.push(temp);
  257. return temp.id;
  258. }
  259. public function Release(id:String)
  260. {
  261. for (var a:int = 0; a < _portPairs.length; a++ )
  262. {
  263. if ((_portPairs[a] as PortPair).id == id)
  264. {
  265. _portPairs.splice(a, 1);
  266. return;
  267. }
  268. }
  269. }
  270. public function PrivateSendStr(evtName:String, length:String, pv:String, id:String) //das;asd;asd;adss
  271. {
  272. var pack:String = "<" + evtName+";" + length;
  273. pack += ";"+pv;
  274. pack += ";port;" + GetPortPair(id).port.toString() + ">";
  275. _leleSocket.Send(pack);
  276. }
  277. public function PrivateSend(evtName:String, paramName:Array, paramValue:Array, id:String)
  278. {
  279. var pack:String = "<" + evtName+";"+(paramName.length+1).toString();
  280. for (var a:int = 0; a < paramName.length; a++ )
  281. {
  282. pack += ";" + paramName[a] + ";" + paramValue[a];
  283. }
  284. pack += ";port;" + GetPortPair(id).port.toString() + ">";
  285. _leleSocket.Send(pack);
  286. }
  287. private function GetPortPair(id:String):PortPair
  288. {
  289. for (var a:int = 0; a < _portPairs.length; a++ )
  290. {
  291. if ((_portPairs[a] as PortPair).id == id)
  292. {
  293. return (_portPairs[a]);
  294. }
  295. }
  296. return null;
  297. }
  298. private function GetPortPairPort(port:int):PortPair
  299. {
  300. for (var a:int = 0; a < _portPairs.length; a++ )
  301. {
  302. if ((_portPairs[a] as PortPair).port == port)
  303. {
  304. return (_portPairs[a]);
  305. }
  306. }
  307. return null;
  308. }
  309. }
  310. }
  311. class PortPair
  312. {
  313. public var id:String;
  314. public var port:int;
  315. public var callBack:Function;
  316. }