ServerZoneNode.cs 34 KB

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