From 05e1b19d11d47a608c54cbc5b7abdbb3eaa1ba91 Mon Sep 17 00:00:00 2001 From: ntr Date: Sun, 22 Sep 2019 17:25:28 +1000 Subject: [PATCH] account and player img --- client/src/components/player.box.jsx | 17 +++--- ops/migrations/20190922154304_account-img.js | 11 ++++ server/src/account.rs | 54 ++++++++++++-------- server/src/img.rs | 4 ++ server/src/instance.rs | 7 ++- server/src/player.rs | 28 +++++++++- server/src/rpc.rs | 3 ++ 7 files changed, 89 insertions(+), 35 deletions(-) create mode 100644 ops/migrations/20190922154304_account-img.js diff --git a/client/src/components/player.box.jsx b/client/src/components/player.box.jsx index 787524a0..87ec4dae 100644 --- a/client/src/components/player.box.jsx +++ b/client/src/components/player.box.jsx @@ -26,30 +26,29 @@ function Scoreboard(args) { }; */ + const imgStyle = player.img + ? { 'background-image': `url(/imgs/${player.img}.svg)` } + : null; + if (!isPlayer) { return (
{scoreText()}
{player.name}
-
-
-
glhf
+
+
 
); } + return (
hfhf
{scoreText()}
{player.name}
-
-
+
); } diff --git a/ops/migrations/20190922154304_account-img.js b/ops/migrations/20190922154304_account-img.js new file mode 100644 index 00000000..f95b3bf9 --- /dev/null +++ b/ops/migrations/20190922154304_account-img.js @@ -0,0 +1,11 @@ +const uuidv4 = require('uuid/v4'); + +// give everybody the shapes mtx +exports.up = async knex => { + await knex.raw(` + ALTER TABLE accounts + ADD COLUMN img UUID DEFAULT uuid_generate_v4(); + `); +}; + +exports.down = async () => {}; \ No newline at end of file diff --git a/server/src/account.rs b/server/src/account.rs index ed918a55..ddbf01cf 100644 --- a/server/src/account.rs +++ b/server/src/account.rs @@ -14,6 +14,7 @@ use construct::{Construct, ConstructSkeleton, construct_spawn}; use instance::{Instance, instance_delete}; use mtx::{Mtx, FREE_MTX}; use pg::Db; +use img; use failure::Error; @@ -24,6 +25,7 @@ static PASSWORD_MIN_LEN: usize = 11; #[derive(Debug,Clone,Serialize,Deserialize)] pub struct Account { pub id: Uuid, + pub img: Uuid, pub name: String, pub balance: u32, pub subscribed: bool, @@ -34,6 +36,7 @@ impl<'a> TryFrom> for Account { fn try_from(row: postgres::rows::Row) -> Result { let id: Uuid = row.get("id"); + let img: Uuid = row.get("img"); let db_balance: i64 = row.get("balance"); let balance = u32::try_from(db_balance) @@ -42,13 +45,13 @@ impl<'a> TryFrom> for Account { let subscribed: bool = row.get("subscribed"); let name: String = row.get("name"); - Ok(Account { id, name, balance, subscribed }) + Ok(Account { id, name, balance, subscribed, img }) } } pub fn select(db: &Db, id: Uuid) -> Result { let query = " - SELECT id, name, balance, subscribed + SELECT id, name, balance, subscribed, img FROM accounts WHERE id = $1; "; @@ -64,7 +67,7 @@ pub fn select(db: &Db, id: Uuid) -> Result { pub fn select_name(db: &Db, name: &String) -> Result { let query = " - SELECT id, name, balance, subscribed + SELECT id, name, balance, subscribed, img FROM accounts WHERE name = $1; "; @@ -80,7 +83,7 @@ pub fn select_name(db: &Db, name: &String) -> Result { pub fn from_token(db: &Db, token: &String) -> Result { let query = " - SELECT id, name, subscribed, balance + SELECT id, name, balance, subscribed, img FROM accounts WHERE token = $1 AND token_expiry > now(); @@ -97,7 +100,7 @@ pub fn from_token(db: &Db, token: &String) -> Result { pub fn login(tx: &mut Transaction, name: &String, password: &String) -> Result { let query = " - SELECT id, password, name, balance, subscribed + SELECT id, password, name, balance, subscribed, img FROM accounts WHERE name = $1 "; @@ -125,8 +128,10 @@ pub fn login(tx: &mut Transaction, name: &String, password: &String) -> Result Result { @@ -246,7 +251,7 @@ pub fn debit(tx: &mut Transaction, id: Uuid, debit: i64) -> Result Result Result { @@ -298,6 +297,7 @@ pub fn create(name: &String, password: &String, tx: &mut Transaction) -> Result< } let id = Uuid::new_v4(); + let img = Uuid::new_v4(); let rounds = 8; let password = hash(&password, rounds)?; @@ -308,13 +308,13 @@ pub fn create(name: &String, password: &String, tx: &mut Transaction) -> Result< .collect(); let query = " - INSERT INTO accounts (id, name, password, token, token_expiry) - VALUES ($1, $2, $3, $4, now() + interval '1 week') + INSERT INTO accounts (id, name, password, token, token_expiry, img) + VALUES ($1, $2, $3, $4, now() + interval '1 week', $5) RETURNING id, name; "; let result = tx - .query(query, &[&id, &name, &password, &token])?; + .query(query, &[&id, &name, &password, &token, &img])?; match result.iter().next() { Some(row) => row, @@ -331,6 +331,8 @@ pub fn create(name: &String, password: &String, tx: &mut Transaction) -> Result< .insert(tx)?; } + img::shapes_write(img)?; + info!("registration account={:?}", name); Ok(token) @@ -458,3 +460,15 @@ pub fn account_instances(tx: &mut Transaction, account: &Account) -> Result Result { + match account.subscribed { + true => match img::exists(account.img) { + true => Ok(account.img), + false => img::shapes_write(account.img) + }, + false => Ok(account.img), + } +} diff --git a/server/src/img.rs b/server/src/img.rs index 93572360..c10d8e57 100644 --- a/server/src/img.rs +++ b/server/src/img.rs @@ -257,6 +257,10 @@ fn _hieroglyph() -> String { return s; } +pub fn exists(id: Uuid) -> bool { + std::path::Path::new(&format!("/var/lib/mnml/public/imgs/{}.svg", id)).exists() +} + #[cfg(test)] mod tests { use super::*; diff --git a/server/src/instance.rs b/server/src/instance.rs index 22ad03e5..9cc55221 100644 --- a/server/src/instance.rs +++ b/server/src/instance.rs @@ -719,8 +719,7 @@ pub fn instance_practice(tx: &mut Transaction, account: &Account) -> Result Result Score::Win, // } // Score::Adv => Score::Win, - + _ => panic!("faulty score increment {:?}", self), } } @@ -56,6 +57,7 @@ impl Score { #[derive(Debug,Clone,Serialize,Deserialize)] pub struct Player { pub id: Uuid, + pub img: Option, pub name: String, pub vbox: Vbox, pub constructs: Vec, @@ -66,9 +68,31 @@ pub struct Player { } impl Player { + pub fn from_account(tx: &mut Transaction, account: &Account) -> Result { + let constructs = account::team(tx, account)?; + + let img = match account.subscribed { + true => Some(account.img), + false => None, + }; + + Ok(Player { + id: account.id, + img, + name: account.name.clone(), + vbox: Vbox::new(), + constructs, + bot: false, + ready: false, + warnings: 0, + score: Score::Zero, + }) + } + pub fn new(account: Uuid, name: &String, constructs: Vec) -> Player { Player { id: account, + img: Some(account), name: name.clone(), vbox: Vbox::new(), constructs, @@ -379,7 +403,7 @@ mod tests { player.score = player.score.add_win(&Score::Zero); player.score = player.score.add_win(&Score::Zero); assert_eq!(player.score, Score::Win); // 40 / 0 - + // Bo7 tennis scoring /*assert_eq!(player.score, Score::Three); // 40 / 0 diff --git a/server/src/rpc.rs b/server/src/rpc.rs index d006a316..208cac07 100644 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -272,6 +272,9 @@ impl Handler for Connection { self.ws.send(RpcMessage::AccountState(a.clone())).unwrap(); self.events.send(Event::Subscribe(self.id, a.id)).unwrap(); + // check if they have an image that needs to be generated + account::img_check(&a).unwrap(); + let db = self.pool.get().unwrap(); let mut tx = db.transaction().unwrap();