summaryrefslogtreecommitdiff
path: root/src/worldmessages.rs
blob: 34b6a6b9aedcf2e092d6bbfdc066e71671f7b686 (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
53

use serde_json::{Value, json};
use super::util::ToJson;
// use serde::Serialize;

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

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

#[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
		})
	}
}