summaryrefslogtreecommitdiff
path: root/src/worldmessages.rs
blob: 8feb29f432cb31e5fb2e73d24f82ea158fff171c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

use serde_json::{Value, json};
use super::util::ToJson;
use super::pos::Pos;

#[derive(Clone)]
pub struct WorldMessage {
	pub updates: Vec<WorldUpdate>
}

impl ToJson for WorldMessage {
	fn to_json(&self) -> Value {
		let updates: Vec<Value> = self.updates.iter().map(|u| u.to_json()).collect();
		json!(["world", updates])
	}
}

#[derive(Clone)]
pub enum WorldUpdate {
	Field(FieldMessage),
	Pos(Pos)
}

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()])
		}
	}
}

#[derive(Clone)]
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
		})
	}
}