Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ async function build_js(lang) {
(await readFile(TMP_FILE, 'ascii'))
.split('content').join('c')
.split('loaded').join('l')
.split('downl').join('downloaded'),
.split('"transform":"cs"').join('"transform":"contents"')
.split('downl').join('downloaded')
.split('downloadedoading').join('downloading'),
'ascii'
);

Expand Down
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.11.7",
"version": "0.11.9",
"description": "voxel-based 3d game, written in javascript",
"homepage": "https://l3p3.de/minicraft",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,10 @@ button {
width: 1rem;
height: 1rem;
border-radius: 50%;
opacity: 50%;
opacity: .5;
cursor: pointer;
transition: opacity .2s;
}
.window_button:hover {
opacity: 100%;
opacity: 1;
}
53 changes: 39 additions & 14 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ import {
app_state,
hook_app_state,
} from './etc/state.js';
import {
chunks_db_promise,
} from './etc/storage.js';

import {
game_save,
Expand All @@ -47,6 +50,7 @@ import {
} from './game/m_renderer.js';
import {
world_store_init,
world_store_sync_check,
} from './game/m_world_store.js';

import Window from './os/c_window.js';
Expand All @@ -59,6 +63,7 @@ export const in_event_set = value => {
in_event = value;
}
let last_touch_event = 0;
let sync_check_timeout = 0;

function Root() {
hook_app_state();
Expand Down Expand Up @@ -155,6 +160,16 @@ function Root() {
}, [flag_touch]);
hook_effect(tiles_set, [app_state.config.textures]);

hook_effect(() => {
clearTimeout_(sync_check_timeout);
if (!app_state.connection_error) {
sync_check_timeout = setTimeout_(world_store_sync_check);
}
}, [
app_state.connection_error,
app_state.worlds_merged,
]);

hook_dom('', {
onkeydown: handler_key,
onkeyup: handler_key,
Expand All @@ -170,7 +185,7 @@ function Root() {
node_dom('div[className=cursormask]', {
S: {
cursor: app_state.cursor,
}
},
}),
];
}
Expand All @@ -187,7 +202,11 @@ if (window.SSR) {
}
else if (BroadcastChannel_) {
const channel_lock = new BroadcastChannel_('minicraft.lock');
const timeout = setTimeout_(init, 100, Root);
const timeout = setTimeout_(() => (
chunks_db_promise.then(() =>
init(Root)
)
), 100);
channel_lock.addEventListener('message', event => {
if (event.data === 'yes') {
clearTimeout_(timeout);
Expand All @@ -202,17 +221,23 @@ else if (BroadcastChannel_) {
channel_lock.postMessage('anyone there?');
}
else {
// first make sure lock has expired
const lock_found = Number_(localStorage_getItem('minicraft.lock'));
const lock_limit = Date_now() - 1000;
// if already expired
if (lock_found < lock_limit) init(Root);
// if not, wait and check again
else setTimeout_(() => {
init(
Number_(localStorage_getItem('minicraft.lock')) === lock_found
? Root
: ErrorOpened
);
}, lock_found - lock_limit);
const lock_limit = Date_now() - 1500;
// if lock still valid, wait and check again
if (lock_found > lock_limit) {
setTimeout_(() => {
// chunks_db_promise will already be resolved by then
init(
Number_(localStorage_getItem('minicraft.lock')) === lock_found
? Root
: ErrorOpened
);
}, lock_found - lock_limit);
}
// if not, init right away
else {
chunks_db_promise.then(() =>
init(Root)
)
}
}
3 changes: 3 additions & 0 deletions src/etc/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export const COORDINATE_OFFSET = 1 << 16;

export const WORLD_FORMAT = 1;

export const WORLD_STORED_NOT = 0;
export const WORLD_STORED_SHOULD = 1;

export const BLOCK_TYPE_AIR = 0;
export const BLOCK_TYPE_STONE = 1;
export const BLOCK_TYPE_GRASS = 2;
Expand Down
33 changes: 29 additions & 4 deletions src/etc/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
locale_error_connection,
locale_error_no_permission_logged_in,
locale_today,
} from '../etc/locale.js';

Expand Down Expand Up @@ -26,15 +28,17 @@ const Date_ = Date;
export const Date_now = () => Date_.now();
const JSON_ = JSON;
export const Promise_ = Promise;
export const Promise_resolve = Promise_.resolve;
export const JSON_stringify = JSON_.stringify;
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 indexedDB_ = window_.indexedDB;
export const indexedDB_ = window_.indexedDB || null;
export const fetch_ = fetch;
export const Error_ = msg => new Error(msg);
export const Array_ = Array;
export const Uint8Array_ = Uint8Array;
export const Uint32Array_ = Uint32Array;
export const Set_ = Set;
Expand All @@ -54,6 +58,16 @@ export const confirm_ = confirm;
export const prompt_ = prompt;
export const flag_chromium = navigator_.userAgent.includes('WebKit');

// old safari does not support fill for typed arrays
/*if (!Uint32Array_.prototype.fill) {
Uint32Array_.prototype.fill = Array_.prototype.fill;
}*/

export const headers_json_post = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
};

/**
@param {number} num
@return {string}
Expand All @@ -66,11 +80,11 @@ export const number_toFixed2 = num => num.toFixed(2);
@param {string=} pad
@return {string}
@noinline
@suppress {checkTypes}
*/
export const number_padStart2 = (num, pad) => (
Math_floor(num)
.toString()
.padStart(2, pad)
num = Math_floor(num).toString(),
num.length < 2 ? pad + num : num
);

/**
Expand Down Expand Up @@ -130,3 +144,14 @@ export const datify = (time, short) => {

return result + date_then.getHours() + ':' + number_padStart2(date_then.getMinutes(), '0');
}

export const response_parse = response => {
if (!response.ok) {
throw Error_(
response.status === 403
? locale_error_no_permission_logged_in
: locale_error_connection
);
}
return response.json();
}
47 changes: 18 additions & 29 deletions src/etc/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
WINDOW_MODE_FULL,
WINDOW_TYPE_EMPTY,
WINDOW_TYPE_GAME,
WORLD_STORED_NOT,
} from './constants.js';
import {
VERSION,
Expand Down Expand Up @@ -117,7 +118,7 @@ const reducers = {
prompt_(locale_unknown_world_found, '') || locale_unknown_world
).substring(0, 16),
mod_l: Date_now(),
mod_r: 0,
mod_r: WORLD_STORED_NOT,
};
needs_save = true;
}
Expand All @@ -144,8 +145,8 @@ const reducers = {
world_list_cooldown: true,
world_list_loading: true,
world_syncing: null,
worlds_merged: world_store_lists_merge(config),
worlds_opened: [],
...world_store_lists_merge(config),
};
if (needs_save) {
state.config_saved = null;
Expand Down Expand Up @@ -234,35 +235,28 @@ const reducers = {
],
}
),
world_add: (state, world) => {
const config = {
world_add: (state, world) => ({
...state,
...world_store_lists_merge({
...state.config,
worlds: [
...state.config.worlds,
world,
],
};
return {
...state,
config,
worlds_merged: world_store_lists_merge(config),
};
},
world_remove: (state, id) => {
const config = {
}),
}),
world_remove: (state, id) => ({
...state,
...world_store_lists_merge({
...state.config,
worlds: state.config.worlds.filter(
world => world.id !== id
),
};
return {
...state,
config,
worlds_merged: world_store_lists_merge(config),
};
},
world_prop: (state, id, patch) => {
const config = {
}),
}),
world_prop: (state, id, patch) => ({
...state,
...world_store_lists_merge({
...state.config,
worlds: state.config.worlds.map(world => (
world.id === id
Expand All @@ -272,13 +266,8 @@ const reducers = {
}
: world
)),
};
return {
...state,
config,
worlds_merged: world_store_lists_merge(config),
};
},
}),
}),
};

export let app_state;
Expand Down
Loading