LocationProxyComponent.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Threading.Tasks;
  2. namespace Model
  3. {
  4. [ObjectEvent]
  5. public class LocationProxyComponentEvent : ObjectEvent<LocationProxyComponent>, IAwake
  6. {
  7. public void Awake()
  8. {
  9. this.Get().Awake();
  10. }
  11. }
  12. public class LocationProxyComponent : Component
  13. {
  14. public string LocationAddress;
  15. public int AppId;
  16. public void Awake()
  17. {
  18. StartConfigComponent startConfigComponent = Game.Scene.GetComponent<StartConfigComponent>();
  19. this.AppId = startConfigComponent.StartConfig.AppId;
  20. StartConfig startConfig = startConfigComponent.LocationConfig;
  21. this.LocationAddress = startConfig.GetComponent<InnerConfig>().Address;
  22. }
  23. public async Task Add(long key)
  24. {
  25. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.LocationAddress);
  26. await session.Call<ObjectAddResponse>(new ObjectAddRequest() { Key = key, AppId = this.AppId });
  27. }
  28. public async Task Lock(long key, int time = 1000)
  29. {
  30. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.LocationAddress);
  31. await session.Call<ObjectLockResponse>(new ObjectLockRequest() { Key = key, LockAppId = this.AppId, Time = time });
  32. }
  33. public async Task UnLock(long key, int value)
  34. {
  35. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.LocationAddress);
  36. await session.Call<ObjectUnLockResponse>(new ObjectUnLockRequest() { Key = key, LockAppId = this.AppId, AppId = value});
  37. }
  38. public async Task Remove(long key)
  39. {
  40. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.LocationAddress);
  41. await session.Call<ObjectRemoveResponse>(new ObjectRemoveRequest() { Key = key });
  42. }
  43. public async Task<int> Get(long key)
  44. {
  45. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.LocationAddress);
  46. ObjectGetResponse response = await session.Call<ObjectGetResponse>(new ObjectGetRequest() { Key = key });
  47. return response.AppId;
  48. }
  49. public override void Dispose()
  50. {
  51. if (this.Id == 0)
  52. {
  53. return;
  54. }
  55. base.Dispose();
  56. }
  57. }
  58. }