if (args.Button.Parent?.Parent is not CargoOrderRow row || row.Order == null)
return;
- SendMessage(new CargoConsoleRemoveOrderMessage(row.Order.OrderIndex));
+ SendMessage(new CargoConsoleRemoveOrderMessage(row.Order.OrderId));
}
private void ApproveOrder(ButtonEventArgs args)
if (OrderCount >= OrderCapacity)
return;
- SendMessage(new CargoConsoleApproveOrderMessage(row.Order.OrderIndex));
+ SendMessage(new CargoConsoleApproveOrderMessage(row.Order.OrderId));
// Most of the UI isn't predicted anyway so.
// _menu?.UpdateCargoCapacity(OrderCount + row.Order.Amount, OrderCapacity);
}
Text = Loc.GetString(
"cargo-console-menu-populate-orders-cargo-order-row-product-name-text",
("productName", productName),
- ("orderAmount", order.Amount),
+ ("orderAmount", order.OrderQuantity),
("orderRequester", order.Requester))
},
Description = {Text = Loc.GetString("cargo-console-menu-order-reason-description",
Text = Loc.GetString(
"cargo-console-menu-populate-orders-cargo-order-row-product-name-text",
("productName", productName),
- ("orderAmount", order.Amount),
+ ("orderAmount", order.OrderQuantity - order.NumDispatched),
("orderRequester", order.Requester))
},
Description = {Text = Loc.GetString("cargo-console-menu-order-reason-description",
public int Capacity = 20;
[ViewVariables(VVAccess.ReadWrite), DataField("orders")]
- public Dictionary<int, CargoOrderData> Orders = new();
+ public List<CargoOrderData> Orders = new();
/// <summary>
- /// Tracks the next order index available.
+ /// Used to determine unique order IDs
/// </summary>
- public int Index;
+ public int NumOrdersCreated;
[DataField("cargoShuttleProto", customTypeSerializer:typeof(PrototypeIdSerializer<CargoShuttlePrototype>))]
public string? CargoShuttleProto = "CargoShuttle";
-using System.Linq;
+using System.Diagnostics.CodeAnalysis;
using Content.Server.Access.Systems;
using Content.Server.Cargo.Components;
+using Content.Server.Labels.Components;
using Content.Server.MachineLinking.System;
using Content.Server.Popups;
using Content.Server.Station.Systems;
using Content.Shared.Cargo.Prototypes;
using Content.Shared.Database;
using Content.Shared.GameTicking;
+using Content.Server.Paper;
using Robust.Server.GameObjects;
-using Robust.Shared.Audio;
-using Robust.Shared.Player;
+using Robust.Shared.Map;
using Robust.Shared.Players;
namespace Content.Server.Cargo.Systems
return;
}
- // No order to approve?
- if (!orderDatabase.Orders.TryGetValue(args.OrderIndex, out var order) ||
- order.Approved) return;
+ // Find our order again. It might have been dispatched or approved already
+ var order = orderDatabase.Orders.Find(order => (args.OrderId == order.OrderId) && !order.Approved);
+ if(order == null)
+ {
+ return;
+ }
// Invalid order
if (!_protoMan.TryIndex<CargoProductPrototype>(order.ProductId, out var product))
return;
}
- var amount = GetOrderCount(orderDatabase);
+ var amount = GetOutstandingOrderCount(orderDatabase);
var capacity = orderDatabase.Capacity;
// Too many orders, avoid them getting spammed in the UI.
}
// Cap orders so someone can't spam thousands.
- var orderAmount = Math.Min(capacity - amount, order.Amount);
+ var cappedAmount = Math.Min(capacity - amount, order.OrderQuantity);
- if (orderAmount != order.Amount)
+ if (cappedAmount != order.OrderQuantity)
{
- order.Amount = orderAmount;
+ order.OrderQuantity = cappedAmount;
ConsolePopup(args.Session, Loc.GetString("cargo-console-snip-snip"));
PlayDenySound(uid, component);
}
- var cost = product.PointCost * order.Amount;
+ var cost = product.PointCost * order.OrderQuantity;
// Not enough balance
if (cost > bankAccount.Balance)
// Log order approval
_adminLogger.Add(LogType.Action, LogImpact.Low,
- $"{ToPrettyString(player):user} approved order [orderIdx:{order.OrderIndex}, amount:{order.Amount}, product:{order.ProductId}, requester:{order.Requester}, reason:{order.Reason}] with balance at {bankAccount.Balance}");
+ $"{ToPrettyString(player):user} approved order [orderId:{order.OrderId}, quantity:{order.OrderQuantity}, product:{order.ProductId}, requester:{order.Requester}, reason:{order.Reason}] with balance at {bankAccount.Balance}");
DeductFunds(bankAccount, cost);
UpdateOrders(orderDatabase);
{
var orderDatabase = GetOrderDatabase(component);
if (orderDatabase == null) return;
- RemoveOrder(orderDatabase, args.OrderIndex);
+ RemoveOrder(orderDatabase, args.OrderId);
}
private void OnAddOrderMessage(EntityUid uid, CargoOrderConsoleComponent component, CargoConsoleAddOrderMessage args)
var orderDatabase = GetOrderDatabase(component);
if (orderDatabase == null) return;
- var data = GetOrderData(args, GetNextIndex(orderDatabase));
+ var data = GetOrderData(args, GenerateOrderId(orderDatabase));
if (!TryAddOrder(orderDatabase, data))
{
// Log order addition
_adminLogger.Add(LogType.Action, LogImpact.Low,
- $"{ToPrettyString(player):user} added order [orderIdx:{data.OrderIndex}, amount:{data.Amount}, product:{data.ProductId}, requester:{data.Requester}, reason:{data.Reason}]");
+ $"{ToPrettyString(player):user} added order [orderId:{data.OrderId}, quantity:{data.OrderQuantity}, product:{data.ProductId}, requester:{data.Requester}, reason:{data.Reason}]");
}
var state = new CargoConsoleInterfaceState(
MetaData(station.Value).EntityName,
- GetOrderCount(orderDatabase),
+ GetOutstandingOrderCount(orderDatabase),
orderDatabase.Capacity,
bankAccount.Balance,
- orderDatabase.Orders.Values.ToList());
+ orderDatabase.Orders);
_uiSystem.GetUiOrNull(component.Owner, CargoConsoleUiKey.Orders)?.SetState(state);
}
_audio.PlayPvs(_audio.GetSound(component.ErrorSound), uid);
}
- private CargoOrderData GetOrderData(CargoConsoleAddOrderMessage args, int index)
+ private CargoOrderData GetOrderData(CargoConsoleAddOrderMessage args, int id)
{
- return new CargoOrderData(index, args.ProductId, args.Amount, args.Requester, args.Reason);
+ return new CargoOrderData(id, args.ProductId, args.Amount, args.Requester, args.Reason);
}
- private int GetOrderCount(StationCargoOrderDatabaseComponent component)
+ private int GetOutstandingOrderCount(StationCargoOrderDatabaseComponent component)
{
var amount = 0;
- foreach (var (_, order) in component.Orders)
+ foreach (var order in component.Orders)
{
if (!order.Approved) continue;
- amount += order.Amount;
+ amount += order.OrderQuantity - order.NumDispatched;
}
return amount;
public bool TryAddOrder(StationCargoOrderDatabaseComponent component, CargoOrderData data)
{
- component.Orders.Add(data.OrderIndex, data);
+ component.Orders.Add(data);
UpdateOrders(component);
return true;
}
- private int GetNextIndex(StationCargoOrderDatabaseComponent component)
+ private int GenerateOrderId(StationCargoOrderDatabaseComponent orderDB)
{
- var index = component.Index;
- component.Index++;
- return index;
+ // We need an arbitrary unique ID to idenitfy orders, since they may
+ // want to be cancelled later.
+ return ++orderDB.NumOrdersCreated;
}
- public void RemoveOrder(StationCargoOrderDatabaseComponent component, int index)
+ public void RemoveOrder(StationCargoOrderDatabaseComponent orderDB, int index)
{
- if (!component.Orders.Remove(index)) return;
- UpdateOrders(component);
+ var sequenceIdx = orderDB.Orders.FindIndex(order => order.OrderId == index);
+ if (sequenceIdx != -1)
+ {
+ orderDB.Orders.RemoveAt(sequenceIdx);
+ }
+ UpdateOrders(orderDB);
}
public void ClearOrders(StationCargoOrderDatabaseComponent component)
Dirty(component);
}
+ private bool PopFrontOrder(StationCargoOrderDatabaseComponent orderDB, [NotNullWhen(true)] out CargoOrderData? orderOut)
+ {
+ var orderIdx = orderDB.Orders.FindIndex(order => order.Approved);
+ if (orderIdx == -1)
+ {
+ orderOut = null;
+ return false;
+ }
+
+ orderOut = orderDB.Orders[orderIdx];
+ orderOut.NumDispatched++;
+
+ if(orderOut.NumDispatched >= orderOut.OrderQuantity)
+ {
+ // Order is complete. Remove from the queue.
+ orderDB.Orders.RemoveAt(orderIdx);
+ }
+ return true;
+ }
+
+ private bool FulfillOrder(StationCargoOrderDatabaseComponent orderDB, EntityCoordinates whereToPutIt,
+ string? paperPrototypeToPrint)
+ {
+ if (PopFrontOrder(orderDB, out var order))
+ {
+ // Create the item itself
+ var item = Spawn(_protoMan.Index<CargoProductPrototype>(order.ProductId).Product, whereToPutIt);
+
+ // Create a sheet of paper to write the order details on
+ var printed = EntityManager.SpawnEntity(paperPrototypeToPrint, whereToPutIt);
+ if (TryComp<PaperComponent>(printed, out var paper))
+ {
+ // fill in the order data
+ var val = Loc.GetString("cargo-console-paper-print-name", ("orderNumber", order.OrderId));
+ MetaData(printed).EntityName = val;
+
+ _paperSystem.SetContent(printed, Loc.GetString(
+ "cargo-console-paper-print-text",
+ ("orderNumber", order.OrderId),
+ ("itemName", MetaData(item).EntityName),
+ ("requester", order.Requester),
+ ("reason", order.Reason),
+ ("approver", order.Approver ?? string.Empty)),
+ paper);
+
+ // attempt to attach the label to the item
+ if (TryComp<PaperLabelComponent>(item, out var label))
+ {
+ _slots.TryInsert(item, label.LabelSlot, printed, null);
+ }
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
private void DeductFunds(StationBankAccountComponent component, int amount)
{
component.Balance = Math.Max(0, component.Balance - amount);
if (component == null || shuttle == null || component.Orders.Count == 0)
return orders;
- var space = GetCargoSpace(shuttle);
-
- if (space == 0) return orders;
-
- var indices = component.Orders.Keys.ToList();
- indices.Sort();
- var amount = 0;
-
- foreach (var index in indices)
+ var spaceRemaining = GetCargoSpace(shuttle);
+ for( var i = 0; i < component.Orders.Count && spaceRemaining > 0; i++)
{
- var order = component.Orders[index];
- if (!order.Approved) continue;
-
- var cappedAmount = Math.Min(space - amount, order.Amount);
- amount += cappedAmount;
- DebugTools.Assert(amount <= space);
-
- if (cappedAmount < order.Amount)
+ var order = component.Orders[i];
+ if(order.Approved)
{
- var reducedOrder = new CargoOrderData(order.OrderIndex, order.ProductId, cappedAmount, order.Requester, order.Reason);
-
- orders.Add(reducedOrder);
- break;
+ var numToShip = order.OrderQuantity - order.NumDispatched;
+ if (numToShip > spaceRemaining)
+ {
+ // We won't be able to fit the whole oder 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);
+ orders.Add(reducedOrder);
+ }
+ else
+ {
+ orders.Add(order);
+ }
+ spaceRemaining -= numToShip;
}
-
- orders.Add(order);
-
- if (amount == space) break;
}
return orders;
_sawmill.Info($"Retrieved cargo shuttle {ToPrettyString(shuttle.Owner)} from {ToPrettyString(orderDatabase.Owner)}");
}
- private void AddCargoContents(CargoShuttleComponent component, StationCargoOrderDatabaseComponent orderDatabase)
+ private void AddCargoContents(CargoShuttleComponent shuttle, StationCargoOrderDatabaseComponent orderDatabase)
{
var xformQuery = GetEntityQuery<TransformComponent>();
- var orders = GetProjectedOrders(orderDatabase, component);
-
- var pads = GetCargoPallets(component);
- DebugTools.Assert(orders.Sum(o => o.Amount) <= pads.Count);
- for (var i = 0; i < pads.Count; i++)
+ var pads = GetCargoPallets(shuttle);
+ while (pads.Count > 0)
{
- if (orders.Count == 0)
- break;
-
- var order = orders[0];
- var coordinates = new EntityCoordinates(component.Owner, xformQuery.GetComponent(_random.PickAndTake(pads).Owner).LocalPosition);
- var item = Spawn(_protoMan.Index<CargoProductPrototype>(order.ProductId).Product, coordinates);
- SpawnAndAttachOrderManifest(item, order, coordinates, component);
- order.Amount--;
-
- if (order.Amount == 0)
- {
- // Yes this is functioning as a stack, I was too lazy to re-jig the shuttle state event.
- orders.RemoveSwap(0);
- orderDatabase.Orders.Remove(order.OrderIndex);
- }
- else
+ var coordinates = new EntityCoordinates(shuttle.Owner, xformQuery.GetComponent(_random.PickAndTake(pads).Owner).LocalPosition);
+ if(!FulfillOrder(orderDatabase, coordinates, shuttle.PrinterOutput))
{
- orderDatabase.Orders[order.OrderIndex] = order;
+ break;
}
}
}
- /// <summary>
- /// In this method we are printing and attaching order manifests to the orders.
- /// </summary>
- private void SpawnAndAttachOrderManifest(EntityUid item, CargoOrderData order, EntityCoordinates coordinates, CargoShuttleComponent component)
- {
- if (!_protoMan.TryIndex(order.ProductId, out CargoProductPrototype? prototype))
- return;
-
- // spawn a piece of paper.
- var printed = EntityManager.SpawnEntity(component.PrinterOutput, coordinates);
-
- if (!TryComp<PaperComponent>(printed, out var paper))
- return;
-
- // fill in the order data
- var val = Loc.GetString("cargo-console-paper-print-name", ("orderNumber", order.PrintableOrderNumber));
-
- MetaData(printed).EntityName = val;
-
- _paperSystem.SetContent(printed, Loc.GetString(
- "cargo-console-paper-print-text",
- ("orderNumber", order.PrintableOrderNumber),
- ("itemName", prototype.Name),
- ("requester", order.Requester),
- ("reason", order.Reason),
- ("approver", order.Approver ?? string.Empty)),
- paper);
-
- // attempt to attach the label
- if (TryComp<PaperLabelComponent>(item, out var label))
- {
- _slots.TryInsert(item, label.LabelSlot, printed, null);
- }
- }
-
private void RecallCargoShuttle(EntityUid uid, CargoShuttleConsoleComponent component, CargoRecallShuttleMessage args)
{
var player = args.Session.AttachedEntity;
continue;
}
- var orderIndices = new ValueList<int>();
-
- foreach (var (oIndex, oOrder) in orderDatabase.Orders)
+ var xform = Transform(comp.Owner);
+ if(FulfillOrder(orderDatabase, xform.Coordinates,comp.PrinterOutput))
{
- if (!oOrder.Approved) continue;
- orderIndices.Add(oIndex);
- }
+ _audio.PlayPvs(_audio.GetSound(comp.TeleportSound), comp.Owner, AudioParams.Default.WithVolume(-8f));
+ UpdateOrders(orderDatabase);
- if (orderIndices.Count == 0)
- {
- comp.Accumulator += comp.Delay;
- continue;
+ comp.CurrentState = CargoTelepadState.Teleporting;
+ _appearance.SetData(comp.Owner, CargoTelepadVisuals.State, CargoTelepadState.Teleporting, appearance);
}
- orderIndices.Sort();
- var index = orderIndices[0];
- var order = orderDatabase.Orders[index];
- order.Amount--;
-
- if (order.Amount <= 0)
- orderDatabase.Orders.Remove(index);
-
- _audio.PlayPvs(_audio.GetSound(comp.TeleportSound), comp.Owner, AudioParams.Default.WithVolume(-8f));
- SpawnProduct(comp, order);
- UpdateOrders(orderDatabase);
-
- comp.CurrentState = CargoTelepadState.Teleporting;
- _appearance.SetData(comp.Owner, CargoTelepadVisuals.State, CargoTelepadState.Teleporting, appearance);
comp.Accumulator += comp.Delay;
}
}
{
SetEnabled(component);
}
-
- /// <summary>
- /// Spawn the product and a piece of paper. Attempt to attach the paper to the product.
- /// </summary>
- private void SpawnProduct(CargoTelepadComponent component, CargoOrderData data)
- {
- // spawn the order
- if (!_protoMan.TryIndex(data.ProductId, out CargoProductPrototype? prototype))
- return;
-
- var xform = Transform(component.Owner);
-
- var product = EntityManager.SpawnEntity(prototype.Product, xform.Coordinates);
-
- Transform(product).Anchored = false;
-
- // spawn a piece of paper.
- var printed = EntityManager.SpawnEntity(component.PrinterOutput, xform.Coordinates);
-
- if (!TryComp<PaperComponent>(printed, out var paper))
- return;
-
- // fill in the order data
- var val = Loc.GetString("cargo-console-paper-print-name", ("orderNumber", data.PrintableOrderNumber));
-
- MetaData(printed).EntityName = val;
-
- _paperSystem.SetContent(printed, Loc.GetString(
- "cargo-console-paper-print-text",
- ("orderNumber", data.PrintableOrderNumber),
- ("itemName", prototype.Name),
- ("requester", data.Requester),
- ("reason", data.Reason),
- ("approver", data.Approver ?? string.Empty)),
- paper);
-
- // attempt to attach the label
- if (TryComp<PaperLabelComponent>(product, out var label))
- {
- _slots.TryInsert(product, label.LabelSlot, printed, null);
- }
- }
}
using Content.Server.Cargo.Components;
using Content.Server.Station.Systems;
using Content.Shared.Cargo;
-using Content.Shared.Cargo.Components;
using Content.Shared.Containers.ItemSlots;
using Robust.Shared.Prototypes;
[NetSerializable, Serializable]
public sealed class CargoOrderData
{
- public int OrderIndex;
- /// The human-readable number, when displaying this order
- public int PrintableOrderNumber { get { return OrderIndex + 1; } }
- public string ProductId;
- public int Amount;
- public string Requester;
+ /// <summary>
+ /// A unique (arbitrary) ID which identifies this order.
+ /// </summary>
+ public readonly int OrderId;
+
+ /// <summary>
+ /// Prototype id for the item to create
+ /// </summary>
+ public readonly string ProductId;
+
+ /// <summary>
+ /// The number of items in the order. Not readonly, as it might change
+ /// due to caps on the amount of orders that can be placed.
+ /// </summary>
+ public int OrderQuantity;
+
+ /// <summary>
+ /// How many instances of this order that we've already dispatched
+ /// <summary>
+ public int NumDispatched = 0;
+
+ public readonly string Requester;
// public String RequesterRank; // TODO Figure out how to get Character ID card data
// public int RequesterId;
- public string Reason;
- public bool Approved => Approver is not null;
+ public readonly string Reason;
+ public bool Approved => Approver is not null;
public string? Approver;
- public CargoOrderData(int orderIndex, string productId, int amount, string requester, string reason)
+ public CargoOrderData(int orderId, string productId, int amount, string requester, string reason)
{
- OrderIndex = orderIndex;
+ OrderId = orderId;
ProductId = productId;
- Amount = amount;
+ OrderQuantity = amount;
Requester = requester;
Reason = reason;
}
[Serializable, NetSerializable]
public sealed class CargoConsoleApproveOrderMessage : BoundUserInterfaceMessage
{
- public int OrderIndex;
+ public int OrderId;
- public CargoConsoleApproveOrderMessage(int orderIndex)
+ public CargoConsoleApproveOrderMessage(int orderId)
{
- OrderIndex = orderIndex;
+ OrderId = orderId;
}
}
[Serializable, NetSerializable]
public sealed class CargoConsoleRemoveOrderMessage : BoundUserInterfaceMessage
{
- public int OrderIndex;
+ public int OrderId;
- public CargoConsoleRemoveOrderMessage(int orderIndex)
+ public CargoConsoleRemoveOrderMessage(int orderId)
{
- OrderIndex = orderIndex;
+ OrderId = orderId;
}
}