sending snapshots

This commit is contained in:
Thilo Behnke
2022-06-27 22:47:14 +02:00
parent 0f1f7c62e0
commit 321dda9934
2 changed files with 21 additions and 8 deletions

View File

@@ -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}

View File

@@ -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.