diff --git a/js/003-data/gameVariableData.js b/js/003-data/gameVariableData.js index c2897fd64f6b6fde43d11c35dce56f093c7cb043..0270b02f7eaeeab9640de1504f751fc5ad502d2e 100644 --- a/js/003-data/gameVariableData.js +++ b/js/003-data/gameVariableData.js @@ -183,6 +183,10 @@ App.Data.defaultGameStateVariables = { curativeSideEffects: 1, disableTiredness: 1, disableLongDamage: 1, + // Last-used strings in Locate Slave + findName: "", + findBackground: "", + findData: "", // eslint-disable-next-line camelcase pedo_mode: 0, diff --git a/src/js/findSlave.js b/src/js/findSlave.js index 8bf1329a87b05e25bef09c4df4594d7cd3598c31..6587925d17b6a6508b246b1b342a3ca0b6c7e6d6 100644 --- a/src/js/findSlave.js +++ b/src/js/findSlave.js @@ -25,15 +25,34 @@ App.FindSlave._slaveIDs = function(predicate) { }, []); }; +/** + * Display a list of results, or text indicating that there were none + * @param {number[]} ids + * @param {DocumentFragment} frag + */ +App.FindSlave._appendResultList = function(ids, frag) { + if (ids.length === 0) { + App.UI.DOM.appendNewElement("p", frag, "No matching slaves."); + } else { + frag.appendChild(App.UI.SlaveList.render.listDOM(ids, [], App.UI.SlaveList.SlaveInteract.stdInteract)); + } +}; + /** * Generate a slave list as the result of fragment searching all the name-type fields * @param {string} query * @returns {DocumentFragment} */ App.FindSlave.searchByName = function(query) { - const needles = query.split(" ").map((needle) => { return new RegExp(needle, "i"); }); - const ids = this._slaveIDs((slave) => { return this._fragmentSearch([slave.slaveName, slave.slaveSurname, slave.birthName, slave.birthSurname], needles); }); - return App.UI.SlaveList.render.listDOM(ids, [], App.UI.SlaveList.SlaveInteract.stdInteract); + const frag = document.createDocumentFragment(); + if (query) { + const resultTitle = App.UI.DOM.appendNewElement("p", frag, "Query results for name: "); + App.UI.DOM.appendNewElement("code", resultTitle, query); + const needles = query.split(" ").map((needle) => { return new RegExp(needle, "i"); }); + const ids = this._slaveIDs((slave) => { return this._fragmentSearch([slave.slaveName, slave.slaveSurname, slave.birthName, slave.birthSurname], needles); }); + this._appendResultList(ids, frag); + } + return frag; }; /** @@ -42,9 +61,15 @@ App.FindSlave.searchByName = function(query) { * @returns {DocumentFragment} */ App.FindSlave.searchByBackground = function(query) { - const needles = query.split(" ").map((needle) => { return new RegExp(needle, "i"); }); - const ids = this._slaveIDs((slave) => { return this._fragmentSearch([slave.career, slave.origin], needles); }); - return App.UI.SlaveList.render.listDOM(ids, [], App.UI.SlaveList.SlaveInteract.stdInteract); + const frag = document.createDocumentFragment(); + if (query) { + const resultTitle = App.UI.DOM.appendNewElement("p", frag, "Query results for background: "); + App.UI.DOM.appendNewElement("code", resultTitle, query); + const needles = query.split(" ").map((needle) => { return new RegExp(needle, "i"); }); + const ids = this._slaveIDs((slave) => { return this._fragmentSearch([slave.career, slave.origin], needles); }); + this._appendResultList(ids, frag); + } + return frag; }; /** @@ -53,7 +78,13 @@ App.FindSlave.searchByBackground = function(query) { * @returns {DocumentFragment} */ App.FindSlave.searchByExpression = function(query) { - const pred = new Function("slave", "return (" + query + ");"); - const ids = runWithReadonlyProxy(() => { return this._slaveIDs(pred); }); - return App.UI.SlaveList.render.listDOM(ids, [], App.UI.SlaveList.SlaveInteract.stdInteract); + const frag = document.createDocumentFragment(); + if (query) { + const resultTitle = App.UI.DOM.appendNewElement("p", frag, "Query results from expression: "); + App.UI.DOM.appendNewElement("code", resultTitle, query); + const pred = new Function("slave", "return (" + query + ");"); + const ids = runWithReadonlyProxy(() => { return this._slaveIDs(pred); }); + this._appendResultList(ids, frag); + } + return frag; }; diff --git a/src/npc/findSlave.tw b/src/npc/findSlave.tw index 204f8a2b4b9f614b74a95a9c5bd366c9f03189e3..e7aebb3b75b9076c0e94cb51891a4b38d3a95018 100644 --- a/src/npc/findSlave.tw +++ b/src/npc/findSlave.tw @@ -7,23 +7,23 @@ After spending a minute trying to remember some details about one of your slaves "Certainly, <<= properMaster()>>. What can you tell me about them?"<br><br> "They're called something like: -<<textbox "_nameSearch" "" autofocus>> +<<textbox "$findName" $findName autofocus>> <<link "Locate">> - <<script>>$('#slaveList').empty().append(App.FindSlave.searchByName(State.temporary.nameSearch));<</script>> + <<script>>$('#slaveList').empty().append(App.FindSlave.searchByName(V.findName));<</script>> <</link>> <br>//(Enter a fragment of their nickname, name, surname, birth name, or birth surname)//<br><br> "In the past, they were: -<<textbox "_backgroundSearch" "">> +<<textbox "$findBackground" $findBackground>> <<link "Locate">> - <<script>>$('#slaveList').empty().append(App.FindSlave.searchByBackground(State.temporary.backgroundSearch));<</script>> + <<script>>$('#slaveList').empty().append(App.FindSlave.searchByBackground(V.findBackground));<</script>> <</link>> <br>//(Enter a fragment of their origin or past job, for example, "shelter" or "lawyer")//<br><br> "Their data should meet this condition: -<<textbox "_dataSearch" "">> +<<textbox "$findData" $findData>> <<link "Locate">> - <<script>>$('#slaveList').empty().append(App.FindSlave.searchByExpression(State.temporary.dataSearch));<</script>> + <<script>>$('#slaveList').empty().append(App.FindSlave.searchByExpression(V.findData));<</script>> <</link>> <br>//(Enter a conditional expression which evaluates to true for the slave you want to find, such as "slave.physicalAge >= 18 && slave.physicalAge < 21")//<br><br>