Delta-v/Content.Shared/_DV/Fishing/Systems/FishingPointsSystem.cs

109 lines
3.1 KiB
C#

using Content.Shared.Access.Systems;
using Content.Shared._DV.Fishing.Components;
using Robust.Shared.Audio.Systems;
namespace Content.Shared._DV.Fishing.Systems;
public sealed class FishingPointsSystem : EntitySystem
{
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedIdCardSystem _idCard = default!;
private EntityQuery<FishingPointsComponent> _query;
public override void Initialize()
{
base.Initialize();
_query = GetEntityQuery<FishingPointsComponent>();
}
#region Public API
/// <summary>
/// Tries to find the user's id card and gets its <see cref="FishingPointsComponent"/>.
/// </summary>
/// <remarks>
/// Component is nullable for easy usage with the API due to Entity&lt;T&gt; not being usable for Entity&lt;T?&gt; arguments.
/// </remarks>
public Entity<FishingPointsComponent?>? TryFindIdCard(EntityUid user)
{
if (!_idCard.TryFindIdCard(user, out var idCard))
return null;
if (!_query.TryComp(idCard, out var comp))
return null;
return (idCard, comp);
}
/// <summary>
/// Returns true if the user has at least some number of points on their ID card.
/// </summary>
public bool UserHasPoints(EntityUid user, uint points)
{
if (TryFindIdCard(user)?.Comp is not {} comp)
return false;
return comp.Points >= points;
}
/// <summary>
/// Removes points from a holder, returning true if it succeeded.
/// </summary>
public bool RemovePoints(Entity<FishingPointsComponent?> ent, uint amount)
{
if (!_query.Resolve(ent, ref ent.Comp) || amount > ent.Comp.Points)
return false;
ent.Comp.Points -= amount;
Dirty(ent);
return true;
}
/// <summary>
/// Add points to a holder.
/// </summary>
public bool AddPoints(Entity<FishingPointsComponent?> ent, uint amount)
{
if (!_query.Resolve(ent, ref ent.Comp))
return false;
ent.Comp.Points += amount;
Dirty(ent);
return true;
}
/// <summary>
/// Transfer a number of points from source to destination.
/// Returns true if the transfer succeeded.
/// </summary>
public bool Transfer(Entity<FishingPointsComponent?> src, Entity<FishingPointsComponent?> dest, uint amount)
{
// don't make a sound or anything
if (amount == 0)
return true;
if (!_query.Resolve(src, ref src.Comp) || !_query.Resolve(dest, ref dest.Comp))
return false;
if (!RemovePoints(src, amount))
return false;
AddPoints(dest, amount);
_audio.PlayPvs(src.Comp.TransferSound, src);
return true;
}
/// <summary>
/// Transfers all points from source to destination.
/// Returns true if the transfer succeeded.
/// </summary>
public bool TransferAll(Entity<FishingPointsComponent?> src, Entity<FishingPointsComponent?> dest)
{
return _query.Resolve(src, ref src.Comp) && Transfer(src, dest, src.Comp.Points);
}
#endregion
}