chiark / gitweb /
wip
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 17 Jun 2020 22:55:26 +0000 (23:55 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 17 Jun 2020 22:55:26 +0000 (23:55 +0100)
Cargo.lock.example
Cargo.toml
TODO
junk/global.rs
src/global.rs [new file with mode: 0644]
src/imports.rs
src/lib.rs

index d2351096acceebc525a3b57063405880c2ef9deb..06d889f1afbbd908a2bf56adcfb96bec57575431 100644 (file)
@@ -339,6 +339,7 @@ version = "0.0.1"
 dependencies = [
  "anyhow",
  "handy",
+ "lazy_static",
  "rocket",
  "rocket_contrib",
  "serde",
index f87aeb9400e02e20f9ed34f2f7e0c560062a0bb7..d7aba15934db4ad9baf9c7431caa370f52f32787 100644 (file)
@@ -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 85da99a25fb8c0a917701b497e7def341f12892a..d6a35fce002c02d3163f2ce03a929e2b4f8315ef 100644 (file)
--- 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
index 928568e70699fab2553cbcc7522f7fc72e9b46d5..5872f43711b6150066f99362efd85ca197a7b58f 100644 (file)
@@ -1,23 +1,4 @@
 
-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();
diff --git a/src/global.rs b/src/global.rs
new file mode 100644 (file)
index 0000000..b21542f
--- /dev/null
@@ -0,0 +1,69 @@
+
+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
+    );
+  }
+}
index dc4c222b810f9a3c715c932a03b7d21e51232dcd..71d99c716a6af3aab810faff152e29569643ea1c 100644 (file)
@@ -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<u8>;
index 4202e99260e1f53d9122b2b2d3685d51eca11685..743d4498d9328efcb4b112f593b4ae6f8dedae8a 100644 (file)
@@ -1,2 +1,3 @@
 
 pub mod imports;
+pub mod global;