PlayerSystem.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System.Collections.Generic;
  2. using System.Text.Json;
  3. using BattleIce;
  4. using JsonSerializer = System.Text.Json.JsonSerializer;
  5. namespace ET.Server
  6. {
  7. [FriendOf(typeof (WNPlayer))]
  8. [FriendOf(typeof (BattleIceAgentComponent))]
  9. public static class PlayerSystem
  10. {
  11. public class PlayerAwakeSystem: AwakeSystem<WNPlayer, Session, PlayerInfo>
  12. {
  13. /// <summary>
  14. /// 玩家实体创建
  15. /// </summary>
  16. /// <param name="self"></param>
  17. /// <param name="session"></param>
  18. /// <param name="playerInfo"></param>
  19. protected override void Awake(WNPlayer self, Session session, PlayerInfo playerInfo)
  20. {
  21. Log.Info($"创建玩家实体...");
  22. }
  23. }
  24. public class PlayerDestroySystem: DestroySystem<WNPlayer>
  25. {
  26. /// <summary>
  27. /// 玩家实体销毁
  28. /// </summary>
  29. /// <param name="self"></param>
  30. protected override void Destroy(WNPlayer self)
  31. {
  32. Log.Info($"销毁玩家实体...");
  33. }
  34. }
  35. /// <summary>
  36. /// 获取角色id
  37. /// </summary>
  38. /// <param name="self"></param>
  39. /// <returns></returns>
  40. public static long GetId(this WNPlayer self)
  41. {
  42. return self.GetComponent<PlayerDataComponent>().Data.Id;
  43. }
  44. /// <summary>
  45. /// 获取玩家类型
  46. /// </summary>
  47. /// <param name="self"></param>
  48. /// <returns></returns>
  49. public static int GetPlayerType(this WNPlayer self)
  50. {
  51. return self.GetComponent<PlayerDataComponent>().Data.PlayerType;
  52. }
  53. /// <summary>
  54. /// 获取直播间id
  55. /// </summary>
  56. /// <param name="self"></param>
  57. /// <returns></returns>
  58. public static string GetRoomId(this WNPlayer self)
  59. {
  60. return self.GetComponent<PlayerDataComponent>().Data.RoomId;
  61. }
  62. /// <summary>
  63. /// 获取名称
  64. /// </summary>
  65. /// <param name="self"></param>
  66. /// <returns></returns>
  67. public static string GetName(this WNPlayer self)
  68. {
  69. return self.GetComponent<PlayerDataComponent>().Data.Name;
  70. }
  71. /// <summary>
  72. /// 获取性别
  73. /// </summary>
  74. /// <param name="self"></param>
  75. /// <returns></returns>
  76. public static int GetSex(this WNPlayer self)
  77. {
  78. return self.GetComponent<PlayerDataComponent>().Data.Sex;
  79. }
  80. /// <summary>
  81. /// 获取职业
  82. /// </summary>
  83. /// <param name="self"></param>
  84. /// <returns></returns>
  85. public static int GetPro(this WNPlayer self)
  86. {
  87. return self.GetComponent<PlayerDataComponent>().Data.Pro;
  88. }
  89. /// <summary>
  90. /// 获取经验
  91. /// </summary>
  92. /// <param name="self"></param>
  93. /// <returns></returns>
  94. public static long GetExp(this WNPlayer self)
  95. {
  96. return self.GetComponent<PlayerDataComponent>().Data.Exp;
  97. }
  98. /// <summary>
  99. /// 获取等级
  100. /// </summary>
  101. /// <param name="self"></param>
  102. /// <returns></returns>
  103. public static int GetLevel(this WNPlayer self)
  104. {
  105. return self.GetComponent<PlayerDataComponent>().Data.Level;
  106. }
  107. /// <summary>
  108. /// 获取金币
  109. /// </summary>
  110. /// <param name="self"></param>
  111. /// <returns></returns>
  112. public static long GetGold(this WNPlayer self)
  113. {
  114. return self.GetComponent<PlayerDataComponent>().Data.Gold;
  115. }
  116. /// <summary>
  117. /// 获取场景实例id
  118. /// </summary>
  119. /// <param name="self"></param>
  120. /// <returns></returns>
  121. public static long GetMapInstanceId(this WNPlayer self)
  122. {
  123. return self.Map.InstanceId;
  124. }
  125. /** 获取服务器id **/
  126. public static int GetLogicServerId(this WNPlayer self)
  127. {
  128. return self.GetComponent<PlayerDataComponent>().Data.LogicServerId;
  129. }
  130. public static ZoneManagerPrx GetZoneManager(this WNPlayer self)
  131. {
  132. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceZoneManager;
  133. }
  134. public static XmdsManagerPrx GetXmdsManager(this WNPlayer self)
  135. {
  136. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceXmdsManager;
  137. }
  138. /** bornData相关初始化 **/
  139. public static void InitBornData(this WNPlayer self)
  140. {
  141. switch (self.BornType)
  142. {
  143. case (int)BORN_TYPE.HISTORY:
  144. self.GetComponent<PlayerTempDataComponent>().MapData.mapId = self.GetComponent<PlayerTempDataComponent>().MapData.historyMapId;
  145. self.GetComponent<PlayerTempDataComponent>().MapData.mapInstanceId =
  146. self.GetComponent<PlayerTempDataComponent>().MapData.historyMapInstanceId;
  147. self.GetComponent<PlayerTempDataComponent>().MapData.x = self.GetComponent<PlayerTempDataComponent>().MapData.historyX;
  148. self.GetComponent<PlayerTempDataComponent>().MapData.y = self.GetComponent<PlayerTempDataComponent>().MapData.historyY;
  149. break;
  150. case (int)BORN_TYPE.BORN:
  151. self.GetComponent<PlayerTempDataComponent>().MapData.mapId = self.GetComponent<PlayerTempDataComponent>().MapData.bornMapId;
  152. self.GetComponent<PlayerTempDataComponent>().MapData.mapInstanceId =
  153. self.GetComponent<PlayerTempDataComponent>().MapData.bornMapInstanceId;
  154. self.GetComponent<PlayerTempDataComponent>().MapData.x = self.GetComponent<PlayerTempDataComponent>().MapData.bornX;
  155. self.GetComponent<PlayerTempDataComponent>().MapData.y = self.GetComponent<PlayerTempDataComponent>().MapData.bornY;
  156. break;
  157. }
  158. }
  159. /** 客户端资源加载完成通知 给客户端推送的数据要在这里 **/
  160. public static void OnReady(this WNPlayer self)
  161. {
  162. self.OnEndEnterScene();
  163. // 登录第一次进场景处理,此次登陆登出期间只处理一次
  164. if (self.ReadyFirst)
  165. {
  166. self.ReadyFirst = false;
  167. }
  168. }
  169. /** 玩家登录事件 **/
  170. public static void OnLogin(this WNPlayer self)
  171. {
  172. self.ReadyFirst = true;
  173. self.DomainScene().GetComponent<GamePlayerComponent>().Add(self.GetId(), self);
  174. }
  175. /** 向客户端推送角色相关数据 **/
  176. public static void OnEndEnterScene(this WNPlayer self)
  177. {
  178. bool ready = self.GetComponent<PlayerTempDataComponent>().MapData.ready;
  179. if (ready)
  180. {
  181. Log.Warning($"$OnEndEnterScene跳过 : playerId={self.GetId()}, 玩家场景:{self.Map.MapId}, 进入场景:" + (self.Map?.MapId ?? -1));
  182. return;
  183. }
  184. self.GetComponent<PlayerTempDataComponent>().MapData.ready = true;
  185. self.GetXmdsManager().playerReady(self.GetId().ToString().Trim());
  186. //PKMode设置为All
  187. self.GetXmdsManager().refreshPlayerPKMode(self.GetId().ToString().Trim(), false, (int)PkModel.All);
  188. //设置为自动战斗
  189. self.GetXmdsManager().autoBattle(self.Map.Id.ToString().Trim(), self.GetId().ToString().Trim(), true);
  190. }
  191. /// <summary>
  192. /// 战斗服角色数据
  193. /// </summary>
  194. /// <param name="self"></param>
  195. /// <returns></returns>
  196. private static Dictionary<string, object> GetBattlerServerBasic(this WNPlayer self)
  197. {
  198. Dictionary<string, object> data = new Dictionary<string, object>();
  199. data.Add("name", self.GetName());
  200. data.Add("alliesForce", 0);
  201. data.Add("force", 1);
  202. data.Add("pro", self.GetPro());
  203. data.Add("serverId", ConstGame.GameServerId);
  204. data.Add("titleId", 0);
  205. data.Add("level", self.GetLevel());
  206. data.Add("vip", 0);
  207. data.Add("upLevel", 1);
  208. // 无悬赏
  209. data.Add("beReward", 0);
  210. data.Add("logicServerId", ConstGame.GameServerId);
  211. data.Add("sex", self.GetSex());
  212. data.Add("uuid", self.GetId().ToString());
  213. data.Add("potionAddition", 0);
  214. return data;
  215. }
  216. /// <summary>
  217. /// 场景中角色需求数据
  218. /// </summary>
  219. /// <param name="self"></param>
  220. /// <param name="map"></param>
  221. /// <returns></returns>
  222. public static string ToJSON4EnterScene(this WNPlayer self, Map map)
  223. {
  224. var json = new
  225. {
  226. effects = self.GetComponent<PlayerBtlComponent>().GetBattleServerEffects(),
  227. effectsExt = new { },
  228. skills = self.GetComponent<PlayerSkillComponent>().ToJson4BattleServerSkills,
  229. tasks = new { },
  230. flags = new { },
  231. playerEntered = false,
  232. avatars = new { },
  233. basic = self.GetBattlerServerBasic(),
  234. connectServerId = "bs-" + ConstGame.GameServerId,
  235. uid = self.GetId().ToString(),
  236. unitTemplateID = self.BasicProp.TemplateId,
  237. robot = false,
  238. tempData = self.GetComponent<PlayerTempDataComponent>().ToJson4BattleServerTempData,
  239. pkInfo = new { mode = 0, value = 0, level = 1, },
  240. //petBase,
  241. addTestPetData = 0,
  242. sceneData = new { allowAutoGuard = 3, },
  243. };
  244. return JsonSerializer.Serialize(json, new JsonSerializerOptions { IncludeFields = true });
  245. }
  246. }
  247. }