From e77314bfa076cb8dff60d09d3979cdc8748b40d9 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 18 Nov 2020 20:20:37 +0000 Subject: [PATCH] cli: wip restructure game joining Signed-off-by: Ian Jackson --- src/bin/otter.rs | 106 +++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 55 deletions(-) diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 70000889..0abd35de 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -359,6 +359,55 @@ impl DerefMut for ConnForGame { } impl ConnForGame { + #[throws(AE)] + fn prep_access_game(&mut self, ma: &MainOpts) { + #[derive(Debug)] + struct Wantup(bool); + impl Wantup { + fn u(&mut self, rhs: &Option) -> Option { + if rhs.is_some() { self.0 = true } + rhs.clone() + } + } + let mut wantup = Wantup(false); + let mut ad = AccountDetails { + account: ma.account.clone(), + nick: wantup.u(&ma.nick), + timezone: wantup.u(&ma.timezone), + access: wantup.u(&ma.access).map(Into::into), + }; + + fn is_no_account(r: &Result) -> bool { + if_chain! { + if let Err(e) = r; + if let Some(&ME::AccountNotFound(_)) = e.downcast_ref(); + then { return true } + else { return false } + } + } + + { + let mut desc; + let mut resp; + if wantup.0 { + desc = "UpdateAccount"; + resp = self.conn.cmd(&MC::UpdateAccount(clone_via_serde(&ad))) + .map(|_|()); + } else { + desc = "AlterGame--Noop"; + resp = self.alter_game(vec![MGI::Noop], None) + .map(|_|()); + }; + if is_no_account(&resp) { + ad.access.get_or_insert(Box::new(UrlOnStdout)); + desc = "CreateAccount"; + resp = self.conn.cmd(&MC::CreateAccount(clone_via_serde(&ad))) + .map(|_|()); + } + resp.with_context(||format!("response to {}", &desc))?; + } + } + #[throws(AE)] fn join_game(&mut self, ma: &MainOpts) { let insns = vec![ @@ -570,67 +619,14 @@ fn read_spec(filename: &str, what: &str) -> T { } #[throws(AE)] -fn prep_access_game(ma: &MainOpts, table_name: &String) -> ConnForGame { +fn access_game(ma: &MainOpts, table_name: &String) -> ConnForGame { let conn = connect(&ma)?; - - #[derive(Debug)] - struct Wantup(bool); - impl Wantup { - fn u(&mut self, rhs: &Option) -> Option { - if rhs.is_some() { self.0 = true } - rhs.clone() - } - } - let mut wantup = Wantup(false); - let mut ad = AccountDetails { - account: ma.account.clone(), - nick: wantup.u(&ma.nick), - timezone: wantup.u(&ma.timezone), - access: wantup.u(&ma.access).map(Into::into), - }; - - fn is_no_account(r: &Result) -> bool { - if_chain! { - if let Err(e) = r; - if let Some(&ME::AccountNotFound(_)) = e.downcast_ref(); - then { return true } - else { return false } - } - } - let mut chan = ConnForGame { conn, game: ma.instance_name(table_name), how: MgmtGameUpdateMode::Online, }; - - { - let mut desc; - let mut resp; - if wantup.0 { - desc = "UpdateAccount"; - resp = chan.conn.cmd(&MC::UpdateAccount(clone_via_serde(&ad))) - .map(|_|()); - } else { - desc = "AlterGame--Noop"; - resp = chan.alter_game(vec![MGI::Noop], None) - .map(|_|()); - }; - if is_no_account(&resp) { - ad.access.get_or_insert(Box::new(UrlOnStdout)); - desc = "CreateAccount"; - resp = chan.conn.cmd(&MC::CreateAccount(clone_via_serde(&ad))) - .map(|_|()); - } - resp.with_context(||format!("response to {}", &desc))?; - } - - chan -} - -#[throws(AE)] -fn access_game(ma: &MainOpts, table_name: &String) -> ConnForGame { - let mut chan = prep_access_game(ma, table_name)?; + chan.prep_access_game(ma)?; chan.join_game(&ma)?; chan } -- 2.30.2