chiark / gitweb /
delete game
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 23 Nov 2020 00:25:49 +0000 (00:25 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 23 Nov 2020 00:26:06 +0000 (00:26 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/otter.rs
src/cmdlistener.rs
src/global.rs

index 57ce6c1ebad3e08c8b5e6b9b4007f06b43813924..878599f5c0ee2e7231cfae5cf155810fc1e46e9e 100644 (file)
@@ -908,6 +908,39 @@ mod leave_game {
   )}
 }
 
+//---------- delete-game ----------
+
+mod delete_game {
+  use super::*;
+
+  #[derive(Default,Debug)]
+  struct Args {
+    table_name: String,
+  }
+
+  fn subargs(sa: &mut Args) -> ArgumentParser {
+    use argparse::*;
+    let mut ap = ArgumentParser::new();
+    ap.refer(&mut sa.table_name).required()
+      .add_argument("TABLE-NAME",Store,"table name");
+    ap
+  }
+
+  fn call(_sc: &Subcommand, ma: MainOpts, args: Vec<String>) ->Result<(),AE> {
+    let args = parse_args::<Args,_>(args, &subargs, &ok_id, None);
+    let mut chan = access_game(&ma, &args.table_name)?;
+    let game = chan.game.clone();
+    chan.cmd(&MC::DestroyGame { game })?;
+    Ok(())
+  }
+
+  inventory::submit!{Subcommand(
+    "delete-game",
+    "Delete a game (throwing all the players out of it)",
+    call,
+  )}
+}
+
 //---------- library-list ----------
 
 #[derive(Debug)]
index ab23556feb97fe42389438a470c5573d5e2cec26..35a956dca7adea9428264276f29c0d589aa66173 100644 (file)
@@ -206,7 +206,7 @@ fn execute(cs: &mut CommandStream, cmd: MgmtCommand) -> MgmtResponse {
       let mut ag = AccountsGuard::lock();
       let mut games = games_lock();
       let auth = authorise_by_account(cs, &mut ag, &game)?;
-      let gref = Instance::lookup_by_name(&game, auth)?;
+      let gref = Instance::lookup_by_name_locked(&games, &game, auth)?;
       let ig = gref.lock_even_poisoned();
       Instance::destroy_game(&mut games, ig, auth)?;
       Fine
index 5550880d98243e68a39a0fffeebbc649e915b91c..938dba6d96ce9fa2655a7cd0e333d3b7ecf2e2bd 100644 (file)
@@ -332,11 +332,12 @@ impl Instance {
   }
 
   #[throws(MgmtError)]
-  pub fn lookup_by_name_unauth(name: &InstanceName)
-      -> Unauthorised<InstanceRef, InstanceName>
+  pub fn lookup_by_name_locked_unauth
+    (games_table: &GamesTable, name: &InstanceName)
+     -> Unauthorised<InstanceRef, InstanceName>
   {
     Unauthorised::of(
-      GLOBAL.games_table.read().unwrap()
+      games_table
         .get(name)
         .ok_or(MgmtError::GameNotFound)?
         .clone()
@@ -344,6 +345,22 @@ impl Instance {
     )
   }
 
+  #[throws(MgmtError)]
+  pub fn lookup_by_name_locked(games: &GamesTable,
+                               name: &InstanceName,
+                               auth: Authorisation<InstanceName>)
+                               -> InstanceRef {
+    Self::lookup_by_name_locked_unauth(games, name)?.by(auth)
+  }
+
+  #[throws(MgmtError)]
+  pub fn lookup_by_name_unauth(name: &InstanceName)
+      -> Unauthorised<InstanceRef, InstanceName>
+  {
+    let games = GLOBAL.games_table.read().unwrap();
+    Self::lookup_by_name_locked_unauth(&games, name)?
+  }
+
   #[throws(MgmtError)]
   pub fn lookup_by_name(name: &InstanceName, auth: Authorisation<InstanceName>)
                         -> InstanceRef {