"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var lottery_exports = {};
__export(lottery_exports, {
commands: () => commands,
pages: () => pages
});
module.exports = __toCommonJS(lottery_exports);
var import_lib = require("../../lib");
const LOTTERY_FILE = "config/chat-plugins/lottery.json";
const lotteriesContents = (0, import_lib.FS)(LOTTERY_FILE).readIfExistsSync();
const lotteries = lotteriesContents ? Object.assign(/* @__PURE__ */ Object.create(null), JSON.parse(lotteriesContents)) : /* @__PURE__ */ Object.create(null);
function createLottery(roomid, maxWinners, name, markup) {
if (lotteries[roomid] && !lotteries[roomid].running) {
delete lotteries[roomid];
}
const lottery = lotteries[roomid];
lotteries[roomid] = {
maxWinners,
name,
markup,
participants: lottery?.participants || /* @__PURE__ */ Object.create(null),
winners: lottery?.winners || [],
running: true
};
writeLotteries();
}
function writeLotteries() {
for (const roomid of Object.keys(lotteries)) {
if (!Rooms.get(roomid)) {
delete lotteries[roomid];
}
}
(0, import_lib.FS)(LOTTERY_FILE).writeUpdate(() => JSON.stringify(lotteries));
}
function destroyLottery(roomid) {
delete lotteries[roomid];
writeLotteries();
}
function endLottery(roomid, winners) {
const lottery = lotteries[roomid];
if (!lottery)
return;
lottery.winners = winners;
lottery.running = false;
Object.freeze(lottery);
writeLotteries();
}
function isSignedUp(roomid, user) {
const lottery = lotteries[roomid];
if (!lottery)
return;
const participants = lottery.participants;
const participantNames = Object.values(participants).map(toID);
if (participantNames.includes(user.id))
return true;
if (Config.noipchecks)
return false;
return !!participants[user.latestIp];
}
function addUserToLottery(roomid, user) {
const lottery = lotteries[roomid];
if (!lottery)
return;
const participants = lottery.participants;
if (!isSignedUp(roomid, user)) {
participants[user.latestIp] = user.name;
writeLotteries();
return true;
}
return false;
}
function removeUserFromLottery(roomid, user) {
const lottery = lotteries[roomid];
if (!lottery)
return;
const participants = lottery.participants;
for (const [ip, participant] of Object.entries(participants)) {
if (toID(participant) === user.id || ip === user.latestIp) {
delete participants[ip];
writeLotteries();
return true;
}
}
return false;
}
function getWinnersInLottery(roomid) {
const lottery = lotteries[roomid];
if (!lottery)
return;
const winners = [];
const participants = Object.values(lottery.participants);
for (let i = 0; i < lottery.maxWinners; i++) {
const randomIdx = participants.length * Math.random() << 0;
const winner = participants[randomIdx];
winners.push(winner);
participants.splice(randomIdx, 1);
}
return winners;
}
const commands = {
lottery: {
""(target, room) {
room = this.requireRoom();
const lottery = lotteries[room.roomid];
if (!lottery) {
return this.errorReply("This room doesn't have a lottery running.");
}
return this.parse(`/join view-lottery-${room.roomid}`);
},
edit: "create",
create(target, room, user, connection, cmd) {
room = this.requireRoom();
this.checkCan("declare", null, room);
if (room.battle || !room.persist) {
return this.errorReply("This room does not support the creation of lotteries.");
}
const lottery = lotteries[room.roomid];
const edited = lottery?.running;
if (cmd === "edit" && !target && lottery) {
this.sendReply("Source:");
const markup2 = import_lib.Utils.html`${lottery.markup}`.replace(/\n/g, "
");
return this.sendReplyBox(`/lottery edit ${lottery.maxWinners}, ${lottery.name}, ${markup2}
`);
}
const [maxWinners, name, markup] = import_lib.Utils.splitFirst(target, ",", 2).map((val) => val.trim());
if (!(maxWinners && name && markup.length)) {
return this.errorReply("You're missing a command parameter - see /help lottery for this command's syntax.");
}
const maxWinnersNum = parseInt(maxWinners);
this.checkHTML(markup);
if (isNaN(maxWinnersNum)) {
return this.errorReply(`${maxWinners} is not a valid number.`);
}
if (maxWinnersNum < 1) {
return this.errorReply("The maximum winners should be at least 1.");
}
if (maxWinnersNum > Number.MAX_SAFE_INTEGER) {
return this.errorReply("The maximum winners number is too large, please pick a smaller number.");
}
if (name.length > 50) {
return this.errorReply("Name needs to be under 50 characters.");
}
createLottery(room.roomid, maxWinnersNum, name, markup);
this.sendReply(`The lottery was successfully ${edited ? "edited" : "created"}.`);
if (!edited) {
this.add(
import_lib.Utils.html`|raw|
This lottery has already ended. The winners are:
'; buf += '