Skip to content

Commit

Permalink
dev: lock url so app restores the same game state after reloads
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Dec 11, 2024
1 parent 37b84ae commit 50d1d37
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/browserfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,28 +480,29 @@ export const openWorldFromHttpDir = async (fileDescriptorUrls: string[]/* | und
let index
let baseUrl
for (const url of fileDescriptorUrls) {
let response: Response | undefined
let file
try {
setLoadingScreenStatus(`Trying to get world descriptor from ${new URL(url).host}`)
const controller = new AbortController()
setTimeout(() => {
controller.abort()
}, 3000)
// eslint-disable-next-line no-await-in-loop
response = await fetch(url, { signal: controller.signal })
const response = await fetch(url, { signal: controller.signal })
// eslint-disable-next-line no-await-in-loop
file = await response.json()
} catch (err) {
console.error('Error fetching file descriptor', url, err)
}
if (!response) continue
// eslint-disable-next-line no-await-in-loop
const file = await response.json()
if (!file) continue
if (file.baseUrl) {
baseUrl = new URL(file.baseUrl, baseUrl).toString()
index = file.index
} else {
index = file
baseUrl = baseUrlParam ?? url.split('/').slice(0, -1).join('/')
}
break
}
if (!index) throw new Error(`The provided mapDir file is not valid descriptor file! ${fileDescriptorUrls.join(', ')}`)
await new Promise<void>(async resolve => {
Expand Down
2 changes: 1 addition & 1 deletion src/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ const alwaysPressedHandledCommand = (command: Command) => {
}
}

function lockUrl () {
export function lockUrl () {
let newQs = ''
if (fsState.saveLoaded) {
const save = localServer!.options.worldFolder.split('/').at(-1)
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import PrismarineItem from 'prismarine-item'

import { options, watchValue } from './optionsStorage'
import './reactUi'
import { contro, onBotCreate } from './controls'
import { contro, lockUrl, onBotCreate } from './controls'
import './dragndrop'
import { possiblyCleanHandle, resetStateAfterDisconnect } from './browserfs'
import { watchOptionsAfterViewerInit, watchOptionsAfterWorldViewInit } from './watchOptions'
Expand Down Expand Up @@ -692,6 +692,9 @@ async function connect (connectOptions: ConnectOptions) {
setLoadingScreenStatus('Placing blocks (starting viewer)')
localStorage.lastConnectOptions = JSON.stringify(connectOptions)
connectOptions.onSuccessfulPlay?.()
if (process.env.NODE_ENV === 'development' && !localStorage.lockUrl && new URLSearchParams(location.search).size === 0) {
lockUrl()
}
updateDataAfterJoin()
if (connectOptions.autoLoginPassword) {
bot.chat(`/login ${connectOptions.autoLoginPassword}`)
Expand Down

0 comments on commit 50d1d37

Please sign in to comment.