From 01349724a938db9a76b7317dd6ea379d2382c36a Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Sun, 16 Feb 2020 00:42:58 -0500 Subject: [PATCH] stub "loopback" server --- src/Game/Main.ts | 3 +++ src/Net/LockstepClient.ts | 4 ++-- src/Net/LoopbackServer.ts | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/Net/LoopbackServer.ts diff --git a/src/Game/Main.ts b/src/Game/Main.ts index f45dac9..521cb09 100644 --- a/src/Game/Main.ts +++ b/src/Game/Main.ts @@ -8,6 +8,7 @@ import { Create } from "../Ecs/Data"; import {} from "../Ecs/Lockstep"; import { RunRenderBounds } from "../Ecs/Renderers"; import { LockstepClient } from "../Net/LockstepClient"; +import { Loopback } from "../Net/LoopbackServer"; import { Data, Engine } from "./GameComponents"; import { Buttons } from "./Input"; @@ -20,6 +21,8 @@ export class Main extends LockstepClient { super(new Engine()); keys.setHandler(this.buttons); + this.connect(Loopback); + subscribe((frame: Data) => { const drawSet = new DrawSet(); diff --git a/src/Net/LockstepClient.ts b/src/Net/LockstepClient.ts index 7d09322..2f1d0d9 100644 --- a/src/Net/LockstepClient.ts +++ b/src/Net/LockstepClient.ts @@ -13,13 +13,13 @@ export const enum MessageTypes { export type Packet = { t: TypeId } & Payload; export type ClientMessage = - | Packet }>; + | Packet; export type ServerMessage = | Packet }> | Packet; -type Server = Callbag, ServerMessage>; +export type Server = Callbag, ServerMessage>; export abstract class LockstepClient { diff --git a/src/Net/LoopbackServer.ts b/src/Net/LoopbackServer.ts new file mode 100644 index 0000000..e2bbb1b --- /dev/null +++ b/src/Net/LoopbackServer.ts @@ -0,0 +1,24 @@ +import { Callbag } from "callbag"; + +import { ClientMessage, MessageTypes, ServerMessage } from "./LockstepClient"; + +type Client = Callbag, ClientMessage>; + +export function Loopback(start: number, data?: Client | ClientMessage) { + if(start != 0) return; + + const sink = data as Client; + + sink(0, (type: number, data?: Client | ClientMessage) => { + if(type == 1) { + // message from client; just reflect for now + const message = data as ClientMessage; + switch(message.t) { + case MessageTypes.INPUT: + sink(1, message); + break; + } + } + }); + sink(1, {t: MessageTypes.RESET, s: {}}); +};