mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-07-11 02:29:25 +00:00
sending snapshots
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
<script lang="ts">
|
||||
|
||||
import {networkSessionStateEvents, sessionInputs} from "../store/session";
|
||||
import {networkEvents, networkSessionStateEvents, sessionInputs} from "../store/session";
|
||||
import type {NetworkSession} from "../store/model/session";
|
||||
import {SessionState} from "../store/model/session";
|
||||
import {SessionState, SessionType} from "../store/model/session";
|
||||
import CopyToClipboard from "./CopyToClipboard.svelte";
|
||||
import api from "../api/session";
|
||||
|
||||
@@ -16,6 +16,21 @@
|
||||
console.log("NetworkSessionWrapper ready, now setting up sessionEvents")
|
||||
joinLink = api.createJoinLink(session.session_id);
|
||||
}
|
||||
$: if(session) {
|
||||
switch(session.type) {
|
||||
case SessionType.HOST:
|
||||
networkEvents.produce({inputs: $sessionInputs, session_id: session.session_id, objects: [], player: session.you.id, ts: Date.now()})
|
||||
break;
|
||||
case SessionType.PEER:
|
||||
networkEvents.produce({inputs: $sessionInputs, session_id: session.session_id, player: session.you.id, ts: Date.now()})
|
||||
break;
|
||||
case SessionType.OBSERVER:
|
||||
networkEvents.produce({session_id: session.session_id, player: session.you.id, ts: Date.now()})
|
||||
break;
|
||||
default:
|
||||
throw new Error("session snapshot update not implemented for session type " + session.type);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
{#if !session}
|
||||
|
||||
@@ -47,11 +47,6 @@ const player2KeyboardInputs = derived(
|
||||
}
|
||||
)
|
||||
|
||||
export type NetworkEventStore = {
|
||||
subscribe: any,
|
||||
produce: (snapshot: SessionSnapshot) => void
|
||||
}
|
||||
|
||||
function createNetworkEvents() {
|
||||
const {subscribe, set, update} = writable<GameEventWrapper[]>([]);
|
||||
|
||||
@@ -94,6 +89,9 @@ function createNetworkEvents() {
|
||||
|
||||
function produce(sessionSnapshot: SessionSnapshot) {
|
||||
const ws = get(websocket);
|
||||
if (!ws) {
|
||||
return;
|
||||
}
|
||||
console.debug("producing snapshot to ws: ", sessionSnapshot);
|
||||
ws.send(JSON.stringify(sessionSnapshot));
|
||||
}
|
||||
@@ -110,7 +108,7 @@ function createNetworkEvents() {
|
||||
}
|
||||
}
|
||||
|
||||
const networkEvents = createNetworkEvents();
|
||||
export const networkEvents = createNetworkEvents();
|
||||
|
||||
// TODO: Use websocket to write snapshots.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user