Refactor UISnapshotPanelBuyAll to improve block purchase logic

This commit is contained in:
2025-02-25 15:49:33 +01:00
parent 9d40375b8c
commit 1d8699761e

View File

@@ -43,87 +43,118 @@ namespace TerraTech {
m_TechAvailLookup = m_TechAvailLookupTraverse.GetValue<Dictionary<Snapshot, TechDataAvailValidation>>();
}
public void OnPointerClick(PointerEventData eventData) {
if (Main.debugBuyAll.Value)
Console.WriteLine("UISnapshotPanel.OnPointerClick: {0} {1}", gameObject.name, eventData.button);
try {
if (eventData.button == PointerEventData.InputButton.Right) {
SnapshotLiveData selectedSnapshotData = UISnapshotPanelBuyAll.selectedData;
if (selectedSnapshotData == null) {
if (Main.debugBuyAll.Value)
Console.WriteLine("Selected snapshot data is null wtf??: {0} {1}", gameObject.name,
eventData.button);
return;
}
Snapshot selectedSnapshot = selectedSnapshotData.m_Snapshot;
if (selectedSnapshot == null) {
if (Main.debugBuyAll.Value)
Console.WriteLine("Selected snapshot is null wtf??: {0} {1}", gameObject.name,
eventData.button);
return;
}
TechDataAvailValidation techDataAvail;
bool ok = m_TechAvailLookup.TryGetValue(selectedSnapshot, out techDataAvail);
if (!ok) {
if (Main.debugBuyAll.Value)
Console.WriteLine("Failed to find TechDataAvailValidation for snapshot: {0}",
selectedSnapshot);
return;
}
var trav = Traverse.Create(techDataAvail);
var m_BlockAvailabilityField = trav.Field("m_BlockAvailability");
var m_BlockAvailability =
m_BlockAvailabilityField
.GetValue<Dictionary<BlockTypes, TechDataAvailValidation.BlockTypeAvailability>>();
private Dictionary<BlockTypes, int> CalculateMissingBlocks(
Dictionary<BlockTypes, TechDataAvailValidation.BlockTypeAvailability> blockAvailability, bool isSpawning) {
Dictionary<BlockTypes, int> missing = new Dictionary<BlockTypes, int>();
foreach (var kvp in blockAvailability) {
int numMissing;
if (isSpawning)
numMissing = kvp.Value.numRequired - kvp.Value.numInInventory;
else
numMissing = kvp.Value.numRequired - kvp.Value.numInInventory - kvp.Value.numOnPlayerTech;
if (numMissing > 0 || !isSpawning)
missing.Add(kvp.Key, isSpawning ? numMissing : kvp.Value.numRequired);
}
return missing;
}
private int CalculateTotalCost(Dictionary<BlockTypes, int> missingBlocks) {
int totalCost = 0;
RecipeManager recipeManager = Singleton.Manager<RecipeManager>.inst;
foreach (var kvp in m_BlockAvailability) {
if (kvp.Value.numRequired - kvp.Value.numInInventory > 0) {
int numMissing = kvp.Value.numRequired - kvp.Value.numInInventory;
missing.Add(kvp.Key, numMissing);
if (Main.debugBuyAll.Value)
Console.WriteLine("Missing {0} of {1}", numMissing, kvp.Key);
int cost = recipeManager.GetBlockBuyPrice(kvp.Key) * numMissing;
foreach (var kvp in missingBlocks) {
int cost = recipeManager.GetBlockBuyPrice(kvp.Key) * kvp.Value;
totalCost += cost;
if (Main.debugBuyAll.Value)
Console.WriteLine("{0} of {1} would cost {2}, total now {3}", numMissing, kvp.Key, cost,
totalCost);
Console.WriteLine("{0} of {1} would cost {2}, total now {3}", kvp.Value, kvp.Key, cost, totalCost);
}
return totalCost;
}
if (totalCost > 0) {
if (Main.debugBuyAll.Value)
Console.WriteLine("Total cost: {0}", totalCost);
}
else {
if (Main.debugBuyAll.Value)
Console.WriteLine("No blocks missing, nothing to do");
return;
}
private bool TryPurchaseBlocks(Dictionary<BlockTypes, int> missingBlocks, int totalCost) {
ManPlayer player = Singleton.Manager<ManPlayer>.inst;
if (player.GetCurrentMoney() < totalCost) {
if (Main.debugBuyAll.Value)
Console.WriteLine("Not enough money, have {0} but need {1}, nothing to do",
player.GetCurrentMoney(), totalCost);
return;
return false;
}
player.PayMoney(totalCost);
foreach (var kvp in missing) {
foreach (var kvp in missingBlocks) {
if (Main.debugBuyAll.Value)
Console.WriteLine("Buying {0} of {1}", kvp.Value, kvp.Key);
player.PlayerInventory.HostAddItem(kvp.Key, kvp.Value);
}
return true;
}
private bool ProcessPurchase(
Dictionary<BlockTypes, TechDataAvailValidation.BlockTypeAvailability> blockAvailability, bool isSpawning) {
try {
if (blockAvailability == null) {
if (Main.debugBuyAll.Value)
Console.WriteLine("Block availability is null (wtf?), nothing to do");
return false;
}
var missingBlocks = CalculateMissingBlocks(blockAvailability, isSpawning);
int totalCost = CalculateTotalCost(missingBlocks);
if (totalCost > 0) {
if (Main.debugBuyAll.Value)
Console.WriteLine("Total cost: {0}", totalCost);
return TryPurchaseBlocks(missingBlocks, totalCost);
}
if (Main.debugBuyAll.Value)
Console.WriteLine("No blocks missing or no cost calculated");
return false;
} catch (Exception e) {
if (Main.debugBuyAll.Value)
Console.WriteLine("Error during purchase processing: {0}", e);
return false;
}
}
catch (Exception e) {
private Dictionary<BlockTypes, TechDataAvailValidation.BlockTypeAvailability> GetCurrentBlockAvailability() {
SnapshotLiveData selectedSnapshotData = UISnapshotPanelBuyAll.selectedData;
Snapshot selectedSnapshot = selectedSnapshotData.m_Snapshot;
if (selectedSnapshot == null) {
if (Main.debugBuyAll.Value)
Console.WriteLine("Selected snapshot is null wtf??: {0}", gameObject.name);
return null;
}
TechDataAvailValidation techDataAvail;
if (!m_TechAvailLookup.TryGetValue(selectedSnapshot, out techDataAvail)) {
if (Main.debugBuyAll.Value)
Console.WriteLine("Failed to find TechDataAvailValidation for snapshot: {0}", selectedSnapshot);
return null;
}
var trav = Traverse.Create(techDataAvail);
var mBlockAvailabilityField = trav.Field("m_BlockAvailability");
var mBlockAvailability =
mBlockAvailabilityField
.GetValue<Dictionary<BlockTypes, TechDataAvailValidation.BlockTypeAvailability>>();
return mBlockAvailability;
}
public void OnPointerClick(PointerEventData eventData) {
if (Main.debugBuyAll.Value)
Console.WriteLine("UISnapshotPanel.OnPointerClick: {0} {1}", gameObject.name, eventData.button);
try {
if (eventData.button == PointerEventData.InputButton.Right) {
// For now, we're using the spawn new tech logic (isSpawning = true)
// You can add another button/input handler that calls this with isSpawning = false for morphing
ProcessPurchase(GetCurrentBlockAvailability(), true);
}
} catch (Exception e) {
if (Main.debugBuyAll.Value)
Console.WriteLine("Shit exploded fml: {0}", e);
}