Skip to content

Commit

Permalink
Change-URL-seed-structure (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryceg authored Jun 25, 2021
1 parent 1a17228 commit ba5cf6a
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 22 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/Settings/Breadcrumb.twee
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<<nosp>><<liveblock>>
<<set $town.ignoreGender to settings.ignoreGender>>
<nav>
<<link "The $town.type of $town.name">><<if $town.generated === 'biome'>><<set $town to setup.createTown($town)>><</if>><<unset $currentPassage>><<unset $currentNPC>><<set $history to []>><<goto "Start">><</link>>
<<link "The $town.type of $town.name">><<if $town.generated === 'biome'>><<set $town to setup.createTown($town)>><</if>><<unset $currentPassage>><<unset $currentNPC>><<set $history to []>><<run history.pushState('', document.title, window.location.pathname + window.location.search)>><<goto "Start">><</link>>
</nav>
<<for _i, _passage range $history>><<set _link to lib.toTitleCase(_passage.linkDescription)>><<capture _i, _passage, _link>><<if _i < $history.length - 1>><nav class="breadcrumb-link"><<link _link _passage.passageName>><<set $currentPassage to _passage>><<run $history.length = _i>><<run setup.history(_passage, _passage.passageName, _passage.linkDescription)>><</link>></nav><</if>><</capture>><</for>><<if def $history.last()>><nav class="breadcrumb-link last"><<print lib.toTitleCase($history.last().linkDescription)>></nav><</if>>
<</liveblock>><</nosp>>
2 changes: 1 addition & 1 deletion src/Settings/CSS/details.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ details[open][data-tags~="nofx"] > *:not(summary) {



summary::-webkit-details-marker {
summary::marker {
display: none;
}
summary:before {
Expand Down
5 changes: 3 additions & 2 deletions src/Start/StoryInit.twee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<<run setup.init()>>

<<set setup.data to {
versionNumber: "2.8.11",
versionNumber: "2.8.12",
badges: {
github: lib.createBadge([
lib.badges.stats.githubForks,
Expand Down Expand Up @@ -61,4 +61,5 @@
$(this).empty().wiki(lib.createBadge(lib.badges.fun.random(), {imgArgs: 'style=width:100%'}));
})
.appendTo($fun);
<</script>>
<</script>>
<<run setup.navigateToObj()>>
11 changes: 11 additions & 0 deletions src/Tools/findViaKey.ts
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/Tools/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ function addToHistory (
}

window.history.pushState(state, passageName)
// window.location.hash = key
window.location.hash = key || passageName
}
71 changes: 57 additions & 14 deletions src/World/urlSeed.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
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)
})

/**
* Validates and adjust a seed.
* @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
}

Expand All @@ -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
})
}
8 changes: 6 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -160,6 +162,7 @@ Object.assign(setup, {
graveStone,
townSquare,
urlSeed,
navigateToObj,
deleteFaction,
leaderFaction,
plothooks,
Expand All @@ -168,6 +171,7 @@ Object.assign(setup, {
createTown,
getTownType,
findViaKey,
findIfExistsViaKey,
createBlacksmithProject,
createSmithyName,
createSmithy,
Expand Down

0 comments on commit ba5cf6a

Please sign in to comment.