Update server/static/main.js

This commit is contained in:
2025-11-13 15:48:48 +00:00
parent 713f7e875d
commit 15cf5cd026

View File

@@ -7,7 +7,7 @@ import { MapView } from './map.js';
const socket = io(); const socket = io();
// global-ish state for modules // shared state
const state = { const state = {
me: null, me: null,
world: null, world: null,
@@ -16,7 +16,6 @@ const state = {
nodes: [] nodes: []
}; };
// expose to UIs if they need it
window.GameState = state; window.GameState = state;
window.GameSocket = socket; window.GameSocket = socket;
@@ -57,14 +56,13 @@ class GameScene extends Phaser.Scene {
if (res?.ok) { if (res?.ok) {
window.updateXP?.(res.level); window.updateXP?.(res.level);
window.addChatLine?.(`You gather 1 ${res.item} (+${res.xp} xp)`); window.addChatLine?.(`You gather 1 ${res.item} (+${res.xp} xp)`);
// inventory not re-fetched here for brevity
} else if (res?.error) { } else if (res?.error) {
window.addChatLine?.(`Gather failed: ${res.error}`); window.addChatLine?.(`Gather failed: ${res.error}`);
} }
}); });
}); });
// socket updates hooked to MapView // resource nodes from server
socket.on('nodes:init', list => { socket.on('nodes:init', list => {
state.nodes = list; state.nodes = list;
this.mapView.replaceNodes(list); this.mapView.replaceNodes(list);
@@ -72,12 +70,14 @@ class GameScene extends Phaser.Scene {
socket.on('node:update', u => { socket.on('node:update', u => {
this.mapView.updateNode(u); this.mapView.updateNode(u);
}); });
// player positions
socket.on('state', snap => { socket.on('state', snap => {
state.players = snap.players; state.players = snap.players;
this.mapView.updateOtherPlayers(snap.players); this.mapView.updateOtherPlayers(snap.players);
}); });
// zoom from HUD // HUD zoom hook
window.setGameZoom = z => this.mapView.setZoom(z); window.setGameZoom = z => this.mapView.setZoom(z);
} }
update(time, delta) { update(time, delta) {
@@ -86,18 +86,18 @@ class GameScene extends Phaser.Scene {
} }
async function boot() { async function boot() {
// mount UIs // Mount UI modules
setupAuthPanel(document.getElementById('ui-auth'), socket, bootAfterLogin); setupAuthPanel(document.getElementById('ui-auth'), socket, bootAfterLogin);
setupHUD(document.getElementById('ui-hud')); setupHUD(document.getElementById('ui-hud'));
setupChat(document.getElementById('ui-chat'), socket); setupChat(document.getElementById('ui-chat'), socket);
setupInventory(document.getElementById('ui-inventory')); setupInventory(document.getElementById('ui-inventory'));
setupCrafting(document.getElementById('ui-crafting')); setupCrafting(document.getElementById('ui-crafting'));
// attempt auto-login // auto-login attempt
const meData = await fetchMe(); const meData = await fetchMe();
if (!meData) { if (!meData) {
// show auth const authPanel = document.getElementById('ui-auth').firstElementChild;
document.getElementById('ui-auth').firstElementChild?.classList.remove('hidden'); if (authPanel) authPanel.classList.remove('hidden');
return; return;
} }
await bootAfterLogin(); await bootAfterLogin();
@@ -110,18 +110,19 @@ async function bootAfterLogin() {
state.inventory = data.inventory; state.inventory = data.inventory;
state.world = data.world; state.world = data.world;
// join socket world // join world socket
await new Promise(resolve => { await new Promise(resolve => {
socket.emit('auth:join', ack => { socket.emit('auth:join', ack => resolve(ack));
resolve(ack);
});
}); });
document.getElementById('ui-auth').innerHTML = ''; // hide auth // show HUD + chat; hide auth
document.getElementById('ui-hud').firstElementChild?.classList.remove('hidden'); document.getElementById('ui-auth').innerHTML = '';
document.getElementById('ui-chat').firstElementChild?.classList.remove('hidden'); const hudPanel = document.getElementById('ui-hud').firstElementChild;
if (hudPanel) hudPanel.classList.remove('hidden');
const chatPanel = document.getElementById('ui-chat').firstElementChild;
if (chatPanel) chatPanel.classList.remove('hidden');
// start Phaser game // start Phaser
const config = { const config = {
type: Phaser.AUTO, type: Phaser.AUTO,
parent: 'game', parent: 'game',