From ba5cf6abe9e9d54fa72d8caaa83defc5edeba00b Mon Sep 17 00:00:00 2001 From: Rhys <36913739+ryceg@users.noreply.github.com> Date: Fri, 25 Jun 2021 18:02:31 +1000 Subject: [PATCH] Change-URL-seed-structure (#643) --- CHANGELOG.md | 8 ++++ package.json | 2 +- src/Settings/Breadcrumb.twee | 2 +- src/Settings/CSS/details.css | 2 +- src/Start/StoryInit.twee | 5 ++- src/Tools/findViaKey.ts | 11 ++++++ src/Tools/history.ts | 2 +- src/World/urlSeed.ts | 71 +++++++++++++++++++++++++++++------- src/main.ts | 8 +++- 9 files changed, 89 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 570dea2a1..448d443b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). +## 2.8.12 + +### Added +- You can now link directly to buildings and the owners of buildings that were pre-generated. + +### Changed +- The generator no longer uses the `location.hash` for the seed, and instead uses the `location.search` property. + ## 2.8.11 ### Added diff --git a/package.json b/package.json index 316df2b47..fa84d5a40 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "private": true, "name": "eigengraus-essential-establishment-generator", - "version": "2.8.11", + "version": "2.8.12", "description": "Eigengrau's Essential Establishment Generator", "main": "main.txt", "repository": { diff --git a/src/Settings/Breadcrumb.twee b/src/Settings/Breadcrumb.twee index 8bc94063d..eeaa98226 100644 --- a/src/Settings/Breadcrumb.twee +++ b/src/Settings/Breadcrumb.twee @@ -2,7 +2,7 @@ <><> <> <><><><><><><><><> <><> \ No newline at end of file diff --git a/src/Settings/CSS/details.css b/src/Settings/CSS/details.css index 1c160b324..f6e123e71 100644 --- a/src/Settings/CSS/details.css +++ b/src/Settings/CSS/details.css @@ -23,7 +23,7 @@ details[open][data-tags~="nofx"] > *:not(summary) { -summary::-webkit-details-marker { +summary::marker { display: none; } summary:before { diff --git a/src/Start/StoryInit.twee b/src/Start/StoryInit.twee index 2d37819c7..e82b4506a 100644 --- a/src/Start/StoryInit.twee +++ b/src/Start/StoryInit.twee @@ -5,7 +5,7 @@ <> <> \ No newline at end of file +<> +<> \ No newline at end of file diff --git a/src/Tools/findViaKey.ts b/src/Tools/findViaKey.ts index 05094b49f..04f63ad26 100644 --- a/src/Tools/findViaKey.ts +++ b/src/Tools/findViaKey.ts @@ -1,5 +1,16 @@ import type { Building, Deity, Faction, NPC, Road } from '@lib' +export const findIfExistsViaKey = (key: string): boolean => { + if (State.variables.npcs[key]) return true + if (State.variables.town.factions[key]) return true + if (State.variables.town.roads[key]) return true + const building = State.variables.town.buildings.find(building => { + return building.key === key + }) + if (building) return true + return false +} + export const findViaKey = (key: string): Faction | Building | NPC | Road | Deity => { if (State.variables.npcs[key]) return State.variables.npcs[key] if (State.variables.town.factions[key]) return State.variables.town.factions[key] diff --git a/src/Tools/history.ts b/src/Tools/history.ts index f390d3ef5..fd6ecea13 100644 --- a/src/Tools/history.ts +++ b/src/Tools/history.ts @@ -36,5 +36,5 @@ function addToHistory ( } window.history.pushState(state, passageName) - // window.location.hash = key + window.location.hash = key || passageName } diff --git a/src/World/urlSeed.ts b/src/World/urlSeed.ts index 27b4a05e4..adb7efbae 100644 --- a/src/World/urlSeed.ts +++ b/src/World/urlSeed.ts @@ -1,18 +1,28 @@ -export const urlSeed = () => { - const seed = getValidSeed(location.hash.replace('#', '')) +const RESET_SEED = 'resetSeed' - console.log(`Setting the location hash to ${seed}`) - State.metadata.set('seed', seed) - location.hash = seed +/** + * This handles the allocation of a new URL seed. + */ +export const urlSeed = () => { + const params = new URLSearchParams(document.location.search) + let origSeed = params.get('seed') + if (recall(RESET_SEED, false)) { + origSeed = null + forget(RESET_SEED) + } + const seed = getValidSeed(origSeed) + if (origSeed !== seed) { + params.set('seed', seed) + document.location.search = params.toString() + } - console.log('Spinning up PRNG') + console.log(`Spinning up PRNG with "${seed}"`) State.prng.init(seed) } +/** This tells the engine that it needs to generate a new seed. */ $(document).one(':enginerestart', () => { - console.log('Creating a new seed...') - location.hash = createSeed() - console.log('Restarting the engine...') + memorize(RESET_SEED, true) }) /** @@ -20,17 +30,16 @@ $(document).one(':enginerestart', () => { * @param seed - Seed to validate/adjust. * @returns A valid seed. */ -function getValidSeed (seed: string): string { +function getValidSeed (seed: string | null): string { + if (!seed) seed = createSeed() if (seed.length <= 0) { console.log('Creating a seed...') - return createSeed() + seed = createSeed() } - if (seed.length <= 16) { console.warn(`Seed not long enough! Appending some filler to ${seed}...`) - return seed + createSeed() + seed += createSeed() } - return seed } @@ -41,3 +50,37 @@ function createSeed () { const { adjectives, animals } = lib.urlData return `${adjectives.random()}${adjectives.random()}${animals.random()}` } + +const passageExists = (key: string): boolean => { + if (Story.get(key)) return true + return false +} + +export function navigateToObj () { + const hash = document.location.hash.replace('#', '') + if (hash) { + if (passageExists(hash)) { + setup.history(State.variables.town, hash, hash) + Engine.play(hash) + } else if (setup.findIfExistsViaKey(hash) === true) { + const obj = setup.findViaKey(hash) + State.variables.currentPassage = obj + setup.history(obj, obj.passageName, obj.name) + Engine.play(obj.passageName) + } else { + removeHash() + } + } +} + +function removeHash () { + history.pushState('', document.title, window.location.pathname + window.location.search) + $(document).trigger({ + type: ':notify', + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + message: 'Sorry, unfortunately this key was not linked to a pregenerated object; linking only works to building owners that are generated at start with no user interaction.', + delay: 8000, + classes: false + }) +} diff --git a/src/main.ts b/src/main.ts index e770f6cdc..7aa566189 100644 --- a/src/main.ts +++ b/src/main.ts @@ -31,14 +31,14 @@ import { openDialog, rerenderPage } from './Dialog/openDialog' import { addSettingButton } from './Settings/settingButton' import { getLocation, getEncounter, getEventDescription } from './World/events' import { graveStone } from './World/graveStone' -import { urlSeed } from './World/urlSeed' +import { urlSeed, navigateToObj } from './World/urlSeed' import { deleteFaction } from './Factions/deleteFaction' import { leaderFaction } from './Factions/leader' import { plothooks } from './PlotHook/plothooks' import { createTownBiome } from './Town/js/createTownBiome' import { createTownName } from './Town/js/createTownName' import { createTown, getTownType } from './Town/js/createTown' -import { findViaKey } from './Tools/findViaKey' +import { findViaKey, findIfExistsViaKey } from './Tools/findViaKey' import { createBlacksmithProject } from './Blacksmith/js/blacksmithProject' import { createSmithyName } from './Blacksmith/js/createSmithyName' import { createSmithy } from './Blacksmith/js/createSmithy' @@ -94,6 +94,7 @@ declare global { graveStone: typeof graveStone townSquare: typeof townSquare urlSeed: typeof urlSeed + navigateToObj: typeof navigateToObj deleteFaction: typeof deleteFaction leaderFaction: typeof leaderFaction plothooks: typeof plothooks @@ -102,6 +103,7 @@ declare global { createTown: typeof createTown getTownType: typeof getTownType findViaKey: typeof findViaKey + findIfExistsViaKey: typeof findIfExistsViaKey createBlacksmithProject: typeof createBlacksmithProject createSmithyName: typeof createSmithyName createSmithy: typeof createSmithy @@ -160,6 +162,7 @@ Object.assign(setup, { graveStone, townSquare, urlSeed, + navigateToObj, deleteFaction, leaderFaction, plothooks, @@ -168,6 +171,7 @@ Object.assign(setup, { createTown, getTownType, findViaKey, + findIfExistsViaKey, createBlacksmithProject, createSmithyName, createSmithy,