Change message type enum to serde-friendly strings; remove unused Ping message

This commit is contained in:
Tangent Wantwight 2020-05-12 23:00:06 -04:00
parent a22ca95d3d
commit ca5bf65e9d

View file

@ -10,27 +10,22 @@ export const enum MessageTypes {
/** /**
* Reserved, likely for transient messages that wouldn't make sense appearing in a replay * Reserved, likely for transient messages that wouldn't make sense appearing in a replay
*/ */
META = 0, META = "m",
/** /**
* From server: initialize client state / load new level, plus informing client of their "controller number" * From server: initialize client state / load new level, plus informing client of their "controller number"
* From client: (op only) force a new game state, likely loading a level * From client: (op only) force a new game state, likely loading a level
*/ */
SET_STATE = 1, SET_STATE = "s",
/** /**
* From server: array of canonical inputs for a frame * From server: array of canonical inputs for a frame
* From client: input for their controller for the next frame (TBD: strategy for handling late inputs) * From client: input for their controller for the next frame (TBD: strategy for handling late inputs)
*/ */
INPUT = 2, INPUT = "i",
/** /**
* From server: issue request for the current game state, with a cookie to identify the response * From server: issue request for the current game state, with a cookie to identify the response
* From client: serialized game state, with the cookie of the request * From client: serialized game state, with the cookie of the request
*/ */
GET_STATE = 3, GET_STATE = "g",
/**
* For liveness checks / latency estimate. Echos a cookie.
* May be refactored into one of the above messages.
*/
PING = 4
} }
export type Packet<TypeId, Payload> = { t: TypeId } & Payload; export type Packet<TypeId, Payload> = { t: TypeId } & Payload;
@ -39,14 +34,12 @@ export type ClientMessage<LocalInput, State> =
| Packet<MessageTypes.SET_STATE, { s: Partial<State> }> | Packet<MessageTypes.SET_STATE, { s: Partial<State> }>
| Packet<MessageTypes.INPUT, { i: LocalInput }> | Packet<MessageTypes.INPUT, { i: LocalInput }>
| Packet<MessageTypes.GET_STATE, { c: number, s: State }> | Packet<MessageTypes.GET_STATE, { c: number, s: State }>
| Packet<MessageTypes.PING, { z: number }>
; ;
export type ServerMessage<GlobalInput, State> = export type ServerMessage<GlobalInput, State> =
| Packet<MessageTypes.SET_STATE, { u: number, s: Partial<State> }> | Packet<MessageTypes.SET_STATE, { u: number, s: Partial<State> }>
| Packet<MessageTypes.INPUT, { i: GlobalInput }> | Packet<MessageTypes.INPUT, { i: GlobalInput }>
| Packet<MessageTypes.GET_STATE, { c: number }> | Packet<MessageTypes.GET_STATE, { c: number }>
| Packet<MessageTypes.PING, { z: number }>
; ;
export type Server<LocalInput, GlobalInput, State> = Callbag<ClientMessage<LocalInput, State>, ServerMessage<GlobalInput, State>>; export type Server<LocalInput, GlobalInput, State> = Callbag<ClientMessage<LocalInput, State>, ServerMessage<GlobalInput, State>>;