Create a PlayerId typedef, don't overload -1 for unassigned.

This commit is contained in:
Tangent Wantwight 2020-05-26 23:32:28 -04:00
parent 764e4d85cf
commit 750cfe1630
3 changed files with 9 additions and 7 deletions

View file

@ -42,7 +42,7 @@ export type ClientMessage<LocalInput, State> =
export type ServerMessage<GlobalInput, State> = export type ServerMessage<GlobalInput, State> =
| Packet<MessageTypes.META, Meta> | Packet<MessageTypes.META, Meta>
| Packet<MessageTypes.SET_STATE, { u: number, s: Partial<State> }> | Packet<MessageTypes.SET_STATE, { u: number | null, 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 }>
; ;
@ -51,7 +51,7 @@ export type Server<LocalInput, GlobalInput, State> = Callbag<ClientMessage<Local
export abstract class LockstepClient<LocalInput, GlobalInput, State> { export abstract class LockstepClient<LocalInput, GlobalInput, State> {
private playerNumber = -1; private playerNumber: number | null = null;
private state: LockstepState<LocalInput, GlobalInput, State>; private state: LockstepState<LocalInput, GlobalInput, State>;
private serverTalkback: Server<LocalInput, GlobalInput, State> | null = null; private serverTalkback: Server<LocalInput, GlobalInput, State> | null = null;
@ -100,7 +100,7 @@ export abstract class LockstepClient<LocalInput, GlobalInput, State> {
private sampleInput = () => { private sampleInput = () => {
if (this.serverTalkback) { if (this.serverTalkback) {
const input = this.gatherInput(); const input = this.gatherInput();
if(this.playerNumber >= 0) { if(this.playerNumber !== null) {
this.state.addLocalInput(this.playerNumber, input); this.state.addLocalInput(this.playerNumber, input);
this.serverTalkback(1, { t: MessageTypes.INPUT, i: input }); this.serverTalkback(1, { t: MessageTypes.INPUT, i: input });
} }

View file

@ -3,6 +3,8 @@ use serde_json::Value;
pub mod server; pub mod server;
pub type PlayerId = usize;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
#[serde(tag = "t")] #[serde(tag = "t")]
pub enum ClientMessage<I = Value, S = Value> { pub enum ClientMessage<I = Value, S = Value> {
@ -14,13 +16,13 @@ pub enum ClientMessage<I = Value, S = Value> {
GetState { c: usize, s: S }, GetState { c: usize, s: S },
} }
#[derive(Serialize, Debug)] #[derive(Serialize, Debug, Clone)]
pub struct Meta { pub struct Meta {
pub helo: Option<String>, pub helo: Option<String>,
pub version: &'static str, pub version: &'static str,
} }
#[derive(Serialize, Debug)] #[derive(Serialize, Debug, Clone)]
#[serde(tag = "t")] #[serde(tag = "t")]
pub enum ServerMessage<I = Vec<Value>, S = Value> { pub enum ServerMessage<I = Vec<Value>, S = Value> {
#[serde(rename = "m")] #[serde(rename = "m")]
@ -29,7 +31,7 @@ pub enum ServerMessage<I = Vec<Value>, S = Value> {
m: Meta, m: Meta,
}, },
#[serde(rename = "s")] #[serde(rename = "s")]
SetState { u: i8, s: S }, SetState { u: Option<PlayerId>, s: S },
#[serde(rename = "i")] #[serde(rename = "i")]
Input { i: I }, Input { i: I },
#[serde(rename = "g")] #[serde(rename = "g")]

View file

@ -16,7 +16,7 @@ where
helo: Some("Dedicated base2020 server".into()), helo: Some("Dedicated base2020 server".into()),
}, },
}, },
ServerMessage::SetState { u: 0, s: json!({}) }, ServerMessage::SetState { u: Some(0), s: json!({}) },
]) ])
.map(Ok); .map(Ok);