stub "loopback" server

This commit is contained in:
Tangent Wantwight 2020-02-16 00:42:58 -05:00
parent 2bff1f6cdf
commit 01349724a9
3 changed files with 29 additions and 2 deletions

View file

@ -8,6 +8,7 @@ import { Create } from "../Ecs/Data";
import {} from "../Ecs/Lockstep"; import {} from "../Ecs/Lockstep";
import { RunRenderBounds } from "../Ecs/Renderers"; import { RunRenderBounds } from "../Ecs/Renderers";
import { LockstepClient } from "../Net/LockstepClient"; import { LockstepClient } from "../Net/LockstepClient";
import { Loopback } from "../Net/LoopbackServer";
import { Data, Engine } from "./GameComponents"; import { Data, Engine } from "./GameComponents";
import { Buttons } from "./Input"; import { Buttons } from "./Input";
@ -20,6 +21,8 @@ export class Main extends LockstepClient<KeyName[], Data> {
super(new Engine()); super(new Engine());
keys.setHandler(this.buttons); keys.setHandler(this.buttons);
this.connect(Loopback);
subscribe((frame: Data) => { subscribe((frame: Data) => {
const drawSet = new DrawSet(); const drawSet = new DrawSet();

View file

@ -13,13 +13,13 @@ export const enum MessageTypes {
export type Packet<TypeId, Payload> = { t: TypeId } & Payload; export type Packet<TypeId, Payload> = { t: TypeId } & Payload;
export type ClientMessage<Input, State> = export type ClientMessage<Input, State> =
| Packet<MessageTypes.INPUT, { i: Partial<Input> }>; | Packet<MessageTypes.INPUT, { i: Input }>;
export type ServerMessage<Input, State> = export type ServerMessage<Input, State> =
| Packet<MessageTypes.RESET, { s: Partial<State> }> | Packet<MessageTypes.RESET, { s: Partial<State> }>
| Packet<MessageTypes.INPUT, { i: Input }>; | Packet<MessageTypes.INPUT, { i: Input }>;
type Server<Input, State> = Callbag<ClientMessage<Input, State>, ServerMessage<Input, State>>; export type Server<Input, State> = Callbag<ClientMessage<Input, State>, ServerMessage<Input, State>>;
export abstract class LockstepClient<Input, State> { export abstract class LockstepClient<Input, State> {

24
src/Net/LoopbackServer.ts Normal file
View file

@ -0,0 +1,24 @@
import { Callbag } from "callbag";
import { ClientMessage, MessageTypes, ServerMessage } from "./LockstepClient";
type Client<Input, State> = Callbag<ServerMessage<Input, State>, ClientMessage<Input, State>>;
export function Loopback<Input, State>(start: number, data?: Client<Input, State> | ClientMessage<Input, State>) {
if(start != 0) return;
const sink = data as Client<Input, State>;
sink(0, (type: number, data?: Client<Input, State> | ClientMessage<Input, State>) => {
if(type == 1) {
// message from client; just reflect for now
const message = data as ClientMessage<Input, State>;
switch(message.t) {
case MessageTypes.INPUT:
sink(1, message);
break;
}
}
});
sink(1, {t: MessageTypes.RESET, s: {}});
};