ServerZoneNode.cs 34 KB

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