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