diff --git a/src/commandDetails/coin/transfer.ts b/src/commandDetails/coin/transfer.ts index ed5c4b92..11bfaa36 100644 --- a/src/commandDetails/coin/transfer.ts +++ b/src/commandDetails/coin/transfer.ts @@ -1,15 +1,16 @@ import { SapphireClient, container } from '@sapphire/framework'; import { CommandInteraction, EmbedBuilder, Message, User, userMention } from 'discord.js'; import { - CodeyCommandOptionType, CodeyCommandDetails, + CodeyCommandOptionType, + SapphireAfterReplyType, SapphireMessageExecuteType, - getUserFromMessage, SapphireMessageResponseWithMetadata, - SapphireAfterReplyType, + getUserFromMessage, } from '../../codeyCommand'; import { getCoinBalanceByUserId, transferTracker } from '../../components/coin'; import { getCoinEmoji } from '../../components/emojis'; +import { gamesByPlayerId } from '../../components/games/blackjack.js'; const coinTransferExecuteCommand: SapphireMessageExecuteType = async ( client, @@ -57,6 +58,20 @@ const coinTransferExecuteCommand: SapphireMessageExecuteType = async ( return new SapphireMessageResponseWithMetadata(`You can't transfer less than 1 coin.`, {}); } + if (transferTracker.transferringUsers.has(sendingUser.id)) { + return new SapphireMessageResponseWithMetadata( + `Please finish your current transfer first.`, + {}, + ); + } + + if (gamesByPlayerId.has(sendingUser.id)) { + return new SapphireMessageResponseWithMetadata( + `Please finish your current blackjack game before transferring coins.`, + {}, + ); + } + const transfer = await transferTracker.startTransfer( sendingUser, receivingUser, diff --git a/src/commandDetails/games/blackjack.ts b/src/commandDetails/games/blackjack.ts index 665df393..2776598d 100644 --- a/src/commandDetails/games/blackjack.ts +++ b/src/commandDetails/games/blackjack.ts @@ -1,17 +1,25 @@ import { container } from '@sapphire/framework'; import { - Colors, - EmbedBuilder, - ButtonBuilder, - ButtonStyle, ActionRowBuilder, + ButtonBuilder, ButtonInteraction, + ButtonStyle, + Colors, ComponentType, + EmbedBuilder, } from 'discord.js'; import { + CodeyCommandDetails, + CodeyCommandOptionType, + SapphireMessageExecuteType, + SapphireMessageResponse, + getUserFromMessage, +} from '../../codeyCommand'; +import { + UserCoinEvent, adjustCoinBalanceByUserId, getCoinBalanceByUserId, - UserCoinEvent, + transferTracker, } from '../../components/coin'; import { getCoinEmoji, getEmojiByName } from '../../components/emojis'; import { @@ -19,19 +27,13 @@ import { BlackjackHand, BlackjackStage, CardSuit, - endGame, GameState, + endGame, + gamesByPlayerId, performGameAction, startGame, } from '../../components/games/blackjack'; import { pluralize } from '../../utils/pluralize'; -import { - CodeyCommandDetails, - CodeyCommandOptionType, - SapphireMessageExecuteType, - SapphireMessageResponse, - getUserFromMessage, -} from '../../codeyCommand'; // CodeyCoin constants const DEFAULT_BET = 10; @@ -223,6 +225,14 @@ const blackjackExecuteCommand: SapphireMessageExecuteType = async ( return `You don't have enough coins to place that bet. ${getEmojiByName('codey_sad')}`; } + if (transferTracker.transferringUsers.has(author)) { + return `Please finish your current coin transfer before starting a game.`; + } + + if (gamesByPlayerId.has(author)) { + return `Please finish your current game before starting another one!`; + } + // Initialize the game let game = startGame(bet, author, channel); if (!game) { diff --git a/src/components/coin.ts b/src/components/coin.ts index 65ef1679..328443b4 100644 --- a/src/components/coin.ts +++ b/src/components/coin.ts @@ -1,19 +1,19 @@ -import _, { uniqueId } from 'lodash'; -import { openDB } from './db'; import { SapphireClient } from '@sapphire/framework'; import { - Colors, ActionRowBuilder, + BaseMessageOptions, ButtonBuilder, + ButtonStyle, + Colors, EmbedBuilder, User, - ButtonStyle, - BaseMessageOptions, } from 'discord.js'; +import _, { uniqueId } from 'lodash'; import { SapphireSentMessageType } from '../codeyCommand'; +import { updateMessageEmbed } from '../utils/embeds'; import { pluralize } from '../utils/pluralize'; +import { openDB } from './db'; import { getCoinEmoji, getEmojiByName } from './emojis'; -import { updateMessageEmbed } from '../utils/embeds'; export enum BonusType { Daily = 0, @@ -323,9 +323,11 @@ const TransferTimeout = 600000; // in ms (600000 ms == 10 mins) class TransferTracker { transfers: Map; // id, transfer + transferringUsers: Set; constructor() { this.transfers = new Map(); + this.transferringUsers = new Set(); } getTransferFromId(id: string): Transfer | undefined { return this.transfers.get(id); @@ -351,8 +353,9 @@ class TransferTracker { reason: reason, result: TransferResult.Pending, }; - const transfer = new Transfer(channelId, client, transferId, transferState); + const transfer = new Transfer(channelId, client, transferId, transferState, this); this.transfers.set(transferId, transfer); + this.transferringUsers.add(sender.id); setTimeout(async () => { if (transfer.state.result === TransferResult.Pending) { transfer.state.result = TransferResult.Timeout; @@ -370,6 +373,7 @@ class TransferTracker { throw new Error(`No transfer with transfer ID ${transferId} found`); } + this.transferringUsers.delete(transfer.state.sender.id); if (transfer.state.result === TransferResult.Pending) return; await transfer.handleTransaction(); } @@ -383,17 +387,20 @@ export class Transfer { state: TransferState; transferId: string; transferMessage!: SapphireSentMessageType; + tracker: TransferTracker; constructor( channelId: string, client: SapphireClient, transferId: string, transferState: TransferState, + tracker: TransferTracker, ) { this.channelId = channelId; this.state = transferState; this.client = client; this.transferId = transferId; + this.tracker = tracker; } // called if state is (believed to be) no longer pending. Transfers coins and updates balances if transfer is confirmed @@ -420,6 +427,8 @@ export class Transfer { (this.state.reason ?? ''), this.client.user?.id, ); + + this.tracker.transferringUsers.delete(this.state.sender.id); } } diff --git a/src/components/games/blackjack.ts b/src/components/games/blackjack.ts index 3cc998a3..5b0dc18e 100644 --- a/src/components/games/blackjack.ts +++ b/src/components/games/blackjack.ts @@ -38,7 +38,7 @@ export enum CardSuit { } // keeps track of games by player's Discord IDs -const gamesByPlayerId = new Map(); +export const gamesByPlayerId = new Map(); // maps action Enum to game action const gameActionsMap = new Map Action>([