using System.Diagnostics.CodeAnalysis; using System.Linq; using Content.Server.GameTicking; using Content.Server.Station.Components; using Content.Shared.CCVar; using Content.Shared.GameTicking; using Content.Shared.Preferences; using Content.Shared.Roles; using JetBrains.Annotations; using Robust.Server.Player; using Robust.Shared.Configuration; using Robust.Shared.Player; using Robust.Shared.Random; namespace Content.Server.Station.Systems; /// /// Manages job slots for stations. /// [PublicAPI] public sealed partial class StationJobsSystem : EntitySystem { [Dependency] private readonly IConfigurationManager _configurationManager = default!; [Dependency] private readonly IRobustRandom _random = default!; [Dependency] private readonly GameTicker _gameTicker = default!; [Dependency] private readonly StationSystem _stationSystem = default!; [Dependency] private readonly IPlayerManager _playerManager = default!; /// public override void Initialize() { SubscribeLocalEvent(OnStationInitialized); SubscribeLocalEvent(OnStationRenamed); SubscribeLocalEvent(OnStationDeletion); SubscribeLocalEvent(OnPlayerJoinedLobby); _configurationManager.OnValueChanged(CCVars.GameDisallowLateJoins, _ => UpdateJobsAvailable(), true); } public override void Update(float _) { if (_availableJobsDirty) { _cachedAvailableJobs = GenerateJobsAvailableEvent(); RaiseNetworkEvent(_cachedAvailableJobs, Filter.Empty().AddPlayers(_playerManager.ServerSessions)); _availableJobsDirty = false; } } private void OnStationDeletion(EntityUid uid, StationJobsComponent component, ComponentShutdown args) { UpdateJobsAvailable(); // we no longer exist so the jobs list is changed. } private void OnStationInitialized(StationInitializedEvent msg) { if (!TryComp(msg.Station, out var stationJobs)) return; var mapJobList = stationJobs.SetupAvailableJobs; stationJobs.RoundStartTotalJobs = mapJobList.Values.Where(x => x[0] is not null && x[0] > 0).Sum(x => x[0]!.Value); stationJobs.MidRoundTotalJobs = mapJobList.Values.Where(x => x[1] is not null && x[1] > 0).Sum(x => x[1]!.Value); stationJobs.TotalJobs = stationJobs.MidRoundTotalJobs; stationJobs.JobList = mapJobList.ToDictionary(x => x.Key, x => { if (x.Value[1] <= -1) return null; return (uint?) x.Value[1]; }); stationJobs.RoundStartJobList = mapJobList.ToDictionary(x => x.Key, x => { if (x.Value[0] <= -1) return null; return (uint?) x.Value[0]; }); stationJobs.OverflowJobs = stationJobs.OverflowJobs.ToHashSet(); UpdateJobsAvailable(); } #region Public API /// /// Station to assign a job on. /// Job to assign. /// Resolve pattern, station jobs component of the station. public bool TryAssignJob(EntityUid station, JobPrototype job, StationJobsComponent? stationJobs = null) { return TryAssignJob(station, job.ID, stationJobs); } /// /// Attempts to assign the given job once. (essentially, it decrements the slot if possible). /// /// Station to assign a job on. /// Job prototype ID to assign. /// Resolve pattern, station jobs component of the station. /// Whether or not assignment was a success. /// Thrown when the given station is not a station. public bool TryAssignJob(EntityUid station, string jobPrototypeId, StationJobsComponent? stationJobs = null) { return TryAdjustJobSlot(station, jobPrototypeId, -1, false, false, stationJobs); } /// /// Station to adjust the job slot on. /// Job to adjust. /// Amount to adjust by. /// Whether or not it should create the slot if it doesn't exist. /// Whether or not to clamp to zero if you'd remove more jobs than are available. /// Resolve pattern, station jobs component of the station. public bool TryAdjustJobSlot(EntityUid station, JobPrototype job, int amount, bool createSlot = false, bool clamp = false, StationJobsComponent? stationJobs = null) { return TryAdjustJobSlot(station, job.ID, amount, createSlot, clamp, stationJobs); } /// /// Attempts to adjust the given job slot by the amount provided. /// /// Station to adjust the job slot on. /// Job prototype ID to adjust. /// Amount to adjust by. /// Whether or not it should create the slot if it doesn't exist. /// Whether or not to clamp to zero if you'd remove more jobs than are available. /// Resolve pattern, station jobs component of the station. /// Whether or not slot adjustment was a success. /// Thrown when the given station is not a station. public bool TryAdjustJobSlot(EntityUid station, string jobPrototypeId, int amount, bool createSlot = false, bool clamp = false, StationJobsComponent? stationJobs = null) { if (!Resolve(station, ref stationJobs)) throw new ArgumentException("Tried to use a non-station entity as a station!", nameof(station)); var jobList = stationJobs.JobList; // This should: // - Return true when zero slots are added/removed. // - Return true when you add. // - Return true when you remove and do not exceed the number of slot available. // - Return false when you remove from a job that doesn't exist. // - Return false when you remove and exceed the number of slots available. // And additionally, if adding would add a job not previously on the manifest when createSlot is false, return false and do nothing. switch (jobList.ContainsKey(jobPrototypeId)) { case false when amount < 0: return false; case false: if (!createSlot) return false; stationJobs.TotalJobs += amount; jobList[jobPrototypeId] = (uint?)amount; UpdateJobsAvailable(); return true; case true: // Job is unlimited so just say we adjusted it and do nothing. if (jobList[jobPrototypeId] == null) return true; // Would remove more jobs than we have available. if (amount < 0 && (jobList[jobPrototypeId] + amount < 0 && !clamp)) return false; stationJobs.TotalJobs += amount; //C# type handling moment if (amount > 0) jobList[jobPrototypeId] += (uint)amount; else { if ((int)jobList[jobPrototypeId]!.Value - Math.Abs(amount) <= 0) jobList[jobPrototypeId] = 0; else jobList[jobPrototypeId] -= (uint) Math.Abs(amount); } UpdateJobsAvailable(); return true; } } /// /// Station to adjust the job slot on. /// Job prototype to adjust. /// Amount to set to. /// Whether or not it should create the slot if it doesn't exist. /// Resolve pattern, station jobs component of the station. /// public bool TrySetJobSlot(EntityUid station, JobPrototype jobPrototype, int amount, bool createSlot = false, StationJobsComponent? stationJobs = null) { return TrySetJobSlot(station, jobPrototype.ID, amount, createSlot, stationJobs); } /// /// Attempts to set the given job slot to the amount provided. /// /// Station to adjust the job slot on. /// Job prototype ID to adjust. /// Amount to set to. /// Whether or not it should create the slot if it doesn't exist. /// Resolve pattern, station jobs component of the station. /// Whether or not setting the value succeeded. /// Thrown when the given station is not a station. public bool TrySetJobSlot(EntityUid station, string jobPrototypeId, int amount, bool createSlot = false, StationJobsComponent? stationJobs = null) { if (!Resolve(station, ref stationJobs)) throw new ArgumentException("Tried to use a non-station entity as a station!", nameof(station)); if (amount < 0) throw new ArgumentException("Tried to set a job to have a negative number of slots!", nameof(amount)); var jobList = stationJobs.JobList; switch (jobList.ContainsKey(jobPrototypeId)) { case false: if (!createSlot) return false; stationJobs.TotalJobs += amount; jobList[jobPrototypeId] = (uint?)amount; UpdateJobsAvailable(); return true; case true: stationJobs.TotalJobs += amount - (int) (jobList[jobPrototypeId] ?? 0); jobList[jobPrototypeId] = (uint)amount; UpdateJobsAvailable(); return true; } } /// /// Station to make a job unlimited on. /// Job to make unlimited. /// Resolve pattern, station jobs component of the station. public void MakeJobUnlimited(EntityUid station, JobPrototype job, StationJobsComponent? stationJobs = null) { MakeJobUnlimited(station, job.ID, stationJobs); } /// /// Makes the given job have unlimited slots. /// /// Station to make a job unlimited on. /// Job prototype ID to make unlimited. /// Resolve pattern, station jobs component of the station. /// Thrown when the given station is not a station. public void MakeJobUnlimited(EntityUid station, string jobPrototypeId, StationJobsComponent? stationJobs = null) { if (!Resolve(station, ref stationJobs)) throw new ArgumentException("Tried to use a non-station entity as a station!", nameof(station)); // Subtract out the job we're fixing to make have unlimited slots. if (stationJobs.JobList.ContainsKey(jobPrototypeId) && stationJobs.JobList[jobPrototypeId] != null) stationJobs.TotalJobs -= (int)stationJobs.JobList[jobPrototypeId]!.Value; stationJobs.JobList[jobPrototypeId] = null; UpdateJobsAvailable(); } /// /// Station to check. /// Job to check. /// Resolve pattern, station jobs component of the station. public bool IsJobUnlimited(EntityUid station, JobPrototype job, StationJobsComponent? stationJobs = null) { return IsJobUnlimited(station, job.ID, stationJobs); } /// /// Checks if the given job is unlimited. /// /// Station to check. /// Job prototype ID to check. /// Resolve pattern, station jobs component of the station. /// Returns if the given slot is unlimited. /// Thrown when the given station is not a station. public bool IsJobUnlimited(EntityUid station, string jobPrototypeId, StationJobsComponent? stationJobs = null) { if (!Resolve(station, ref stationJobs)) throw new ArgumentException("Tried to use a non-station entity as a station!", nameof(station)); var res = stationJobs.JobList.TryGetValue(jobPrototypeId, out var job) && job == null; return res; } /// /// Station to get slot info from. /// Job to get slot info for. /// The number of slots remaining. Null if infinite. /// Resolve pattern, station jobs component of the station. public bool TryGetJobSlot(EntityUid station, JobPrototype job, out uint? slots, StationJobsComponent? stationJobs = null) { return TryGetJobSlot(station, job.ID, out slots, stationJobs); } /// /// Returns information about the given job slot. /// /// Station to get slot info from. /// Job prototype ID to get slot info for. /// The number of slots remaining. Null if infinite. /// Resolve pattern, station jobs component of the station. /// Whether or not the slot exists. /// Thrown when the given station is not a station. /// slots will be null if the slot doesn't exist, as well, so make sure to check the return value. public bool TryGetJobSlot(EntityUid station, string jobPrototypeId, out uint? slots, StationJobsComponent? stationJobs = null) { if (!Resolve(station, ref stationJobs)) throw new ArgumentException("Tried to use a non-station entity as a station!", nameof(station)); if (stationJobs.JobList.TryGetValue(jobPrototypeId, out var job)) { slots = job; return true; } else // Else if slot isn't present return null. { slots = null; return false; } } /// /// Returns all jobs available on the station. /// /// Station to get jobs for /// Resolve pattern, station jobs component of the station. /// Set containing all jobs available. /// Thrown when the given station is not a station. public IReadOnlySet GetAvailableJobs(EntityUid station, StationJobsComponent? stationJobs = null) { if (!Resolve(station, ref stationJobs)) throw new ArgumentException("Tried to use a non-station entity as a station!", nameof(station)); return stationJobs.JobList.Where(x => x.Value != 0).Select(x => x.Key).ToHashSet(); } /// /// Returns all overflow jobs available on the station. /// /// Station to get jobs for /// Resolve pattern, station jobs component of the station. /// Set containing all overflow jobs available. /// Thrown when the given station is not a station. public IReadOnlySet GetOverflowJobs(EntityUid station, StationJobsComponent? stationJobs = null) { if (!Resolve(station, ref stationJobs)) throw new ArgumentException("Tried to use a non-station entity as a station!", nameof(station)); return stationJobs.OverflowJobs.ToHashSet(); } /// /// Returns a readonly dictionary of all jobs and their slot info. /// /// Station to get jobs for /// Resolve pattern, station jobs component of the station. /// List of all jobs on the station. /// Thrown when the given station is not a station. public IReadOnlyDictionary GetJobs(EntityUid station, StationJobsComponent? stationJobs = null) { if (!Resolve(station, ref stationJobs)) throw new ArgumentException("Tried to use a non-station entity as a station!", nameof(station)); return stationJobs.JobList; } /// /// Returns a readonly dictionary of all round-start jobs and their slot info. /// /// Station to get jobs for /// Resolve pattern, station jobs component of the station. /// List of all round-start jobs. /// Thrown when the given station is not a station. public IReadOnlyDictionary GetRoundStartJobs(EntityUid station, StationJobsComponent? stationJobs = null) { if (!Resolve(station, ref stationJobs)) throw new ArgumentException("Tried to use a non-station entity as a station!", nameof(station)); return stationJobs.RoundStartJobList; } /// /// Looks at the given priority list, and picks the best available job (optionally with the given exclusions) /// /// Station to pick from. /// The priority list to use for selecting a job. /// Whether or not to pick from the overflow list. /// A set of disallowed jobs, if any. /// The selected job, if any. public string? PickBestAvailableJobWithPriority(EntityUid station, IReadOnlyDictionary jobPriorities, bool pickOverflows, IReadOnlySet? disallowedJobs = null) { if (station == EntityUid.Invalid) return null; var available = GetAvailableJobs(station); bool TryPick(JobPriority priority, [NotNullWhen(true)] out string? jobId) { var filtered = jobPriorities .Where(p => p.Value == priority && disallowedJobs != null && !disallowedJobs.Contains(p.Key) && available.Contains(p.Key)) .Select(p => p.Key) .ToList(); if (filtered.Count != 0) { jobId = _random.Pick(filtered); return true; } jobId = default; return false; } if (TryPick(JobPriority.High, out var picked)) { return picked; } if (TryPick(JobPriority.Medium, out picked)) { return picked; } if (TryPick(JobPriority.Low, out picked)) { return picked; } if (!pickOverflows) return null; var overflows = GetOverflowJobs(station); return overflows.Count != 0 ? _random.Pick(overflows) : null; } #endregion Public API #region Latejoin job management private bool _availableJobsDirty; private TickerJobsAvailableEvent _cachedAvailableJobs = new (new Dictionary(), new Dictionary>()); /// /// Assembles an event from the current available-to-play jobs. /// This is moderately expensive to construct. /// /// The event. private TickerJobsAvailableEvent GenerateJobsAvailableEvent() { // If late join is disallowed, return no available jobs. if (_gameTicker.DisallowLateJoin) return new TickerJobsAvailableEvent(new Dictionary(), new Dictionary>()); var jobs = new Dictionary>(); var stationNames = new Dictionary(); var query = EntityQueryEnumerator(); while (query.MoveNext(out var station, out var comp)) { var netStation = GetNetEntity(station); var list = comp.JobList.ToDictionary(x => x.Key, x => x.Value); jobs.Add(netStation, list); stationNames.Add(netStation, Name(station)); } return new TickerJobsAvailableEvent(stationNames, jobs); } /// /// Updates the cached available jobs. Moderately expensive. /// private void UpdateJobsAvailable() { _availableJobsDirty = true; } private void OnPlayerJoinedLobby(PlayerJoinedLobbyEvent ev) { RaiseNetworkEvent(_cachedAvailableJobs, ev.PlayerSession.ConnectedClient); } private void OnStationRenamed(EntityUid uid, StationJobsComponent component, StationRenamedEvent args) { UpdateJobsAvailable(); } #endregion }