author | alfadur <mail@none> |
Mon, 23 Dec 2019 18:08:55 +0300 | |
changeset 15520 | fd3a20e9d095 |
parent 15102 | 80ff12edf5e6 |
child 15547 | 863059f61793 |
permissions | -rw-r--r-- |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
1 |
use serde_derive::{Deserialize, Serialize}; |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
2 |
|
13478 | 3 |
pub type ClientId = usize; |
4 |
pub type RoomId = usize; |
|
5 |
||
13795
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13478
diff
changeset
|
6 |
pub const MAX_HEDGEHOGS_PER_TEAM: u8 = 8; |
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13478
diff
changeset
|
7 |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12131
diff
changeset
|
8 |
#[derive(PartialEq, Eq, Clone, Debug)] |
12130 | 9 |
pub enum ServerVar { |
10 |
MOTDNew(String), |
|
11 |
MOTDOld(String), |
|
14783 | 12 |
LatestProto(u16), |
12130 | 13 |
} |
14 |
||
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12131
diff
changeset
|
15 |
#[derive(PartialEq, Eq, Clone, Debug)] |
12130 | 16 |
pub enum GameCfg { |
13422 | 17 |
FeatureSize(u32), |
18 |
MapType(String), |
|
19 |
MapGenerator(u32), |
|
20 |
MazeSize(u32), |
|
21 |
Seed(String), |
|
22 |
Template(u32), |
|
12130 | 23 |
|
13422 | 24 |
Ammo(String, Option<String>), |
13801 | 25 |
Scheme(String, Vec<String>), |
13422 | 26 |
Script(String), |
27 |
Theme(String), |
|
14457 | 28 |
DrawnMap(String), |
12130 | 29 |
} |
30 |
||
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12131
diff
changeset
|
31 |
#[derive(PartialEq, Eq, Clone, Debug)] |
12130 | 32 |
pub struct TeamInfo { |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
33 |
pub owner: String, |
13419 | 34 |
pub name: String, |
35 |
pub color: u8, |
|
36 |
pub grave: String, |
|
37 |
pub fort: String, |
|
38 |
pub voice_pack: String, |
|
39 |
pub flag: String, |
|
40 |
pub difficulty: u8, |
|
41 |
pub hedgehogs_number: u8, |
|
13795
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13478
diff
changeset
|
42 |
pub hedgehogs: [HedgehogInfo; MAX_HEDGEHOGS_PER_TEAM as usize], |
12130 | 43 |
} |
44 |
||
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12131
diff
changeset
|
45 |
#[derive(PartialEq, Eq, Clone, Debug)] |
12130 | 46 |
pub struct HedgehogInfo { |
13419 | 47 |
pub name: String, |
48 |
pub hat: String, |
|
12130 | 49 |
} |
13478 | 50 |
|
15102 | 51 |
#[derive(Clone, Serialize, Deserialize, Debug)] |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
52 |
pub struct Ammo { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
53 |
pub name: String, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
54 |
pub settings: Option<String>, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
55 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
56 |
|
15102 | 57 |
#[derive(Clone, Serialize, Deserialize, Debug)] |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
58 |
pub struct Scheme { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
59 |
pub name: String, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
60 |
pub settings: Vec<String>, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
61 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
62 |
|
15102 | 63 |
#[derive(Clone, Serialize, Deserialize, Debug)] |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
64 |
pub struct RoomConfig { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
65 |
pub feature_size: u32, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
66 |
pub map_type: String, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
67 |
pub map_generator: u32, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
68 |
pub maze_size: u32, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
69 |
pub seed: String, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
70 |
pub template: u32, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
71 |
|
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
72 |
pub ammo: Ammo, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
73 |
pub scheme: Scheme, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
74 |
pub script: String, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
75 |
pub theme: String, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
76 |
pub drawn_map: Option<String>, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
77 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
78 |
|
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
79 |
impl RoomConfig { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
80 |
pub fn new() -> RoomConfig { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
81 |
RoomConfig { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
82 |
feature_size: 12, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
83 |
map_type: "+rnd+".to_string(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
84 |
map_generator: 0, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
85 |
maze_size: 0, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
86 |
seed: "seed".to_string(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
87 |
template: 0, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
88 |
|
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
89 |
ammo: Ammo { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
90 |
name: "Default".to_string(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
91 |
settings: None, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
92 |
}, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
93 |
scheme: Scheme { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
94 |
name: "Default".to_string(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
95 |
settings: Vec::new(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
96 |
}, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
97 |
script: "Normal".to_string(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
98 |
theme: "\u{1f994}".to_string(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
99 |
drawn_map: None, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
100 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
101 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
102 |
|
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
103 |
pub fn set_config(&mut self, cfg: GameCfg) { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
104 |
match cfg { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
105 |
GameCfg::FeatureSize(s) => self.feature_size = s, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
106 |
GameCfg::MapType(t) => self.map_type = t, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
107 |
GameCfg::MapGenerator(g) => self.map_generator = g, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
108 |
GameCfg::MazeSize(s) => self.maze_size = s, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
109 |
GameCfg::Seed(s) => self.seed = s, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
110 |
GameCfg::Template(t) => self.template = t, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
111 |
|
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
112 |
GameCfg::Ammo(n, s) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
113 |
self.ammo = Ammo { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
114 |
name: n, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
115 |
settings: s, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
116 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
117 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
118 |
GameCfg::Scheme(n, s) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
119 |
self.scheme = Scheme { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
120 |
name: n, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
121 |
settings: s, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
122 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
123 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
124 |
GameCfg::Script(s) => self.script = s, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
125 |
GameCfg::Theme(t) => self.theme = t, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
126 |
GameCfg::DrawnMap(m) => self.drawn_map = Some(m), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
127 |
}; |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
128 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
129 |
|
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
130 |
pub fn to_map_config(&self) -> Vec<String> { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
131 |
vec![ |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
132 |
self.feature_size.to_string(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
133 |
self.map_type.to_string(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
134 |
self.map_generator.to_string(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
135 |
self.maze_size.to_string(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
136 |
self.seed.to_string(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
137 |
self.template.to_string(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
138 |
] |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
139 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
140 |
|
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
141 |
pub fn to_game_config(&self) -> Vec<GameCfg> { |
15074 | 142 |
use GameCfg::*; |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
143 |
let mut v = vec![ |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
144 |
Ammo(self.ammo.name.to_string(), self.ammo.settings.clone()), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
145 |
Scheme(self.scheme.name.to_string(), self.scheme.settings.clone()), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
146 |
Script(self.script.to_string()), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
147 |
Theme(self.theme.to_string()), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
148 |
]; |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
149 |
if let Some(ref m) = self.drawn_map { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
150 |
v.push(DrawnMap(m.to_string())) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
151 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
152 |
v |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
153 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
154 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
155 |
|
15102 | 156 |
#[derive(Debug)] |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
157 |
pub struct Replay { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
158 |
pub config: RoomConfig, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
159 |
pub teams: Vec<TeamInfo>, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
160 |
pub message_log: Vec<String>, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
161 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
162 |
|
13478 | 163 |
#[derive(PartialEq, Eq, Clone, Debug)] |
164 |
pub enum VoteType { |
|
165 |
Kick(String), |
|
166 |
Map(Option<String>), |
|
167 |
Pause, |
|
168 |
NewSeed, |
|
14457 | 169 |
HedgehogsPerTeam(u8), |
13478 | 170 |
} |
171 |
||
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
172 |
pub struct Vote { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
173 |
pub is_pro: bool, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
174 |
pub is_forced: bool, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
175 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
176 |
|
13478 | 177 |
#[derive(Clone, Debug)] |
178 |
pub struct Voting { |
|
179 |
pub ttl: u32, |
|
180 |
pub voters: Vec<ClientId>, |
|
181 |
pub votes: Vec<(ClientId, bool)>, |
|
14457 | 182 |
pub kind: VoteType, |
13478 | 183 |
} |
184 |
||
185 |
impl Voting { |
|
186 |
pub fn new(kind: VoteType, voters: Vec<ClientId>) -> Voting { |
|
187 |
Voting { |
|
14457 | 188 |
kind, |
189 |
voters, |
|
190 |
ttl: 2, |
|
191 |
votes: Vec::new(), |
|
13478 | 192 |
} |
193 |
} |
|
14457 | 194 |
} |