lottery
- builtin
- by Takaro
- Takaro >=0.0.1
- all
Built-in modules ship with Takaro — no import needed.
Players can buy tickets for a lottery, and the winner is chosen at random.
Configuration 1
Settings you fill in when installing the module on a server.
| Setting | Type | Default | Description |
|---|---|---|---|
profitMargin | number | 0.1 | The profit margin the server takes from the lottery. |
Raw config schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"profitMargin": {
"type": "number",
"maximum": 1,
"minimum": 0,
"description": "The profit margin the server takes from the lottery.",
"default": 0.1
}
},
"required": [],
"additionalProperties": false
} Raw UI schema
{} Commands 3
Chat commands players trigger in game.
-
buyTicket
Buy a lottery ticket.
Argument Type Default Help amountnumber — The amount of tickets to buy. Command source
import { takaro, data, TakaroUserError } from '@takaro/helpers'; async function main() { const { pog, gameServerId, arguments: args, module: mod } = data; const varKey = 'lottery_tickets_bought'; if (args.amount < 1) { throw new TakaroUserError('You must buy at least 1 ticket.'); } const tickets = (await takaro.variable.variableControllerSearch({ filters: { gameServerId: [gameServerId], key: [varKey], moduleId: [mod.moduleId], playerId: [pog.playerId], }, })).data.data; // Player already has some tickets bought if (tickets.length > 0) { const ticketsBought = tickets[0]; const ticketsBoughtAmount = parseInt(JSON.parse(ticketsBought.value).amount, 10); await takaro.variable.variableControllerUpdate(ticketsBought.id, { key: varKey, playerId: pog.playerId, moduleId: mod.moduleId, gameServerId, value: JSON.stringify({ amount: ticketsBoughtAmount + args.amount }), }); } // Player has no tickets bought else { await takaro.variable.variableControllerCreate({ key: varKey, value: JSON.stringify({ amount: args.amount, }), gameServerId, moduleId: mod.moduleId, playerId: pog.playerId, }); } const ticketPrice = args.amount * mod.systemConfig.commands.buyTicket.cost; // The price of the first ticket is deducted by the command execution itself. if (args.amount > 1) { await takaro.playerOnGameserver.playerOnGameServerControllerDeductCurrency(gameServerId, pog.playerId, { currency: ticketPrice - 1, reason: 'Lottery ticket purchase', }); } const currencyName = (await takaro.settings.settingsControllerGetOne('currencyName', gameServerId)).data.data.value; await pog.pm(`You have successfully bought ${args.amount} tickets for ${ticketPrice} ${currencyName}. Good luck!`); } await main(); //# sourceMappingURL=buyTicket.js.map -
viewTickets
View your lottery tickets.
Command source
import { takaro, data } from '@takaro/helpers'; async function main() { const { pog, gameServerId, module: mod } = data; const varKey = 'lottery_tickets_bought'; const tickets = (await takaro.variable.variableControllerSearch({ filters: { gameServerId: [gameServerId], key: [varKey], moduleId: [mod.moduleId], playerId: [pog.playerId], }, })).data.data; let ticketsBought = 0; if (tickets.length === 1) { ticketsBought = parseInt(JSON.parse(tickets[0].value).amount, 10); } await pog.pm(`You have bought ${ticketsBought} tickets.`); } await main(); //# sourceMappingURL=viewTickets.js.map -
nextDraw
View when the next draw is.
Command source
import { nextCronJobRun, data } from '@takaro/helpers'; function formatTimeToReach(cronJob) { const targetDate = nextCronJobRun(cronJob); // Get the current date and time const currentDate = new Date(); // Calculate the time difference in milliseconds const delta = targetDate - currentDate; // Calculate days, hours, minutes, and seconds const days = Math.floor(delta / (1000 * 60 * 60 * 24)); const hours = Math.floor((delta % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const minutes = Math.floor((delta % (1000 * 60 * 60)) / (1000 * 60)); const seconds = Math.floor((delta % (1000 * 60)) / 1000); // Build the formatted string let formattedString = ''; if (days > 0) { formattedString += `${days} day${days > 1 ? 's' : ''} `; } if (hours > 0) { formattedString += `${hours} hour${hours > 1 ? 's' : ''} `; } if (minutes > 0) { formattedString += `${minutes} minute${minutes > 1 ? 's' : ''} `; } if (seconds > 0) { formattedString += `${seconds} second${seconds > 1 ? 's' : ''} `; } return formattedString.trim(); } async function main() { const { player, module: mod } = data; await player.pm(`The next lottery draw is in about ${formatTimeToReach(mod.systemConfig.cronJobs.drawLottery.temporalValue)}`); } await main(); //# sourceMappingURL=nextDraw.js.map
Cron jobs 1
Work the module runs on a schedule.
-
drawLottery
Cron job source
import { takaro, data } from '@takaro/helpers'; function getTotalPrize(tickets, ticketPrice, profitMargin) { const amount = tickets.reduce((acc, ticket) => { const ticketAmount = parseInt(JSON.parse(ticket.value).amount, 10); return acc + ticketAmount; }, 0); const rawTotal = amount * ticketPrice; const profit = rawTotal * profitMargin; const totalPrize = rawTotal - profit; return totalPrize; } async function drawWinner(takaro, gameServerId, tickets) { const randomIndex = Math.floor(Math.random() * tickets.length); const winnerTicket = tickets[randomIndex]; const winner = (await takaro.player.playerControllerGetOne(winnerTicket.playerId)).data.data; const pog = await takaro.playerOnGameserver.playerOnGameServerControllerSearch({ filters: { gameServerId: [gameServerId], playerId: [winner.id], }, }); return { name: winner.name, playerId: pog.data.data[0].playerId, }; } async function refundPlayer(takaro, gameServerId, playerId, amount, currencyName) { const pog = (await takaro.playerOnGameserver.playerOnGameServerControllerSearch({ filters: { gameServerId: [gameServerId], playerId: [playerId], }, })).data.data[0]; await takaro.playerOnGameserver.playerOnGameServerControllerAddCurrency(gameServerId, pog.playerId, { currency: amount, reason: 'Lottery refund', }); await takaro.gameserver.gameServerControllerSendMessage(gameServerId, { message: `You have been refunded ${amount} ${currencyName} because the lottery has been cancelled.`, opts: { recipient: { gameId: pog.gameId, }, }, }); } async function cleanUp(takaro, tickets) { const deleteTasks = tickets.map((ticket) => takaro.variable.variableControllerDelete(ticket.id)); await Promise.allSettled(deleteTasks); } async function main() { const { gameServerId, module: mod } = data; let tickets = []; try { const currencyName = (await takaro.settings.settingsControllerGetOne('currencyName', gameServerId)).data.data.value; const ticketCost = mod.systemConfig.commands.buyTicket.cost; tickets = (await takaro.variable.variableControllerSearch({ filters: { gameServerId: [gameServerId], moduleId: [mod.moduleId], key: ['lottery_tickets_bought'], }, })).data.data; if (tickets.length === 0) { await takaro.gameserver.gameServerControllerSendMessage(gameServerId, { message: 'No one has bought any tickets. The lottery has been cancelled.', }); return; } if (tickets.length === 1) { await takaro.gameserver.gameServerControllerSendMessage(gameServerId, { message: 'Only one person has bought a ticket. The lottery has been cancelled.', }); const amount = parseInt(JSON.parse(tickets[0].value).amount, 10) * ticketCost; await refundPlayer(takaro, gameServerId, tickets[0].playerId, amount, currencyName); return; } const totalPrize = getTotalPrize(tickets, ticketCost, mod.userConfig.profitMargin); const { name: winnerName, playerId } = await drawWinner(takaro, gameServerId, tickets); await takaro.gameserver.gameServerControllerSendMessage(gameServerId, { message: 'The lottery raffle is about to start!', }); await takaro.gameserver.gameServerControllerSendMessage(gameServerId, { message: 'drumrolls please...' }); await takaro.gameserver.gameServerControllerSendMessage(gameServerId, { message: 'The winner is...' }); await takaro.playerOnGameserver.playerOnGameServerControllerAddCurrency(gameServerId, playerId, { currency: totalPrize, reason: 'Lottery prize', }); await takaro.gameserver.gameServerControllerSendMessage(gameServerId, { message: `${winnerName}! Congratulations! You have won ${totalPrize} ${currencyName}!`, }); } finally { await cleanUp(takaro, tickets); } } await main(); //# sourceMappingURL=drawLottery.js.map
Permissions 2
Roles you can grant to decide who may use what.
-
Buy Lottery Tickets
Allows the player to buy lottery tickets.
-
View Lottery Tickets
Allows the player to view his lottery tickets.