From: Ian Jackson Date: Mon, 23 Nov 2020 00:25:49 +0000 (+0000) Subject: delete game X-Git-Tag: otter-0.2.0~390 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=e8237a2e3a41f8b26e6927686e46f32b3d613e18;p=otter.git delete game Signed-off-by: Ian Jackson --- diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 57ce6c1e..878599f5 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -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) ->Result<(),AE> { + let args = parse_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)] diff --git a/src/cmdlistener.rs b/src/cmdlistener.rs index ab23556f..35a956dc 100644 --- a/src/cmdlistener.rs +++ b/src/cmdlistener.rs @@ -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 diff --git a/src/global.rs b/src/global.rs index 5550880d..938dba6d 100644 --- a/src/global.rs +++ b/src/global.rs @@ -332,11 +332,12 @@ impl Instance { } #[throws(MgmtError)] - pub fn lookup_by_name_unauth(name: &InstanceName) - -> Unauthorised + pub fn lookup_by_name_locked_unauth + (games_table: &GamesTable, name: &InstanceName) + -> Unauthorised { 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) + -> InstanceRef { + Self::lookup_by_name_locked_unauth(games, name)?.by(auth) + } + + #[throws(MgmtError)] + pub fn lookup_by_name_unauth(name: &InstanceName) + -> Unauthorised + { + 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) -> InstanceRef {