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 _query; public override void Initialize() { base.Initialize(); _query = GetEntityQuery(); } #region Public API /// /// Tries to find the user's id card and gets its . /// /// /// Component is nullable for easy usage with the API due to Entity<T> not being usable for Entity<T?> arguments. /// public Entity? TryFindIdCard(EntityUid user) { if (!_idCard.TryFindIdCard(user, out var idCard)) return null; if (!_query.TryComp(idCard, out var comp)) return null; return (idCard, comp); } /// /// Returns true if the user has at least some number of points on their ID card. /// public bool UserHasPoints(EntityUid user, uint points) { if (TryFindIdCard(user)?.Comp is not {} comp) return false; return comp.Points >= points; } /// /// Removes points from a holder, returning true if it succeeded. /// public bool RemovePoints(Entity ent, uint amount) { if (!_query.Resolve(ent, ref ent.Comp) || amount > ent.Comp.Points) return false; ent.Comp.Points -= amount; Dirty(ent); return true; } /// /// Add points to a holder. /// public bool AddPoints(Entity ent, uint amount) { if (!_query.Resolve(ent, ref ent.Comp)) return false; ent.Comp.Points += amount; Dirty(ent); return true; } /// /// Transfer a number of points from source to destination. /// Returns true if the transfer succeeded. /// public bool Transfer(Entity src, Entity 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; } /// /// Transfers all points from source to destination. /// Returns true if the transfer succeeded. /// public bool TransferAll(Entity src, Entity dest) { return _query.Resolve(src, ref src.Comp) && Transfer(src, dest, src.Comp.Points); } #endregion }