using ODMO.Commons.Models.Asset;
namespace ODMO.Commons.Models
{
public partial class Buff
{
///
/// Flag for expired buffs.
///
public bool Expired => Duration == -1 || (Duration > 0 && DateTime.Now.AddSeconds(3) >= EndDate);
public bool DebuffExpired => DateTime.Now >= EndDate;
///
/// Remaining time in seconds.
///
public int RemainingSeconds => (EndDate - DateTime.Now).TotalSeconds > 0 ? (int)(EndDate - DateTime.Now).TotalSeconds : 0;
///
/// Increase buff duration.
///
/// Duration (in seconds) to increase
public void IncreaseDuration(int duration)
{
Duration += duration;
EndDate = DateTime.Now.AddSeconds(Duration);
}
public void IncreaseEndDate(int duration)
{
Duration = duration;
EndDate = DateTime.Now.AddSeconds(duration);
}
///
/// Creates a new buff object.
///
/// Buff id (client reference)
/// Skill id (client reference)
/// Duration (in seconds)
public static Buff Create(int buffId, int skillId, int duration = 0)
{
return new Buff()
{
BuffId = buffId,
SkillId = skillId,
Duration = duration,
EndDate = DateTime.Now.AddSeconds(duration)
};
}
///
/// Updates the buff info.
///
/// The info asset
public void SetBuffInfo(BuffInfoAssetModel? buffInfo) => BuffInfo ??= buffInfo;
///
/// Updates the buff id.
///
/// The new buff id
public void SetBuffId(int buffId) => BuffId = buffId;
public void SetTypeN(int typeN) => TypeN = typeN;
public void SetCooldown(int cooldown)
{
Cooldown = cooldown;
CoolEndDate = DateTime.Now.AddSeconds(cooldown);
}
///
/// Updates the buff skill id.
///
/// The new skill id
public void SetSkillId(int skillId) => SkillId = skillId;
///
/// Updates the buff duration.
///
/// The new buff duration
public void SetDuration(int duration, bool fixedValue = false)
{
if (fixedValue)
{
Duration = duration;
EndDate = DateTime.UtcNow.AddSeconds(Duration);
}
else
{
Duration += duration;
EndDate = EndDate.AddSeconds(duration);
}
}
public void SetEndDate(DateTime endDate) => EndDate = endDate;
///
/// Serializes the buff object.
///
///
public byte[] ToArray()
{
using MemoryStream m = new();
m.Write(BitConverter.GetBytes(BuffId), 0, 4);
m.Write(BitConverter.GetBytes(Duration), 0, 4);
m.Write(BitConverter.GetBytes(SkillId), 0, 4);
return m.ToArray();
}
}
}