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:
@@ -14,6 +14,7 @@ using Content.Shared.Cargo.Prototypes;
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.GameTicking;
|
||||
using Content.Server.Paper;
|
||||
using Content.Shared.Access.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Players;
|
||||
@@ -243,7 +244,7 @@ namespace Content.Server.Cargo.Systems
|
||||
return new CargoOrderData(id, args.ProductId, args.Amount, args.Requester, args.Reason);
|
||||
}
|
||||
|
||||
private int GetOutstandingOrderCount(StationCargoOrderDatabaseComponent component)
|
||||
public int GetOutstandingOrderCount(StationCargoOrderDatabaseComponent component)
|
||||
{
|
||||
var amount = 0;
|
||||
|
||||
@@ -285,7 +286,31 @@ namespace Content.Server.Cargo.Systems
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryAddOrder(StationCargoOrderDatabaseComponent component, CargoOrderData data)
|
||||
public bool AddAndApproveOrder(StationCargoOrderDatabaseComponent component, string productId, int qty, string sender, string description, string dest)
|
||||
{
|
||||
if (!_prototypeManager.HasIndex<CargoProductPrototype>(productId))
|
||||
{
|
||||
_sawmill.Warning($"CargoSystem.Orders could not find CargoProductPrototype for '{productId}' in {description}.");
|
||||
// Pretend that it worked OK, since we don't want the caller to try again.
|
||||
return true;
|
||||
}
|
||||
|
||||
// Make an order
|
||||
var id = GenerateOrderId(component);
|
||||
var order = new CargoOrderData(id, productId, qty, sender, description);
|
||||
|
||||
// Approve it now
|
||||
order.SetApproverData(new IdCardComponent(){FullName = dest, JobTitle = sender});
|
||||
|
||||
// Log order addition
|
||||
_adminLogger.Add(LogType.Action, LogImpact.Low,
|
||||
$"AddAndApproveOrder {description} added order [orderId:{order.OrderId}, quantity:{order.OrderQuantity}, product:{order.ProductId}, requester:{order.Requester}, reason:{order.Reason}]");
|
||||
|
||||
// Add it to the list
|
||||
return TryAddOrder(component, order);
|
||||
}
|
||||
|
||||
private bool TryAddOrder(StationCargoOrderDatabaseComponent component, CargoOrderData data)
|
||||
{
|
||||
component.Orders.Add(data);
|
||||
UpdateOrders(component);
|
||||
@@ -294,7 +319,7 @@ namespace Content.Server.Cargo.Systems
|
||||
|
||||
private int GenerateOrderId(StationCargoOrderDatabaseComponent orderDB)
|
||||
{
|
||||
// We need an arbitrary unique ID to idenitfy orders, since they may
|
||||
// We need an arbitrary unique ID to identify orders, since they may
|
||||
// want to be cancelled later.
|
||||
return ++orderDB.NumOrdersCreated;
|
||||
}
|
||||
|
||||
@@ -282,7 +282,7 @@ public sealed partial class CargoSystem
|
||||
var numToShip = order.OrderQuantity - order.NumDispatched;
|
||||
if (numToShip > spaceRemaining)
|
||||
{
|
||||
// We won't be able to fit the whole oder on, so make one
|
||||
// We won't be able to fit the whole order on, so make one
|
||||
// which represents the space we do have left:
|
||||
var reducedOrder = new CargoOrderData(order.OrderId,
|
||||
order.ProductId, spaceRemaining, order.Requester, order.Reason);
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
using Content.Server.StationEvents.Events;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Server.StationEvents.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Used an event that gifts the station with certian cargo
|
||||
/// </summary>
|
||||
[RegisterComponent, Access(typeof(CargoGiftsRule))]
|
||||
public sealed class CargoGiftsRuleComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// The base announcement string (which then incorporates the strings below)
|
||||
/// </summary>
|
||||
[DataField("announce"), ViewVariables(VVAccess.ReadWrite)]
|
||||
public string Announce = "cargo-gifts-event-announcement";
|
||||
|
||||
/// <summary>
|
||||
/// What is being sent
|
||||
/// </summary>
|
||||
[DataField("description"), ViewVariables(VVAccess.ReadWrite)]
|
||||
public string Description = "cargo-gift-default-description";
|
||||
|
||||
/// <summary>
|
||||
/// Sender of the gifts
|
||||
/// </summary>
|
||||
[DataField("sender"), ViewVariables(VVAccess.ReadWrite)]
|
||||
public string Sender = "cargo-gift-default-sender";
|
||||
|
||||
/// <summary>
|
||||
/// Destination of the gifts (who they get sent to on the station)
|
||||
/// </summary>
|
||||
[DataField("dest"), ViewVariables(VVAccess.ReadWrite)]
|
||||
public string Dest = "cargo-gift-default-dest";
|
||||
|
||||
/// <summary>
|
||||
/// Cargo that you would like gifted to the station, with the quantity for each
|
||||
/// Use Ids from cargoProduct Prototypes
|
||||
/// </summary>
|
||||
[DataField("gifts"), ViewVariables(VVAccess.ReadWrite)]
|
||||
public Dictionary<string, int> Gifts = new Dictionary<string, int>();
|
||||
|
||||
/// <summary>
|
||||
/// How much space (minimum) you want to leave in the order database for supply to actually do their work
|
||||
/// </summary>
|
||||
[DataField("orderSpaceToLeave"), ViewVariables(VVAccess.ReadWrite)]
|
||||
public int OrderSpaceToLeave = 5;
|
||||
|
||||
/// <summary>
|
||||
/// Time until we consider next lot of gifts (if supply is overflowing with orders)
|
||||
/// </summary>
|
||||
[DataField("timeUntilNextGifts"), ViewVariables(VVAccess.ReadWrite)]
|
||||
public float TimeUntilNextGifts = 10.0f;
|
||||
}
|
||||
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