PlayerSystem.cs 10 KB

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