Merge pull request #3 from JustEngineerLGTM/main

Fixing max charge auto buying
This commit is contained in:
JustEngineerLGTM authored and GitHub committed 2025-10-16 16:33:22 +03:00
commit 3fff49a461
1 file changed
+26 -4
+26 -4
View File
@@ -10467,13 +10467,21 @@ localStorage.removeItem("lp");
console.error("Error: Invalid purchase type provided.");
return;
}
const { droplets } = await WPlaceService.getCharges();
// Refresh and update full account data, then use it for purchasing
const accountData = await WPlaceService.getCharges();
// Update the current account with the latest fetched info
if (accountData) {
try { accountManager.updateAccountData(accountData); } catch (e) { console.warn('⚠️ Failed to update account manager from getCharges()', e); }
}
// Prefer droplets from the AccountManager (updated), fallback to fetched value
const current = typeof accountManager !== 'undefined' ? accountManager.getCurrentAccount() : null;
const droplets = (current && typeof current.Droplets === 'number') ? current.Droplets : (accountData?.droplets || 0);
console.log("There are currently : ", droplets, "droplets.");
try {
const amounts = Math.floor(droplets / 500);
if (amounts < 1) {
console.log("Not enough droplets to purchase.");
return;
return 1;
}
const payload = {
"product": {
@@ -10490,9 +10498,23 @@ localStorage.removeItem("lp");
credentials: "include"
});
// POST request completed
const { droplets: newDroplets } = await WPlaceService.getCharges();
if (droplets != newDroplets) {
if (res.ok) {
console.log("Successfully bought", amounts * chargeMultiplier, type.replace('_', ' '), ".");
// Update AccountManager to reflect new limits/charges after successful purchase
try {
const acc = (typeof accountManager !== 'undefined' && accountManager?.getCurrentAccount) ? accountManager.getCurrentAccount() : null;
if (acc) {
if (type === "max_charges") {
const newMax = Math.floor((typeof acc.Max === 'number' ? acc.Max : 0) + (amounts * chargeMultiplier));
accountManager.updateAccountData({ Max: newMax });
} else if (type === "paint_charges") {
const newCharges = Math.floor((typeof acc.Charges === 'number' ? acc.Charges : 0) + (amounts * chargeMultiplier));
accountManager.updateAccountData({ Charges: newCharges });
}
}
} catch (e) {
console.warn('⚠️ Failed to update account manager after purchase', e);
}
return 2;
} else {
console.log("Failed to buy charges");