feat: add server and frontend initial - wood chopping
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { mountWorld, type WorldHandle } from '@idle/render';
|
||||
import { connectWorld, type ConnectHandlers, type NetStatus } from './net';
|
||||
import { connectWorld, type NetStatus } from './net';
|
||||
|
||||
const STATUS_TEXT: Record<NetStatus, string> = {
|
||||
connecting: 'подключение…',
|
||||
@@ -19,13 +19,14 @@
|
||||
if (host) {
|
||||
void mountWorld(host, { backgroundAlpha: 0 }).then((w) => {
|
||||
world = w;
|
||||
const handlers: ConnectHandlers = {
|
||||
disposeNet = connectWorld({
|
||||
onStatus: (s) => {
|
||||
status = s;
|
||||
},
|
||||
onSnapshot: (snap) => world?.applySnapshot(snap.avatars),
|
||||
};
|
||||
disposeNet = connectWorld(handlers);
|
||||
onWelcome: (msg) => world?.setWelcome(msg),
|
||||
onSnapshot: (msg) => world?.applySnapshot(msg),
|
||||
onDelta: (msg) => world?.applyDelta(msg),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -39,6 +40,10 @@
|
||||
<div class="root">
|
||||
<div class="world" bind:this={host}></div>
|
||||
<div class="badge">{STATUS_TEXT[status]}</div>
|
||||
<!-- онбординг — в самом виджете, чат ботом не засоряем -->
|
||||
{#if status === 'online'}
|
||||
<div class="hint">!рубить — валить лес · !стоп — отдых</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
@@ -60,4 +65,16 @@
|
||||
padding: 2px 8px;
|
||||
border-radius: 8px;
|
||||
}
|
||||
.hint {
|
||||
position: absolute;
|
||||
bottom: 14px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
font: 13px/1.4 monospace;
|
||||
color: #eef4fa;
|
||||
background: rgba(16, 19, 26, 0.6);
|
||||
padding: 5px 14px;
|
||||
border-radius: 10px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { PROTOCOL_VERSION, type ClientMessage, type ServerMessage } from '@idle/shared';
|
||||
import { PROTOCOL_VERSION, type ClientMessage, type DeltaMsg, type ServerMessage, type SnapshotMsg, type WelcomeMsg } from '@idle/shared';
|
||||
|
||||
export type Snapshot = Extract<ServerMessage, { t: 'snapshot' }>;
|
||||
export type NetStatus = 'connecting' | 'online' | 'reconnecting';
|
||||
|
||||
export function wsUrl(): string {
|
||||
@@ -13,7 +12,9 @@ export function wsUrl(): string {
|
||||
|
||||
export interface ConnectHandlers {
|
||||
onStatus: (status: NetStatus) => void;
|
||||
onSnapshot: (snap: Snapshot) => void;
|
||||
onWelcome: (msg: WelcomeMsg) => void;
|
||||
onSnapshot: (msg: SnapshotMsg) => void;
|
||||
onDelta: (msg: DeltaMsg) => void;
|
||||
}
|
||||
|
||||
/** Подключение к миру с автопереподключением; возвращает функцию закрытия. */
|
||||
@@ -41,8 +42,11 @@ export function connectWorld(handlers: ConnectHandlers): () => void {
|
||||
}
|
||||
if (msg.t === 'welcome') {
|
||||
handlers.onStatus('online');
|
||||
handlers.onWelcome(msg);
|
||||
} else if (msg.t === 'snapshot') {
|
||||
handlers.onSnapshot(msg);
|
||||
} else if (msg.t === 'delta') {
|
||||
handlers.onDelta(msg);
|
||||
}
|
||||
};
|
||||
ws.onclose = () => {
|
||||
|
||||
Reference in New Issue
Block a user