using Content.Shared.Access.Systems; using Content.Shared.DeltaV.Salvage.Components; using Content.Shared.Lathe; using Robust.Shared.Audio.Systems; namespace Content.Shared.DeltaV.Salvage.Systems; public sealed class MiningPointsSystem : EntitySystem { [Dependency] private readonly SharedAudioSystem _audio = default!; [Dependency] private readonly SharedIdCardSystem _idCard = default!; private EntityQuery _query; public override void Initialize() { base.Initialize(); _query = GetEntityQuery(); SubscribeLocalEvent(OnStartPrinting); Subs.BuiEvents(LatheUiKey.Key, subs => { subs.Event(OnClaimMiningPoints); }); } #region Event Handlers private void OnStartPrinting(Entity ent, ref LatheStartPrintingEvent args) { var points = args.Recipe.MiningPoints; if (points > 0) AddPoints(ent.Owner, points); } private void OnClaimMiningPoints(Entity ent, ref LatheClaimMiningPointsMessage args) { var user = args.Actor; if (TryFindIdCard(user) is {} dest) TransferAll(ent.Owner, dest); } #endregion #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); } /// /// 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 }