From 81e3b5f5ba8571399e103ffd1efbfd3e6b8b4004 Mon Sep 17 00:00:00 2001 From: liyunze <50455574+liyunze-coding@users.noreply.github.com> Date: Fri, 25 Oct 2024 19:47:40 +1100 Subject: [PATCH] ver 3.3 --- configs.js | 4 +- scripts/defaultConfigs.js | 13 ++++-- scripts/processCommand.js | 14 +----- scripts/taskList.js | 91 +++++++++++++++++++++++---------------- 4 files changed, 67 insertions(+), 55 deletions(-) diff --git a/configs.js b/configs.js index ec7f28d..c3dd284 100644 --- a/configs.js +++ b/configs.js @@ -11,7 +11,7 @@ const configs = (function () { taskSeparator: [";", ","], // array of strings openQuote: '"', // string closeQuote: '"', // string - testTasks: true, // true or false + testTasks: false, // true or false headerGoogleFont: true, // true: use google font, false: use system font taskGoogleFont: true, // true: use google font, false: use system font displayTaskCount: true, // true or false @@ -334,6 +334,8 @@ const configs = (function () { nowTask: "{taskName} {task} is now the task you are working on, {user}!", taskLogged: "Task(s) {task} has been logged, {user}!", + taskAlreadyCompleted: + "Looks like you already completed that {taskName} {task} up there {user}!", // Grouped by task progression responses taskNext: diff --git a/scripts/defaultConfigs.js b/scripts/defaultConfigs.js index 00080e0..4eb5bb1 100644 --- a/scripts/defaultConfigs.js +++ b/scripts/defaultConfigs.js @@ -129,7 +129,6 @@ const defaultConfigs = (function () { "!task", "!add", "!todo", - "!tasks", "!a", ], @@ -197,7 +196,6 @@ const defaultConfigs = (function () { "!next", "!n", ], - logTaskCommands: ["!log", "!tasklog", "!logtask", "!logtasks"], // focus task commands - please add commands in the exact format @@ -279,7 +277,14 @@ const defaultConfigs = (function () { resetTaskMasterCommands: ["!resettaskmaster"], // Help commands - please add commands in the exact format - helpCommands: ["!taskh", "!taskhelp", "!htask", "!helptask", "!help"], + helpCommands: [ + "!taskh", + "!taskhelp", + "!htask", + "!helptask", + "!tasks", + "!help", + ], // Admin clear all except streamer - please add commands following the exact format adminClearNotStreamerCommands: [ @@ -329,6 +334,8 @@ const defaultConfigs = (function () { nowTask: "{taskName} {task} is now the task you are working on, {user}!", taskLogged: "Task(s) {task} has been logged, {user}!", + taskAlreadyCompleted: + "Looks like you already completed that {taskName} {task} up there {user}!", // Grouped by task progression responses taskNext: diff --git a/scripts/processCommand.js b/scripts/processCommand.js index 855aeea..16c6846 100644 --- a/scripts/processCommand.js +++ b/scripts/processCommand.js @@ -170,7 +170,7 @@ async function processCommand(user, command, message, flags, extra) { } // task logged - let loggedResponse = getResponse("taskLogged"); + let loggedResponse = await getResponse("taskLogged"); params.task = `${openQuote}${logRequest.body.task}${closeQuote}`; @@ -179,18 +179,6 @@ async function processCommand(user, command, message, flags, extra) { } await respond(loggedResponse, params); - - // let addRequest = await addTask(user, extra.userColor, message); - - // if (addRequest.status !== 200) { - // respond(addRequest.body.error, params); - // return; - // } - - // let addedResponse = getResponse("taskAdded"); - // params.task = `${openQuote}${addRequest.body.task}${closeQuote}`; - - // respond(addedResponse, params); } else if (getCommand("focusTaskCommands").includes(command)) { let focusRequest = await focusTask(user, message); diff --git a/scripts/taskList.js b/scripts/taskList.js index f6971fd..31a8644 100644 --- a/scripts/taskList.js +++ b/scripts/taskList.js @@ -851,13 +851,16 @@ async function _addTask(username, userColor, task, oneTaskOnly = false) { .split(taskSeparator.find((char) => task.includes(char))) .map((t) => t.trim()); - tasksToAdd.forEach((t) => { + for (const t of tasksToAdd) { if ( - (t && - !tasks[username].todos.find((task) => task.text === t) && - !isInt(t) && - !getSetting("taskCharacterLimitEnabled")) || - t.length <= getSetting("taskCharacterLimit") + t && + !tasks[username].todos.find( + (task) => task.text === t && !task.completed + ) && + !isInt(t) && + t.trim() !== "" && + (!getSetting("taskCharacterLimitEnabled") || + t.length <= getSetting("taskCharacterLimit")) ) { tasks[username].todos.push({ text: t, @@ -869,21 +872,16 @@ async function _addTask(username, userColor, task, oneTaskOnly = false) { addedTaskIndices.push({ index: index, task: t }); // store index of added task taskListMemory.totalTaskCount++; } else { - console.log( - !tasks[username].todos.find((task) => task.text === t) - ); - console.log(!isInt(t)); - console.log(!getSetting("taskCharacterLimitEnabled")); - console.log(task.length <= getSetting("taskCharacterLimit")); tasksFailedToAdd.push(t); } - }); + } await DBHandler.set("tasks", tasks); } else { - const taskExists = tasks[username].todos.find( - (t) => t.text.toLowerCase() === task.toLowerCase() + const taskExistsAndIncomplete = tasks[username].todos.find( + (t) => t.text.toLowerCase() === task.toLowerCase() && !t.done ); + const taskIsInvalid = !task || !task.trim() || @@ -898,7 +896,7 @@ async function _addTask(username, userColor, task, oneTaskOnly = false) { (await incompleteTasksCount(username)) >= getSetting("limit") && getSetting("enableLimit"); - if (taskExists) { + if (taskExistsAndIncomplete) { return createErrorResponse( `@${username} already has this task`, getResponse("duplicateTask") @@ -1676,15 +1674,10 @@ async function markTaskDone(username, task) { for (const t of tasksToMarkDone) { let index = tasks[username].todos.findIndex( - (task) => task.text.toLowerCase() === t.toLowerCase() + (task) => + task.text.toLowerCase() === t.toLowerCase() && !task.done ); - // check if task is already marked done - if (index !== -1 && tasks[username].todos[index].done) { - tasksFailedToComplete.push(t); - continue; - } - if (index === -1) { if (isInt(t)) { index = parseInt(t) - 1; // ACTUAL INDEX @@ -1748,7 +1741,7 @@ async function markTaskDone(username, task) { // if there's no comma in the task (single task) let index = tasks[username].todos.findIndex( - (t) => t.text.toLowerCase() === task.toLowerCase() + (t) => t.text.toLowerCase() === task.toLowerCase() && !task.done ); // is there a task with focus on? @@ -1777,6 +1770,7 @@ async function markTaskDone(username, task) { }; } + // index out of range if (index < 0 || index > tasks[username].todos.length - 1) { return { status: 1, @@ -2175,19 +2169,30 @@ async function logTask(username, userColor, task) { }; } - const taskExists = tasks[username].todos.find( - (t) => t.text.toLowerCase() === task.toLowerCase() + const taskExistsAndIncomplete = tasks[username].todos.find( + (t) => t.text.toLowerCase() === task.toLowerCase() && !t.done + ); + + const taskExistsAndCompleted = tasks[username].todos.find( + (t) => t.text.toLowerCase() === task.toLowerCase() && t.done ); const taskIsInvalid = !task || !task.trim() || task.toLowerCase() === "all" || isInt(task); - if (taskExists) { + if (taskExistsAndIncomplete) { return createErrorResponse( `@${username} already has this task`, getResponse("duplicateTask") ); } + if (taskExistsAndCompleted) { + return createErrorResponse( + `@${username} already has this task completed`, + getResponse("taskAlreadyCompleted") + ); + } + if (taskIsInvalid) { return createErrorResponse( `@${username} empty task or reserved keyword used`, @@ -2195,45 +2200,55 @@ async function logTask(username, userColor, task) { ); } - const tasksToAdd = taskSeparator.some((char) => task.includes(char)) + const tasksToLog = taskSeparator.some((char) => task.includes(char)) ? task .split(taskSeparator.find((char) => task.includes(char))) .map((t) => t.trim()) : [task]; - let tasksFailedToAdd = []; - let addedTaskIndices = []; // new array to store indices of added tasks - tasksToAdd.forEach((t) => { + let tasksFailedToLog = []; + let loggedTaskIndices = []; // new array to store indices of added tasks + + for (const t of tasksToLog) { if ( t && !tasks[username].todos.find((task) => task.text === t) && - !isInt(t) + !isInt(t) && + t.trim() !== "" && + (!getSetting("taskCharacterLimitEnabled") || + t.length <= getSetting("taskCharacterLimit")) ) { tasks[username].todos.push({ text: t, done: true, focus: false }); + // increment count + await addDoneCount(username, 1); + let index = tasks[username].todos.length - 1; - addedTaskIndices.push({ index: index, task: t }); // store index of added task + console.log({ index: index, task: t }); + loggedTaskIndices.push({ index: index, task: t }); // store index of added task + console.log(loggedTaskIndices); + taskListMemory.totalTaskCount++; } else { - tasksFailedToAdd.push(t); + tasksFailedToLog.push(t); } - }); + } await DBHandler.set("tasks", tasks); if (!scrolling) { await renderTaskListToDOM(); } - let displayedTasksAdded = addedTaskIndices.map((t) => { + let displayedTasksLogged = loggedTaskIndices.map((t) => { return `(${t.index + 1}) ${t.task}`; }); return { status: 200, body: { - task: displayedTasksAdded.join(`${closeQuote}, ${openQuote}`), + task: displayedTasksLogged.join(`${closeQuote}, ${openQuote}`), tasksFailedToLog: - tasksFailedToAdd.join(`${closeQuote}, ${openQuote}`) || "", + tasksFailedToLog.join(`${closeQuote}, ${openQuote}`) || "", }, }; }