rust/hedgewars-server/src/core/server.rs
changeset 15538 b907b9071ec5
parent 15514 395be40faa51
child 15539 abd5eb807166
--- a/rust/hedgewars-server/src/core/server.rs	Sat Nov 30 02:54:49 2019 +0100
+++ b/rust/hedgewars-server/src/core/server.rs	Tue Dec 17 18:54:17 2019 +0300
@@ -6,11 +6,10 @@
 };
 use crate::{protocol::messages::HwProtocolMessage::Greeting, utils};
 
-use crate::core::server::JoinRoomError::WrongProtocol;
 use bitflags::*;
 use log::*;
 use slab;
-use std::{borrow::BorrowMut, collections::HashSet, iter, num::NonZeroU16};
+use std::{borrow::BorrowMut, collections::HashSet, iter, mem::replace, num::NonZeroU16};
 
 type Slab<T> = slab::Slab<T>;
 
@@ -67,6 +66,13 @@
 }
 
 #[derive(Debug)]
+pub struct EndGameResult {
+    pub joined_mid_game_clients: Vec<ClientId>,
+    pub left_teams: Vec<String>,
+    pub unreadied_nicks: Vec<String>,
+}
+
+#[derive(Debug)]
 pub struct UninitializedError();
 #[derive(Debug)]
 pub struct AccessError();
@@ -441,6 +447,46 @@
         }
     }
 
+    pub fn end_game(&mut self, room_id: RoomId) -> EndGameResult {
+        let room = &mut self.rooms[room_id];
+        room.ready_players_number = 1;
+
+        if let Some(info) = replace(&mut room.game_info, None) {
+            let joined_mid_game_clients = self
+                .clients
+                .iter()
+                .filter(|(_, c)| c.room_id == Some(room_id) && c.is_joined_mid_game())
+                .map(|(_, c)| c.id)
+                .collect();
+
+            let unreadied_nicks: Vec<_> = self
+                .clients
+                .iter_mut()
+                .filter(|(_, c)| c.room_id == Some(room_id))
+                .map(|(_, c)| {
+                    c.set_is_ready(c.is_master());
+                    c.set_is_joined_mid_game(false);
+                    c
+                })
+                .filter_map(|c| {
+                    if !c.is_master() {
+                        Some(c.nick.clone())
+                    } else {
+                        None
+                    }
+                })
+                .collect();
+
+            EndGameResult {
+                joined_mid_game_clients,
+                left_teams: info.left_teams.clone(),
+                unreadied_nicks,
+            }
+        } else {
+            unreachable!()
+        }
+    }
+
     #[inline]
     pub fn set_var(&mut self, client_id: ClientId, var: ServerVar) -> Result<(), AccessError> {
         if self.clients[client_id].is_admin() {