RewardedSlotCoordinator.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Fateforge
  4. {
  5. public sealed class RewardedSlotCoordinator : IDisposable
  6. {
  7. readonly IRewardedAdService ads;
  8. readonly Func<Game> current;
  9. readonly Action save;
  10. readonly HashSet<string> closed = new HashSet<string>();
  11. string opportunityRun;
  12. AdPurpose opportunityPurpose;
  13. bool receivedReward;
  14. public AdRequest Pending { get; private set; }
  15. public event Action<AdResult> Finished;
  16. public RewardedSlotCoordinator(IRewardedAdService ads, Func<Game> current, Action save)
  17. {
  18. this.ads = ads;
  19. this.current = current;
  20. this.save = save;
  21. ads.Result += Receive;
  22. }
  23. internal static bool CanRequest(Game game, AdPurpose purpose)
  24. {
  25. if (game == null) return false;
  26. return purpose == AdPurpose.ShopSlot
  27. ? game.State.stage == Stage.Shop && !game.State.adSlot
  28. : purpose == AdPurpose.ExtraCollect && game.State.stage == Stage.ReviveChoice && !game.State.reviveUsed;
  29. }
  30. public bool Request(AdPurpose purpose = AdPurpose.ShopSlot)
  31. {
  32. var game = current();
  33. if (Pending != null || !CanRequest(game, purpose) || !ads.Available) return false;
  34. if (opportunityRun != game.State.id || opportunityPurpose != purpose) closed.Clear();
  35. opportunityRun = game.State.id;
  36. opportunityPurpose = purpose;
  37. receivedReward = false;
  38. Pending = new AdRequest{runId=opportunityRun, requestId=Guid.NewGuid().ToString("N"), purpose=purpose};
  39. if (ads.Show(Pending)) return true;
  40. Pending = null;
  41. return false;
  42. }
  43. void Receive(AdRequest request, AdResult result)
  44. {
  45. if (request == null || request.runId != opportunityRun || request.purpose != opportunityPurpose) return;
  46. bool active = request.requestId == Pending?.requestId;
  47. if (!active && (result != AdResult.Rewarded || !closed.Remove(request.requestId))) return;
  48. var game = current();
  49. if (game == null || game.State.id != request.runId)
  50. {
  51. if (active) Pending = null;
  52. return;
  53. }
  54. if (result == AdResult.Rewarded)
  55. {
  56. bool granted = request.purpose == AdPurpose.ExtraCollect
  57. ? game.GrantExtraCollect(request.runId, request.requestId)
  58. : game.State.stage == Stage.Shop && game.GrantAdSlot(request.runId, request.requestId);
  59. if (!granted) return;
  60. // Commit immediately, then release the UI when the covering video closes.
  61. closed.Clear();
  62. receivedReward = true;
  63. save();
  64. if (Pending == null) Finished?.Invoke(AdResult.Rewarded);
  65. return;
  66. }
  67. if (result == AdResult.AwaitingLateReward && !receivedReward) closed.Add(request.requestId);
  68. Pending = null;
  69. Finished?.Invoke(receivedReward ? AdResult.Rewarded : result);
  70. }
  71. public void Dispose()
  72. {
  73. ads.Result -= Receive;
  74. Pending = null;
  75. }
  76. }
  77. }