DBProxyComponentSystem.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. using MongoDB.Bson;
  8. using MongoDB.Bson.Serialization;
  9. using MongoDB.Bson.Serialization.Serializers;
  10. using MongoDB.Driver;
  11. namespace ETHotfix
  12. {
  13. [ObjectSystem]
  14. public class DbProxyComponentSystem : AwakeSystem<DBProxyComponent>
  15. {
  16. public override void Awake(DBProxyComponent self)
  17. {
  18. self.Awake();
  19. }
  20. }
  21. /// <summary>
  22. /// 用来与数据库操作代理
  23. /// </summary>
  24. public static class DBProxyComponentEx
  25. {
  26. public static void Awake(this DBProxyComponent self)
  27. {
  28. StartConfig dbStartConfig = Game.Scene.GetComponent<StartConfigComponent>().DBConfig;
  29. self.dbAddress = dbStartConfig.GetComponent<InnerConfig>().IPEndPoint;
  30. }
  31. public static async Task Save(this DBProxyComponent self, ComponentWithId component, bool needCache = true)
  32. {
  33. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  34. await session.Call(new DBSaveRequest { Component = component, NeedCache = needCache});
  35. }
  36. public static async Task SaveBatch(this DBProxyComponent self, List<ComponentWithId> components, bool needCache = true)
  37. {
  38. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  39. await session.Call(new DBSaveBatchRequest { Components = components, NeedCache = needCache});
  40. }
  41. public static async Task Save(this DBProxyComponent self, ComponentWithId component, bool needCache, CancellationToken cancellationToken)
  42. {
  43. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  44. await session.Call(new DBSaveRequest { Component = component, NeedCache = needCache}, cancellationToken);
  45. }
  46. public static async void SaveLog(this DBProxyComponent self, ComponentWithId component)
  47. {
  48. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  49. await session.Call(new DBSaveRequest { Component = component, NeedCache = false, CollectionName = "Log" });
  50. }
  51. public static async Task<T> Query<T>(this DBProxyComponent self, long id, bool needCache = true) where T: ComponentWithId
  52. {
  53. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  54. DBQueryResponse dbQueryResponse = (DBQueryResponse)await session.Call(new DBQueryRequest { CollectionName = typeof(T).Name, Id = id, NeedCache = needCache });
  55. return (T)dbQueryResponse.Component;
  56. }
  57. /// <summary>
  58. /// 根据查询表达式查询
  59. /// </summary>
  60. /// <param name="self"></param>
  61. /// <param name="exp"></param>
  62. /// <typeparam name="T"></typeparam>
  63. /// <returns></returns>
  64. public static async Task<List<ComponentWithId>> Query<T>(this DBProxyComponent self, Expression<Func<T ,bool>> exp) where T: ComponentWithId
  65. {
  66. ExpressionFilterDefinition<T> filter = new ExpressionFilterDefinition<T>(exp);
  67. IBsonSerializerRegistry serializerRegistry = BsonSerializer.SerializerRegistry;
  68. IBsonSerializer<T> documentSerializer = serializerRegistry.GetSerializer<T>();
  69. string json = filter.Render(documentSerializer, serializerRegistry).ToJson();
  70. return await self.Query<T>(json);
  71. }
  72. public static async Task<List<ComponentWithId>> Query<T>(this DBProxyComponent self, List<long> ids, bool needCache = true) where T : ComponentWithId
  73. {
  74. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  75. DBQueryBatchResponse dbQueryBatchResponse = (DBQueryBatchResponse)await session.Call(new DBQueryBatchRequest { CollectionName = typeof(T).Name, IdList = ids, NeedCache = needCache});
  76. return dbQueryBatchResponse.Components;
  77. }
  78. /// <summary>
  79. /// 根据json查询条件查询
  80. /// </summary>
  81. /// <param name="self"></param>
  82. /// <param name="json"></param>
  83. /// <typeparam name="T"></typeparam>
  84. /// <returns></returns>
  85. public static async Task<List<ComponentWithId>> Query<T>(this DBProxyComponent self, string json) where T : ComponentWithId
  86. {
  87. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  88. DBQueryJsonResponse dbQueryJsonResponse = (DBQueryJsonResponse)await session.Call(new DBQueryJsonRequest { CollectionName = typeof(T).Name, Json = json });
  89. return dbQueryJsonResponse.Components;
  90. }
  91. }
  92. }