Skip to content

Commit

Permalink
Handle overflow in anon bank deposits (#1967)
Browse files Browse the repository at this point in the history
When depositing into an anon account, the database would overflow if
the amount in the account would exceed MAX_MONEY. Now, the deposit will
be adjusted so that it doesn't exceed MAX_MONEY, and if the account is
already at MAX_MONEY, it will emit a comprehensible error.
  • Loading branch information
hemberger authored Dec 3, 2024
1 parent d90d6f9 commit 65846a4
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/pages/Player/Bank/AnonBankDetailProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,31 @@ public function build(AbstractPlayer $player): never {
]);
$trans_id = $dbResult->record()->getInt('max_id') + 1;

// Get the amount currently in this anon bank
$dbResult = $db->read('SELECT amount FROM anon_bank WHERE anon_id = :anon_id AND game_id = :game_id', [
'anon_id' => $db->escapeNumber($account_num),
'game_id' => $db->escapeNumber($player->getGameID()),
]);
$anonAmount = $dbResult->record()->getInt('amount');

// Update the credit amounts for the player and the bank
if ($action === 'Deposit') {
if ($player->getCredits() < $amount) {
create_error('You don\'t own that much money!');
}
$amount = min($amount, MAX_MONEY - $anonAmount); // handle overflow
if ($amount === 0) {
create_error('This account has reached the maximum credit limit!');
}

// Does not handle overflow!
$player->decreaseCredits($amount);
$db->write('UPDATE anon_bank SET amount = amount + :amount WHERE game_id = :game_id AND anon_id = :anon_id', [
'amount' => $db->escapeNumber($amount),
'game_id' => $db->escapeNumber($player->getGameID()),
'anon_id' => $db->escapeNumber($account_num),
]);
} else {
$dbResult = $db->read('SELECT * FROM anon_bank WHERE anon_id = :anon_id AND game_id = :game_id', [
'anon_id' => $db->escapeNumber($account_num),
'game_id' => $db->escapeNumber($player->getGameID()),
]);
if ($dbResult->record()->getInt('amount') < $amount) {
if ($anonAmount < $amount) {
create_error('You don\'t have that much money on your account!');
}

Expand Down

0 comments on commit 65846a4

Please sign in to comment.