ServerZoneNode.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. using CommonAI.Zone;
  2. using CommonAI.Zone.Instance;
  3. using CommonAI.ZoneServer;
  4. using CommonLang;
  5. using CommonLang.ByteOrder;
  6. using CommonLang.Concurrent;
  7. using CommonLang.IO;
  8. using CommonLang.IO.Attribute;
  9. using CommonLang.Property;
  10. using CommonLang.Protocol;
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. using System.Diagnostics;
  15. using XmdsCommon.Message;
  16. using XmdsCommon.Plugin;
  17. using XmdsCommonServer.Message;
  18. using XmdsCommonServer.Plugin;
  19. using XmdsServerNode.Node.Interface;
  20. using CommonAIServer.Node;
  21. using CommonAI.ZoneClient;
  22. using XmdsCommonServer.Plugin.Units;
  23. using XmdsCommonServer.Plugin.Scene;
  24. using CommonAI.Zone.ZoneEditor;
  25. using System.Dynamic;
  26. using XmdsServerNode.CheatingDeath;
  27. using Pomelo;
  28. using System.Web.Helpers;
  29. using static CommonAI.XmdsConstConfig;
  30. using CommonAI.Zone.Helper;
  31. using CommonAI.Data;
  32. namespace XmdsServerNode.Node
  33. {
  34. public class ServerZoneNode : CommonAIServer.Node.ZoneNode
  35. {
  36. private GameOverEvent mGameOver = null;
  37. public bool PauseOnNoPlayer { get; set; }
  38. public string UUID { get { return Zone.UUID; } }
  39. public bool IsGameOver { get { return mGameOver != null; } }
  40. public ICEZoneSession Callback;
  41. private readonly string BindGameServerId;
  42. public ServerZoneNode(string gameServerId) : base(ZoneNodeManager.Templates, ZoneNodeManager.NodeConfig)
  43. {
  44. this.BindGameServerId = gameServerId;
  45. PauseOnNoPlayer = false;
  46. }
  47. public string GetBindGameSrvId()
  48. {
  49. return this.BindGameServerId;
  50. }
  51. /// <summary>
  52. /// 房间初始化
  53. /// </summary>
  54. public void Start(string instanceID, int scene_id, string monsterHard, byte allowAutoGuard, bool calPKValue,
  55. int average_level, double monsterAddPropPercentWithFloor, double monsterAddPropRatioWithLv, int killInterval,
  56. int killMax, int killMaxCoolTime, bool usespaceDiv, CommonAI.Data.SceneType scenetype, GSCreateAreaData areaData,
  57. CommonAI.XmdsConstConfig.AreaType areaType, bool isteam = false, bool canRiding = true)
  58. {
  59. try
  60. {
  61. var sd = base.DataRoot.LoadScene_Server(scene_id, true, false);
  62. sd.DefaultUnitLevel = average_level;
  63. sd.MonsterAddPropPercentWithFloor = monsterAddPropPercentWithFloor;
  64. sd.MonsterAddPropRatioWithLv = monsterAddPropRatioWithLv;
  65. sd.canRiding = canRiding;
  66. sd.killInterval = killInterval;
  67. sd.killMax = killMax;
  68. sd.killMaxCoolTime = killMaxCoolTime;
  69. sd.isTeam = isteam;
  70. sd.sceneType = scenetype;
  71. XmdsSceneProperties zsp = sd.Properties as XmdsSceneProperties;
  72. XmdsServerSceneData sceneData = new XmdsServerSceneData();
  73. sceneData.AllowAutoGuard = allowAutoGuard;
  74. sceneData.CalPKValue = calPKValue;
  75. sceneData.SceneHard = monsterHard;
  76. sceneData.UsespaceDiv = usespaceDiv;
  77. sceneData.CurAreaType = areaType;
  78. zsp.ServerSceneData = sceneData;
  79. base.Start(sd, areaData, this.BindGameServerId);
  80. base.Zone.UUID = instanceID;
  81. }
  82. catch (System.Exception e)
  83. {
  84. throw new Exception(string.Format("scene {0} start error : {1}", scene_id, e.ToString()), e);
  85. }
  86. }
  87. public void QueueTaskAsync(System.Action<EditorScene> action)
  88. {
  89. base.QueueTask(() =>
  90. {
  91. action(base.Zone);
  92. });
  93. }
  94. public void QueueTaskAsync(System.Action action)
  95. {
  96. base.QueueTask(() =>
  97. {
  98. action();
  99. });
  100. }
  101. public void SendToGameServer(string name, Object param)
  102. {
  103. if (Callback == null)
  104. {
  105. this.Callback = IceManager.instance().getCallback(this.BindGameServerId);
  106. log.Warn("SendToGameServer callback - 1 - null : " + this.BindGameServerId + ", name:" + name + ", data:" + param);
  107. }
  108. string json = null;
  109. if (Callback != null && Callback.callback != null)
  110. {
  111. try
  112. {
  113. json = Json.Encode(param);
  114. Callback.callback.eventNotify(name, json);
  115. //处理失败的逻辑
  116. Callback.RetryFailNotifys();
  117. }
  118. catch (Exception err)
  119. {
  120. if(json == null)
  121. {
  122. log.Error("SendToGameServer序列化就异常了,放弃记录:" + name + ", err:" + err.Message, err);
  123. }
  124. else
  125. {
  126. Callback.notifyFailData.Enqueue(new ICENotifyData(name, json));
  127. log.Error("SendToGameServer: " + name + ", err:" + err.Message, err);
  128. EventNotifyFail();
  129. }
  130. }
  131. }
  132. else
  133. {
  134. log.Warn("SendToGameServer callback - 2- null : " + this.BindGameServerId + ", name:" + name + ", data:" + param);
  135. }
  136. }
  137. private void EventNotifyFail()
  138. {
  139. try
  140. {
  141. if (Callback.callback.ice_getCachedConnection() == null)
  142. {
  143. Callback.failTimes++;
  144. if (Callback.fastSession != null/* && Callback.failTimes >= 3*/)
  145. {
  146. Callback.fastSession.doClose();
  147. Callback.failTimes = 0;
  148. log.Warn("EventNotifyFail 重置连接: " + Callback.fastSession.GetDescribe());
  149. }
  150. else
  151. {
  152. log.Error("EventNotifyFail fastSession null:" + Callback.callback.ToString());
  153. }
  154. }
  155. }
  156. catch (System.Exception e)
  157. {
  158. log.Error("EventNotifyFail catch:" + e);
  159. }
  160. }
  161. //------------------------------------------------------------------------------------------------------------
  162. #region _主线程内回调_线程安全_
  163. public override bool ZoneUpdate(int intervalMS)
  164. {
  165. if (PauseOnNoPlayer && base.PlayerCount == 0) { intervalMS = 0; }
  166. return base.ZoneUpdate(intervalMS);
  167. }
  168. //过滤游戏场景推送出的消息//
  169. protected override bool OnMessageHandlerFromInstanceZone(Event e)
  170. {
  171. //战斗服To游戏服.
  172. if (e is MessagePlayerEventB2R)
  173. {
  174. e.sender = null;
  175. var msg = e as MessagePlayerEventB2R;
  176. msg.eventName = e.GetType().Name;
  177. SendToGameServer("playerEvent", e);
  178. return true;
  179. }
  180. else if (e is MessageZoneEventB2R)
  181. {
  182. e.sender = null;
  183. var msg = e as MessageZoneEventB2R;
  184. msg.eventName = e.GetType().Name;
  185. SendToGameServer("zoneEvent", e);
  186. return true;
  187. }
  188. else if(e is MessageMapEventB2R)
  189. {
  190. var msg = e as MessageMapEventB2R;
  191. msg.eventName = e.GetType().Name;
  192. SendToGameServer("mapNotify", e);
  193. return true;
  194. }
  195. else if (e is GameOverEvent)
  196. {
  197. if (mGameOver == null)
  198. {
  199. mGameOver = e as GameOverEvent;
  200. var eventParam = new
  201. {
  202. eventName = "gameOver",
  203. instanceId = this.UUID,
  204. winForce = mGameOver.WinForce
  205. };
  206. SendToGameServer("areaEvent", eventParam);
  207. }
  208. }
  209. return false;
  210. }
  211. protected override void OnEndUpdate()
  212. {
  213. base.ForEachPlayers((c) =>
  214. {
  215. var zone_client = c.Client as ZoneNodePlayer;
  216. zone_client.Flush();
  217. });
  218. }
  219. protected override void OnPlayerEntered(PlayerClient client)
  220. {
  221. base.OnPlayerEntered(client);
  222. CheatingDeath.CheatingDeathManager.OnPlayerEnter(client.Actor);
  223. //进去默认自动战斗的情况下
  224. if(this.SceneData.Properties.GetAutoFightFlag() == 3 && !client.Actor.IsGuard)
  225. {
  226. client.Actor.doEnterAutoFight();
  227. }
  228. client.Actor.Virtual.doEvent(JSGCustomOpType.UpdateAutoBattleFlag);
  229. }
  230. protected override void OnPlayerLeft(PlayerClient client)
  231. {
  232. base.OnPlayerLeft(client);
  233. this.KillPlayerAOIObjects(client);
  234. }
  235. //干掉所有和玩家同一位面单位//
  236. protected void KillPlayerAOIObjects(PlayerClient client)
  237. {
  238. var player = client.Actor;
  239. if (player != null && player.AoiStatus != null)
  240. {
  241. var src_aoi = player.AoiStatus as XmdsPlayerAOI;
  242. if (src_aoi.Owner == player)
  243. {
  244. src_aoi.Cleanup();
  245. }
  246. }
  247. }
  248. #endregion
  249. //------------------------------------------------------------------------------------------------------------
  250. #region _游戏服控制玩家_
  251. /// <summary>
  252. /// 单位进入场景
  253. /// </summary>
  254. /// <param name="player"></param>
  255. /// <param name="enter"></param>
  256. public void OnPlayerEnter(IPlayer player, PlayerEnterRoomS2R enter, Action<PlayerClient> callback, Action<Exception> callerror)
  257. {
  258. if (enter == null)
  259. {
  260. // 建立绑定关系 //
  261. var zone_player = new ZoneNodePlayer(this, player);
  262. base.PlayerEnter(zone_player, null, 0, 0, 0, null,0, callback, callerror);
  263. }
  264. else
  265. {
  266. // 有出生点则放入出生点 //
  267. CommonLang.Vector.Vector2 enterPos = enter.Pos;
  268. if (enterPos.X == 0 && enterPos.Y == 0)
  269. {
  270. enterPos = null;
  271. }
  272. if (!string.IsNullOrEmpty(enter.FlagName))
  273. {
  274. InstanceFlag p = Zone.getFlag(enter.FlagName);
  275. if(p != null)
  276. {
  277. if (p is ZoneRegion)
  278. {
  279. // Region随机取一个坐标
  280. var pos = (p as ZoneRegion).getRandomPos(Zone.RandomN);
  281. enterPos.SetX(pos.X);
  282. enterPos.SetY(pos.Y);
  283. }
  284. else
  285. {
  286. enterPos = p.Pos;
  287. }
  288. }
  289. }
  290. UnitInfo temp = ZoneNodeManager.Templates.Templates.getUnit(enter.UnitData.UnitTemplateID);
  291. if (temp != null)
  292. {
  293. temp = temp.Clone() as UnitInfo;
  294. temp.UType = UnitInfo.UnitType.TYPE_PLAYER;
  295. XmdsUnitProperties tprop = (temp.Properties as XmdsUnitProperties);
  296. //临时代码.
  297. //默认无限等待.
  298. temp.RebirthTimeMS = int.MaxValue;
  299. // 插入玩家能力属性 //
  300. tprop.ServerData = ((enter.UnitData.UnitPropData) as XmdsUnitProperties).ServerData;
  301. // 建立绑定关系 //
  302. var zone_player = new ZoneNodePlayer(this, player);
  303. base.PlayerEnter(zone_player, temp, enter.UnitData.Force, enter.UnitData.alliesForce,
  304. tprop.ServerData.BaseInfo.UnitLv, enterPos,enter.direction, callback, callerror);
  305. }
  306. else
  307. {
  308. throw new Exception("Unit template not exist : " + enter);
  309. }
  310. }
  311. }
  312. /// <summary>
  313. /// 单位离开场景
  314. /// </summary>
  315. /// <param name="player"></param>
  316. public void OnPlayerLeave(InstancePlayer player, Action<PlayerClient> callback, Action<Exception> callerror, bool keep_object = false)
  317. {
  318. if (player != null)
  319. {
  320. base.PlayerLeave(player, callback, callerror, keep_object);
  321. }
  322. }
  323. /// <summary>
  324. /// 玩家网络状况改变
  325. /// </summary>
  326. /// <param name="player"></param>
  327. /// <param name="state"></param>
  328. public void OnPlayerNetStateChanged(IPlayer player, string state)
  329. {
  330. QueueTask(() =>
  331. {
  332. var zc = player.BindingObject;
  333. if (zc != null)
  334. {
  335. zc.NetStateChanged(state);
  336. }
  337. });
  338. }
  339. /// <summary>
  340. /// 单位收到来自客户端的消息
  341. /// </summary>
  342. /// <param name="client"></param>
  343. /// <param name="message"></param>
  344. public void OnPlayerReceivedMessage(IPlayer player, byte[] msg)
  345. {
  346. QueueTask(() =>
  347. {
  348. //if (doDecodePing(player, msg))
  349. //{
  350. // return;
  351. //}
  352. //else
  353. {
  354. var zone_player = player.BindingObject;
  355. if (zone_player != null)
  356. {
  357. zone_player.Recv(msg);
  358. }
  359. }
  360. });
  361. }
  362. public void ReceiveMsgR2B(IPlayer client, object status)
  363. {
  364. QueueTask(() =>
  365. {
  366. var zc = client.BindingObject;
  367. if (zc != null && zc.BindingActor != null)
  368. {
  369. XmdsVirtual zv = (XmdsVirtual)zc.BindingActor.Virtual;
  370. zv.ReceiveMsgR2B(status);
  371. }
  372. });
  373. }
  374. /// <summary>
  375. /// 玩家复活,分原地复活和复活点复活
  376. /// </summary>
  377. /// <param name="client"></param>
  378. /// <param name="status"></param>
  379. public void OnHelpPlayerRebirth(IPlayer player, IPlayer revivePlayer, int time)
  380. {
  381. QueueTask(() =>
  382. {
  383. var p = player.BindingObject;
  384. var r = revivePlayer.BindingObject;
  385. if (p != null && r != null && p.BindingActor != null && r.BindingActor != null)
  386. {
  387. PlayerSaveEventR2B r2b = new PlayerSaveEventR2B();
  388. XmdsVirtual Decedent = (XmdsVirtual)(r.BindingActor).Virtual;
  389. XmdsVirtual Saver = (XmdsVirtual)(p.BindingActor).Virtual;
  390. r2b.Decedent = Decedent.mUnit;
  391. r2b.Saver = Saver.mUnit;
  392. r2b.SaveTime = time;
  393. Saver.ReceiveMsgR2B(r2b);
  394. }
  395. });
  396. }
  397. public void OnPlayerReadyR2B(IPlayer client)
  398. {
  399. QueueTask(() =>
  400. {
  401. InstancePlayer out_player;
  402. PlayerClient out_client;
  403. if (GetPlayerAndClient(client.PlayerUUID, out out_player, out out_client))
  404. {
  405. if (out_player != null && out_client != null)
  406. {
  407. (out_player as XmdsInstancePlayer).PlayerReady();
  408. }
  409. }
  410. });
  411. }
  412. public void OnPlayerReviveTeamInfoEventR2B(IPlayer client, TeamInfoEventR2B team)
  413. {
  414. QueueTask(() =>
  415. {
  416. InstancePlayer out_player;
  417. PlayerClient out_client;
  418. if (GetPlayerAndClient(client.PlayerUUID, out out_player, out out_client))
  419. {
  420. if (out_player != null && out_client != null)
  421. {
  422. (out_client as BindingPlayerClient).OnPlayerReviveTeamInfoEventR2B(team);
  423. var zv = (XmdsVirtual)out_player.Virtual;
  424. zv.ReceiveMsgR2B(team);
  425. }
  426. }
  427. });
  428. }
  429. public void OnPlayerAddHPByItem(List<IPlayer> notifyPlayers, IPlayer client, int addHP)
  430. {
  431. QueueTask(() =>
  432. {
  433. var nodePlayer = client.BindingObject as ZoneNodePlayer;
  434. if (nodePlayer != null)
  435. {
  436. XmdsVirtual zv = (nodePlayer.BindingActor.Virtual) as XmdsVirtual;
  437. int finalHp = zv.AddHPByItem(addHP);
  438. ZoneNodePlayer temp = null;
  439. foreach (IPlayer p in notifyPlayers)
  440. {
  441. temp = p.BindingObject as ZoneNodePlayer;
  442. XmdsVirtual notifyPlayer = null;
  443. if (temp != null)
  444. {
  445. notifyPlayer = (temp.BindingActor.Virtual) as XmdsVirtual;
  446. notifyPlayer.SendHPChangeMessage(zv.mUnit.ID, finalHp);
  447. }
  448. }
  449. }
  450. });
  451. }
  452. public float GetDropRange(int items)
  453. {
  454. if(items >= 10)
  455. {
  456. return 5.0f;
  457. }
  458. if (items >= 7)
  459. {
  460. return 4.0f;
  461. }
  462. else if(items > 4)
  463. {
  464. return 3.6f;
  465. }
  466. return 2.8f;
  467. }
  468. public float GetDropOffect(float dropRange)
  469. {
  470. int index = (this.Zone.RandomN.Next() % 2 == 0) ? 1 : -1;
  471. return dropRange * this.Zone.RandomN.Next(0, 100) / 100f * index;
  472. }
  473. public void AddDropItem(float x, float y, dynamic items)
  474. {
  475. QueueTask(() =>
  476. {
  477. //阿基米德螺线<-装B用,实际不需要这样
  478. //20170621wuyonghui修改为随机掉落位置算法
  479. float dropRange = GetDropRange(((DynamicJsonArray)items).Length);// 掉落半径范围为5
  480. bool addSuc = false;
  481. foreach (dynamic obj in items)
  482. {
  483. XmdsCommon.Message.DropItem di = new XmdsCommon.Message.DropItem();
  484. di.FileName = (string)obj.showId;
  485. di.FreezeTime = (int)obj.freezeTime;
  486. di.Name = (string)obj.name;
  487. di.ObjID = (string)obj.id;
  488. di.ProtectTime = (int)obj.protectTime;
  489. di.Qty = (int)obj.groupCount;
  490. di.Quality = (int)obj.qColor;
  491. di.TemplateID = (int)obj.itemTypeId;
  492. di.TTL = (int)obj.lifeTime;
  493. di.PlayerUUID = new List<string>();
  494. di.Mode = (byte)obj.distributeType;
  495. di.OriginX = x;
  496. di.OriginY = y;
  497. bool showLifeTimes = (bool)obj.showLifeTime;
  498. di.code = (String)obj.code;
  499. di.bindPlayerId = obj.bindPlayerId;
  500. di.createTime = (int)(CommonLang.CUtils.S_LOCAL_TIMESTAMPMS/1000);
  501. if (obj.PlayerUUID != null)
  502. {
  503. foreach (dynamic playeruuid in obj.PlayerUUID)
  504. {
  505. di.PlayerUUID.Add(playeruuid);
  506. }
  507. }
  508. di.IconName = (string)obj.IconName;
  509. ItemTemplate templateTemp = Zone.Templates.getItem(di.TemplateID);
  510. if(templateTemp == null)
  511. {
  512. log.Warn("AddDropItem没有对应单位信息:" + di.TemplateID + ", " + di.code);
  513. return;
  514. }
  515. ItemTemplate template = templateTemp.Clone() as ItemTemplate;
  516. //每个物品类型可能有多个显示模型,所以按游戏服传过来的策划配置模型名称
  517. template.FileName = di.FileName;
  518. //修改模板的部分数据,由于模板是共享的,所以必须每次都把以下数据完整覆盖
  519. template.LifeTimeMS = di.TTL;
  520. template.GotCoolDownTimeMS = di.FreezeTime;
  521. template.DropForAll = true;
  522. template.GotOnUse = true;
  523. template.showLifeTime = showLifeTimes;
  524. //template.Pickable = false;//这里注释掉,直接用模板里的值,不要改变
  525. XmdsItemProperties props = template.Properties as XmdsItemProperties;
  526. props.ItemType = XmdsItemProperties.XmdsItemType.Equip;
  527. float x1 = this.GetDropOffect(dropRange);//获得X偏移量
  528. float y1 = this.GetDropOffect(dropRange);//获得Y偏移量
  529. if(Zone.TryTouchMap(null,x+x1,y+y1))
  530. {
  531. x1 = 0;
  532. y1 = 0;
  533. }
  534. //System.Console.WriteLine("掉落物:" + di.Name + ", 偏移:" + x1 + ", " + y1 + "----" + (x + x1) + ", " + (y + y1));
  535. AddItemEvent aie;
  536. XmdsDropableInstanceItem drop_item = Zone.AddItem(template, obj.name, x + x1, y + y1, 0, 0, obj.name, out aie, null,1);
  537. if(drop_item == null || aie == null)
  538. {
  539. log.Warn("添加bs掉落物失败:" + template.ID + ", " + template.Name + ", " + this.SceneID);
  540. return;
  541. }
  542. aie.Sync.ExtData = di;
  543. if(drop_item != null)
  544. {
  545. addSuc = true;
  546. }
  547. }
  548. if (addSuc)
  549. {
  550. base.RecordDropItem();
  551. }
  552. });
  553. }
  554. public void FinishPickItem(IPlayer player, string itemIcon, int quality, int num)
  555. {
  556. QueueTask(() =>
  557. {
  558. var p = player.BindingObject;
  559. if (p != null && p.BindingActor != null)
  560. {
  561. PlayerGotItemB2C b2c = new PlayerGotItemB2C();
  562. XmdsVirtual zv = (XmdsVirtual)(p.BindingActor).Virtual;
  563. b2c.IconName = itemIcon;
  564. b2c.Qty = num;
  565. b2c.Quality = quality;
  566. zv.mUnit.queueEvent(b2c);
  567. }
  568. });
  569. }
  570. #endregion
  571. //------------------------------------------------------------------------------------------------------------
  572. #region _PingPong_
  573. //private static readonly int MSG_TYPE_ID_PING = PropertyUtil.GetAttribute<MessageTypeAttribute>(typeof(Ping)).MessageTypeID;
  574. //private static readonly int MSG_TYPE_ID_PONG = PropertyUtil.GetAttribute<MessageTypeAttribute>(typeof(Pong)).MessageTypeID;
  575. //private ArraySegment<byte> pong_data = new ArraySegment<byte>(new byte[8], 0, 8);
  576. //public bool doDecodePing(IPlayer player, byte[] data)
  577. //{
  578. // int pos = 0;
  579. // int msg_id = LittleEdian.GetS32(data, ref pos);
  580. // if (msg_id == MSG_TYPE_ID_PING)
  581. // {
  582. // uint time = LittleEdian.GetU32(data, ref pos);
  583. // pos = 0;
  584. // LittleEdian.PutS32(pong_data.Array, ref pos, MSG_TYPE_ID_PONG);
  585. // LittleEdian.PutU32(pong_data.Array, ref pos, time);
  586. // player.SendToClient(pong_data);
  587. // return true;
  588. // }
  589. // return false;
  590. //}
  591. #endregion
  592. //------------------------------------------------------------------------------------------------------------
  593. protected override PlayerClient CreatePlayerClient(CommonAIServer.Node.Interface.IPlayer client, InstancePlayer actor)
  594. {
  595. var syncIn = base.Config.CLIENT_SYNC_OBJECT_IN_RANGE;
  596. var syncOut = base.Config.CLIENT_SYNC_OBJECT_OUT_RANGE;
  597. var zpp = base.SceneData.Properties as XmdsSceneProperties;
  598. if (zpp.AOIMinRange > 0 && zpp.AOIMaxRange > 0)
  599. {
  600. syncIn = zpp.AOIMinRange;
  601. syncOut = zpp.AOIMaxRange;
  602. }
  603. return new BindingPlayerClient(client, actor, this, syncIn, syncOut);
  604. }
  605. //------------------------------------------------------------------------------------------------------------
  606. public class ZoneNodePlayer : CommonAIServer.Node.Interface.IPlayer
  607. {
  608. private static AtomicInteger s_alloc_object_count = new AtomicInteger(0);
  609. /// <summary>
  610. /// 分配实例数量
  611. /// </summary>
  612. public static int AllocCount { get { return s_alloc_object_count.Value; } }
  613. public readonly ZoneNode node;
  614. public readonly IPlayer player;
  615. private ZoneNodeCodec mCodec = new ZoneNodeCodec(ZoneNodeManager.MessageFactory);
  616. private AtomicReference<PlayerClient> binding_player = new AtomicReference<PlayerClient>(null);
  617. private Action<object> handler;
  618. private PackEvent mSendingQueue = new PackEvent();
  619. private AtomicInteger mSendingSequenceNo = new AtomicInteger(0);
  620. private string display_name = "";
  621. //private Queue<object> mPreQueue = new Queue<object>();
  622. internal ZoneNodePlayer(ZoneNode node, IPlayer player)
  623. {
  624. s_alloc_object_count++;
  625. this.node = node;
  626. this.player = player;
  627. this.player.BindingObject = this;
  628. this.Connected = true;
  629. }
  630. ~ZoneNodePlayer()
  631. {
  632. s_alloc_object_count--;
  633. }
  634. public IPlayer SessionPlayer {get { return player; }}
  635. public PlayerClient BindingPlayer
  636. {
  637. get { return binding_player.Value; }
  638. set
  639. {
  640. binding_player.Value = value;
  641. if (value != null)
  642. {
  643. try
  644. {
  645. display_name = (value.Actor.Properties as XmdsUnitProperties).ServerData.BaseInfo.name;
  646. }
  647. catch (Exception err)
  648. {
  649. display_name = err.Message;
  650. }
  651. }
  652. }
  653. }
  654. public InstancePlayer BindingActor
  655. {
  656. get
  657. {
  658. var player = binding_player.Value;
  659. if (player != null)
  660. {
  661. return player.Actor;
  662. }
  663. return null;
  664. }
  665. }
  666. public bool Connected { get; private set; }
  667. internal void NetStateChanged(string state)
  668. {
  669. BaseZoneNode.log.InfoFormat("Player:{0} NetStateChanged:{1}", player.PlayerUUID, state);
  670. switch (state)
  671. {
  672. case "disconnected":
  673. this.Connected = false;
  674. break;
  675. case "connected":
  676. this.Connected = true;
  677. break;
  678. default:
  679. return;
  680. }
  681. }
  682. internal void Recv(byte[] data)
  683. {
  684. IMessage message;
  685. if (mCodec.doDecode(data, out message))
  686. {
  687. //if(handler == null)
  688. //{
  689. // mPreQueue.Enqueue(message);
  690. //}
  691. //else
  692. //{
  693. // handler.Invoke(message);
  694. //}
  695. if (handler != null)
  696. {
  697. handler.Invoke(message);
  698. }
  699. }
  700. else
  701. {
  702. CheatingDeathManager.SendPlayerException(BindingActor, "decode error");
  703. }
  704. }
  705. public void Send(IMessage msg)
  706. {
  707. if (msg is PlayerLeaveScene)
  708. {
  709. this.Flush();
  710. this.ForceSend(msg);
  711. }
  712. else if (this.Connected)
  713. {
  714. lock (mSendingQueue)
  715. {
  716. mSendingQueue.events.Add(msg);
  717. }
  718. }
  719. }
  720. internal void Flush()
  721. {
  722. lock (mSendingQueue)
  723. {
  724. if (mSendingQueue.events.Count > 0)
  725. {
  726. try
  727. {
  728. if (this.Connected)
  729. {
  730. mSendingQueue.sequenceNo = mSendingSequenceNo.GetAndIncrement();
  731. ForceSend(mSendingQueue);
  732. }
  733. }
  734. finally
  735. {
  736. mSendingQueue.events.Clear();
  737. }
  738. }
  739. }
  740. }
  741. internal void ForceSend(IMessage msg)
  742. {
  743. //BaseZoneNode.log.Debug("B2C msg>>>" + msg);
  744. ArraySegment<byte> data;
  745. if (mCodec.doEncode(msg, out data))
  746. {
  747. this.player.SendToClient(data);
  748. }
  749. }
  750. #region CommonAIServer.Node.Interface.IPlayer.
  751. void CommonAIServer.Node.Interface.IPlayer.OnConnected(PlayerClient bindingg)
  752. {
  753. }
  754. void CommonAIServer.Node.Interface.IPlayer.OnDisconnect(PlayerClient bindingg)
  755. {
  756. this.player.BindingObject = null;
  757. this.player.Dispose();
  758. }
  759. string CommonAIServer.Node.Interface.IPlayer.DisplayName
  760. {
  761. get { return display_name; }
  762. }
  763. string CommonAIServer.Node.Interface.IPlayer.PlayerUUID
  764. {
  765. get { return player.PlayerUUID; }
  766. }
  767. object CommonAIServer.Node.Interface.IPlayer.GetAttribute(string key)
  768. {
  769. return player.GetAttribute(key);
  770. }
  771. bool CommonAIServer.Node.Interface.IPlayer.IsAttribute(string key)
  772. {
  773. return player.IsAttribute(key);
  774. }
  775. void CommonAIServer.Node.Interface.IPlayer.SetAttribute(string key, object value)
  776. {
  777. player.SetAttribute(key, value);
  778. }
  779. void CommonAIServer.Node.Interface.IPlayer.Listen(Action<object> handler)
  780. {
  781. this.handler = handler;
  782. //while(mPreQueue.Count > 0)
  783. //{
  784. // this.handler.Invoke(mPreQueue.Dequeue());
  785. //}
  786. }
  787. #endregion
  788. }
  789. public class BindingPlayerClient : PlayerClient
  790. {
  791. private readonly ZoneNodePlayer Xmds_client;
  792. private readonly HashSet<string> team_info = new HashSet<string>();
  793. private List<string> team_removing = new List<string>(1);
  794. internal BindingPlayerClient(CommonAIServer.Node.Interface.IPlayer client, InstancePlayer actor, CommonAIServer.Node.ZoneNode node, float syncIn, float syncOut)
  795. : base(client, actor, node, syncIn, syncOut)
  796. {
  797. this.Xmds_client = client as ZoneNodePlayer;
  798. }
  799. protected override void OnStart()
  800. {
  801. base.OnStart();
  802. foreach (var obj in base.Zone.AllUnits)
  803. {
  804. if (IsStaticUnit(obj))
  805. {
  806. base.ForceAddObjectInView(obj);
  807. }
  808. }
  809. }
  810. private bool IsStaticUnit(InstanceZoneObject obj)
  811. {
  812. if (obj is InstanceUnit)
  813. {
  814. XmdsUnitProperties prop = (obj as InstanceUnit).Info.Properties as XmdsUnitProperties;
  815. return prop.IsStaticUnit;
  816. }
  817. return false;
  818. }
  819. protected override bool IsLookInRange(InstanceZoneObject obj)
  820. {
  821. if (obj is InstancePlayer)
  822. {
  823. //组队列表中的单位永远不出视野//
  824. var player = obj as InstancePlayer;
  825. if (team_info.Contains(player.PlayerUUID))
  826. {
  827. return true;
  828. }
  829. //if((player.CurrentActionSubstate & (byte)UnitActionSubStatus.Stealth) > 0
  830. // && (this.Actor.CurrentActionSubstate & (byte)UnitActionSubStatus.Stealth) <= 0)
  831. //{
  832. // return false;
  833. //}
  834. }
  835. else if (IsStaticUnit(obj))
  836. {
  837. return true;
  838. }
  839. return base.IsLookInRange(obj);
  840. }
  841. protected override bool IsLookOutRange(InstanceZoneObject obj)
  842. {
  843. if (obj is InstancePlayer)
  844. {
  845. //组队列表中的单位永远不出视野//
  846. var player = obj as InstancePlayer;
  847. if (team_info.Contains(player.PlayerUUID))
  848. {
  849. return false;
  850. }
  851. // if ((player.CurrentActionSubstate & (byte)UnitActionSubStatus.Stealth) > 0
  852. // && (this.Actor.CurrentActionSubstate & (byte)UnitActionSubStatus.Stealth) <= 0
  853. // && !player.Virtual.IsAllies(this.Actor.Virtual))
  854. //{
  855. // return true;
  856. //}
  857. }
  858. else if (IsStaticUnit(obj))
  859. {
  860. return false;
  861. }
  862. return base.IsLookOutRange(obj);
  863. }
  864. public override bool RemoveInRange(InstanceZoneObject o)
  865. {
  866. if (IsLookOutRange(o))
  867. {
  868. m_RemovingList.Add(o.ID);
  869. OnLeaveView(o);
  870. return true;
  871. }
  872. return false;
  873. }
  874. internal void OnPlayerReviveTeamInfoEventR2B(TeamInfoEventR2B team)
  875. {
  876. if (team.UUIDList != null)
  877. {
  878. //检测加入队伍//
  879. foreach (var uuid in team.UUIDList)
  880. {
  881. if (!string.IsNullOrEmpty(uuid) && !team_info.Contains(uuid))
  882. {
  883. InstancePlayer tu = base.Zone.getPlayerByUUID(uuid);
  884. if (tu != null)
  885. {
  886. team_info.Add(uuid);
  887. base.ForceAddObjectInView(tu);
  888. }
  889. }
  890. }
  891. //检测离开队伍//
  892. team_removing.Clear();
  893. foreach (var uuid in team_info)
  894. {
  895. if (!team.UUIDList.Contains(uuid))
  896. {
  897. team_removing.Add(uuid);
  898. }
  899. }
  900. if (team_removing.Count > 0)
  901. {
  902. foreach (var uuid in team_removing)
  903. {
  904. team_info.Remove(uuid);
  905. }
  906. }
  907. }
  908. }
  909. }
  910. //------------------------------------------------------------------------------------------------------------
  911. }
  912. }