base2020/src/Net/LoopbackServer.ts

29 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-02-16 05:42:58 +00:00
import { Callbag } from "callbag";
import { ClientMessage, MessageTypes, ServerMessage } from "./LockstepClient";
type Client<LocalInput, State> = Callbag<ServerMessage<LocalInput[], State>, ClientMessage<LocalInput, State>>;
2020-02-16 05:42:58 +00:00
/** Stub loopback server that handles a single client, for schemes where GlobalInput = LocalInput[] */
export function Loopback<LocalInput, State>(start: number, data?: Client<LocalInput, State> | ClientMessage<LocalInput, State>) {
2020-02-16 05:42:58 +00:00
if(start != 0) return;
const sink = data as Client<LocalInput, State>;
2020-02-16 05:42:58 +00:00
sink(0, (type: number, data?: Client<LocalInput, State> | ClientMessage<LocalInput, State>) => {
2020-02-16 05:42:58 +00:00
if(type == 1) {
// message from client; just reflect for now
const message = data as ClientMessage<LocalInput, State>;
2020-02-16 05:42:58 +00:00
switch(message.t) {
case MessageTypes.INPUT:
sink(1, {
t: MessageTypes.INPUT,
i: [message.i],
});
2020-02-16 05:42:58 +00:00
break;
}
}
});
sink(1, {t: MessageTypes.SET_STATE, u: 0, s: {}});
2020-02-16 05:42:58 +00:00
};