Cleanup cargo shuttle/telepad order code (#13591)

Co-authored-by: Eoin Mcloughlin <helloworld@eoinrul.es>
This commit is contained in:
eoineoineoin
2023-03-05 04:27:30 +00:00
committed by GitHub
parent 2276e74b1d
commit 6722adcd83
11 changed files with 167 additions and 205 deletions

View File

@@ -1,6 +1,7 @@
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;
@@ -12,9 +13,9 @@ using Content.Shared.Cargo.Events;
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
@@ -114,9 +115,12 @@ 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))
@@ -126,7 +130,7 @@ namespace Content.Server.Cargo.Systems
return;
}
var amount = GetOrderCount(orderDatabase);
var amount = GetOutstandingOrderCount(orderDatabase);
var capacity = orderDatabase.Capacity;
// Too many orders, avoid them getting spammed in the UI.
@@ -138,16 +142,16 @@ namespace Content.Server.Cargo.Systems
}
// 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)
@@ -163,7 +167,7 @@ namespace Content.Server.Cargo.Systems
// 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);
@@ -173,7 +177,7 @@ namespace Content.Server.Cargo.Systems
{
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)
@@ -189,7 +193,7 @@ namespace Content.Server.Cargo.Systems
var orderDatabase = GetOrderDatabase(component);
if (orderDatabase == null) return;
var data = GetOrderData(args, GetNextIndex(orderDatabase));
var data = GetOrderData(args, GenerateOrderId(orderDatabase));
if (!TryAddOrder(orderDatabase, data))
{
@@ -199,7 +203,7 @@ namespace Content.Server.Cargo.Systems
// 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}]");
}
@@ -219,10 +223,10 @@ namespace Content.Server.Cargo.Systems
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);
}
@@ -234,19 +238,19 @@ namespace Content.Server.Cargo.Systems
_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;
@@ -278,22 +282,26 @@ namespace Content.Server.Cargo.Systems
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)
@@ -304,6 +312,64 @@ namespace Content.Server.Cargo.Systems
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);