using System; using System.Collections.Generic; using Cysharp.Threading.Tasks; using Gs2.Unity.Core; using Gs2.Unity.Gs2Quest.Model; using Gs2.Unity.Gs2Money2.Model; using Gs2.Unity.Util; public sealed class AdventurePlayer : IDisposable { public Gs2Domain Gs2 { get; } public GameSession Session { get; } public IReadOnlyList Quests { get; private set; } = new List(); public EzProgress Progress { get; private set; } public EzWallet Wallet { get; private set; } public bool Ready { get; private set; } public bool HasPlayerData => Ready && Wallet?.Summary != null; private bool disposed; private Action unsubscribe; public AdventurePlayer(Gs2Domain gs2, GameSession session) { Gs2 = gs2; Session = session; } public async UniTask Load() { var current = Gs2.Quest.Namespace("adventure-quest").Me(Session).Progress(); var progressId = await current.SubscribeWithInitialCallAsync(value => Progress = value); if (!KeepSubscription(() => current.Unsubscribe(progressId))) return; var balance = Gs2.Money2.Namespace("adventure-coin").Me(Session).Wallet(0); var walletId = await balance.SubscribeWithInitialCallAsync(value => Wallet = value); if (!KeepSubscription(() => balance.Unsubscribe(walletId))) return; await LoadQuests(); Ready = !disposed; } private async UniTask LoadQuests() { var group = Gs2.Quest.Namespace("adventure-quest").QuestGroupModel("main"); var items = new List(); await foreach (var item in group.QuestModelsAsync()) { if (item != null) items.Add(item); } if (!disposed) Quests = items; } private bool KeepSubscription(Action stop) { if (disposed) { stop(); return false; } unsubscribe += stop; return true; } public void Dispose() { disposed = true; unsubscribe?.Invoke(); unsubscribe = null; } }