Make trade acceptance daemon authoritative
This commit is contained in:
parent
6f0375f4ad
commit
a330e24d96
11 changed files with 181 additions and 218 deletions
|
|
@ -4278,12 +4278,6 @@ public class DaemonRuntimeDocumentTests
|
|||
command.PositionY = 11;
|
||||
command.PositionZ = 12;
|
||||
command.ScalarValue = 1;
|
||||
command.LootPickup.ItemKey = "loot-cell";
|
||||
command.LootPickup.PickupIndex = 7;
|
||||
command.LootPickup.Quantity = 1;
|
||||
command.LootPickup.PositionX = 10;
|
||||
command.LootPickup.PositionY = 11;
|
||||
command.LootPickup.PositionZ = 12;
|
||||
|
||||
var result = AetheriaRuntimeDaemonOperations.Execute(run, new[] { command });
|
||||
|
||||
|
|
@ -4432,18 +4426,18 @@ public class DaemonRuntimeDocumentTests
|
|||
command.TargetEntityKey = "zone.0.entity.1";
|
||||
command.TextValue = "ore";
|
||||
command.ScalarValue = 200;
|
||||
command.TradePurchase.PurchaseKind = "commodity";
|
||||
command.TradePurchase.ItemKey = "ore";
|
||||
command.TradePurchase.Quantity = 5;
|
||||
command.TradePurchase.UnitPrice = 0;
|
||||
command.TradePurchase.TotalPrice = 0;
|
||||
command.TradePurchase.StationEntityKey = "zone.0.entity.0";
|
||||
command.TradePurchase.StationCargoIndex = 0;
|
||||
command.TradePurchase.TargetEntityKey = "zone.0.entity.1";
|
||||
command.TradePurchase.TargetCargoIndex = 0;
|
||||
command.TradePurchase.SourceX = 2;
|
||||
command.TradePurchase.SourceY = 3;
|
||||
command.TradePurchase.CreatesDockedShip = false;
|
||||
command.TradePurchase.PurchaseKind = "docked_ship";
|
||||
command.TradePurchase.UnitPrice = -999;
|
||||
command.TradePurchase.TotalPrice = -999;
|
||||
command.TradePurchase.StationEntityKey = "forged-station";
|
||||
command.TradePurchase.TargetEntityKey = "forged-target";
|
||||
command.TradePurchase.CreatesDockedShip = true;
|
||||
|
||||
var result = AetheriaRuntimeDaemonOperations.Execute(
|
||||
run,
|
||||
|
|
@ -4479,9 +4473,7 @@ public class DaemonRuntimeDocumentTests
|
|||
"zone.0.entity.1");
|
||||
command.TradePurchase.ItemKey = "ore";
|
||||
command.TradePurchase.Quantity = 1;
|
||||
command.TradePurchase.StationEntityKey = "zone.0.entity.0";
|
||||
command.TradePurchase.StationCargoIndex = 0;
|
||||
command.TradePurchase.TargetEntityKey = "zone.0.entity.1";
|
||||
command.TradePurchase.TargetCargoIndex = 0;
|
||||
command.TradePurchase.SourceX = 2;
|
||||
command.TradePurchase.SourceY = 3;
|
||||
|
|
@ -4528,17 +4520,17 @@ public class DaemonRuntimeDocumentTests
|
|||
command.TargetEntityKey = "zone.0.entity.1";
|
||||
command.TextValue = "starter-hull";
|
||||
command.ScalarValue = 300;
|
||||
command.TradePurchase.PurchaseKind = "docked_ship";
|
||||
command.TradePurchase.ItemKey = "starter-hull";
|
||||
command.TradePurchase.Quantity = 1;
|
||||
command.TradePurchase.UnitPrice = 300;
|
||||
command.TradePurchase.TotalPrice = 300;
|
||||
command.TradePurchase.StationEntityKey = "zone.0.entity.1";
|
||||
command.TradePurchase.StationCargoIndex = 0;
|
||||
command.TradePurchase.TargetEntityKey = "zone.0.entity.1";
|
||||
command.TradePurchase.SourceX = 4;
|
||||
command.TradePurchase.SourceY = 5;
|
||||
command.TradePurchase.CreatesDockedShip = true;
|
||||
command.TradePurchase.PurchaseKind = "commodity";
|
||||
command.TradePurchase.UnitPrice = 0;
|
||||
command.TradePurchase.TotalPrice = 0;
|
||||
command.TradePurchase.StationEntityKey = "forged-station";
|
||||
command.TradePurchase.TargetEntityKey = "forged-target";
|
||||
command.TradePurchase.CreatesDockedShip = false;
|
||||
|
||||
var result = AetheriaRuntimeDaemonOperations.Execute(
|
||||
run,
|
||||
|
|
|
|||
|
|
@ -409,101 +409,40 @@ public class TradeMenu : MonoBehaviour
|
|||
return;
|
||||
}
|
||||
|
||||
var createsDockedShip = !string.IsNullOrWhiteSpace(row.TypedItem.HullType);
|
||||
if (createsDockedShip &&
|
||||
!string.Equals(row.TypedItem.HullType, nameof(HullType.Ship), StringComparison.Ordinal))
|
||||
{
|
||||
ShowUnableToBuy("Unsupported hull purchase!");
|
||||
return;
|
||||
}
|
||||
|
||||
RequestTradePurchase(row, quantity, createsDockedShip);
|
||||
RequestTradePurchase(row, quantity);
|
||||
}
|
||||
|
||||
private void RequestTradePurchase(
|
||||
TradeRow row,
|
||||
int quantity,
|
||||
bool createsDockedShip)
|
||||
int quantity)
|
||||
{
|
||||
if (row?.Stock == null ||
|
||||
string.IsNullOrWhiteSpace(row.ItemKey) ||
|
||||
quantity <= 0 ||
|
||||
row.Price < 0)
|
||||
quantity <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var totalPrice = (long)quantity * row.Price;
|
||||
if (totalPrice > int.MaxValue)
|
||||
return;
|
||||
|
||||
var stationRefit = StationRefitSnapshot();
|
||||
var stationEntityKey = stationRefit?.DockParentEntityKey ?? "";
|
||||
var stationCargoIndex = row.Stock.CargoBayIndex;
|
||||
var sourcePosition = new int2(row.Stock.X, row.Stock.Y);
|
||||
if (string.IsNullOrWhiteSpace(stationEntityKey) ||
|
||||
stationCargoIndex < 0 ||
|
||||
if (stationCargoIndex < 0 ||
|
||||
sourcePosition.x < 0 ||
|
||||
sourcePosition.y < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var targetEntityKey = _targetCargoEntityKey ?? "";
|
||||
var targetCargoIndex = _targetCargoIndex;
|
||||
|
||||
if (createsDockedShip)
|
||||
{
|
||||
if (!TryResolveCurrentDockingTargetEntityKey(out targetEntityKey))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
targetCargoIndex = -1;
|
||||
}
|
||||
else if (string.IsNullOrWhiteSpace(targetEntityKey) ||
|
||||
targetCargoIndex < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var purchaseKind = createsDockedShip
|
||||
? "docked_ship"
|
||||
: row.TypedItem.TryGetSimpleCommodityCategory(out SimpleCommodityCategory _)
|
||||
? "commodity"
|
||||
: "crafted";
|
||||
|
||||
TrySubmitOperation(
|
||||
operations => operations.TradePurchase(
|
||||
purchaseKind,
|
||||
row.ItemKey,
|
||||
quantity,
|
||||
row.Price,
|
||||
(int)totalPrice,
|
||||
stationEntityKey,
|
||||
stationCargoIndex,
|
||||
targetEntityKey,
|
||||
targetCargoIndex,
|
||||
_targetCargoIndex,
|
||||
sourcePosition.x,
|
||||
sourcePosition.y,
|
||||
createsDockedShip),
|
||||
sourcePosition.y),
|
||||
"trade purchase");
|
||||
}
|
||||
|
||||
private bool TryResolveCurrentDockingTargetEntityKey(out string targetEntityKey)
|
||||
{
|
||||
targetEntityKey = "";
|
||||
if (!TryResolveCurrentDocking(out var docking) ||
|
||||
docking.IsDocked != true ||
|
||||
string.IsNullOrWhiteSpace(docking.DockParentEntityKey))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
targetEntityKey = docking.DockParentEntityKey;
|
||||
return true;
|
||||
}
|
||||
|
||||
private AetheriaRuntimeStationRefitDocument StationRefitSnapshot()
|
||||
{
|
||||
try
|
||||
|
|
|
|||
Loading…
Reference in a new issue