Skip to content

Commit

Permalink
[build]
Browse files Browse the repository at this point in the history
  • Loading branch information
insraq committed Mar 5, 2025
1 parent e69a43f commit 1655cd9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 49 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"cSpell.words": ["Cooldown", "Gameplay", "hitmap", "Koffi", "lerp", "rebirthed", "unlockable"],
"task.allowAutomaticTasks": "on",
"typescript.tsdk": "node_modules\\typescript\\lib",
"editor.formatOnSave": true
"editor.formatOnSave": true,
"typescript.preferences.preferTypeOnlyAutoImports": false
}
80 changes: 32 additions & 48 deletions src/scripts/ui/FillPlayerTradeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { deductResourceFrom } from "../../../shared/logic/ResourceLogic";
import { Tick } from "../../../shared/logic/TickLogic";
import {
clamp,
forEach,
formatNumber,
formatPercent,
pointToXy,
Expand Down Expand Up @@ -107,71 +106,56 @@ export function FillPlayerTradeModal({ tradeId, xy }: { tradeId: string; xy?: Ti
playError();
return;
}
let total = 0;
let success = 0;
let fillAmount = 0;
let receivedAmount = 0;
const errors: string[] = [];
let totalAmount = 0;
const queue: Array<{ amount: number; rollback: () => void; tile: Tile }> = [];
for (const [tile, amount] of fills) {
if (amount <= 0) continue;
// We reserve the amount first, otherwise resource might go negative if a player
// clicks really fast
++total;
const r = deductResourceFrom(trade.buyResource, amount, [tile], gs);
queue.push({ amount: r.amount, rollback: r.rollback, tile });
totalAmount += r.amount;
}
for (const r of queue) {
try {
const result = await client.fillTrade({
id: trade.id,
amount: r.amount,
path: tiles,
seaTileCost: getSeaTileCost(gs),
});
forEach(result, (res, amount) => {
if (amount > 0) {
receivedAmount += amount;
}
if (amount < 0) {
fillAmount += Math.abs(amount);
}
safeAdd(allTradeBuildings.get(r.tile)!.resources, res, amount);
});
++success;
} catch (error) {
errors.push(String(error));
} finally {
// If the trade fails, we should refund the resource. If the trade success, the result
// from the server contains the correct amount to deduct, we *also* refund the resource
r.rollback();
try {
const result = await client.fillTrade({
id: trade.id,
amount: totalAmount,
path: tiles,
seaTileCost: getSeaTileCost(gs),
});
const receivedAmount = result[trade.sellResource] ?? 0;
for (const r of queue) {
const building = allTradeBuildings.get(r.tile);
if (building) {
safeAdd(building.resources, trade.sellResource, (receivedAmount * r.amount) / totalAmount);
}
}
}
if (success > 0) {
playKaching();
errors.unshift(
t(L.PlayerTradeFillSuccessV2, {
success,
total,
fillAmount: formatNumber(fillAmount),
fillResource: Config.Resource[trade.buyResource].name(),
receivedAmount: formatNumber(receivedAmount),
receivedResource: Config.Resource[trade.sellResource].name(),
}),
);
const eic = Tick.current.specialBuildings.get("EastIndiaCompany");
if (eic) {
safeAdd(
eic.building.resources,
"TradeValue",
fillAmount * (Config.ResourcePrice[trade.buyResource] ?? 0),
receivedAmount * (Config.ResourcePrice[trade.sellResource] ?? 0),
);
}
showToast(errors.join("<br />"));
playKaching();
showToast(
t(L.PlayerTradeFillSuccessV2, {
success: queue.length,
total: queue.length,
fillAmount: formatNumber(totalAmount),
fillResource: Config.Resource[trade.buyResource].name(),
receivedAmount: formatNumber(receivedAmount),
receivedResource: Config.Resource[trade.sellResource].name(),
}),
);
hideModal();
} else {
} catch (error) {
for (const r of queue) {
r.rollback();
}
playError();
showToast(errors.join("<br />"));
showToast(String(error));
}
};

Expand Down

0 comments on commit 1655cd9

Please sign in to comment.