hellbound/Assets/Scripts/Game/TimeRewards/Offline/OfflineReward.cs

64 lines
1.6 KiB
C#
Raw Permalink Normal View History

2021-11-26 11:16:25 +03:00
using System;
using game;
using UniRx;
public class OfflineReward : ITimeReward
{
public bool IsAvailable => _lastEntryTime != null; //TODO: to reactive
private readonly ConfOfflineReward _config = null;
private DateTime? _lastEntryTime = null;
public OfflineReward(ConfOfflineReward conf)
{
_config = conf;
NetTime.Instance.IsAvailableAsObservable()
.Subscribe(x =>
{
_lastEntryTime = x ? (DateTime?)NetTime.Instance.Time : null;
});
}
public void Load(DataGame data)
{
if (string.IsNullOrEmpty(data.timeRewards.offline.lastEntryTime))
ResetTime();
else
_lastEntryTime = DateTime.Parse(data.timeRewards.offline.lastEntryTime);
}
public bool TryGetReward(out ConfRewardItem reward)
{
reward = null;
if (IsAvailable == false)
return false;
var offlineTime = CalculateOfflineTime();
if (offlineTime.Hours <= 0)
return false;
return false;//(int) (Mathf.Clamp(offlineTime.Hours, 1, 24) * _config.bonusPerHour);
}
private TimeSpan CalculateOfflineTime()
{
if (IsAvailable == false)
return default;
return NetTime.Instance.Time - (DateTime)_lastEntryTime;
}
private void ResetTime()
{
_lastEntryTime = NetTime.Instance.Time;
}
public void Save(DataGame data)
{
data.timeRewards.offline.lastEntryTime = _lastEntryTime.ToString();
}
}