diff --git a/src/facilities/farmyard/animals/animals.js b/src/facilities/farmyard/animals/animals.js index 2c94185aed50444ac0acad0574b71d24bc770edc..3317ed910867e035cc1a945e578a084d8089b710 100644 --- a/src/facilities/farmyard/animals/animals.js +++ b/src/facilities/farmyard/animals/animals.js @@ -2,49 +2,20 @@ App.Facilities.Farmyard.animals = function() { App.Facilities.Farmyard.animals.init(); const frag = new DocumentFragment(); + const hrMargin = '0'; App.UI.DOM.appendNewElement("h1", frag, 'Animals'); - const activeDiv = App.UI.DOM.appendNewElement("div", frag, null, ['margin-bottom']); - const domesticDiv = App.UI.DOM.appendNewElement("div", frag, null, ['margin-bottom']); - const exoticDiv = App.UI.DOM.appendNewElement("div", frag, null, ['margin-bottom']); - const hrMargin = '0'; + const activeDiv = App.UI.DOM.appendNewElement("div", frag, activeAnimals(), ['margin-bottom']); - const canine = 'canine'; - const hooved = 'hooved'; - const feline = 'feline'; - const exotic = 'exotic'; - const domestic = 'domestic'; + App.UI.DOM.appendNewElement("div", frag, domestic(), ['margin-bottom']); + App.UI.DOM.appendNewElement("div", frag, exotic(), ['margin-bottom']); V.nextButton = "Back"; V.nextLink = "Farmyard"; V.returnTo = "Farmyard Animals"; V.encyclopedia = "Farmyard"; - if (V.active.canine || V.active.hooved || V.active.feline) { - App.UI.DOM.appendNewElement("h2", activeDiv, 'Active Animals'); - } - - App.UI.DOM.appendNewElement("h2", domesticDiv, 'Domestic Animals'); - - if (V.farmyardKennels > 1 || V.farmyardStables > 1 || V.farmyardCages > 1) { - App.UI.DOM.appendNewElement("h2", exoticDiv, 'Exotic Animals'); - } - - activeDiv.append(activeAnimals()); - - domesticDiv.append( - domesticCanines(), - domesticHooved(), - domesticFelines(), - ); - - exoticDiv.append( - exoticCanines(), - exoticHooved(), - exoticFelines(), - ); - if (V.debugMode || V.cheatMode) { frag.append(addAnimal()); } @@ -60,7 +31,7 @@ App.Facilities.Farmyard.animals = function() { hr.style.margin = hrMargin; - App.UI.DOM.appendNewElement("span", div, 'Canine', ['bold']); + App.UI.DOM.appendNewElement("h2", div, 'Active Animals'); div.append(hr); @@ -77,12 +48,14 @@ App.Facilities.Farmyard.animals = function() { return div; } - return document.createElement("div"); + return new DocumentFragment(); function canine() { const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); const options = new App.UI.OptionsGroup(); + App.UI.DOM.appendNewElement("h3", div, 'Canine'); + const option = options.addOption(null, "canine", V.active); V.animals.canine.forEach(canine => { option.addValue(capFirstChar(canine.name), canine.name).pulldown(); @@ -97,6 +70,8 @@ App.Facilities.Farmyard.animals = function() { const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); const options = new App.UI.OptionsGroup(); + App.UI.DOM.appendNewElement("h3", div, 'Hooved'); + const option = options.addOption(null, "hooved", V.active); V.animals.hooved.forEach(hooved => { option.addValue(capFirstChar(hooved.name), hooved.name).pulldown(); @@ -111,6 +86,8 @@ App.Facilities.Farmyard.animals = function() { const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); const options = new App.UI.OptionsGroup(); + App.UI.DOM.appendNewElement("h3", div, 'Feline'); + const option = options.addOption(null, "feline", V.active); V.animals.feline.forEach(feline => { option.addValue(capFirstChar(feline.name), feline.name).pulldown(); @@ -124,108 +101,138 @@ App.Facilities.Farmyard.animals = function() { // Domestic Animals - function domesticCanines() { - if (V.farmyardKennels) { - const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); - const hr = document.createElement("hr"); + function domestic() { + const frag = new DocumentFragment(); - hr.style.margin = hrMargin; + App.UI.DOM.appendNewElement("h2", frag, 'Domestic Animals'); - App.UI.DOM.appendNewElement("span", div, 'Dogs', ['bold']); + frag.append( + canine(), + hooved(), + feline(), + ); - div.append(hr, animalList(canine, domestic, 5000, canine)); + return frag; - return div; + function canine() { + if (V.farmyardKennels) { + const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); + const hr = document.createElement("hr"); + + hr.style.margin = hrMargin; + + App.UI.DOM.appendNewElement("span", div, 'Dogs', ['bold']); + + div.append(hr, animalList('canine', 'domestic', 5000, 'canine')); + + return div; + } + + return new DocumentFragment(); } - return document.createElement("div"); - } + function hooved() { + if (V.farmyardStables) { + const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); + const hr = document.createElement("hr"); - function domesticHooved() { - if (V.farmyardStables) { - const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); - const hr = document.createElement("hr"); + hr.style.margin = hrMargin; - hr.style.margin = hrMargin; + App.UI.DOM.appendNewElement("span", div, 'Hooved Animals', ['bold']); - App.UI.DOM.appendNewElement("span", div, 'Hooved Animals', ['bold']); + div.append(hr, animalList('hooved', 'domestic', 20000, 'hooved')); - div.append(hr, animalList(hooved, domestic, 20000, hooved)); + return div; + } - return div; + return new DocumentFragment(); } - return document.createElement("div"); - } + function feline() { + if (V.farmyardCages) { + const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); + const hr = document.createElement("hr"); - function domesticFelines() { - if (V.farmyardCages) { - const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); - const hr = document.createElement("hr"); + hr.style.margin = hrMargin; - hr.style.margin = hrMargin; + App.UI.DOM.appendNewElement("span", div, 'Cats', ['bold']); - App.UI.DOM.appendNewElement("span", div, 'Cats', ['bold']); + div.append(hr, animalList('feline', 'domestic', 1000, 'feline')); - div.append(hr, animalList(feline, domestic, 1000, feline)); + return div; + } - return div; + return new DocumentFragment(); } - - return document.createElement("div"); } // Exotic Animals - function exoticCanines() { - if (V.farmyardKennels > 1) { - const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); - const hr = document.createElement("hr"); + function exotic() { + const frag = new DocumentFragment(); - hr.style.margin = hrMargin; + if (V.farmyardKennels > 1 || V.farmyardStables > 1 || V.farmyardCages > 1) { + App.UI.DOM.appendNewElement("h2", frag, 'Exotic Animals'); + } - App.UI.DOM.appendNewElement("span", div, 'Canines', ['bold']); + frag.append( + canine(), + hooved(), + feline(), + ); - div.append(hr, animalList(canine, exotic, 50000, canine)); + return frag; - return div; + function canine() { + if (V.farmyardKennels > 1) { + const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); + const hr = document.createElement("hr"); + + hr.style.margin = hrMargin; + + App.UI.DOM.appendNewElement("span", div, 'Canines', ['bold']); + + div.append(hr, animalList('canine', 'exotic', 50000, 'canine')); + + return div; + } + + return new DocumentFragment(); } - return document.createElement("div"); - } + function hooved() { + if (V.farmyardStables > 1) { + const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); + const hr = document.createElement("hr"); - function exoticHooved() { - if (V.farmyardStables > 1) { - const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); - const hr = document.createElement("hr"); + hr.style.margin = hrMargin; - hr.style.margin = hrMargin; + App.UI.DOM.appendNewElement("span", div, 'Hooved Animals', ['bold']); - App.UI.DOM.appendNewElement("span", div, 'Hooved Animals', ['bold']); + div.append(hr, animalList('hooved', 'exotic', 75000, 'hooved')); - div.append(hr, animalList(hooved, exotic, 75000, hooved)); + return div; + } - return div; + return new DocumentFragment(); } - return document.createElement("div"); - } + function feline() { + if (V.farmyardCages > 1) { + const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); + const hr = document.createElement("hr"); - function exoticFelines() { - if (V.farmyardCages > 1) { - const div = App.UI.DOM.makeElement("div", null, ['margin-bottom']); - const hr = document.createElement("hr"); + hr.style.margin = hrMargin; - hr.style.margin = hrMargin; + App.UI.DOM.appendNewElement("span", div, 'Felines', ['bold']); - App.UI.DOM.appendNewElement("span", div, 'Felines', ['bold']); + div.append(hr, animalList('feline', 'exotic', 100000, 'feline')); - div.append(hr, animalList(feline, exotic, 100000, feline)); + return div; + } - return div; + return new DocumentFragment(); } - - return document.createElement("div"); } // Helper Functions @@ -263,58 +270,65 @@ App.Facilities.Farmyard.animals = function() { * @param {'domestic'|'exotic'} rarity One of 'domestic' or 'exotic'. * @param {number} price * @param {string} active The name of the current active animal of the given type. - * @returns {HTMLDivElement} + * @returns {DocumentFragment} */ function animalList(type, rarity, price, active) { - const mainDiv = document.createElement("div"); - const filteredArray = App.Data.animals.filter(animal => animal.rarity === rarity && animal.type === type); - - filteredArray.forEach(animal => { - const animalDiv = document.createElement("div"); - const optionSpan = document.createElement("span"); - - const args = { - animal: animal, - active: active, - type: type, - price: price, - setActiveHandler() { - animal.setActive(); - App.UI.DOM.replace(mainDiv, animalList(type, rarity, price, active)); - App.UI.DOM.replace(activeDiv, activeAnimals()); - }, - purchaseHandler() { - cashX(forceNeg(price), "farmyard"); - animal.purchase(); - if (!V.active[animal.type]) { + const frag = new DocumentFragment(); + [...App.Data.animals] + .filter(animal => animal.rarity === rarity && animal.type === type) + .forEach(animal => { + const animalDiv = document.createElement("div"); + const optionSpan = document.createElement("span"); + + const args = { + animal: animal, + active: active, + type: type, + price: price, + setActiveHandler() { animal.setActive(); + App.UI.DOM.replace(frag, animalList(type, rarity, price, active)); + App.UI.DOM.replace(activeDiv, activeAnimals()); + }, + purchaseHandler() { + cashX(forceNeg(price), "farmyard"); + animal.purchase(); + if (!V.active[animal.type]) { + animal.setActive(); + } + App.UI.DOM.replace(activeDiv, activeAnimals()); + App.UI.DOM.replace(frag, animalList(type, rarity, price, active)); } - App.UI.DOM.replace(activeDiv, activeAnimals()); - App.UI.DOM.replace(mainDiv, animalList(type, rarity, price, active)); - } - }; + }; - optionSpan.append(animalLink(args)); + optionSpan.append(animalLink(args)); - animalDiv.append(capFirstChar(animal.name), ' ', optionSpan); + animalDiv.append(capFirstChar(animal.name), ' ', optionSpan); - mainDiv.appendChild(animalDiv); - }); + frag.append(animalDiv); + }); - return mainDiv; + return frag; } function addAnimal() { - const addAnimalDiv = document.createElement("div"); + const frag = new DocumentFragment(); + + const nameDiv = document.createElement("div"); + const speciesDiv = document.createElement("div"); + const typeDiv = document.createElement("div"); + const rarityDiv = document.createElement("div"); + const articleDiv = document.createElement("div"); const dickDiv = document.createElement("div"); const deadlinessDiv = document.createElement("div"); - const addDiv = App.UI.DOM.makeElement("div", null, ['animal-add']); + const addDiv = App.UI.DOM.makeElement("div", null, ['margin-top']); const animal = new App.Entity.Animal(null, null, "canine", "domestic"); + let animalName = animal.name || 'animal'; - App.UI.DOM.appendNewElement("div", addAnimalDiv, `Add a New Animal`, ['uppercase', 'bold']); + App.UI.DOM.appendNewElement("h2", frag, `Custom Animals`); - addAnimalDiv.append( + frag.append( name(), species(), type(), @@ -325,156 +339,78 @@ App.Facilities.Farmyard.animals = function() { add(), ); - return addAnimalDiv; + return frag; function name() { - const nameDiv = document.createElement("div"); - nameDiv.append( `Name: `, App.UI.DOM.makeTextBox(animal.name || '', value => { animal.setName(value); + animal.setSpecies(value); + animalName = animal.name; - App.UI.DOM.replace(nameDiv, name); - App.UI.DOM.replace(dickDiv, dick); - App.UI.DOM.replace(deadlinessDiv, deadliness); - App.UI.DOM.replace(addDiv, add); + refresh(); }), + App.UI.DOM.makeElement("span", ` Can be lowercase.`, ['note']), ); return nameDiv; } function species() { - const speciesDiv = document.createElement("div"); - speciesDiv.append( `Species: `, - App.UI.DOM.makeTextBox(animal.species || '', value => { + App.UI.DOM.makeTextBox(animal.species || animal.name || '', value => { animal.setSpecies(value); - App.UI.DOM.replace(speciesDiv, species); - App.UI.DOM.replace(addDiv, add); + refresh(); }), + App.UI.DOM.makeElement("span", ` Can be different than the animal's name.`, ['note']), ); return speciesDiv; } function type() { - const typeDiv = document.createElement("div"); - - const typeLinks = []; + const options = new App.UI.OptionsGroup().customRefresh(refresh); + options.addOption(null, "type", animal) + .addValue('Canine', 'canine') + .addValue('Hooved', 'hooved') + .addValue('Feline', 'feline'); if (animal.type === "canine") { - typeLinks.push( - App.UI.DOM.disabledLink(`Canine`, [`Already selected.`]), - App.UI.DOM.link(`Hooved`, () => { - animal.setType('hooved'); - - App.UI.DOM.replace(typeDiv, type); - }), - App.UI.DOM.link(`Feline`, () => { - animal.setType("feline"); - - App.UI.DOM.replace(typeDiv, type); - }), - ); + typeDiv.append(`The ${animalName} is a canine.`, options.render()); } else if (animal.type === "hooved") { - typeLinks.push( - App.UI.DOM.link(`Canine`, () => { - animal.setType("canine"); - - App.UI.DOM.replace(typeDiv, type); - }), - App.UI.DOM.disabledLink(`Hooved`, [`Already selected.`]), - App.UI.DOM.link(`Feline`, () => { - animal.setType("feline"); - - App.UI.DOM.replace(typeDiv, type); - }), - ); + typeDiv.append(`The ${animalName} is a hooved animal.`, options.render()); } else { - typeLinks.push( - App.UI.DOM.link(`Canine`, () => { - animal.setType("canine"); - - App.UI.DOM.replace(typeDiv, type); - }), - App.UI.DOM.link(`Hooved`, () => { - animal.setType("hooved"); - - App.UI.DOM.replace(typeDiv, type); - }), - App.UI.DOM.disabledLink(`Feline`, [`Already selected.`]), - ); + typeDiv.append(`The ${animalName} is a feline.`, options.render()); } - typeDiv.append(`Type: `, App.UI.DOM.generateLinksStrip(typeLinks)); - return typeDiv; } function rarity() { - const rarityDiv = document.createElement("div"); - - const rarityLinks = []; + const options = new App.UI.OptionsGroup().customRefresh(refresh); + options.addOption(null, "rarity", animal) + .addValue('Domestic', 'domestic') + .addValue('Exotic', 'exotic'); if (animal.rarity === "domestic") { - rarityLinks.push( - App.UI.DOM.disabledLink(`Domestic`, [`Already selected.`]), - App.UI.DOM.link(`Exotic`, () => { - animal.setRarity('exotic'); - - App.UI.DOM.replace(rarityDiv, rarity); - }), - ); + rarityDiv.append(`The ${animalName} is domesticated.`, options.render()); } else { - rarityLinks.push( - App.UI.DOM.link(`Domestic`, () => { - animal.setRarity('domestic'); - - App.UI.DOM.replace(rarityDiv, rarity); - }), - App.UI.DOM.disabledLink(`Exotic`, [`Already selected.`]), - ); + rarityDiv.append(`The ${animalName} is exotic and tamed.`, options.render()); } - rarityDiv.append(`Rarity: `, App.UI.DOM.generateLinksStrip(rarityLinks)); - return rarityDiv; } function article() { - const articleDiv = document.createElement("div"); + const options = new App.UI.OptionsGroup().customRefresh(refresh); + options.addOption(null, "articleAn", animal) + .addValue('Yes', 'an') + .addValue('No', 'a'); - const articleLinks = []; - - if (animal.articleAn === 'a') { - articleLinks.push( - App.UI.DOM.link("Yes", () => { - animal.articleAn = 'an'; - - App.UI.DOM.replace(articleDiv, article); - App.UI.DOM.replace(dickDiv, dick); - App.UI.DOM.replace(deadlinessDiv, deadliness); - }), - App.UI.DOM.disabledLink("No", [`Already selected.`]), - ); - } else { - articleLinks.push( - App.UI.DOM.disabledLink("Yes", [`Already selected.`]), - App.UI.DOM.link("No", () => { - animal.articleAn = 'a'; - - App.UI.DOM.replace(articleDiv, article); - App.UI.DOM.replace(dickDiv, dick); - App.UI.DOM.replace(deadlinessDiv, deadliness); - }), - ); - } - - articleDiv.append(`Is this animal's name preceded by an 'an'? `, App.UI.DOM.generateLinksStrip(articleLinks)); + articleDiv.append(`Is the ${animalName}'s name preceded by an 'an'?`, options.render()); return articleDiv; } @@ -488,13 +424,13 @@ App.Facilities.Farmyard.animals = function() { const dickSizeDiv = document.createElement("div"); dickSizeDiv.append( - `How large is ${animal.name ? `${animal.articleAn} male ${animal.name}` : `a male`}'s penis? `, + `How large is a male${animal.name ? ` ${animalName}` : ``}'s penis? `, App.UI.DOM.makeTextBox(animal.dick.size || 2, value => { animal.setDick(value, animal.dick.desc || null); - App.UI.DOM.replace(dickSizeDiv, dickSize); + refresh(); }, true), - App.UI.DOM.makeElement("span", `1 is smallest, and default is 2. `, ['note']), + App.UI.DOM.makeElement("span", ` 1 is smallest and default is 2.`, ['note']), ); return dickSizeDiv; @@ -504,13 +440,13 @@ App.Facilities.Farmyard.animals = function() { const dickDescDiv = document.createElement("div"); dickDescDiv.append( - `What does it look like? `, + `What does the penis look like? `, App.UI.DOM.makeTextBox(animal.dick.desc || '', value => { animal.setDick(animal.dick.size || 2, value); - App.UI.DOM.replace(dickDescDiv, dickDesc); + refresh(); }), - App.UI.DOM.makeElement("span", `Default is 'large'. `, ['note']), + App.UI.DOM.makeElement("span", ` Default is 'large', but you can use any descriptor.`, ['note']), ); return dickDescDiv; @@ -523,7 +459,7 @@ App.Facilities.Farmyard.animals = function() { App.UI.DOM.makeTextBox(5, value => { animal.setDick(value); }, true), - App.UI.DOM.makeElement("span", `Default is 5. `, ['note']), + App.UI.DOM.makeElement("span", ` Default is 5.`, ['note']), ); return deadlinessDiv; @@ -532,8 +468,6 @@ App.Facilities.Farmyard.animals = function() { function add() { const disabledReasons = []; - let link; - if (!animal.name) { disabledReasons.push(`Animal must have a name.`); } @@ -543,19 +477,28 @@ App.Facilities.Farmyard.animals = function() { } if (disabledReasons.length > 0) { - link = App.UI.DOM.disabledLink(`Add`, disabledReasons); + App.UI.DOM.appendNewElement("div", addDiv, App.UI.DOM.disabledLink(`Add`, disabledReasons), ['margin-top']); } else { - link = App.UI.DOM.link(`Add`, () => { - App.Data.animals.push(animal); + App.UI.DOM.appendNewElement("div", addDiv, App.UI.DOM.link(`Add`, () => { + App.Data.animals.add(animal); - App.UI.DOM.replace(addAnimalDiv, addAnimal); - }); + App.UI.reload(); + }), ['margin-top']); } - addDiv.appendChild(link); - return addDiv; } + + function refresh() { + App.UI.DOM.replace(nameDiv, name); + App.UI.DOM.replace(speciesDiv, species); + App.UI.DOM.replace(typeDiv, type); + App.UI.DOM.replace(rarityDiv, rarity); + App.UI.DOM.replace(articleDiv, article); + App.UI.DOM.replace(dickDiv, dick); + App.UI.DOM.replace(deadlinessDiv, deadliness); + App.UI.DOM.replace(addDiv, add); + } } }; @@ -571,7 +514,7 @@ globalThis.getAnimal = function(name) { }; App.Facilities.Farmyard.animals.init = function() { - if (!App.Data.animals || App.Data.animals.length === 0) { + if (!App.Data.animals || App.Data.animals.size === 0) { class Animal extends App.Entity.Animal {} const dog = 'dog'; @@ -584,7 +527,7 @@ App.Facilities.Farmyard.animals.init = function() { const an = 'an'; /** @type {Animal[]} */ - App.Data.animals = [ + [ new Animal("beagle", dog, canine, domestic) .setDeadliness(2), new Animal("bulldog", dog, canine, domestic), @@ -638,9 +581,9 @@ App.Facilities.Farmyard.animals.init = function() { new Animal("lynx", "lynx", feline, exotic), new Animal("puma", "puma", feline, exotic), new Animal("tiger", "tiger", feline, exotic), - ]; + ].forEach(animal => App.Data.animals.add(animal)); } }; -/** @type {App.Entity.Animal[]} */ -App.Data.animals = App.Data.animals || []; +/** @type {Set<App.Entity.Animal>} */ +App.Data.animals = App.Data.animals || new Set;