Skip to content

Commit

Permalink
#30 download/upload/sync worlds
Browse files Browse the repository at this point in the history
  • Loading branch information
L3P3 committed Jan 3, 2024
1 parent 0ddf656 commit 8e7fb27
Show file tree
Hide file tree
Showing 13 changed files with 462 additions and 89 deletions.
7 changes: 4 additions & 3 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ const exec = cmd => (
)
);

const env_set = (version, debug, api) => (
const env_set = (version, debug, api, api_download) => (
writeFile(
'./src/etc/env.js',
`export const VERSION = '${version}';
export const DEBUG = ${debug};
export const API = '${api}';
export const API_DOWNLOAD = '${api_download}';
`,
'utf8'
)
Expand All @@ -55,7 +56,7 @@ async function build_css() {
}

async function build_js() {
await env_set(version, false, '/api/minicraft/');
await env_set(version, false, '/api/minicraft/', '/static/minicraft/worlds/');

console.log('js pass 1...');
console.log((await exec(
Expand Down Expand Up @@ -128,7 +129,7 @@ try {
]);
}
finally {
await env_set('dev', true, '//l3p3.de/api/minicraft/');
await env_set('dev', true, '//l3p3.de/api/minicraft/', '//l3p3.de/static/minicraft/worlds/');
}

console.log('done.');
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minicraft",
"version": "0.8.1",
"version": "0.9.0",
"description": "voxel-based 3d game, written in javascript",
"homepage": "https://l3p3.de/minicraft",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ button {
max-width: 30rem;
margin: 1rem auto;
height: calc(100vh - 18rem);
overflow-y: auto;
overflow-y: scroll;
touch-action: pan-y;
}
.game > .menu > .worlds > * {
Expand Down
23 changes: 22 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import {
hook_assert,
hook_effect,
hook_model,
hook_static,
lui_,
node,
} from './etc/lui.js';

import {
DEBUG,
} from './etc/env.js';
import {
reducers,
} from './etc/state.js';
import {
Object_keys,
handler_noop,
localStorage_,
localStorage_removeItem,
} from './etc/helpers.js';

import {
Expand All @@ -28,18 +35,32 @@ lui_.init(() => {
});

hook_effect(() => {
if (
DEBUG &&
location.hash === '#purge'
) {
for (const key of Object_keys(localStorage_)) {
localStorage_removeItem(key);
}
location.href = '/app-dev.html';
hook_assert(false);
}

let unloaded = false;

// shotgun method
onbeforeunload = onunload = onpagehide = onblur = () => {
if (unloaded) return;
unloaded = true;
actions.config_save();
if (ref.game) game_save(ref.game);
actions.config_save();
};
onpageshow = onfocus = () => {
unloaded = false;
};
setInterval(() => {
actions.config_save();
}, 500);
});

const handler_key = hook_static(event => {
Expand Down
1 change: 1 addition & 0 deletions src/etc/env.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const VERSION = 'dev';
export const DEBUG = true;
export const API = '//l3p3.de/api/minicraft/';
export const API_DOWNLOAD = '//l3p3.de/static/minicraft/worlds/';
4 changes: 4 additions & 0 deletions src/etc/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ export const JSON_parse = JSON_.parse;
export const localStorage_ = localStorage;
export const localStorage_getItem = key => localStorage_.getItem(key);
export const localStorage_setItem = localStorage_.setItem.bind(localStorage_);
export const localStorage_removeItem = localStorage_.removeItem.bind(localStorage_);
export const Uint8Array_ = Uint8Array;
export const Uint32Array_ = Uint32Array;
export const Map_ = Map;
export const Number_ = Number;
export const Object_ = Object;
export const Object_keys = Object_.keys;
export const Object_entries = Object_.entries;
export const setTimeout_ = setTimeout;
export const setInterval_ = setInterval;
export const clearTimeout_ = clearTimeout;
Expand Down
50 changes: 50 additions & 0 deletions src/etc/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export const reducers = {
view_angle: 120,
view_distance: 64,
world_last: 0,
/** @type {!Array<TYPE_WORLD_LISTING_LOCAL>} */
worlds: [],
};
const config_raw = localStorage_getItem('minicraft.config');
if (config_raw) {
Expand All @@ -35,6 +37,22 @@ export const reducers = {
tmp = config_loaded['world_last']
) != null)
config.world_last = tmp;
if ((
tmp = config_loaded['worlds']
) != null)
config.worlds = tmp;
else if (
localStorage_getItem('minicraft.world.0:meta')
) {
config.worlds[0] = {
id: 0,
label: (
prompt('Es wurde eine namenlose lokale Welt gefunden. Wie soll sie heißen?', '') || 'Unbekannte Welt'
).substring(0, 16),
mod_l: Date.now(),
mod_r: 0,
};
}
}
return {
config,
Expand All @@ -54,6 +72,7 @@ export const reducers = {
'view_angle': config.view_angle,
'view_distance': config.view_distance,
'world_last': config.world_last,
'worlds': config.worlds,
}));
return {
...state,
Expand All @@ -73,4 +92,35 @@ export const reducers = {
...patch,
},
}),
world_add: (state, world) => ({
...state,
config: {
...state.config,
worlds: [
...state.config.worlds,
world,
],
},
}),
world_remove: (state, id) => ({
...state,
config: {
...state.config,
worlds: state.config.worlds.filter(world => world.id !== id),
},
}),
world_prop: (state, id, patch) => ({
...state,
config: {
...state.config,
worlds: state.config.worlds.map(world => (
world.id === id
? {
...world,
...patch,
}
: world
)),
},
}),
};
14 changes: 13 additions & 1 deletion src/externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,24 @@ var ASSETS;
*/
var TYPE_WORLD_META;

/**
@typedef {{
id: number,
label: string,
mod_l: number,
mod_r: number,
}}
*/
var TYPE_WORLD_LISTING_LOCAL;

/**
@typedef {{
account_name: string,
hash: number,
id: number,
label: string,
modified: number,
writable: boolean,
}}
*/
var TYPE_WORLD_LISTING;
var TYPE_WORLD_LISTING_REMOTE;
1 change: 0 additions & 1 deletion src/game/c_game.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
hook_dom,
hook_effect,
hook_memo,
hook_rerender,
Expand Down
Loading

0 comments on commit 8e7fb27

Please sign in to comment.