Compare commits

...
39 Commits
Author SHA1 Message Date
ThePendulum 3196877c37 1.29.3 2024-06-09 18:07:55 +02:00
ThePendulum ab25066936 Improved dictionary. 2024-06-09 18:07:52 +02:00
ThePendulum f384d595e4 Using regular space to join wordle letterboard. 2024-06-05 22:45:04 +02:00
ThePendulum 4ada601fb2 1.29.2 2024-06-04 23:56:52 +02:00
ThePendulum 13e0bb9a8c Improved repeat letter marking in wordle. 2024-06-04 23:56:51 +02:00
ThePendulum 055440418e Added colorless hints to IRC wordle instructions. 2024-06-04 17:31:33 +02:00
ThePendulum 352efb9147 Using correct format in IRC wordle instructions. 2024-06-04 17:25:43 +02:00
ThePendulum 1212261b21 Changed symbol for 6> faced dice. 2024-06-04 01:50:51 +02:00
ThePendulum a807cbc2aa 1.29.1 2024-06-03 22:46:19 +02:00
ThePendulum f85cac7f2f Improved wordle readability for IRC. 2024-06-03 22:46:17 +02:00
ThePendulum 402dbc3923 Mapped IRC orange to yellow instead of brown. 2024-06-03 02:17:41 +02:00
ThePendulum 54968f3fb4 Mapped IRC orange to brown. 2024-06-03 02:16:36 +02:00
ThePendulum 1228928592 Added guess count to wordle finish. 2024-06-03 02:05:02 +02:00
ThePendulum 6a35049609 Fixed orange highlight overwriting green highlight in wordle game. 2024-06-03 01:57:44 +02:00
ThePendulum a29eba70f0 Added instructions to wordle start message. 2024-06-03 01:10:07 +02:00
ThePendulum 064b4eb0d4 1.29.0 2024-06-03 01:02:33 +02:00
ThePendulum 5e396a4abe Added/implemented wordle game. 2024-06-03 01:02:30 +02:00
ThePendulum 453a3b1b42 Fixed letters game board timeout. 2024-04-02 01:09:31 +02:00
ThePendulum a1a9d698e2 Showing board 5 instead of 4 times in letters game. 2024-04-02 01:04:58 +02:00
ThePendulum 48cf433a5c 1.28.5 2024-04-02 00:56:21 +02:00
ThePendulum 61e935b925 Showing board 4 instead of 3 times in letters game. 2024-04-02 00:56:19 +02:00
ThePendulum 9a7ef61d93 1.28.4 2024-03-08 00:49:44 +01:00
ThePendulum bad5dc52f7 Added user point merging utility. 2024-03-08 00:49:37 +01:00
ThePendulum d510caa123 1.28.3 2024-02-11 17:33:21 +01:00
ThePendulum b6cc3d716b Using cookie instead of http session so we can strip Domain attribute. 2024-02-11 17:33:19 +01:00
ThePendulum 6a36df3593 1.28.2 2024-01-13 03:55:55 +01:00
ThePendulum 69bc1c9e6e Added points merge utility. 2024-01-13 03:55:46 +01:00
ThePendulum aeb405967b 1.28.1 2024-01-13 02:26:05 +01:00
ThePendulum 73e60b81f1 Added ping. 2024-01-13 02:26:03 +01:00
ThePendulum c9b985f768 1.28.0 2024-01-13 01:29:14 +01:00
ThePendulum cb5de62e1c Reverted SocketIO to ws. 2024-01-13 01:29:10 +01:00
ThePendulum b3cab90810 1.27.3 2024-01-02 16:15:39 +01:00
ThePendulum 47484ba7e2 Preventing ~solve when numbers game is in progress. 2024-01-02 16:15:37 +01:00
ThePendulum 4017f1cd07 1.27.2 2024-01-02 02:13:14 +01:00
ThePendulum 03dfe8e437 Exposing numbers game solver through command. 2024-01-02 02:13:11 +01:00
ThePendulum 1a992a6026 1.27.1 2023-12-07 22:05:53 +01:00
ThePendulum 9f8f503f13 Removed reconnect logic, handled by SocketIO. 2023-12-07 22:05:51 +01:00
ThePendulum d460ba13c5 1.27.0 2023-12-05 20:55:47 +01:00
ThePendulum ed5337d828 Fixed socket close event listener. 2023-12-05 20:55:39 +01:00
16 changed files with 369931 additions and 102298 deletions
Executable → Regular
+87904 -102249
View File
File diff suppressed because one or more lines are too long
+102260
View File
File diff suppressed because one or more lines are too long
+89675
View File
File diff suppressed because it is too large Load Diff
+25
View File
@@ -0,0 +1,25 @@
'use strict';
const fs = require('fs').promises;
const words = require('./words.json');
const dictionary = require('./dictionary_old.json');
async function init() {
const definitions = Object.fromEntries(words.map((word) => {
const normalizedWord = word.normalize('NFD').replace(/\p{Diacritic}/ug, '').toLowerCase().trim();
const definition = dictionary[normalizedWord];
const singular = normalizedWord.replace(/s$/, '');
const singularDefinition = dictionary[singular] ? `Plural of ${singular}: ${dictionary[singular]}` : null;
return [normalizedWord, definition || singularDefinition];
}));
const string = JSON.stringify(definitions, null, 4);
await fs.writeFile('./dictionary.json', string);
console.log(`Wrote ${Object.keys(definitions).length} words to ./dictionary.json`);
}
init();
+89677
View File
File diff suppressed because it is too large Load Diff
+5
View File
@@ -27,6 +27,7 @@ module.exports = {
'chat', 'chat',
'mash', 'mash',
'trivia', 'trivia',
'wordle',
'letters', 'letters',
'numbers', 'numbers',
'hunt', 'hunt',
@@ -133,6 +134,10 @@ module.exports = {
z: 1, z: 1,
}, },
}, },
wordle: {
minLength: 3,
defaultLength: 5,
},
numbers: { numbers: {
length: 6, length: 6,
timeout: 90, timeout: 90,
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "schat2-clive", "name": "schat2-clive",
"version": "1.26.12", "version": "1.29.3",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "schat2-clive", "name": "schat2-clive",
"version": "1.26.12", "version": "1.29.3",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"better-sqlite3": "^8.3.0", "better-sqlite3": "^8.3.0",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "schat2-clive", "name": "schat2-clive",
"version": "1.26.12", "version": "1.29.3",
"description": "Game host for SChat 2-powered chat sites", "description": "Game host for SChat 2-powered chat sites",
"main": "src/app.js", "main": "src/app.js",
"scripts": { "scripts": {
+1 -1
View File
@@ -37,7 +37,7 @@ function onCommand(args, context) {
return `${style.yellow(`(${coinFaces[result - 1]})`)} ${style.bold(result === 1 ? 'heads' : 'tails')}`; // eslint-disable-line no-irregular-whitespace return `${style.yellow(`(${coinFaces[result - 1]})`)} ${style.bold(result === 1 ? 'heads' : 'tails')}`; // eslint-disable-line no-irregular-whitespace
} }
return `${style.grey(dieFaces[result - 1] || '☐')} ${style.bold(result)}`; // eslint-disable-line no-irregular-whitespace return `${style.grey(dieFaces[result - 1] || '⬡')} ${style.bold(result)}`; // eslint-disable-line no-irregular-whitespace
}); });
context.sendMessage(results.join(style.grey(' | ')), context.room.id, { label: type }); context.sendMessage(results.join(style.grey(' | ')), context.room.id, { label: type });
+12 -5
View File
@@ -91,13 +91,20 @@ async function play(context) {
context.sendMessage(`${getBoard(context)} You have ${style.bold(style.green(settings.timeout))} seconds, let's start!`, context.room.id); context.sendMessage(`${getBoard(context)} You have ${style.bold(style.green(settings.timeout))} seconds, let's start!`, context.room.id);
try { try {
await timers.setTimeout((settings.timeout / 3) * 1000, null, { signal: game.ac.signal }); await timers.setTimeout((settings.timeout / 5) * 1000, null, { signal: game.ac.signal });
context.sendMessage(`${getBoard(context)} You have ${style.bold(style.green(`${Math.round((settings.timeout / 3) * 2)} seconds`))} left`, context.room.id);
await timers.setTimeout((settings.timeout / 3) * 1000, null, { signal: game.ac.signal }); context.sendMessage(`${getBoard(context)} You have ${style.bold(style.green(`${Math.round((settings.timeout / 5) * 4)} seconds`))} left`, context.room.id);
context.sendMessage(`${getBoard(context)} You have ${style.bold(style.green(`${Math.round(settings.timeout / 3)} seconds`))} left`, context.room.id); await timers.setTimeout((settings.timeout / 5) * 1000, null, { signal: game.ac.signal });
context.sendMessage(`${getBoard(context)} You have ${style.bold(style.green(`${Math.round((settings.timeout / 5) * 3)} seconds`))} left`, context.room.id);
await timers.setTimeout((settings.timeout / 5) * 1000, null, { signal: game.ac.signal });
context.sendMessage(`${getBoard(context)} You have ${style.bold(style.green(`${Math.round((settings.timeout / 5) * 2)} seconds`))} left`, context.room.id);
await timers.setTimeout((settings.timeout / 5) * 1000, null, { signal: game.ac.signal });
context.sendMessage(`${getBoard(context)} You have ${style.bold(style.green(`${Math.round(settings.timeout / 5)} seconds`))} left`, context.room.id);
await timers.setTimeout((settings.timeout / 5) * 1000, null, { signal: game.ac.signal });
await timers.setTimeout((settings.timeout / 3) * 1000, null, { signal: game.ac.signal });
stop(context); stop(context);
} catch (error) { } catch (error) {
// abort expected, probably not an error // abort expected, probably not an error
+52 -2
View File
@@ -336,7 +336,7 @@ function start(context, numbers) {
context.sendMessage('Let\'s play the numbers! Would you like a large number or a small one?', context.room.id); context.sendMessage('Let\'s play the numbers! Would you like a large number or a small one?', context.room.id);
} }
function solve(calculation, context) { function calc(calculation, context) {
try { try {
const parsed = math.parse(calculation.replace(/`/g, '')); // backticks may be used to prevent the expression from being expanded by Markdown in SChat const parsed = math.parse(calculation.replace(/`/g, '')); // backticks may be used to prevent the expression from being expanded by Markdown in SChat
const answer = parsed.evaluate(); const answer = parsed.evaluate();
@@ -347,6 +347,51 @@ function solve(calculation, context) {
} }
} }
async function solve(equation, context) {
const [numberString, targetString] = equation.split('=');
const numbers = numberString?.split(' ').map((string) => Number(string)).filter(Boolean);
const target = Number(targetString);
if (!numberString || !target) {
context.sendMessage('The input should be in the form 1 2 3 4 5 6 7 = 10', context.room.id);
return;
}
if (numbers.length < 2 || numbers.length > 7) {
context.sendMessage('The selection should contain at least 2 and at most 7 numbers', context.room.id);
return;
}
if (target < 100 || target > 999) {
context.sendMessage('The target must be a number between 100 and 999', context.room.id);
return;
}
if (Array.from(games.entries()).some(([roomId, game]) => roomId === context.room.id || game.target === target)) {
context.sendMessage('Nice try! Please wait for this numbers round to end :)', context.room.id);
return;
}
try {
const solutions = await solveAll(numbers, target, 3);
const bestSolution = solutions.reduce((closest, solution) => (!closest || Math.abs(target - solution.answer) < Math.abs(target - closest.answer) ? solution : closest), null);
if (!bestSolution) {
context.sendMessage(`I could not find a solution for ${numbers.join(' ')} = ${target} :(`, context.room.id);
return;
}
if (bestSolution.answer === target) {
context.sendMessage(`My best solution for ${numbers.join(' ')} = ${target} is: ${style.bold(bestSolution.solution)}`, context.room.id);
return;
}
context.sendMessage(`I could not find an exact solution for ${numbers.join(' ')} = ${target}. My closest solution is: ${style.bold(bestSolution.solution)} = ${style.italic(bestSolution.answer)}`, context.room.id);
} catch (error) {
console.log(error);
}
}
function onCommand(args, context) { function onCommand(args, context) {
if (context.subcommand === 'stop') { if (context.subcommand === 'stop') {
stop(context, true); stop(context, true);
@@ -358,7 +403,12 @@ function onCommand(args, context) {
return; return;
} }
if (['calculate', 'calc', 'solve'].includes(context.subcommand || context.command)) { if (['calculate', 'calc'].includes(context.subcommand || context.command)) {
calc(args.join(' '), context); // two from the top, four from the bottom, please Rachel
return;
}
if (['solve'].includes(context.subcommand || context.command)) {
solve(args.join(' '), context); // two from the top, four from the bottom, please Rachel solve(args.join(' '), context); // two from the top, four from the bottom, please Rachel
return; return;
} }
+205
View File
@@ -0,0 +1,205 @@
'use strict';
const config = require('config');
const crypto = require('crypto');
const style = require('../utils/style');
const words = require('../../assets/mash-words.json');
const settings = { ...config.wordle };
const wordles = new Map();
const alphabet = Array.from({ length: 26 }, (value, index) => String.fromCharCode(65 + index));
function getBoard(letters, showLetters, context) {
const wordle = wordles.get(context.room.id);
const prefix = config.platform === 'irc' ? '' : style.grey(style.code('['));
const middle = letters.map((letter) => {
if (letter === null) {
return config.platform === 'irc'
? style.bgsilver(' ? ')
: style.code(' '); // em space, U+2003, charcode 8195
}
if (letter[1] === true) {
return config.platform === 'irc'
? style.bggreen(` ${letter[0].toUpperCase()}${style.green('*')}`)
: style.green(style.bold(style.code(letter[0].toUpperCase())));
}
if (letter[1] === false) {
return config.platform === 'irc'
? style.bgyellow(` ${letter[0].toUpperCase()}${style.yellow('?')}`)
: style.orange(style.bold(style.code(letter[0].toUpperCase())));
}
return config.platform === 'irc'
? style.bgsilver(` ${letter[0].toUpperCase()} `)
: style.grey(style.bold(style.code(letter[0].toUpperCase())));
}).join(config.platform === 'irc' ? '' : style.grey(style.code('|')));
const suffix = config.platform === 'irc' ? '' : `${style.grey(style.code(']'))}`;
if (showLetters) {
const letterBoard = Array.from(wordle.letters).map(([letter, state]) => {
if (state === true) {
return config.platform === 'irc'
? style.bggreen(`${letter}${style.green('*')}`)
: style.green(style.bold(letter));
}
if (state === false) {
return config.platform === 'irc'
? style.bgyellow(`${letter}${style.yellow('?')}`)
: style.orange(style.bold(letter));
}
return config.platform === 'irc'
? letter
: style.grey(letter);
}).join(' ');
return `${prefix}${middle}${suffix} Letters: ${letterBoard}`; // eslint-disable-line no-irregular-whitespace
}
return `${prefix}${middle}${suffix}`;
}
function start(length = settings.defaultLength, context) {
const wordPool = words[length];
if (length < settings.minLength) {
context.sendMessage(`Wordle must be at least ${settings.minLength} letters long.`, context.room.id);
return;
}
if (!wordPool) {
context.sendMessage(`No words with ${length} letters available.`, context.room.id);
return;
}
const wordList = Object.values(wordPool).flat();
const word = wordList[crypto.randomInt(wordList.length)];
wordles.set(context.room.id, {
word: word.word.toUpperCase(),
wordList,
definitions: word.definitions,
letters: new Map(alphabet.map((letter) => [letter, null])),
guesses: [],
});
if (config.platform === 'irc') {
context.sendMessage(`${getBoard(Array.from({ length }, () => null), false, context)} guess the word with ${config.prefix}w [word]! ${style.bgsilver(' X ')} not in word; ${style.bgyellow(` X${style.yellow('?')}`)} wrong position; ${style.bggreen(` X${style.green('*')}`)} correct position.`, context.room.id); // eslint-disable-line no-irregular-whitespace
} else {
context.sendMessage(`${getBoard(Array.from({ length }, () => null), false, context)} guess the word with ${config.prefix}w [word]! ${style.grey(style.bold('X'))} not in word; ${style.orange(style.bold('X'))} wrong position; ${style.green(style.bold('X'))} correct position.`, context.room.id);
}
context.logger.info(`Wordle started: ${word.word.toUpperCase()}`);
}
function play(guess, context) {
const wordle = wordles.get(context.room.id);
if (!wordle) {
context.sendMessage(`There's no wordle going on. Start one with ${config.prefix}wordle [length]!`, context.room.id);
return;
}
if (!guess || guess.length !== wordle.word.length) {
context.sendMessage(`Your guess needs to be ${wordle.word.length} letters.`, context.room.id);
return;
}
if (!wordle.wordList.some((definition) => definition.word.toLowerCase() === guess.toLowerCase())) {
context.sendMessage(`The word '${guess}' is not in my dictionary.`, context.room.id);
return;
}
const upperGuess = guess.toUpperCase();
const occurrences = wordle.word.split('').reduce((acc, letter) => acc.set(letter, (acc.get(letter) || 0) + 1), new Map());
const guessLetters = upperGuess.split('');
const check = guessLetters.map((letter) => [letter, null]);
// correct
guessLetters.forEach((letter, index) => {
if (wordle.word[index] === letter) {
wordle.letters.set(letter, true);
check[index] = [letter, true];
}
});
// wrong place
guessLetters.forEach((letter, index) => {
if (wordle.word.includes(letter)) {
if (wordle.letters.get(letter) !== true) {
wordle.letters.set(letter, false);
}
const marks = check.filter(([checkLetter, status]) => checkLetter === letter && status !== null).length;
if (check[index][1] !== true && marks < occurrences.get(letter)) {
check[index] = [letter, false];
}
return;
}
wordle.letters.delete(letter);
});
wordle.guesses = wordle.guesses.concat([[context.user.username, upperGuess]]);
if (upperGuess === wordle.word) {
const points = Math.max((wordle.word.length + 1) - wordle.guesses.length, 1);
const definition = wordle.definitions[0] ? `: ${style.italic(`${wordle.definitions[0].slice(0, 100)}${wordle.definitions[0].length > 100 ? '...' : ''}`)}` : '';
context.setPoints(context.user, points);
context.sendMessage(`${getBoard(check, false, context)} is correct in ${wordle.guesses.length} guesses! ${style.bold(style.cyan(`${config.usernamePrefix}${context.user.username}`))} gets ${points} ${points > 1 ? 'points' : 'point'}. ${style.bold(wordle.word)}${definition}`, context.room.id);
wordles.delete(context.room.id);
return;
}
context.sendMessage(`${getBoard(check, true, context)}`, context.room.id);
}
function onCommand(args, context) {
const wordle = wordles.get(context.room.id);
const length = Number(args[0]) || settings.defaultLength;
if (['guess', 'w'].includes(context.command)) {
play(args[0], context);
return;
}
if (context.subcommand === 'stop') {
if (wordle) {
context.sendMessage(`The game was stopped by ${config.usernamePrefix}${context.user.username}. The word was ${style.bold(wordle.word)}.`, context.room.id);
wordles.delete(context.room.id);
return;
}
context.sendMessage(`The there is no wordle going on, ${config.usernamePrefix}${context.user.username}, start one with ${config.prefix}wordle [length]!`, context.room.id);
return;
}
if (!wordle && context.room.id) {
start(length, context);
return;
}
context.sendMessage(`There is already a ${wordle.word.length}-letter wordle going on, guess with ${config.prefix}w [word]!`, context.room.id);
}
module.exports = {
name: 'Wordle',
commands: ['wordle', 'guess', 'w'],
help: `Guess the ${settings.defaultLength}-letter word on the board. Submit a guess with ${config.prefix}w [word]. If the letter is green, it is in the correct place. If it is yellow, it is part of the word, but not in the correct place. The number of letters in the word is the highest score you can get. You lose 1 point per guess, down to 1 point.`,
onCommand,
// onMessage,
};
+43 -30
View File
@@ -3,8 +3,7 @@
const config = require('config'); const config = require('config');
const { setTimeout: delay } = require('timers/promises'); const { setTimeout: delay } = require('timers/promises');
const bhttp = require('bhttp'); const bhttp = require('bhttp');
// const WebSocket = require('ws'); const WebSocket = require('ws');
const io = require('socket.io-client');
const logger = require('simple-node-logger').createSimpleLogger(); const logger = require('simple-node-logger').createSimpleLogger();
const { argv } = require('yargs'); const { argv } = require('yargs');
@@ -18,10 +17,9 @@ const instance = process.env.NODE_APP_INSTANCE || 'main';
logger.setLevel(argv.level || 'info'); logger.setLevel(argv.level || 'info');
async function auth() { async function auth() {
const httpSession = bhttp.session();
const username = config.uniqueUsername ? `${config.user.username}-${new Date().getTime().toString().slice(-5)}` : config.user.username; const username = config.uniqueUsername ? `${config.user.username}-${new Date().getTime().toString().slice(-5)}` : config.user.username;
const res = await httpSession.post(`${config.api}/session`, { const res = await bhttp.post(`${config.api}/session`, {
...config.user, ...config.user,
username, username,
}, { }, {
@@ -36,13 +34,17 @@ async function auth() {
return { return {
user: res.body, user: res.body,
httpSession, // auth may return an explicit auth cookie domain, but we connect through the VPN domain that would break the cookie, so don't use a bhttp session and strip the domain
sessionCookie: res.headers['set-cookie'][0], sessionCookie: res.headers['set-cookie'][0].replace(/Domain=.*;/, ''),
}; };
} }
async function getWsId(httpSession) { async function getWsId(sessionCookie) {
const res = await httpSession.get(`${config.api}/socket`); const res = await bhttp.get(`${config.api}/socket`, {
headers: {
cookie: sessionCookie,
},
});
if (res.statusCode !== 200) { if (res.statusCode !== 200) {
throw new Error(`Failed to retrieve WebSocket ID: ${res.body.toString()}`); throw new Error(`Failed to retrieve WebSocket ID: ${res.body.toString()}`);
@@ -136,37 +138,27 @@ async function connect(bot, games) {
socket.connect = async () => { socket.connect = async () => {
try { try {
const { user, httpSession, sessionCookie } = await auth(); const { user, sessionCookie } = await auth();
const wsCreds = await getWsId(httpSession); const wsCreds = await getWsId(sessionCookie);
bot.user = user; bot.user = user;
bot.httpSession = httpSession;
logger.info(`Attempting to connect to ${config.socket}`); logger.info(`Attempting to connect to ${config.socket}`);
const { origin, pathname } = new URL(config.socket); socket.ws = new WebSocket(`${config.socket}?${new URLSearchParams({ v: wsCreds.wsId, t: wsCreds.timestamp }).toString()}`, [], {
headers: {
socket.io = io(origin, {
transports: ['websocket'],
path: pathname,
query: {
v: wsCreds.wsId,
t: wsCreds.timestamp,
},
extraHeaders: {
cookie: sessionCookie, cookie: sessionCookie,
}, },
}); });
socket.io.on('connect', () => { socket.ws.on('message', async (msgData) => {
logger.info(`Connected to ${config.socket}`); const msg = msgData.toString();
});
socket.io.on('connect_error', (error) => { if (typeof msg === 'string' && msg.includes('pong')) {
logger.info(`Failed to connect to ${config.socket}: ${error}`); logger.debug(`Received pong ${msg.split(':')[1]}`);
}); return;
}
socket.io.on('_', async (msg) => {
const [domain, data] = JSON.parse(msg); const [domain, data] = JSON.parse(msg);
logger.debug(`Received ${domain}: ${JSON.stringify(data)}`); logger.debug(`Received ${domain}: ${JSON.stringify(data)}`);
@@ -180,12 +172,18 @@ async function connect(bot, games) {
} }
}); });
socket.io.on('close', async (info) => { socket.ws.on('close', async (info) => {
logger.error(`WebSocket closed, reconnecting in ${config.reconnectDelay} seconds: ${info}`); logger.error(`WebSocket closed, reconnecting in ${config.reconnectDelay} seconds: ${info}`);
await delay(config.reconnectDelay * 1000); await delay(config.reconnectDelay * 1000);
socket.connect(); socket.connect();
}); });
socket.ws.on('error', async (error) => {
logger.error(`WebSocket error: ${error.message}`);
});
logger.info(`Connected to ${config.socket}`);
} catch (error) { } catch (error) {
logger.error(`Failed to connect, retrying in ${config.reconnectDelay} seconds: ${error.message}`); logger.error(`Failed to connect, retrying in ${config.reconnectDelay} seconds: ${error.message}`);
@@ -195,9 +193,24 @@ async function connect(bot, games) {
}; };
socket.transmit = (domain, data) => { socket.transmit = (domain, data) => {
socket.io.emit('_', JSON.stringify([domain, data])); socket.ws.send(JSON.stringify([domain, data]));
}; };
function ping() {
setTimeout(() => {
if (socket.ws && socket.ws?.readyState === socket.ws?.OPEN) {
const now = Date.now();
socket.ws.send(`ping:${now}`);
logger.debug(`Sent ping ${now}`);
}
ping();
}, 10000);
}
ping();
socket.connect(); socket.connect();
return socket; return socket;
+25
View File
@@ -0,0 +1,25 @@
const path = require('path');
const args = require('yargs').argv;
function init() {
const scoresA = require(path.resolve(__dirname, args.a));
const scoresB = require(path.resolve(__dirname, args.b));
const sum = {};
[scoresA, scoresB].forEach((record) => {
Object.entries(record).forEach(([game, scores]) => {
if (!sum[game]) {
sum[game] = {};
}
Object.entries(scores).forEach(([id, score]) => {
sum[game][id] = (sum[game][id] || 0) + score;
});
});
});
console.log(JSON.stringify(sum, null, 4));
}
init();
+35
View File
@@ -0,0 +1,35 @@
const path = require('path');
const args = require('yargs').argv;
const fs = require('fs').promises;
async function init() {
await fs.copyFile(args.file, `${path.basename(args.file, path.extname(args.file))}_backup_${Date.now()}.json`);
const filepath = path.join(process.cwd(), args.file);
const points = require(filepath); // eslint-disable-line
Object.entries(points).forEach(([game, scores]) => {
const originKey = Object.keys(scores).find((userKey) => userKey.includes(`:${args.origin}`));
const originScore = scores[originKey];
const targetKey = Object.keys(scores).find((userKey) => userKey.includes(`:${args.target}`));
const targetScore = scores[targetKey];
if (typeof originScore === 'undefined' || typeof targetScore === 'undefined') {
return;
}
const totalScore = targetScore + originScore;
points[game][targetKey] = targetScore + originScore;
delete points[game][originKey];
console.log(`${game} ${targetScore} (${args.target}) + ${originScore} (${args.origin}) = ${totalScore}`);
});
await fs.writeFile(filepath, JSON.stringify(points, null, 4));
console.log(`Saved ${filepath}`);
}
init();
+1
View File
@@ -42,6 +42,7 @@ const styleMethods = (() => {
return { return {
...styles, ...styles,
forest: styles.green, forest: styles.green,
orange: styles.yellow,
code: bypass, code: bypass,
}; };
} }