2020-02-16 05:42:58 +00:00
|
|
|
import { Callbag } from "callbag";
|
|
|
|
|
|
|
|
import { ClientMessage, MessageTypes, ServerMessage } from "./LockstepClient";
|
|
|
|
|
2020-05-02 02:51:21 +00:00
|
|
|
type Client<LocalInput, State> = Callbag<ServerMessage<LocalInput[], State>, ClientMessage<LocalInput, State>>;
|
2020-02-16 05:42:58 +00:00
|
|
|
|
2020-05-02 02:51:21 +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;
|
|
|
|
|
2020-05-02 02:51:21 +00:00
|
|
|
const sink = data as Client<LocalInput, State>;
|
2020-02-16 05:42:58 +00:00
|
|
|
|
2020-05-02 02:51:21 +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
|
2020-05-02 02:51:21 +00:00
|
|
|
const message = data as ClientMessage<LocalInput, State>;
|
2020-02-16 05:42:58 +00:00
|
|
|
switch(message.t) {
|
|
|
|
case MessageTypes.INPUT:
|
2020-05-02 01:45:31 +00:00
|
|
|
sink(1, {
|
|
|
|
t: MessageTypes.INPUT,
|
|
|
|
i: [message.i],
|
|
|
|
});
|
2020-02-16 05:42:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-05-02 01:45:31 +00:00
|
|
|
sink(1, {t: MessageTypes.SET_STATE, u: 0, s: {}});
|
2020-02-16 05:42:58 +00:00
|
|
|
};
|