RPGServerTemplateManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. using System;
  2. using System.Collections.Generic;
  3. using DeepCore;
  4. using DeepMMO.Data;
  5. using DeepMMO.Protocol.Client;
  6. using DeepCrystal.RPC;
  7. using DeepCore.Reflection;
  8. using DeepCore.Log;
  9. using DeepMMO.Server.Logic;
  10. using DeepMMO.Server.AreaManager;
  11. using DeepMMO.Server.Gate;
  12. using DeepCore.Xml;
  13. using System.Xml;
  14. using DeepCore.IO;
  15. using System.Threading;
  16. namespace DeepMMO.Server
  17. {
  18. public abstract class RPGServerTemplateManager
  19. {
  20. //--------------------------------------------------------------------------------------
  21. #region Singleton
  22. private static readonly object lock_init = new object();
  23. private static bool init_done = false;
  24. private static RPGServerTemplateManager instance;
  25. public static bool IsInitDone { get { return init_done; } }
  26. public static RPGServerTemplateManager Instance
  27. {
  28. get
  29. {
  30. if (instance == null)
  31. {
  32. lock (lock_init)
  33. {
  34. if (!init_done)
  35. {
  36. var config = IService.GlobalConfig;
  37. instance = ReflectionUtil.CreateInterface<RPGServerTemplateManager>(GlobalConfig.RPGServerTemplateManager);
  38. instance.Init();
  39. init_done = true;
  40. }
  41. }
  42. }
  43. return instance;
  44. }
  45. }
  46. #endregion
  47. //--------------------------------------------------------------------------------------
  48. protected readonly Logger log;
  49. protected RPGServerTemplateManager()
  50. {
  51. instance = this;
  52. log = new LazyLogger(GetType().Name);
  53. }
  54. public virtual void Init()
  55. {
  56. LoadServerList();
  57. }
  58. //--------------------------------------------------------------------------------------
  59. #region TemplatesData
  60. public abstract RoleTemplateData[] AllRoleTemplates { get; }
  61. public abstract MapTemplateData[] AllMapTemplates { get; }
  62. public abstract MapTemplateData GetDefaultMapData(RoleEnterZoneRequest enter);
  63. public abstract RoleTemplateData GetRoleTemplate(int id, byte gender);
  64. public abstract MapTemplateData GetMapTemplate(int id);
  65. /// <summary>
  66. ///
  67. /// </summary>
  68. /// <param name="local_code">zh_CN, zh_TW, en_US</param>
  69. /// <returns></returns>
  70. public abstract LanguageManager GetLanguage(string local_code);
  71. #endregion
  72. //--------------------------------------------------------------------------------------
  73. #region RandomName
  74. public virtual string RandomName(RoleTemplateData role)
  75. {
  76. string ret = ":" + new Random().Next();
  77. return ret;
  78. }
  79. #endregion
  80. //--------------------------------------------------------------------------------------
  81. //--------------------------------------------------------------------------------------
  82. /// <summary>
  83. /// 是否在屏蔽字库内.
  84. /// </summary>
  85. /// <param name="word"></param>
  86. /// <returns></returns>
  87. public virtual bool IsBlackWord(string word)
  88. {
  89. return false;
  90. }
  91. public int GetRoleMaxCount()
  92. {
  93. //TODO.
  94. return 5;
  95. }
  96. public virtual ServerRoleData CreateRoleData(ClientCreateRoleRequest req, string accountID, string serverid)
  97. {
  98. var roleTemplate = GetRoleTemplate(req.c2s_template_id, 0);
  99. if (roleTemplate == null)
  100. {
  101. return null;
  102. }
  103. var roleID = (Guid.NewGuid().ToString());
  104. //玩家角色信息.
  105. ServerRoleData srd = new ServerRoleData();
  106. //创建UUID.
  107. srd.uuid = roleID;
  108. srd.account_uuid = accountID;
  109. srd.name = req.c2s_name;
  110. srd.server_id = serverid;
  111. srd.role_template_id = req.c2s_template_id;
  112. srd.unit_template_id = req.c2s_template_id;
  113. srd.create_time = DateTime.UtcNow;
  114. return srd;
  115. }
  116. public virtual RoleIDSnap CreateRoleIDSnapData(ServerRoleData roleData)
  117. {
  118. var ret = new RoleIDSnap();
  119. ret.name = roleData.name;
  120. ret.roleUUID = roleData.uuid;
  121. ret.serverID = roleData.server_id;
  122. ret.lv = roleData.Level;
  123. return ret;
  124. }
  125. //--------------------------------------------------------------------------------------
  126. /// <summary>
  127. /// 野外.
  128. /// </summary>
  129. /// <param name="data"></param>
  130. /// <returns></returns>
  131. public virtual bool IsPublicMap(MapTemplateData data)
  132. {
  133. return true;
  134. }
  135. /// <summary>
  136. /// 是否是连服场景.
  137. /// </summary>
  138. /// <param name="data"></param>
  139. /// <returns></returns>
  140. public virtual bool IsCrossServerMap(MapTemplateData data)
  141. {
  142. return false;
  143. }
  144. /// <summary>
  145. /// 一次性地图,每次都重新分配.
  146. /// </summary>
  147. /// <param name="data"></param>
  148. /// <returns></returns>
  149. public virtual bool IsDisposableMap(MapTemplateData data)
  150. {
  151. return false;
  152. }
  153. public virtual bool AllowChangeLine(MapTemplateData data)
  154. {
  155. return false;
  156. }
  157. //--------------------------------------------------------------------------------------
  158. #region ServerList
  159. private ReaderWriterLockSlim serverListLock = new ReaderWriterLockSlim();
  160. private HashMap<string, ServerInfo> serverList = new HashMap<string, ServerInfo>();
  161. private HashMap<string, List<ServerInfo>> groupList = new HashMap<string, List<ServerInfo>>();
  162. public virtual void LoadServerList()
  163. {
  164. var serverListPath = GlobalConfig.ServerListUrl;
  165. try
  166. {
  167. var sList = new HashMap<string, ServerInfo>();
  168. var gList = new HashMap<string, List<ServerInfo>>();
  169. //log.Warn("Load Server From : " + serverListPath + "\n" + Resource.LoadAllText(GlobalConfig.ServerListUrl));
  170. var doc = ServerInfo.LoadServerList(serverListPath, GlobalConfig.RealmID, sList, gList);
  171. log.Warn("LoadServerList From : " + serverListPath + "\n" + doc.ToXmlString());
  172. using (serverListLock.EnterWrite())
  173. {
  174. foreach (var e in sList)
  175. {
  176. log.Warn($"{e.Value} reload");
  177. }
  178. this.serverList.PutAll(sList);
  179. this.groupList.PutAll(gList);
  180. }
  181. }
  182. catch (Exception err)
  183. {
  184. throw new Exception("LoadServerList Error From : " + serverListPath, err);
  185. }
  186. }
  187. public virtual void LoadServerListTime()
  188. {
  189. var serverListPath = GlobalConfig.ServerListUrl;
  190. try
  191. {
  192. var sList = new HashMap<string, ServerInfo>();
  193. var gList = new HashMap<string, List<ServerInfo>>();
  194. //log.Warn("Load Server From : " + serverListPath + "\n" + Resource.LoadAllText(GlobalConfig.ServerListUrl));
  195. var doc = ServerInfo.LoadServerList(serverListPath, GlobalConfig.RealmID, sList, gList);
  196. log.Warn("LoadServerListTime From : " + serverListPath + "\n" + doc.ToXmlString());
  197. using (serverListLock.EnterWrite())
  198. {
  199. foreach (var e in sList)
  200. {
  201. if (serverList.TryGetValue(e.Key, out var server))
  202. {
  203. server.open_at = e.Value.open_at;
  204. log.Warn($"{server} open_at:{server.open_at}");
  205. }
  206. }
  207. }
  208. }
  209. catch (Exception err)
  210. {
  211. throw new Exception("LoadServerListTime Error From : " + serverListPath, err);
  212. }
  213. }
  214. public virtual string GetServerGroupID(string serverID)
  215. {
  216. using (serverListLock.EnterRead())
  217. {
  218. if (serverList.TryGetValue(serverID, out var info))
  219. {
  220. return info.group;
  221. }
  222. }
  223. return null;
  224. }
  225. public virtual List<string> GetAllServerGroupIdList()
  226. {
  227. var groupIds = groupList.Keys.ToGenericList<string>();
  228. return groupIds;
  229. }
  230. /// <summary>
  231. ///组对应的服务器ID.
  232. /// </summary>
  233. /// <param name="serverGroupID"></param>
  234. /// <returns></returns>
  235. public virtual List<string> GetServers(string serverGroupID)
  236. {
  237. using (serverListLock.EnterRead())
  238. {
  239. if (groupList.TryGetValue(serverGroupID, out var list))
  240. {
  241. return list.ConvertAll(e => e.id);
  242. }
  243. }
  244. return null;
  245. }
  246. public virtual List<ServerInfo> GetServersInfo(string serverGroupID)
  247. {
  248. using (serverListLock.EnterRead())
  249. {
  250. if (groupList.TryGetValue(serverGroupID, out var list))
  251. {
  252. return list;
  253. }
  254. }
  255. return null;
  256. }
  257. public virtual List<string> GetAllServerGroupID()
  258. {
  259. using (serverListLock.EnterRead())
  260. {
  261. return new List<string>(groupList.Keys);
  262. }
  263. }
  264. /// <summary>
  265. ///
  266. /// </summary>
  267. /// <param name="serverID"></param>
  268. /// <returns></returns>
  269. public virtual bool ServerIsOpen(string serverID)
  270. {
  271. using (serverListLock.EnterRead())
  272. {
  273. if (serverList.TryGetValue(serverID, out var info))
  274. {
  275. return info.is_open;
  276. }
  277. }
  278. return false;
  279. }
  280. /// <summary>
  281. /// 获取所有服务器配置
  282. /// </summary>
  283. /// <returns></returns>
  284. public virtual List<ServerInfo> GetAllServers()
  285. {
  286. using (serverListLock.EnterRead())
  287. {
  288. return new List<ServerInfo>(serverList.Values);
  289. }
  290. }
  291. /// <summary>
  292. /// 开服时间,非UTC时间.
  293. /// </summary>
  294. /// <param name="serverID"></param>
  295. /// <returns></returns>
  296. public virtual DateTime GetServerOpenTime(string serverID)
  297. {
  298. using (serverListLock.EnterRead())
  299. {
  300. if (serverList.TryGetValue(serverID, out var info))
  301. {
  302. return info.open_at;
  303. }
  304. }
  305. return new DateTime();
  306. }
  307. /// <summary>
  308. /// 根据group获得开服时间,合服的话以最早开的为主
  309. /// </summary>
  310. /// <param name="groupid"></param>
  311. /// <returns></returns>
  312. public DateTime GetOpenTimeByGroupID(string groupid)
  313. {
  314. DateTime openTime = DateTime.MaxValue;
  315. using (serverListLock.EnterRead())
  316. {
  317. if (groupList.TryGetValue(groupid, out var list))
  318. {
  319. foreach (var server in list)
  320. {
  321. if (DateTime.Compare(openTime, server.open_at) > 0)
  322. {
  323. openTime = server.open_at;
  324. }
  325. }
  326. }
  327. }
  328. return openTime;
  329. }
  330. public virtual HashMap<string, DateTime> GetAllServerOpenTime()
  331. {
  332. HashMap<string, DateTime> dateTimes = new HashMap<string, DateTime>();
  333. using (serverListLock.EnterRead())
  334. {
  335. foreach (var data in serverList)
  336. {
  337. dateTimes.Add(data.Key, data.Value.open_at);
  338. }
  339. }
  340. return dateTimes;
  341. }
  342. #endregion
  343. //--------------------------------------------------------------------------------------
  344. public virtual ISerializable GetCreateZoneExpandData(RoleEnterZoneRequest req)
  345. {
  346. return null;
  347. }
  348. public virtual bool IsValidOfPrivilege(int privilege, string gmContent)
  349. {
  350. return true;
  351. }
  352. // H.Q.Cai 添加代码开始
  353. public virtual void PostGroupServerNumber(string serverGroupID, int number)
  354. {
  355. }
  356. // H.Q.Cai 添加代码结束
  357. }
  358. }