RoleModule.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using DeepCore;
  2. using DeepCrystal.ORM;
  3. using DeepCrystal.ORM.Generic;
  4. using DeepCrystal.RPC;
  5. using DeepMMO.Data;
  6. using DeepMMO.Protocol.Client;
  7. using DeepMMO.Server.AreaManager;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Threading.Tasks;
  11. namespace DeepMMO.Server.Logic.Model
  12. {
  13. public class RoleModule : ILogicModule
  14. {
  15. public MappingReference roleMapping { get; protected set; }
  16. public MappingReference snapMapping { get; protected set; }
  17. public LanguageManager Language { get; protected set; }
  18. public int LastMapID { get => GetRoleData().last_map_template_id; }
  19. public int LastPublicMapID { get => GetRoleData().last_public_mapID; }
  20. public string LastPublicMapUUID { get => GetRoleData().last_public_area_uuid; }
  21. public ZonePosition LastPublicPos { get => GetRoleData().last_public_map_pos; }
  22. public string LastZoneUUID { get => GetRoleData().last_zone_uuid; }
  23. public ZonePosition LastPos { get => GetRoleData().last_zone_pos; }
  24. public RoleModule(LogicService service) : base(service)
  25. {
  26. }
  27. public override async Task OnStartAsync()
  28. {
  29. var role = new MappingReference<ServerRoleData>(RPGServerPersistenceManager.TYPE_ROLE_DATA, service.roleID, service);
  30. await role.LoadDataAsync();
  31. var snap = new MappingReference<RoleSnap>(RPGServerPersistenceManager.TYPE_ROLE_SNAP_DATA, service.roleID, service);
  32. await snap.LoadDataAsync();
  33. roleMapping = role;
  34. snapMapping = snap;
  35. var trans = service.DBAdapter.CreateExecutableObjectTransaction(service);
  36. try
  37. {
  38. role.SetField(nameof(ServerRoleData.onlineState), RoleState.STATE_ONLINE);
  39. role.SetField(nameof(ServerRoleData.last_login_time), DateTime.Now);
  40. role.BatchFlush(trans);
  41. snap.SetField(nameof(RoleSnap.last_login_time), role.Data.last_login_time);
  42. snap.SetField(nameof(RoleSnap.onlineState), RoleState.STATE_ONLINE);
  43. snap.SetField(nameof(RoleSnap.session_name), service.sessionName);
  44. snap.BatchFlush(trans);
  45. }
  46. finally
  47. {
  48. await trans.ExecuteAsync();
  49. }
  50. this.Language = RPGServerTemplateManager.Instance.GetLanguage(GetRoleData().local_code);
  51. }
  52. public override Task OnStartedAsync()
  53. {
  54. return Task.CompletedTask;
  55. }
  56. public override Task OnStopAsync()
  57. {
  58. this.roleMapping.SetField(nameof(ServerRoleData.onlineState), RoleState.STATE_OFFLINE);
  59. this.snapMapping.SetField(nameof(ServerRoleData.onlineState), RoleState.STATE_OFFLINE);
  60. return Task.CompletedTask;
  61. }
  62. public override void OnSaveData(IObjectTransaction trans)
  63. {
  64. this.roleMapping.BatchFlush(trans);
  65. this.BeginSaveRoleSnap(trans);
  66. this.snapMapping.BatchFlush(trans);
  67. }
  68. protected virtual void BeginSaveRoleSnap(IObjectTransaction trans)
  69. {
  70. var data = GetRoleData();
  71. snapMapping.SetField(nameof(RoleSnap.name), data.name);
  72. snapMapping.SetField(nameof(RoleSnap.digitID), data.digitID);
  73. snapMapping.SetField(nameof(RoleSnap.uuid), data.uuid);
  74. snapMapping.SetField(nameof(RoleSnap.account_uuid), data.account_uuid);
  75. snapMapping.SetField(nameof(RoleSnap.role_template_id), data.role_template_id);
  76. snapMapping.SetField(nameof(RoleSnap.unit_template_id), data.unit_template_id);
  77. snapMapping.SetField(nameof(RoleSnap.server_id), data.server_id);
  78. snapMapping.SetField(nameof(RoleSnap.level), data.Level);
  79. }
  80. protected override void Disposing()
  81. {
  82. roleMapping.Dispose();
  83. snapMapping.Dispose();
  84. }
  85. public void NotifyPlayerSingleDynamic(string key, string value, bool isNum)
  86. {
  87. List<PropertyStruct> psList = new List<PropertyStruct>();
  88. PropertyStruct ps = new PropertyStruct(key, value, isNum);
  89. psList.Add(ps);
  90. this.NotifyPlayerDynamic(psList);
  91. }
  92. public void NotifyPlayerDynamic(List<PropertyStruct> ps)
  93. {
  94. PlayerDynamicNotify notify = new PlayerDynamicNotify();
  95. notify.s2c_data = ps;
  96. this.service.session.Invoke(notify);
  97. }
  98. public virtual void SaveEnterZoneInfo(RoleEnterZoneResponse rsp)
  99. {
  100. //如果记录野外场景的uuid和MapID.
  101. //解决公共场景切线以后进入副本出来以后不会原来线的问题
  102. //if (IsPublicMap(rsp.mapTemplateID) && this.GetRoleData().last_map_template_id != rsp.mapTemplateID)
  103. if (IsPublicMap(rsp.mapTemplateID))
  104. {
  105. //如果和上一次不同,记录.
  106. this.roleMapping.SetField(nameof(ServerRoleData.last_public_mapID), rsp.mapTemplateID);
  107. this.roleMapping.SetField(nameof(ServerRoleData.last_public_area_uuid), rsp.zoneUUID);
  108. this.roleMapping.SetField(nameof(ServerRoleData.last_public_map_pos), rsp.roleScenePos);
  109. //公共场景覆盖公会场景记录.
  110. this.roleMapping.SetField(nameof(ServerRoleData.last_guild_mapID), 0);
  111. }
  112. else if (IsGuildMap(rsp.mapTemplateID))//记录上一次的公会场景ID.
  113. {
  114. this.roleMapping.SetField(nameof(ServerRoleData.last_guild_mapID), rsp.mapTemplateID);
  115. }
  116. this.roleMapping.SetField(nameof(ServerRoleData.last_map_template_id), rsp.mapTemplateID);
  117. this.roleMapping.SetField(nameof(ServerRoleData.last_zone_uuid), rsp.zoneUUID);
  118. this.roleMapping.SetField(nameof(ServerRoleData.last_area_name), rsp.areaName);
  119. this.roleMapping.SetField(nameof(ServerRoleData.last_area_node), rsp.areaNode);
  120. this.roleMapping.SetField(nameof(ServerRoleData.last_zone_pos), rsp.roleScenePos);
  121. }
  122. public virtual void SaveLeaveZoneInfo(RoleLeaveZoneResponse pos)
  123. {
  124. this.roleMapping.SetField(nameof(ServerRoleData.last_zone_saved), pos.LeaveZoneSaveData);
  125. this.roleMapping.SetField(nameof(ServerRoleData.last_zone_pos), pos.lastScenePos);
  126. if (IsPublicMap(LastMapID))
  127. {
  128. this.roleMapping.SetField(nameof(ServerRoleData.last_public_map_pos), pos.lastScenePos);
  129. }
  130. }
  131. public virtual ClientRoleData ToClientRoleData()
  132. {
  133. return GetRoleData().ToClientRoleData();
  134. }
  135. protected virtual bool IsPublicMap(int mapID)
  136. {
  137. var temp = RPGServerTemplateManager.Instance.GetMapTemplate(mapID);
  138. return temp.is_public;
  139. }
  140. protected virtual bool IsGuildMap(int mapID)
  141. {
  142. return false;
  143. }
  144. internal ServerRoleData GetRoleData()
  145. {
  146. return ((ServerRoleData)roleMapping.Data);
  147. }
  148. // 死亡掉装备耐久度
  149. public virtual float GetDeadLoseDurablerate()
  150. {
  151. return 0.03f;
  152. }
  153. }
  154. }