169 lines
3.5 KiB
JavaScript
169 lines
3.5 KiB
JavaScript
const {
|
|
getPokerService
|
|
} = require("./crumbPoker");
|
|
|
|
const {
|
|
deferPokerReply,
|
|
editPokerError,
|
|
getPokerGameIdFromInteraction,
|
|
refreshPokerTableMessage
|
|
} = require("./pokerDiscord");
|
|
|
|
|
|
async function executePokerAction(
|
|
interaction,
|
|
action,
|
|
successText
|
|
) {
|
|
await deferPokerReply(
|
|
interaction
|
|
);
|
|
|
|
const gameId =
|
|
getPokerGameIdFromInteraction(
|
|
interaction
|
|
);
|
|
|
|
if (!gameId) {
|
|
return interaction.editReply({
|
|
content:
|
|
"❌ This Poker control is no longer attached to an active table.",
|
|
embeds: [],
|
|
components: []
|
|
});
|
|
}
|
|
|
|
try {
|
|
const service =
|
|
getPokerService();
|
|
|
|
const game =
|
|
await service.act({
|
|
gameId,
|
|
userId:
|
|
interaction.user.id,
|
|
action
|
|
});
|
|
|
|
await refreshPokerTableMessage(
|
|
interaction.client,
|
|
game
|
|
);
|
|
|
|
return interaction.editReply({
|
|
content:
|
|
typeof successText ===
|
|
"function"
|
|
? successText(game)
|
|
: successText,
|
|
embeds: [],
|
|
components: []
|
|
});
|
|
}
|
|
|
|
catch (error) {
|
|
return editPokerError(
|
|
interaction,
|
|
error
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
async function executeMinimumRaise(
|
|
interaction
|
|
) {
|
|
await deferPokerReply(
|
|
interaction
|
|
);
|
|
|
|
const gameId =
|
|
getPokerGameIdFromInteraction(
|
|
interaction
|
|
);
|
|
|
|
if (!gameId) {
|
|
return interaction.editReply({
|
|
content:
|
|
"❌ This Poker control is no longer attached to an active table.",
|
|
embeds: [],
|
|
components: []
|
|
});
|
|
}
|
|
|
|
try {
|
|
const service =
|
|
getPokerService();
|
|
|
|
const legal =
|
|
await service.getLegalPlayerActions({
|
|
gameId,
|
|
userId:
|
|
interaction.user.id
|
|
});
|
|
|
|
const type =
|
|
legal.actions?.includes(
|
|
"bet"
|
|
)
|
|
? "bet"
|
|
: legal.actions?.includes(
|
|
"raise"
|
|
)
|
|
? "raise"
|
|
: null;
|
|
|
|
if (
|
|
!type
|
|
||
|
|
!Number.isSafeInteger(
|
|
legal.minRaiseTo
|
|
)
|
|
) {
|
|
return interaction.editReply({
|
|
content:
|
|
"❌ A bet or raise is not available right now.",
|
|
embeds: [],
|
|
components: []
|
|
});
|
|
}
|
|
|
|
const game =
|
|
await service.act({
|
|
gameId,
|
|
userId:
|
|
interaction.user.id,
|
|
action: {
|
|
type,
|
|
amount:
|
|
legal.minRaiseTo
|
|
}
|
|
});
|
|
|
|
await refreshPokerTableMessage(
|
|
interaction.client,
|
|
game
|
|
);
|
|
|
|
return interaction.editReply({
|
|
content:
|
|
`✅ ${type === "bet" ? "Bet" : "Raised to"} ${legal.minRaiseTo.toLocaleString()} 🍪`,
|
|
embeds: [],
|
|
components: []
|
|
});
|
|
}
|
|
|
|
catch (error) {
|
|
return editPokerError(
|
|
interaction,
|
|
error
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
executePokerAction,
|
|
executeMinimumRaise
|
|
};
|