summaryrefslogtreecommitdiff
path: root/src/worldmessages.rs
blob: 79cd4008e669e7e1da14d636648c1cac95c79742 (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

use serde_json::{Value, json};
use serde::Serialize;
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),
	Change(Vec<(Pos, Vec<String>)>)
}

impl ToJson for WorldUpdate {
	fn to_json(&self) -> Value {
		match self {
			WorldUpdate::Field(msg) => json!(["field", msg]),
			WorldUpdate::Pos(pos) => json!(["playerpos", pos]),
			WorldUpdate::Change(changes) => json!(["changecells", changes])
		}
	}
}

#[derive(Clone, Serialize)]
pub struct FieldMessage {
	pub width: i64,
	pub height: i64,
	pub field: Vec<usize>,
	pub mapping: Vec<Vec<String>>
}