summaryrefslogtreecommitdiff
path: root/src/controls.rs
blob: a27348da01631cc2ddac1d771bf910cc300c3e70 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75


use serde_json::Value;
use crate::{PlayerId, Pos};

#[derive(Debug, Clone)]
pub enum Direction {
	North,
	South,
	East,
	West,
	None
}

impl Direction {
	fn from_json(val: &Value) -> Option<Direction>{
		match val {
			Value::String(txt) => match txt.as_str() {
				"north" => Some(Direction::North),
				"south" => Some(Direction::South),
				"east" => Some(Direction::East),
				"west"=> Some(Direction::West),
				"" => Some(Direction::None),
				_ => None
			}
			Value::Null => Some(Direction::None),
			_ => None
		}
	}
	
	pub fn to_position(&self) -> Pos {
		match self {
			Direction::North => Pos::new(0, -1),
			Direction::South => Pos::new(0, 1),
			Direction::East => Pos::new(1, 0),
			Direction::West => Pos::new(-1, 0),
			Direction::None => Pos::new(0, 0)
		}
	}
}

#[derive(Debug, Clone)]
pub enum Control {
	Move(Direction),
	Take(u64),
	Drop(u64)
}


impl Control {
	pub fn from_json(val: &Value) -> Option<Control>{
		if let Value::String(control_type) = &val[0] {
			match control_type.as_str() {
				"move" => match Direction::from_json(&val[1]) {
					Some(dir) => Some(Control::Move(dir)),
					None => None
				},
				"take" => Some(Control::Take(0)), /*match val[1].as_u64() {
					Some(rank) => Some(Control::Take(rank)),
					_ => None
				}*/
				"drop" => Some(Control::Drop(0)),
				_ => None
			}
		} else {None}
	}
}

#[derive(Debug, Clone)]
pub enum Action {
	Join(PlayerId),
	Leave(PlayerId),
	Input(PlayerId, Control)
}