mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-07-11 02:29:25 +00:00
fix inputs
This commit is contained in:
@@ -9,8 +9,9 @@ import {getPlayerKeyboardInputs, playerKeyboardInputs} from "./input";
|
||||
import type {Subscriber} from "svelte/types/runtime/store";
|
||||
import {combined} from "./utils";
|
||||
import type {Input} from "./model/input";
|
||||
import {init} from "svelte/internal";
|
||||
|
||||
const initialSession = writable<Session>(null);
|
||||
const sessionStore = writable<Session>(null);
|
||||
|
||||
function createNetworkEvents() {
|
||||
const {subscribe, set, update} = writable<GameEventWrapper[]>([]);
|
||||
@@ -20,7 +21,7 @@ function createNetworkEvents() {
|
||||
const playerId = writable<string>(null);
|
||||
const lastSnapshot = writable<SessionSnapshot>(null);
|
||||
|
||||
const unsubscribeSession = initialSession.subscribe(session => {
|
||||
const unsubscribeSession = sessionStore.subscribe(session => {
|
||||
if (!session || isLocalSession(session)) {
|
||||
return;
|
||||
}
|
||||
@@ -116,6 +117,16 @@ export const networkSessionStateEvents = readable<SessionEventPayload[]>([], set
|
||||
}
|
||||
cache.set([...get(cache), ...sessionEvents]);
|
||||
set(get(cache))
|
||||
|
||||
const latestSessionEvent = sessionEvents[sessionEvents.length - 1] as SessionEventPayload;
|
||||
const currentSession = get(sessionStore) as NetworkSession;
|
||||
const session: Session = {
|
||||
...(latestSessionEvent.session as NetworkSession),
|
||||
you: currentSession.you,
|
||||
type: currentSession.type
|
||||
}
|
||||
console.debug("updating current session: ", session)
|
||||
sessionStore.set(session);
|
||||
})
|
||||
|
||||
return () => {
|
||||
@@ -132,7 +143,7 @@ export const networkMoveEvents = derived(networkEvents, $sessionEvents => {
|
||||
return moveEvents.slice(moveEvents.length - 7)
|
||||
})
|
||||
|
||||
const networkInputEvents = derived([networkEvents, initialSession], ([$sessionEvents, $sessionStore]) => $sessionEvents.filter(wrapper => {
|
||||
const networkInputEvents = derived([networkEvents, sessionStore], ([$sessionEvents, $sessionStore]) => $sessionEvents.filter(wrapper => {
|
||||
if (!isInputEvent(wrapper)) {
|
||||
return false;
|
||||
}
|
||||
@@ -140,7 +151,7 @@ const networkInputEvents = derived([networkEvents, initialSession], ([$sessionEv
|
||||
}).map(({event}) => event as InputEventPayload));
|
||||
|
||||
const getPlayerNetworkInputEvents = (player_nr: number): Readable<Input[]> => derived(networkInputEvents, $networkInputEvents => {
|
||||
const session = get(initialSession);
|
||||
const session = get(sessionStore);
|
||||
if (!isNetworkSession(session)) {
|
||||
return [] as Input[];
|
||||
}
|
||||
@@ -191,7 +202,7 @@ export const localSession = () => readable<SessionStore>(null, function (set) {
|
||||
set({loading: true});
|
||||
setTimeout(() => {
|
||||
set({loading: false, session});
|
||||
initialSession.set(session);
|
||||
sessionStore.set(session);
|
||||
}, 2_000);
|
||||
})
|
||||
|
||||
@@ -199,30 +210,15 @@ export type SessionStore = NetworkStore & {
|
||||
session?: Session
|
||||
}
|
||||
|
||||
export const sessionStore = derived(networkSessionStateEvents, $sessionEvents => {
|
||||
if (!$sessionEvents.length) {
|
||||
return get(initialSession);
|
||||
}
|
||||
const latestSessionEvent = $sessionEvents[$sessionEvents.length - 1] as SessionEventPayload;
|
||||
const currentSession = get(initialSession) as NetworkSession;
|
||||
const session: Session = {
|
||||
...(latestSessionEvent.session as NetworkSession),
|
||||
you: currentSession.you,
|
||||
type: currentSession.type
|
||||
}
|
||||
console.debug("updating current session: ", session)
|
||||
return session;
|
||||
})
|
||||
|
||||
export const networkSession = (type: SessionType.HOST | SessionType.PEER | SessionType.OBSERVER, sessionId?: string) => readable<SessionStore>(null, function (set) {
|
||||
function sessionCreator(fn) {
|
||||
set({loading: true});
|
||||
fn().then(session => {
|
||||
set({loading: false, session});
|
||||
initialSession.set(session);
|
||||
sessionStore.set(session);
|
||||
}).catch(e => {
|
||||
set({loading: false, error: {value: e, at: performance.now()}});
|
||||
initialSession.set(null);
|
||||
sessionStore.set(null);
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user