Compare commits

...

2 Commits

Author SHA1 Message Date
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
3 changed files with 50 additions and 5 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "schat2-clive",
"version": "1.27.1",
"version": "1.27.2",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "schat2-clive",
"version": "1.27.1",
"version": "1.27.2",
"license": "ISC",
"dependencies": {
"better-sqlite3": "^8.3.0",

View File

@ -1,6 +1,6 @@
{
"name": "schat2-clive",
"version": "1.27.1",
"version": "1.27.2",
"description": "Game host for SChat 2-powered chat sites",
"main": "src/app.js",
"scripts": {

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);
}
function solve(calculation, context) {
function calc(calculation, context) {
try {
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();
@ -347,6 +347,46 @@ 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;
}
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: ${bestSolution.solution}`, context.room.id);
return;
}
context.sendMessage(`I could not find an exact solution for ${numbers.join(' ')} = ${target}. My closest solution is: ${bestSolution.solution} = ${bestSolution.answer}`, context.room.id);
} catch (error) {
console.log(error);
}
}
function onCommand(args, context) {
if (context.subcommand === 'stop') {
stop(context, true);
@ -358,7 +398,12 @@ function onCommand(args, context) {
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
return;
}