Skip to content

Commit

Permalink
Refactor patrol reward popup
Browse files Browse the repository at this point in the history
- call PatrolRewardPopup.InitializePatrolReward() at States.SelectAvatarAsync()
- refactor PatrolRewardMenu
- delete _disposables in PatrolRewardPopup
- call PatrolReward.Initialize() at PatrolReward
  • Loading branch information
tyrosine1153 committed Oct 17, 2023
1 parent 6156fe7 commit 2af1ee6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 58 deletions.
1 change: 1 addition & 0 deletions nekoyume/Assets/_Scripts/State/States.cs
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ await UniTask.Run(async () =>
await SetCombinationSlotStatesAsync(curAvatarState);
await AddOrReplaceAvatarStateAsync(curAvatarState, CurrentAvatarKey);
await SetPetStates(avatarState.address);
await Widget.Find<PatrolRewardPopup>().InitializePatrolReward();
});
}

Expand Down
33 changes: 15 additions & 18 deletions nekoyume/Assets/_Scripts/UI/Model/PatrolReward.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,6 @@ public class PatrolReward
private const string PatrolRewardPushIdentifierKey = "PATROL_REWARD_PUSH_IDENTIFIER";
private bool _initialized;

public void Initialize()
{
if (_initialized)
{
return;
}

_initialized = true;
PatrolTime = Observable.Timer(TimeSpan.Zero, TimeSpan.FromMinutes(1))
.CombineLatest(LastRewardTime, (_, lastReward) =>
{
var timeSpan = DateTime.Now - lastReward;
return timeSpan > Interval ? Interval : timeSpan;
})
.ToReactiveProperty();
LastRewardTime.Subscribe(_ => SetPushNotification());
}

public async Task InitializeInformation(string avatarAddress, string agentAddress, int level)
{
var serviceClient = Game.Game.instance.PatrolRewardServiceClient;
Expand Down Expand Up @@ -104,6 +86,21 @@ ... on FungibleItemRewardModel {{
Interval = response.Policy.MinimumRequiredInterval;
RewardModels.Value = response.Policy.Rewards;
}

if (_initialized)
{
return;
}

_initialized = true;
PatrolTime = Observable.Timer(TimeSpan.Zero, TimeSpan.FromMinutes(1))
.CombineLatest(LastRewardTime, (_, lastReward) =>
{
var timeSpan = DateTime.Now - lastReward;
return timeSpan > Interval ? Interval : timeSpan;
})
.ToReactiveProperty();
LastRewardTime.Subscribe(_ => SetPushNotification());
}

public async Task LoadAvatarInfo(string avatarAddress, string agentAddress)
Expand Down
21 changes: 4 additions & 17 deletions nekoyume/Assets/_Scripts/UI/Module/Lobby/PatrolRewardMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,14 @@ private void OnEnable()
return;
}

SetData(popup);
}

private async void SetData(PatrolRewardPopup popup)
{
await popup.InitializePatrolReward();

var patrolReward = popup.PatrolReward;
patrolReward.PatrolTime
.Select(time => time < patrolReward.Interval)
popup.PatrolReward.PatrolTime
.Select(time => time < popup.PatrolReward.Interval)
.Where(_ => !popup.Claiming.Value)
.Subscribe(patrolling => SetCanClaim(patrolling, false))
.AddTo(_disposables);

popup.Claiming.Subscribe(value =>
{
if (value)
{
SetCanClaim(false, true);
}
}).AddTo(_disposables);
popup.Claiming.Where(claiming => claiming)
.Subscribe(value => SetCanClaim(false, true)).AddTo(_disposables);
}

private void SetCanClaim(bool patrolling, bool claiming)
Expand Down
32 changes: 9 additions & 23 deletions nekoyume/Assets/_Scripts/UI/Widget/Popup/PatrolRewardPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Libplanet.Crypto;
using Nekoyume.Game.Controller;
using Nekoyume.GraphQL;
using Nekoyume.L10n;
Expand Down Expand Up @@ -45,9 +44,7 @@ private class RewardData
public readonly PatrolReward PatrolReward = new();
public readonly ReactiveProperty<bool> Claiming = new(false);
private readonly Dictionary<PatrolRewardType, int> _rewards = new();
private readonly List<IDisposable> _disposables = new();

private Address _currentAvatarAddress;
private bool _initialized;

public bool CanClaim =>
Expand Down Expand Up @@ -75,30 +72,20 @@ public override void Show(bool ignoreShowAnimation = false)
ShowAsync(ignoreShowAnimation);
}

// Called at CurrentAvatarState isNewlySelected
public async Task InitializePatrolReward()
{
var avatarAddress = Game.Game.instance.States.CurrentAvatarState.address;
if (_currentAvatarAddress.Equals(avatarAddress))
{
return;
}

var agentAddress = Game.Game.instance.States.AgentState.address;
var level = Game.Game.instance.States.CurrentAvatarState.level;
await PatrolReward.InitializeInformation(avatarAddress.ToHex(), agentAddress.ToHex(), level);
PatrolReward.Initialize();

// for changed avatar
Claiming.Value = false;

_currentAvatarAddress = avatarAddress;
}

private async void ShowAsync(bool ignoreShowAnimation = false)
{
await InitializePatrolReward();

SetIntervalText(PatrolReward.Interval);

if (!_initialized)
{
Init();
Expand All @@ -108,33 +95,32 @@ private async void ShowAsync(bool ignoreShowAnimation = false)
if (PatrolReward.NextLevel <= level)
{
await PatrolReward.LoadPolicyInfo(level);

SetIntervalText(PatrolReward.Interval);
}

SetIntervalText(PatrolReward.Interval);
OnChangeTime(PatrolReward.PatrolTime.Value);
base.Show(ignoreShowAnimation);
}

// it must be called after PatrolReward.InitializeInformation (called avatar selected)
private void Init()
{
_disposables.DisposeAllAndClear();

PatrolReward.RewardModels
.Subscribe(OnChangeRewardModels)
.AddTo(_disposables);
.AddTo(gameObject);

PatrolReward.PatrolTime
.Where(_ => gameObject.activeSelf)
.Subscribe(OnChangeTime)
.AddTo(_disposables);
.AddTo(gameObject);

receiveButton.OnSubmitSubject
.Subscribe(_ => ClaimRewardAsync())
.AddTo(_disposables);
.AddTo(gameObject);

Claiming.Where(claiming => claiming)
.Subscribe(_ => receiveButton.Interactable = false);
.Subscribe(_ => receiveButton.Interactable = false)
.AddTo(gameObject);

_initialized = true;
}
Expand Down

0 comments on commit 2af1ee6

Please sign in to comment.