-strut InstanceAccess {
- i : Rc<Instance>,
- user : usize,
-}
-
-#[derive(Default)]
-struct Global {
- tokens : RwLock<HashMap<RawToken, InstanceAccess>,
- // xxx delete instances at some point!
-}
-
-lazy_static! {
- static ref GLOBAL : Global = Default::default();
-}
-
-fn lookup_token(s : &str) -> Option<InstanceAccess> {
- GLOBAL.instances().read().get(s)
-}
-
#[throws(E)]
fn create_instance_access(name : &str, i : Rc<Instance>) {
let w = GLOBAL.instances().write();
--- /dev/null
+
+use crate::imports::*;
+use lazy_static::lazy_static;
+
+type UserId = TypedHandle<User>;
+type ClientId = TypedHandle<Client>;
+
+#[derive(Clone,Debug,Eq,PartialEq,Ord,PartialOrd,Hash)]
+struct RawToken (String);
+impl Borrow<str> for RawToken {
+ fn borrow(&self) -> &str { &self.0 }
+}
+
+struct Client {
+ user : UserId,
+}
+
+struct User {
+ nick : String,
+}
+
+struct Instance {
+ /* game state goes here */
+ users : TypedHandleMap<User>,
+ clients : TypedHandleMap<Client>,
+}
+
+#[derive(Clone)]
+struct InstanceAccess {
+ i : Arc<Mutex<Instance>>,
+ user : UserId,
+}
+
+#[derive(Default)]
+struct Global {
+ // lock hierarchy: this is the innermost lock
+ tokens : RwLock<HashMap<RawToken, InstanceAccess>>,
+ // xxx delete instances at some point!
+}
+
+lazy_static! {
+ static ref GLOBAL : Global = Default::default();
+}
+
+fn lookup_token(s : &str) -> Option<InstanceAccess> {
+ 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
+ );
+ }
+}
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};
pub use rocket::response::NamedFile;
pub use rocket::response;
+pub use handy::typed::{TypedHandle,TypedHandleMap};
+
pub type E = anyhow::Error;
pub type SvgData = Vec<u8>;