diff options
| author | troido <troido@protonmail.com> | 2020-02-03 20:16:09 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-03 20:16:09 +0100 |
| commit | 53f358f73c37e86f4db9e7bd7af309697dc7237e (patch) | |
| tree | d118cdfb10e1519995a92ac0c58db1051c41325d /src/worldmessages.rs | |
| parent | 30509956d4274b2565052dc045eec3742a159357 (diff) | |
only send changed cells, not the whole field each time
Diffstat (limited to 'src/worldmessages.rs')
| -rw-r--r-- | src/worldmessages.rs | 21 |
1 files changed, 7 insertions, 14 deletions
diff --git a/src/worldmessages.rs b/src/worldmessages.rs index 8feb29f..8d9d8c8 100644 --- a/src/worldmessages.rs +++ b/src/worldmessages.rs @@ -1,5 +1,6 @@ use serde_json::{Value, json}; +use serde::Serialize; use super::util::ToJson; use super::pos::Pos; @@ -18,35 +19,27 @@ impl ToJson for WorldMessage { #[derive(Clone)] pub enum WorldUpdate { Field(FieldMessage), - Pos(Pos) + Pos(Pos), + Change(Vec<(Pos, Vec<String>)>) } impl ToJson for WorldUpdate { fn to_json(&self) -> Value { match self { - WorldUpdate::Field(msg) => Value::Array(vec![Value::String("field".to_string()), msg.to_json()]), - WorldUpdate::Pos(pos) => Value::Array(vec![Value::String("playerpos".to_string()), pos.to_json()]) + WorldUpdate::Field(msg) => json!(["field", msg]), + WorldUpdate::Pos(pos) => json!(["playerpos", pos]), + WorldUpdate::Change(changes) => json!(["changecells", changes]) } } } -#[derive(Clone)] +#[derive(Clone, Serialize)] pub struct FieldMessage { pub width: i32, pub height: i32, pub field: Vec<usize>, pub mapping: Vec<Vec<String>> } -impl ToJson for FieldMessage { - fn to_json(&self) -> Value { - json!({ - "width": self.width, - "height": self.height, - "field": self.field, - "mapping": self.mapping - }) - } -} |
