rust/hedgewars-server/src/core/server.rs
changeset 15492 395be40faa51
parent 15487 91f0c5ec37b5
child 15516 b907b9071ec5
--- a/rust/hedgewars-server/src/core/server.rs	Mon Oct 28 20:40:46 2019 +0100
+++ b/rust/hedgewars-server/src/core/server.rs	Mon Oct 28 23:17:35 2019 +0300
@@ -59,6 +59,14 @@
 }
 
 #[derive(Debug)]
+pub enum StartGameError {
+    NotEnoughClans,
+    NotEnoughTeams,
+    NotReady,
+    AlreadyInGame,
+}
+
+#[derive(Debug)]
 pub struct UninitializedError();
 #[derive(Debug)]
 pub struct AccessError();
@@ -407,6 +415,32 @@
         }
     }
 
+    pub fn start_game(&mut self, room_id: RoomId) -> Result<Vec<String>, StartGameError> {
+        let (room_clients, room_nicks): (Vec<_>, Vec<_>) = self
+            .clients
+            .iter()
+            .map(|(id, c)| (id, c.nick.clone()))
+            .unzip();
+
+        let room = &mut self.rooms[room_id];
+
+        if !room.has_multiple_clans() {
+            Err(StartGameError::NotEnoughClans)
+        } else if room.protocol_number <= 43 && room.players_number != room.ready_players_number {
+            Err(StartGameError::NotReady)
+        } else if room.game_info.is_some() {
+            Err(StartGameError::AlreadyInGame)
+        } else {
+            room.start_round();
+            for id in room_clients {
+                let c = &mut self.clients[id];
+                c.set_is_in_game(true);
+                c.team_indices = room.client_team_indices(c.id);
+            }
+            Ok(room_nicks)
+        }
+    }
+
     #[inline]
     pub fn set_var(&mut self, client_id: ClientId, var: ServerVar) -> Result<(), AccessError> {
         if self.clients[client_id].is_admin() {