From: Ian Jackson Date: Wed, 17 Jun 2020 22:55:26 +0000 (+0100) Subject: wip X-Git-Tag: otter-0.2.0~1579 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=3b712d1ac9142f34a106ba5cd1711765cf24a0c7;p=otter.git wip --- diff --git a/Cargo.lock.example b/Cargo.lock.example index d2351096..06d889f1 100644 --- a/Cargo.lock.example +++ b/Cargo.lock.example @@ -339,6 +339,7 @@ version = "0.0.1" dependencies = [ "anyhow", "handy", + "lazy_static", "rocket", "rocket_contrib", "serde", diff --git a/Cargo.toml b/Cargo.toml index f87aeb94..d7aba159 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ serde = { version = "1", features = ["derive"] } serde_json = "1" handy = "0.1" +lazy_static = "1.0.0" rocket = "0.4" rocket_contrib = { version = "0.4", default-features=false, features=["tera_templates","helmet"] } diff --git a/TODO b/TODO index 85da99a2..d6a35fce 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,7 @@ token in main url client ? struct ? instance struct + page should tell us nick and client id communicate with server about moves sync problem wrt xhr api grab call and ui updates diff --git a/junk/global.rs b/junk/global.rs index 928568e7..5872f437 100644 --- a/junk/global.rs +++ b/junk/global.rs @@ -1,23 +1,4 @@ -strut InstanceAccess { - i : Rc, - user : usize, -} - -#[derive(Default)] -struct Global { - tokens : RwLock, - // xxx delete instances at some point! -} - -lazy_static! { - static ref GLOBAL : Global = Default::default(); -} - -fn lookup_token(s : &str) -> Option { - GLOBAL.instances().read().get(s) -} - #[throws(E)] fn create_instance_access(name : &str, i : Rc) { let w = GLOBAL.instances().write(); diff --git a/src/global.rs b/src/global.rs new file mode 100644 index 00000000..b21542ff --- /dev/null +++ b/src/global.rs @@ -0,0 +1,69 @@ + +use crate::imports::*; +use lazy_static::lazy_static; + +type UserId = TypedHandle; +type ClientId = TypedHandle; + +#[derive(Clone,Debug,Eq,PartialEq,Ord,PartialOrd,Hash)] +struct RawToken (String); +impl Borrow for RawToken { + fn borrow(&self) -> &str { &self.0 } +} + +struct Client { + user : UserId, +} + +struct User { + nick : String, +} + +struct Instance { + /* game state goes here */ + users : TypedHandleMap, + clients : TypedHandleMap, +} + +#[derive(Clone)] +struct InstanceAccess { + i : Arc>, + user : UserId, +} + +#[derive(Default)] +struct Global { + // lock hierarchy: this is the innermost lock + tokens : RwLock>, + // xxx delete instances at some point! +} + +lazy_static! { + static ref GLOBAL : Global = Default::default(); +} + +fn lookup_token(s : &str) -> Option { + GLOBAL.tokens.read().unwrap().get(s).cloned() +} + +const XXX_USERS_TOKENS : &[(&str, &str)] = &[ + ("kmqAKPwK4TfReFjMor8MJhdRPBcwIBpe", "alice"), + ("ccg9kzoTh758QrVE1xMY7BQWB36dNJTx", "bob"), +]; + +pub fn xxx_global_setup() { + let i = Instance { + users : Default::default(), + clients : Default::default(), + }; + let i = Arc::new(Mutex::new(i)); + let ig = i.lock().unwrap(); + for (token, nick) in XXX_USERS_TOKENS { + let nu = User { nick : nick.to_owned() }; + let user = ig.users.insert(nu); + let ia = InstanceAccess { i, user }; + GLOBAL.tokens().write().unwrap().insert( + RawToken(token.to_string()), ia + ); + } +} diff --git a/src/imports.rs b/src/imports.rs index dc4c222b..71d99c71 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -3,6 +3,9 @@ pub use std::io; pub use std::io::{BufReader,Read}; pub use std::thread; pub use std::time::Duration; +pub use std::sync::{Arc,Mutex,RwLock}; +pub use std::collections::HashMap; +pub use std::borrow::Borrow; pub use thiserror::Error; pub use anyhow::{Context,anyhow}; @@ -20,6 +23,8 @@ pub use rocket::request::{FromParam,FromRequest,FromFormValue,LenientForm}; pub use rocket::response::NamedFile; pub use rocket::response; +pub use handy::typed::{TypedHandle,TypedHandleMap}; + pub type E = anyhow::Error; pub type SvgData = Vec; diff --git a/src/lib.rs b/src/lib.rs index 4202e992..743d4498 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod imports; +pub mod global;