Skip to content

Commit

Permalink
account integration
Browse files Browse the repository at this point in the history
  • Loading branch information
L3P3 committed Jan 6, 2024
1 parent 3567f30 commit 1fd2225
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 33 deletions.
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.9.3",
"version": "0.9.4",
"description": "voxel-based 3d game, written in javascript",
"homepage": "https://l3p3.de/minicraft",
"repository": {
Expand Down
1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ lui_.init(() => {
ondragstart: handler_noop,
}, [
node(App, {
account: state.account,
actions,
config: state.config,
ref,
Expand Down
21 changes: 14 additions & 7 deletions src/etc/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,19 @@ export const reducers = {
needs_save = true;
}
}
if (needs_save) {
return reducers.config_save({
config,
config_saved: null,
});
}
return {
const state = {
account: {
label: '',
rank: 0,
},
config,
config_saved: config,
};
if (needs_save) {
state.config_saved = null;
return reducers.config_save(state);
}
return state;
},
config_save: state => {
const {
Expand All @@ -87,6 +90,10 @@ export const reducers = {
config_saved: config,
};
},
account_set: (state, account) => ({
...state,
account,
}),
config_reduce: (state, reducer) => (
reducers.config_set(
state,
Expand Down
11 changes: 11 additions & 0 deletions src/externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,14 @@ var TYPE_WORLD_LISTING_REMOTE;
}}
*/
var TYPE_WORLD_API;

/**
@typedef {{
account: {
label: string,
rank: number,
},
worlds: !Array<TYPE_WORLD_LISTING_REMOTE>,
}}
*/
var TYPE_RESPONSE_INITIAL;
3 changes: 3 additions & 0 deletions src/game/c_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Game from './c_game.js';
import MenuStart from './c_menu_start.js';

export default function App({
account,
actions,
config,
ref,
Expand All @@ -24,12 +25,14 @@ export default function App({
return [
view === APP_VIEW_WORLDS &&
node(MenuStart, {
account,
actions,
config,
view_set,
}),
view === APP_VIEW_GAME &&
node(Game, {
account,
actions,
config,
frame,
Expand Down
3 changes: 2 additions & 1 deletion src/game/c_game.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import Terminal from './c_terminal.js';
import Touch from './c_touch.js';

export default function Game({
account,
actions,
config,
frame,
Expand All @@ -53,7 +54,7 @@ export default function Game({
const pointer_locked = document_.pointerLockElement === frame;

const model = hook_memo(() => (
ref.game = game_create(actions, frame, config)
ref.game = game_create(actions, frame, config, account)
));

hook_effect(() => {
Expand Down
56 changes: 36 additions & 20 deletions src/game/c_menu_start.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function WorldItem({
}

export default function MenuStart({
account,
actions,
config,
view_set,
Expand All @@ -88,33 +89,38 @@ export default function MenuStart({
refreshes_set(refreshes_get() + 1);
});

/**
last fetched world list
@type {{
value: ?Array<TYPE_WORLD_LISTING_REMOTE>,
}}
*/
const world_list_remote_ref = hook_static({
value: null,
});
const world_list_remote = hook_async(
async () => {
try {
const response = await fetch(API + 'world?what=meta_all');
const initial = !world_list_remote_ref.value && !refreshes;
const response = await fetch(API + `world?what=${initial ? 'initial' : 'meta_all'}`);
if (!response.ok) throw new Error('Verbindungsfehler.');
return await response.json();
const json = await response.json();
if (!initial) return /** @type {!Array<TYPE_WORLD_LISTING_REMOTE>} */ (json);
const json_initial = /** @type {TYPE_RESPONSE_INITIAL} */ (json);
defer();
actions.account_set(json_initial.account);
return json_initial.worlds;
}
catch (error) {
alert('Fehler beim Laden der Weltliste: ' + error.message);
alert('Fehler beim Laden der Weltenliste: ' + error.message);
return [];
}
},
[refreshes],
null
);
/**
last fetched world list
@type {{
value: ?Array<TYPE_WORLD_LISTING_REMOTE>,
}}
*/
const world_list_remote_ref = hook_static({
value: null,
});
// merged local and remote world list
const world_list = hook_memo(() => {
const first = !world_list_remote_ref.value;
if (world_list_remote) {
world_list_remote_ref.value = world_list_remote;
}
Expand Down Expand Up @@ -359,6 +365,17 @@ export default function MenuStart({

return [
node_dom('h1[innerText=Welten]'),
node_dom('button[innerText=Aktualisieren][style=position:absolute;left:0;top:0;height:2rem][title=Liste neu laden]', {
disabled: !world_list_remote,
onclick: refresh,
}),
node_dom('button[style=position:absolute;right:0;top:0;height:2rem]', {
disabled: account.rank > 0,
innerText: account.rank ? account.label : 'Anmelden',
onclick: () => {
location.href = '/account?redir=minicraft';
},
}),
node_dom('div[className=worlds]', null, [
node_map(WorldItem, world_list, {
refreshes, // refresh dates as well
Expand Down Expand Up @@ -396,7 +413,8 @@ export default function MenuStart({
disabled: (
!world_list_remote ||
!world_selected ||
world_selected.local > 0 && world_selected.remote > 0
world_selected.local > 0 && world_selected.remote > 0 ||
!world_selected.remote && !account.rank
),
innerText: (
world_selected && !world_selected.local
Expand Down Expand Up @@ -427,15 +445,13 @@ export default function MenuStart({
? 'Keine Welt ausgewählt!'
: !world_selected.local
? 'Welt von Server herunterladen'
: !world_selected.remote
: world_selected.remote
? 'Die Welt ist schon auf beiden Seiten vorhanden!'
: account.rank
? 'Welt auf den Server hochladen'
: 'Die Welt ist schon auf beiden Seiten vorhanden!'
: 'Nicht angemeldet!'
),
}),
node_dom('button[innerText=Aktualisieren][title=Liste neu laden]', {
disabled: !world_list_remote,
onclick: refresh,
}),
]),
node_dom('hr'),
node_dom('center', null, [
Expand Down
4 changes: 2 additions & 2 deletions src/game/m_game.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ import {

let message_id_counter = 0;

export const game_create = (actions, frame_element, config) => {
export const game_create = (actions, frame_element, config, account) => {
const world = world_create(config.world_last);

const player = player_create(world);
const player = player_create(world, account);

world_load(world, player);

Expand Down
4 changes: 2 additions & 2 deletions src/game/m_player.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
slot_create,
} from './m_slot.js';

export const player_create = world => ({
export const player_create = (world, account) => ({
accel_x: 0.0,
accel_y: 0.0,
accel_z: 0.0,
Expand All @@ -34,7 +34,7 @@ export const player_create = world => ({
.fill(null)
.map(() => slot_create(null)),
mouse_mode: MOUSE_MODE_NORMAL,
name: 'Gast',
name: account.label || 'Spieler',
position_x: world.spawn_x,
position_y: world.spawn_y,
position_z: world.spawn_z,
Expand Down

0 comments on commit 1fd2225

Please sign in to comment.