fix: javascript error on gambling
This commit is contained in:
parent
ccbc0aeaf0
commit
bb7062987b
2 changed files with 16 additions and 11 deletions
|
|
@ -47,16 +47,19 @@ export const POST: RequestHandler = async ({ request }) => {
|
||||||
|
|
||||||
const currentBalance = Number(userData.baseCurrencyBalance);
|
const currentBalance = Number(userData.baseCurrencyBalance);
|
||||||
|
|
||||||
if (amount > currentBalance) {
|
const roundedAmount = Math.round(amount * 100000000) / 100000000;
|
||||||
throw new Error(`Insufficient funds. You need *${amount.toFixed(2)} but only have *${currentBalance.toFixed(2)}`);
|
const roundedBalance = Math.round(currentBalance * 100000000) / 100000000;
|
||||||
|
|
||||||
|
if (roundedAmount > roundedBalance) {
|
||||||
|
throw new Error(`Insufficient funds. You need *${roundedAmount.toFixed(2)} but only have *${roundedBalance.toFixed(2)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const gameResult: 'heads' | 'tails' = randomBytes(1)[0] < 128 ? 'heads' : 'tails';
|
const gameResult: 'heads' | 'tails' = randomBytes(1)[0] < 128 ? 'heads' : 'tails';
|
||||||
const won = gameResult === side;
|
const won = gameResult === side;
|
||||||
|
|
||||||
const multiplier = 2;
|
const multiplier = 2;
|
||||||
const payout = won ? amount * multiplier : 0;
|
const payout = won ? roundedAmount * multiplier : 0;
|
||||||
const newBalance = currentBalance - amount + payout;
|
const newBalance = roundedBalance - roundedAmount + payout;
|
||||||
|
|
||||||
await tx
|
await tx
|
||||||
.update(user)
|
.update(user)
|
||||||
|
|
@ -71,7 +74,7 @@ export const POST: RequestHandler = async ({ request }) => {
|
||||||
result: gameResult,
|
result: gameResult,
|
||||||
newBalance,
|
newBalance,
|
||||||
payout,
|
payout,
|
||||||
amountWagered: amount
|
amountWagered: roundedAmount
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,10 +45,12 @@ export const POST: RequestHandler = async ({ request }) => {
|
||||||
|
|
||||||
const currentBalance = Number(userData.baseCurrencyBalance);
|
const currentBalance = Number(userData.baseCurrencyBalance);
|
||||||
|
|
||||||
if (amount > currentBalance) {
|
const roundedAmount = Math.round(amount * 100000000) / 100000000;
|
||||||
throw new Error(`Insufficient funds. You need *${amount.toFixed(2)} but only have *${currentBalance.toFixed(2)}`);
|
const roundedBalance = Math.round(currentBalance * 100000000) / 100000000;
|
||||||
}
|
|
||||||
|
|
||||||
|
if (roundedAmount > roundedBalance) {
|
||||||
|
throw new Error(`Insufficient funds. You need *${roundedAmount.toFixed(2)} but only have *${roundedBalance.toFixed(2)}`);
|
||||||
|
}
|
||||||
|
|
||||||
// Generate random symbols
|
// Generate random symbols
|
||||||
const gameResult = [
|
const gameResult = [
|
||||||
|
|
@ -71,8 +73,8 @@ export const POST: RequestHandler = async ({ request }) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const won = multiplier > 0;
|
const won = multiplier > 0;
|
||||||
const payout = won ? amount * multiplier : 0;
|
const payout = won ? roundedAmount * multiplier : 0;
|
||||||
const newBalance = currentBalance - amount + payout;
|
const newBalance = roundedBalance - roundedAmount + payout;
|
||||||
|
|
||||||
await tx
|
await tx
|
||||||
.update(user)
|
.update(user)
|
||||||
|
|
@ -87,7 +89,7 @@ export const POST: RequestHandler = async ({ request }) => {
|
||||||
symbols: gameResult,
|
symbols: gameResult,
|
||||||
newBalance,
|
newBalance,
|
||||||
payout,
|
payout,
|
||||||
amountWagered: amount,
|
amountWagered: roundedAmount,
|
||||||
winType: won ? winType : undefined
|
winType: won ? winType : undefined
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Reference in a new issue