DBProxyComponentSystem.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using ETModel;
  7. namespace ETHotfix
  8. {
  9. [ObjectSystem]
  10. public class DbProxyComponentSystem : AwakeSystem<DBProxyComponent>
  11. {
  12. public override void Awake(DBProxyComponent self)
  13. {
  14. self.Awake();
  15. }
  16. }
  17. /// <summary>
  18. /// 用来与数据库操作代理
  19. /// </summary>
  20. public static class DBProxyComponentEx
  21. {
  22. public static void Awake(this DBProxyComponent self)
  23. {
  24. StartConfig dbStartConfig = Game.Scene.GetComponent<StartConfigComponent>().DBConfig;
  25. self.dbAddress = dbStartConfig.GetComponent<InnerConfig>().IPEndPoint;
  26. }
  27. public static async Task Save(this DBProxyComponent self, ComponentWithId component, bool needCache = true)
  28. {
  29. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  30. await session.Call(new DBSaveRequest { Component = component, NeedCache = needCache});
  31. }
  32. public static async Task SaveBatch(this DBProxyComponent self, List<ComponentWithId> components, bool needCache = true)
  33. {
  34. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  35. await session.Call(new DBSaveBatchRequest { Components = components, NeedCache = needCache});
  36. }
  37. public static async Task Save(this DBProxyComponent self, ComponentWithId component, bool needCache, CancellationToken cancellationToken)
  38. {
  39. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  40. await session.Call(new DBSaveRequest { Component = component, NeedCache = needCache}, cancellationToken);
  41. }
  42. public static async void SaveLog(this DBProxyComponent self, ComponentWithId component)
  43. {
  44. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  45. await session.Call(new DBSaveRequest { Component = component, NeedCache = false, CollectionName = "Log" });
  46. }
  47. public static async Task<T> Query<T>(this DBProxyComponent self, long id, bool needCache = true) where T: ComponentWithId
  48. {
  49. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  50. DBQueryResponse dbQueryResponse = (DBQueryResponse)await session.Call(new DBQueryRequest { CollectionName = typeof(T).Name, Id = id, NeedCache = needCache });
  51. return (T)dbQueryResponse.Component;
  52. }
  53. public static async Task<List<T>> QueryBatch<T>(this DBProxyComponent self, List<long> ids, bool needCache = true) where T : ComponentWithId
  54. {
  55. List<T> list = new List<T>();
  56. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  57. DBQueryBatchResponse dbQueryBatchResponse = (DBQueryBatchResponse)await session.Call(new DBQueryBatchRequest { CollectionName = typeof(T).Name, IdList = ids, NeedCache = needCache});
  58. foreach (ComponentWithId component in dbQueryBatchResponse.Components)
  59. {
  60. list.Add((T)component);
  61. }
  62. return list;
  63. }
  64. public static async Task<List<T>> Query<T>(this DBProxyComponent self, Expression<Func<T, bool>> func) where T : ComponentWithId
  65. {
  66. ExpressionVistor vistor = new ExpressionVistor(func);
  67. return await self.QueryJson<T>(vistor.Output);
  68. }
  69. public static async Task<List<T>> QueryJson<T>(this DBProxyComponent self, string json) where T : ComponentWithId
  70. {
  71. List<T> list = new List<T>();
  72. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  73. DBQueryJsonResponse dbQueryJsonResponse = (DBQueryJsonResponse)await session.Call(new DBQueryJsonRequest { CollectionName = typeof(T).Name, Json = json });
  74. foreach (ComponentWithId component in dbQueryJsonResponse.Components)
  75. {
  76. list.Add((T)component);
  77. }
  78. return list;
  79. }
  80. }
  81. }