131 AddVote { |
116 AddVote { |
132 vote: bool, |
117 vote: bool, |
133 is_forced: bool, |
118 is_forced: bool, |
134 }, |
119 }, |
135 ApplyVoting(VoteType, RoomId), |
120 ApplyVoting(VoteType, RoomId), |
136 Warn(String), |
|
137 ProtocolError(String), |
|
138 } |
121 } |
139 |
122 |
140 use self::Action::*; |
123 use self::Action::*; |
141 |
124 |
142 pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
125 pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
143 match action { |
126 match action { |
144 Send(msg) => server.send(client_id, &msg.destination, msg.message), |
|
145 CheckRegistered => { |
|
146 let client = &server.clients[client_id]; |
|
147 if client.protocol_number > 0 && client.nick != "" { |
|
148 let has_nick_clash = server |
|
149 .clients |
|
150 .iter() |
|
151 .any(|(id, c)| id != client_id && c.nick == client.nick); |
|
152 |
|
153 let actions = if !client.is_checker() && has_nick_clash { |
|
154 if client.protocol_number < 38 { |
|
155 //ByeClient("Nickname is already in use".to_string()) |
|
156 vec![] |
|
157 } else { |
|
158 server.clients[client_id].nick.clear(); |
|
159 vec![Notice("NickAlreadyInUse".to_string()).send_self().action()] |
|
160 } |
|
161 } else { |
|
162 vec![JoinLobby] |
|
163 }; |
|
164 server.react(client_id, actions); |
|
165 } |
|
166 } |
|
167 JoinLobby => { |
|
168 server.clients[client_id].room_id = Some(server.lobby_id); |
|
169 |
|
170 let mut lobby_nicks = Vec::new(); |
|
171 for (_, c) in server.clients.iter() { |
|
172 if c.room_id.is_some() { |
|
173 lobby_nicks.push(c.nick.clone()); |
|
174 } |
|
175 } |
|
176 let joined_msg = LobbyJoined(lobby_nicks); |
|
177 |
|
178 let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]); |
|
179 let flags_msg = ClientFlags( |
|
180 "+i".to_string(), |
|
181 server |
|
182 .clients |
|
183 .iter() |
|
184 .filter(|(_, c)| c.room_id.is_some()) |
|
185 .map(|(_, c)| c.nick.clone()) |
|
186 .collect(), |
|
187 ); |
|
188 let server_msg = ServerMessage("\u{1f994} is watching".to_string()); |
|
189 let rooms_msg = Rooms( |
|
190 server |
|
191 .rooms |
|
192 .iter() |
|
193 .filter(|(id, _)| *id != server.lobby_id) |
|
194 .flat_map(|(_, r)| r.info(r.master_id.map(|id| &server.clients[id]))) |
|
195 .collect(), |
|
196 ); |
|
197 server.react( |
|
198 client_id, |
|
199 vec![ |
|
200 everyone_msg.send_all().but_self().action(), |
|
201 joined_msg.send_self().action(), |
|
202 flags_msg.send_self().action(), |
|
203 server_msg.send_self().action(), |
|
204 rooms_msg.send_self().action(), |
|
205 ], |
|
206 ); |
|
207 } |
|
208 RemoveRoom(room_id) => { |
|
209 let r = &mut server.rooms[room_id]; |
|
210 let actions = vec![RoomRemove(r.name.clone()) |
|
211 .send_all() |
|
212 .with_protocol(r.protocol_number) |
|
213 .action()]; |
|
214 server.rooms.remove(room_id); |
|
215 server.react(client_id, actions); |
|
216 } |
|
217 MoveToRoom(room_id) => { |
|
218 let r = &mut server.rooms[room_id]; |
|
219 let c = &mut server.clients[client_id]; |
|
220 r.players_number += 1; |
|
221 c.room_id = Some(room_id); |
|
222 |
|
223 let is_master = r.master_id == Some(c.id); |
|
224 c.set_is_master(is_master); |
|
225 c.set_is_ready(is_master); |
|
226 c.set_is_joined_mid_game(false); |
|
227 |
|
228 if is_master { |
|
229 r.ready_players_number += 1; |
|
230 } |
|
231 |
|
232 let mut v = vec![ |
|
233 RoomJoined(vec![c.nick.clone()]) |
|
234 .send_all() |
|
235 .in_room(room_id) |
|
236 .action(), |
|
237 ClientFlags("+i".to_string(), vec![c.nick.clone()]) |
|
238 .send_all() |
|
239 .action(), |
|
240 SendRoomUpdate(None), |
|
241 ]; |
|
242 |
|
243 if !r.greeting.is_empty() { |
|
244 v.push( |
|
245 ChatMsg { |
|
246 nick: "[greeting]".to_string(), |
|
247 msg: r.greeting.clone(), |
|
248 } |
|
249 .send_self() |
|
250 .action(), |
|
251 ); |
|
252 } |
|
253 |
|
254 if !c.is_master() { |
|
255 let team_names: Vec<_>; |
|
256 if let Some(ref mut info) = r.game_info { |
|
257 c.set_is_in_game(true); |
|
258 c.set_is_joined_mid_game(true); |
|
259 |
|
260 { |
|
261 let teams = info.client_teams(c.id); |
|
262 c.teams_in_game = teams.clone().count() as u8; |
|
263 c.clan = teams.clone().next().map(|t| t.color); |
|
264 team_names = teams.map(|t| t.name.clone()).collect(); |
|
265 } |
|
266 |
|
267 if !team_names.is_empty() { |
|
268 info.left_teams.retain(|name| !team_names.contains(&name)); |
|
269 info.teams_in_game += team_names.len() as u8; |
|
270 r.teams = info |
|
271 .teams_at_start |
|
272 .iter() |
|
273 .filter(|(_, t)| !team_names.contains(&t.name)) |
|
274 .cloned() |
|
275 .collect(); |
|
276 } |
|
277 } else { |
|
278 team_names = Vec::new(); |
|
279 } |
|
280 |
|
281 v.push(SendRoomData { |
|
282 to: client_id, |
|
283 teams: true, |
|
284 config: true, |
|
285 flags: true, |
|
286 }); |
|
287 |
|
288 if let Some(ref info) = r.game_info { |
|
289 v.push(RunGame.send_self().action()); |
|
290 v.push( |
|
291 ClientFlags("+g".to_string(), vec![c.nick.clone()]) |
|
292 .send_all() |
|
293 .in_room(r.id) |
|
294 .action(), |
|
295 ); |
|
296 v.push( |
|
297 ForwardEngineMessage(vec![to_engine_msg("e$spectate 1".bytes())]) |
|
298 .send_self() |
|
299 .action(), |
|
300 ); |
|
301 v.push( |
|
302 ForwardEngineMessage(info.msg_log.clone()) |
|
303 .send_self() |
|
304 .action(), |
|
305 ); |
|
306 |
|
307 for name in &team_names { |
|
308 v.push( |
|
309 ForwardEngineMessage(vec![to_engine_msg( |
|
310 once(b'G').chain(name.bytes()), |
|
311 )]) |
|
312 .send_all() |
|
313 .in_room(r.id) |
|
314 .action(), |
|
315 ); |
|
316 } |
|
317 if info.is_paused { |
|
318 v.push( |
|
319 ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
|
320 .send_all() |
|
321 .in_room(r.id) |
|
322 .action(), |
|
323 ) |
|
324 } |
|
325 } |
|
326 } |
|
327 server.react(client_id, v); |
|
328 } |
|
329 SendRoomData { |
127 SendRoomData { |
330 to, |
128 to, |
331 teams, |
129 teams, |
332 config, |
130 config, |
333 flags, |
131 flags, |
334 } => { |
132 } => { |
335 let mut actions = Vec::new(); |
133 let mut actions = Vec::new(); |
336 let room_id = server.clients[client_id].room_id; |
134 let room_id = server.clients[client_id].room_id; |
337 if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) { |
135 if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) { |
338 if config { |
136 if config { |
339 actions.push( |
137 /* actions.push( |
340 ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config()) |
138 ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config()) |
341 .send(to) |
139 .send(to) |
342 .action(), |
140 .action(), |
343 ); |
141 )*/ |
|
142 ; |
344 for cfg in r.game_config() { |
143 for cfg in r.game_config() { |
345 actions.push(cfg.to_server_msg().send(to).action()); |
144 //actions.push(cfg.to_server_msg().send(to).action()); |
346 } |
145 } |
347 } |
146 } |
348 if teams { |
147 if teams { |
349 let current_teams = match r.game_info { |
148 let current_teams = match r.game_info { |
350 Some(ref info) => &info.teams_at_start, |
149 Some(ref info) => &info.teams_at_start, |
351 None => &r.teams, |
150 None => &r.teams, |
352 }; |
151 }; |
353 for (owner_id, team) in current_teams.iter() { |
152 for (owner_id, team) in current_teams.iter() { |
354 actions.push( |
153 /*actions.push( |
355 TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team)) |
154 TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team)) |
356 .send(to) |
155 .send(to) |
357 .action(), |
156 .action(), |
358 ); |
157 ); |
359 actions.push(TeamColor(team.name.clone(), team.color).send(to).action()); |
158 actions.push(TeamColor(team.name.clone(), team.color).send(to).action()); |
360 actions.push( |
159 actions.push( |
361 HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
160 HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
362 .send(to) |
161 .send(to) |
363 .action(), |
162 .action(), |
364 ); |
163 );*/ |
365 } |
164 } |
366 } |
165 } |
367 if flags { |
166 if flags { |
368 if let Some(id) = r.master_id { |
167 if let Some(id) = r.master_id { |
369 actions.push( |
168 /* |
370 ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()]) |
169 actions.push( |
371 .send(to) |
170 ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()]) |
372 .action(), |
171 .send(to) |
373 ); |
172 .action(), |
|
173 ); |
|
174 */ |
374 } |
175 } |
375 let nicks: Vec<_> = server |
176 let nicks: Vec<_> = server |
376 .clients |
177 .clients |
377 .iter() |
178 .iter() |
378 .filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready()) |
179 .filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready()) |
379 .map(|(_, c)| c.nick.clone()) |
180 .map(|(_, c)| c.nick.clone()) |
380 .collect(); |
181 .collect(); |
381 if !nicks.is_empty() { |
182 if !nicks.is_empty() { |
382 actions.push(ClientFlags("+r".to_string(), nicks).send(to).action()); |
183 /*actions.push(ClientFlags("+r".to_string(), nicks).send(to).action())*/ |
|
184 ; |
383 } |
185 } |
384 } |
186 } |
385 } |
187 } |
386 server.react(client_id, actions); |
188 server.react(client_id, actions); |
387 } |
189 } |
474 } |
279 } |
475 } |
280 } |
476 VoteType::Pause => { |
281 VoteType::Pause => { |
477 if let Some(ref mut info) = server.rooms[room_id].game_info { |
282 if let Some(ref mut info) = server.rooms[room_id].game_info { |
478 info.is_paused = !info.is_paused; |
283 info.is_paused = !info.is_paused; |
479 actions.push( |
284 /*actions.push( |
480 server_chat("Pause toggled.".to_string()) |
285 server_chat("Pause toggled.".to_string()) |
481 .send_all() |
286 .send_all() |
482 .in_room(room_id) |
287 .in_room(room_id) |
483 .action(), |
288 .action(), |
484 ); |
289 );*/ |
485 actions.push( |
290 /*actions.push( |
486 ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
291 ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
487 .send_all() |
292 .send_all() |
488 .in_room(room_id) |
293 .in_room(room_id) |
489 .action(), |
294 .action(), |
490 ); |
295 );*/ |
491 } |
296 } |
492 } |
297 } |
493 VoteType::NewSeed => { |
298 VoteType::NewSeed => { |
494 let seed = thread_rng().gen_range(0, 1_000_000_000).to_string(); |
299 let seed = thread_rng().gen_range(0, 1_000_000_000).to_string(); |
495 let cfg = GameCfg::Seed(seed); |
300 let cfg = GameCfg::Seed(seed); |
496 actions.push(cfg.to_server_msg().send_all().in_room(room_id).action()); |
301 /*actions.push(cfg.to_server_msg().send_all().in_room(room_id).action());*/ |
497 server.rooms[room_id].set_config(cfg); |
302 server.rooms[room_id].set_config(cfg); |
498 } |
303 } |
499 VoteType::HedgehogsPerTeam(number) => { |
304 VoteType::HedgehogsPerTeam(number) => { |
500 let r = &mut server.rooms[room_id]; |
305 let r = &mut server.rooms[room_id]; |
501 let nicks = r.set_hedgehogs_number(number); |
306 let nicks = r.set_hedgehogs_number(number); |
502 actions.extend(nicks.into_iter().map(|n| { |
307 /*actions.extend(nicks.into_iter().map(|n| { |
503 HedgehogsNumber(n, number) |
308 HedgehogsNumber(n, number) |
504 .send_all() |
309 .send_all() |
505 .in_room(room_id) |
310 .in_room(room_id) |
506 .action() |
311 .action() |
507 })); |
312 }));*/ |
508 } |
313 } |
509 } |
314 } |
510 server.react(id, actions); |
315 server.react(id, actions); |
511 } |
316 } |
512 ChangeMaster(room_id, new_id) => { |
317 ChangeMaster(room_id, new_id) => { |
546 r.set_join_restriction(false); |
351 r.set_join_restriction(false); |
547 r.set_team_add_restriction(false); |
352 r.set_team_add_restriction(false); |
548 let is_fixed = r.is_fixed(); |
353 let is_fixed = r.is_fixed(); |
549 r.set_unregistered_players_restriction(is_fixed); |
354 r.set_unregistered_players_restriction(is_fixed); |
550 if let Some(nick) = new_nick { |
355 if let Some(nick) = new_nick { |
551 actions.push( |
356 /*actions.push( |
552 ClientFlags("+h".to_string(), vec![nick]) |
357 ClientFlags("+h".to_string(), vec![nick]) |
553 .send_all() |
358 .send_all() |
554 .in_room(r.id) |
359 .in_room(r.id) |
555 .action(), |
360 .action(), |
556 ); |
361 );*/ |
557 } |
362 } |
558 } |
363 } |
559 if let Some(id) = new_id { |
364 if let Some(id) = new_id { |
560 server.clients[id].set_is_master(true) |
365 server.clients[id].set_is_master(true) |
561 } |
366 } |
562 server.react(client_id, actions); |
367 server.react(client_id, actions); |
563 } |
368 } |
564 SendRoomUpdate(old_name) => { |
369 SendRoomUpdate(old_name) => { |
565 if let (c, Some(r)) = server.client_and_room(client_id) { |
370 if let (c, Some(r)) = server.client_and_room(client_id) { |
566 let name = old_name.unwrap_or_else(|| r.name.clone()); |
371 let name = old_name.unwrap_or_else(|| r.name.clone()); |
567 let actions = vec![RoomUpdated(name, r.info(Some(&c))) |
372 /*let actions = vec![RoomUpdated(name, r.info(Some(&c))) |
568 .send_all() |
373 .send_all() |
569 .with_protocol(r.protocol_number) |
374 .with_protocol(r.protocol_number) |
570 .action()]; |
375 .action()]; |
571 server.react(client_id, actions); |
376 server.react(client_id, actions);*/ |
572 } |
377 } |
573 } |
378 } |
574 StartRoomGame(room_id) => { |
379 StartRoomGame(room_id) => { |
575 let actions = { |
380 let actions = { |
576 let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server |
381 let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server |
579 .map(|(id, c)| (id, c.nick.clone())) |
384 .map(|(id, c)| (id, c.nick.clone())) |
580 .unzip(); |
385 .unzip(); |
581 let room = &mut server.rooms[room_id]; |
386 let room = &mut server.rooms[room_id]; |
582 |
387 |
583 if !room.has_multiple_clans() { |
388 if !room.has_multiple_clans() { |
584 vec![Warn( |
389 vec![/*Warn( |
585 "The game can't be started with less than two clans!".to_string(), |
390 "The game can't be started with less than two clans!".to_string(), |
586 )] |
391 )*/] |
587 } else if room.protocol_number <= 43 |
392 } else if room.protocol_number <= 43 |
588 && room.players_number != room.ready_players_number |
393 && room.players_number != room.ready_players_number |
589 { |
394 { |
590 vec![Warn("Not all players are ready".to_string())] |
395 vec![/*Warn("Not all players are ready".to_string())*/] |
591 } else if room.game_info.is_some() { |
396 } else if room.game_info.is_some() { |
592 vec![Warn("The game is already in progress".to_string())] |
397 vec![/*Warn("The game is already in progress".to_string())*/] |
593 } else { |
398 } else { |
594 room.start_round(); |
399 room.start_round(); |
595 for id in room_clients { |
400 for id in room_clients { |
596 let c = &mut server.clients[id]; |
401 let c = &mut server.clients[id]; |
597 c.set_is_in_game(false); |
402 c.set_is_in_game(false); |
598 c.team_indices = room.client_team_indices(c.id); |
403 c.team_indices = room.client_team_indices(c.id); |
599 } |
404 } |
600 vec![ |
405 vec![ |
601 RunGame.send_all().in_room(room.id).action(), |
406 /*RunGame.send_all().in_room(room.id).action(),*/ |
602 SendRoomUpdate(None), |
407 SendRoomUpdate(None), |
603 ClientFlags("+g".to_string(), room_nicks) |
408 /*ClientFlags("+g".to_string(), room_nicks) |
604 .send_all() |
409 .send_all() |
605 .in_room(room.id) |
410 .in_room(room.id) |
606 .action(), |
411 .action(),*/ |
607 ] |
412 ] |
608 } |
413 } |
609 }; |
414 }; |
610 server.react(client_id, actions); |
415 server.react(client_id, actions); |
611 } |
416 } |
612 SendTeamRemovalMessage(team_name) => { |
417 SendTeamRemovalMessage(team_name) => { |
613 let mut actions = Vec::new(); |
418 let mut actions = Vec::new(); |
614 if let Some(r) = server.room(client_id) { |
419 if let Some(r) = server.room(client_id) { |
615 if let Some(ref mut info) = r.game_info { |
420 if let Some(ref mut info) = r.game_info { |
616 let msg = once(b'F').chain(team_name.bytes()); |
421 let msg = once(b'F').chain(team_name.bytes()); |
617 actions.push( |
422 /*actions.push( |
618 ForwardEngineMessage(vec![to_engine_msg(msg)]) |
423 ForwardEngineMessage(vec![to_engine_msg(msg)]) |
619 .send_all() |
424 .send_all() |
620 .in_room(r.id) |
425 .in_room(r.id) |
621 .but_self() |
426 .but_self() |
622 .action(), |
427 .action(), |
623 ); |
428 );*/ |
624 info.teams_in_game -= 1; |
429 info.teams_in_game -= 1; |
625 if info.teams_in_game == 0 { |
430 if info.teams_in_game == 0 { |
626 actions.push(FinishRoomGame(r.id)); |
431 actions.push(FinishRoomGame(r.id)); |
627 } |
432 } |
628 let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
433 let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
631 } |
436 } |
632 if info.sync_msg.is_some() { |
437 if info.sync_msg.is_some() { |
633 info.sync_msg = None |
438 info.sync_msg = None |
634 } |
439 } |
635 info.msg_log.push(remove_msg.clone()); |
440 info.msg_log.push(remove_msg.clone()); |
636 actions.push( |
441 /*actions.push( |
637 ForwardEngineMessage(vec![remove_msg]) |
442 ForwardEngineMessage(vec![remove_msg]) |
638 .send_all() |
443 .send_all() |
639 .in_room(r.id) |
444 .in_room(r.id) |
640 .but_self() |
445 .but_self() |
641 .action(), |
446 .action(), |
642 ); |
447 );*/ |
643 } |
448 } |
644 } |
449 } |
645 server.react(client_id, actions); |
450 server.react(client_id, actions); |
646 } |
451 } |
647 FinishRoomGame(room_id) => { |
452 FinishRoomGame(room_id) => { |
648 let mut actions = Vec::new(); |
453 let mut actions = Vec::new(); |
649 |
454 |
650 let r = &mut server.rooms[room_id]; |
455 let r = &mut server.rooms[room_id]; |
651 r.ready_players_number = 1; |
456 r.ready_players_number = 1; |
652 actions.push(SendRoomUpdate(None)); |
457 actions.push(SendRoomUpdate(None)); |
653 actions.push(RoundFinished.send_all().in_room(r.id).action()); |
458 //actions.push(RoundFinished.send_all().in_room(r.id).action()); |
654 |
459 |
655 if let Some(info) = replace(&mut r.game_info, None) { |
460 if let Some(info) = replace(&mut r.game_info, None) { |
656 for (_, c) in server.clients.iter() { |
461 for (_, c) in server.clients.iter() { |
657 if c.room_id == Some(room_id) && c.is_joined_mid_game() { |
462 if c.room_id == Some(room_id) && c.is_joined_mid_game() { |
658 actions.push(SendRoomData { |
463 actions.push(SendRoomData { |