From 3a2d0426c7f4d1251411b20737b72f8b858022ad Mon Sep 17 00:00:00 2001 From: raeleus Date: Sun, 6 Oct 2024 21:48:39 -0700 Subject: [PATCH] Added #removeenemy. Fixed minor issues with removals of notes and locations. --- Input.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++--------- Output.js | 6 ++--- 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/Input.js b/Input.js index 0d1bcc7..7356d30 100644 --- a/Input.js +++ b/Input.js @@ -201,7 +201,8 @@ const modifier = (text) => { if (text == null) text = processCommandSynonyms(command, commandName, versionSynonyms, doVersion) if (text == null) text = processCommandSynonyms(command, commandName, setAcSynonyms, doSetAc) if (text == null) text = processCommandSynonyms(command, commandName, encounterSynonyms, doEncounter) - if (text == null) text = processCommandSynonyms(command, commandName, showEnemiesSynonyms, doShowEnemies) + if (text == null) text = processCommandSynonyms(command, commandName, showEnemiesSynonyms, doShowEnemies) + if (text == null) text = processCommandSynonyms(command, commandName, removeEnemySynonyms, doRemoveEnemy) if (text == null) text = processCommandSynonyms(command, commandName, helpSynonyms, doHelp) if (text == null) { var character = getCharacter() @@ -1334,7 +1335,7 @@ function doGoNorth(command) { if (arg0 == null) arg0 = 1 else { if (isNaN(arg0)) { - state.show = none + state.show = "none" return "\n[Error: Expected a number. See #help]\n" } arg0 = parseInt(arg0) @@ -1349,7 +1350,7 @@ function doGoSouth(command) { if (arg0 == null) arg0 = 1 else { if (isNaN(arg0)) { - state.show = none + state.show = "none" return "\n[Error: Expected a number. See #help]\n" } arg0 = parseInt(arg0) @@ -1364,7 +1365,7 @@ function doGoEast(command) { if (arg0 == null) arg0 = 1 else { if (isNaN(arg0)) { - state.show = none + state.show = "none" return "\n[Error: Expected a number. See #help]\n" } arg0 = parseInt(arg0) @@ -1379,7 +1380,7 @@ function doGoWest(command) { if (arg0 == null) arg0 = 1 else { if (isNaN(arg0)) { - state.show = none + state.show = "none" return "\n[Error: Expected a number. See #help]\n" } arg0 = parseInt(arg0) @@ -1413,7 +1414,7 @@ function doGoToLocation(command) { var location var locationName = getArgumentRemainder(command, locationArgIndex) if (locationName == null && locationArgIndex == 0) { - state.show = none + state.show = "none" return "\n[Error: Not enough parameters. See #help]\n" } @@ -1421,7 +1422,7 @@ function doGoToLocation(command) { arg0 = parseInt(arg0) - 1 if (arg0 < 0 || arg0 >= state.locations.length) { - state.show = none + state.show = "none" return "\n[Error: Incorrect location number. See #help]\n" } @@ -1496,7 +1497,7 @@ function doRemoveLocation(command) { if (/\d+\D+(\d+\D*)+/gi.test(arg0)) { - var list = arg0.split(/\D/) + var list = arg0.split(/\D+/) list.sort(function(a, b) { return b - a; }); @@ -1514,7 +1515,7 @@ function doRemoveLocation(command) { text += `[The location ${toTitleCase(location.name)} has been removed]\n` }) - state.show = none + state.show = "none" return text } @@ -1559,7 +1560,7 @@ function doEncounter(command) { if (encounter.enemies.length > 0) { state.prefix += "You encounter the following enemies:\n" for (var enemy of encounter.enemies) { - state.prefix += `${enemy.name} (Health: ${enemy.health} AC: ${enemy.ac} Initiative: ${enemy.initiative})\n` + state.prefix += `${toTitleCase(enemy.name)} (Health: ${enemy.health} AC: ${enemy.ac} Initiative: ${enemy.initiative})\n` } } @@ -1574,6 +1575,56 @@ function doShowEnemies(command) { return " " } +function doRemoveEnemy(command) { + var arg0 = getArgumentRemainder(command, 0) + if (arg0 == null) { + state.show = "none" + return "\n[Error: Not enough parameters. See #help]\n" + } + + if (/\d+\D+(\d+\D*)+/gi.test(arg0)) { + + var list = arg0.split(/\D+/) + list.sort(function(a, b) { + return b - a; + }); + + var text = "\n" + list.forEach(x => { + var num = parseInt(x) - 1 + if (num >= state.enemies.length) { + state.show = "none" + return `\n[Error: Enemy ${x} does not exist. See #showenemies.]\n` + } + + var enemy = state.enemies[num] + state.enemies.splice(num, 1) + text += `[The enemy ${toTitleCase(enemy.name)} has been removed]\n` + }) + + state.show = "none" + return text + } + + var enemy + if (isNaN(arg0)) arg0 = state.enemies.findIndex(x => x.name.toLowerCase() == arg0.toLowerCase()) + else arg0-- + + if (arg0 == -1) { + state.show = "none" + return "\n[Error: Enemy not found. See #showenemies]\n" + } else if (arg0 >= state.enemies.length || arg0 < 0) { + state.show = "none" + return "\n[Error: Location number out of bounds. See #showenemies]\n" + } else { + enemy = state.enemies[arg0] + state.enemies.splice(arg0, 1) + } + + state.show = "none" + return `\n[The enemy ${toTitleCase(enemy.name)} has been removed]\n` +} + function doTake(command) { var arg0 = getArgument(command, 0) if (arg0 == null) { @@ -2135,7 +2186,7 @@ function doEraseNote(command) { var arg0 = getArgumentRemainder(command, 0) if (arg0 == null) arg0 = 1 - var list = arg0.split(/\D/) + var list = arg0.split(/\D+/) list.sort(function(a, b) { return b - a; }); diff --git a/Output.js b/Output.js index c23a528..41351f0 100644 --- a/Output.js +++ b/Output.js @@ -225,7 +225,7 @@ const modifier = (text) => { } else { var index = 0 for (var enemy of state.enemies) { - text += `${++index}. ${enemy.name} (Health: ${enemy.health} AC: ${enemy.ac} Initiative: ${enemy.initiative})\n` + text += `${++index}. ${toTitleCase(enemy.name)} (Health: ${enemy.health} AC: ${enemy.ac} Initiative: ${enemy.initiative})\n` } } @@ -368,7 +368,7 @@ const modifier = (text) => { text += "\n#addenemy (ac) (damage_modifier) name" text += "\n Adds the specified enemy to the list of enemies. It will automatically be assigned an inititiative. You can specify an armor class or both an armor class and damage modifier." text += "\n#removeenemy name or index" - text += "\n Removes the enemy as specified by the name or index. " + text += "\n Removes the enemy as specified by the name or index. To delete multiple enemies, type the numbers with spaces or commas between them. This is safer than calling #removenote multiple times because the numbers shift as enemies are deleted. Quotes are not necessary." text += "\n#initiative" text += "\n Assigns initiative to all characters and enemies. This begins combat." text += "\n#turn" @@ -396,7 +396,7 @@ const modifier = (text) => { text += "\n#showlocations (sort)" text += "\n Shows a list of all discovered locations with their coordinates and their distance from the party's current location. If the parameter \"sort\" is added, the locations will be listed by their distance to the party. Note that the location numbers will only be displayed in the unsorted list." text += "\n#removelocation location_name or location_number" - text += "\n Removes the specified location by location_name or location_number as listed in #showlocations. To delete multiple locations, type the numbers with spaces or commas between them. This is safer than calling #removenote multiple times because the numbers shift as notes are deleted. Quotes are not necessary." + text += "\n Removes the specified location by location_name or location_number as listed in #showlocations. To delete multiple locations, type the numbers with spaces or commas between them. This is safer than calling #removenote multiple times because the numbers shift as locations are deleted. Quotes are not necessary." text += "\n#clearlocations" text += "\n Deletes all discovered locations." text += "\n#map"