Compare commits
14 Commits
socketio
...
6a36df3593
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a36df3593 | ||
|
|
69bc1c9e6e | ||
|
|
aeb405967b | ||
|
|
73e60b81f1 | ||
|
|
c9b985f768 | ||
|
|
cb5de62e1c | ||
|
|
b3cab90810 | ||
|
|
47484ba7e2 | ||
|
|
4017f1cd07 | ||
|
|
03dfe8e437 | ||
|
|
1a992a6026 | ||
|
|
9f8f503f13 | ||
|
|
d460ba13c5 | ||
|
|
ed5337d828 |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "schat2-clive",
|
"name": "schat2-clive",
|
||||||
"version": "1.26.12",
|
"version": "1.28.2",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "schat2-clive",
|
"name": "schat2-clive",
|
||||||
"version": "1.26.12",
|
"version": "1.28.2",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"better-sqlite3": "^8.3.0",
|
"better-sqlite3": "^8.3.0",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "schat2-clive",
|
"name": "schat2-clive",
|
||||||
"version": "1.26.12",
|
"version": "1.28.2",
|
||||||
"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": {
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
25
src/merge.js
Normal file
25
src/merge.js
Normal 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();
|
||||||
53
src/schat.js
53
src/schat.js
@@ -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');
|
||||||
|
|
||||||
@@ -144,29 +143,20 @@ async function connect(bot, games) {
|
|||||||
|
|
||||||
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 +170,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 +191,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;
|
||||||
|
|||||||
Reference in New Issue
Block a user