NanoTrasen might send Gifts to the station via Cargo (#16556)
* Rebase Cargo Gifts * Remove Chaos values from gifts (for now) * Translate CargoGifts, rename fields * Fix gift errors, detect missing products * Fix order Id, some crate IDs * Fix get Station. Gifts for Sec, Med, Fire, spacing * Minimum players for various gifts # Conflicts: # Resources/Prototypes/GameRules/cargo_gifts.yml
This commit is contained in:
84
Content.Server/StationEvents/Events/CargoGiftsRule.cs
Normal file
84
Content.Server/StationEvents/Events/CargoGiftsRule.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Anomaly;
|
||||
using Content.Server.Cargo.Components;
|
||||
using Content.Server.Cargo.Systems;
|
||||
using Content.Server.GameTicking;
|
||||
using Content.Server.GameTicking.Rules.Components;
|
||||
using Content.Server.Station.Components;
|
||||
using Content.Server.Station.Systems;
|
||||
using Content.Server.StationEvents.Components;
|
||||
using Content.Shared.Access.Components;
|
||||
using Content.Shared.Administration.Logs;
|
||||
using Content.Shared.Cargo;
|
||||
using Content.Shared.Cargo.Prototypes;
|
||||
using Content.Shared.Database;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server.StationEvents.Events;
|
||||
|
||||
public sealed class CargoGiftsRule : StationEventSystem<CargoGiftsRuleComponent>
|
||||
{
|
||||
[Dependency] private readonly CargoSystem _cargoSystem = default!;
|
||||
[Dependency] private readonly StationSystem _stationSystem = default!;
|
||||
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly GameTicker _ticker = default!;
|
||||
|
||||
protected override void Added(EntityUid uid, CargoGiftsRuleComponent component, GameRuleComponent gameRule, GameRuleAddedEvent args)
|
||||
{
|
||||
base.Added(uid, component, gameRule, args);
|
||||
|
||||
var str = Loc.GetString(component.Announce,
|
||||
("sender", Loc.GetString(component.Sender)), ("description", Loc.GetString(component.Description)), ("dest", Loc.GetString(component.Dest)));
|
||||
ChatSystem.DispatchGlobalAnnouncement(str, colorOverride: Color.FromHex("#18abf5"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called on an active gamerule entity in the Update function
|
||||
/// </summary>
|
||||
protected override void ActiveTick(EntityUid uid, CargoGiftsRuleComponent component, GameRuleComponent gameRule, float frameTime)
|
||||
{
|
||||
if (component.Gifts.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (component.TimeUntilNextGifts > 0)
|
||||
{
|
||||
component.TimeUntilNextGifts -= frameTime;
|
||||
return;
|
||||
}
|
||||
component.TimeUntilNextGifts = 30f;
|
||||
|
||||
if (!TryGetRandomStation(out var station, HasComp<StationCargoOrderDatabaseComponent>))
|
||||
return;
|
||||
|
||||
if (!TryComp<StationCargoOrderDatabaseComponent>(station, out var cargoDb))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Add some presents
|
||||
int outstanding = _cargoSystem.GetOutstandingOrderCount(cargoDb);
|
||||
while (outstanding < cargoDb.Capacity - component.OrderSpaceToLeave && component.Gifts.Count > 0)
|
||||
{
|
||||
// I wish there was a nice way to pop this
|
||||
var (productId, qty) = component.Gifts.First();
|
||||
component.Gifts.Remove(productId);
|
||||
|
||||
if (!_cargoSystem.AddAndApproveOrder(cargoDb, productId, qty, Loc.GetString(component.Sender), Loc.GetString(component.Description), Loc.GetString(component.Dest)))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (component.Gifts.Count == 0)
|
||||
{
|
||||
// We're done here!
|
||||
_ticker.EndGameRule(uid, gameRule);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user