From 162ddd1fb4fecdb1a011a843978d70177e3b5fc7 Mon Sep 17 00:00:00 2001 From: ntr Date: Mon, 24 Sep 2018 23:18:15 +1000 Subject: [PATCH] cryps list --- client/src/actions.jsx | 7 +++- client/src/components/cryp.container.js | 16 -------- client/src/components/cryps.container.js | 16 ++++++++ .../{cryp.panel.jsx => cryps.panel.jsx} | 18 ++++++--- client/src/main.jsx | 11 +++-- client/src/reducers.jsx | 8 ++-- client/src/socket.jsx | 13 ++++-- server/WORKLOG.md | 12 ++++++ server/src/account.rs | 40 ++++++++++++++++--- server/src/combat.rs | 5 +-- server/src/cryp.rs | 3 +- server/src/rpc.rs | 23 +++++++++-- 12 files changed, 119 insertions(+), 53 deletions(-) delete mode 100644 client/src/components/cryp.container.js create mode 100644 client/src/components/cryps.container.js rename client/src/components/{cryp.panel.jsx => cryps.panel.jsx} (85%) diff --git a/client/src/actions.jsx b/client/src/actions.jsx index 15f4cfb4..5d1990bf 100644 --- a/client/src/actions.jsx +++ b/client/src/actions.jsx @@ -1,8 +1,11 @@ export const SET_ACCOUNT = 'SET_ACCOUNT'; export const setAccount = (value) => ({ type: SET_ACCOUNT, value }); -export const SET_CRYP = 'SET_CRYP'; -export const setCryp = (value) => ({ type: SET_CRYP, value }); +export const SET_CRYPS = 'SET_CRYPS'; +export const setCryps = (value) => ({ type: SET_CRYPS, value }); + +export const SET_ACTIVE_CRYP = 'SET_ACTIVE_CRYP'; +export const setActiveCryp = (value) => ({ type: SET_ACTIVE_CRYP, value }); export const SET_WS = 'SET_WS'; export const setWs = (value) => ({ type: SET_WS, value }); diff --git a/client/src/components/cryp.container.js b/client/src/components/cryp.container.js deleted file mode 100644 index bc899048..00000000 --- a/client/src/components/cryp.container.js +++ /dev/null @@ -1,16 +0,0 @@ -const { connect } = require('preact-redux'); - -const CrypPanel = require('./cryp.panel'); - -const addState = connect( - function receiveState(state) { - const { ws, cryp } = state; - function sendCombatPve() { - return ws.sendCombatPve(cryp.id); - } - - return { cryp, sendCombatPve }; - } -); - -module.exports = addState(CrypPanel); diff --git a/client/src/components/cryps.container.js b/client/src/components/cryps.container.js new file mode 100644 index 00000000..7888b0b9 --- /dev/null +++ b/client/src/components/cryps.container.js @@ -0,0 +1,16 @@ +const { connect } = require('preact-redux'); + +const CrypsPanel = require('./cryps.panel'); + +const addState = connect( + function receiveState(state) { + const { ws, cryps } = state; + function sendCombatPve(crypId) { + return ws.sendCombatPve(crypId); + } + + return { cryps, sendCombatPve }; + } +); + +module.exports = addState(CrypsPanel); diff --git a/client/src/components/cryp.panel.jsx b/client/src/components/cryps.panel.jsx similarity index 85% rename from client/src/components/cryp.panel.jsx rename to client/src/components/cryps.panel.jsx index 6de8e6af..a6801e17 100755 --- a/client/src/components/cryp.panel.jsx +++ b/client/src/components/cryps.panel.jsx @@ -1,10 +1,10 @@ const preact = require('preact'); -function CrypPanel({ cryp, sendCombatPve }) { - if (!cryp) return
not ready
; +function CrypPanel({ cryps, sendCombatPve }) { + if (!cryps) return
not ready
; - return ( -
+ const crypPanels = cryps.map(cryp => ( +
@@ -51,16 +51,22 @@ function CrypPanel({ cryp, sendCombatPve }) {
- ); + )); // map is a function that is called on every element of an array // so in this ^^ case it calls Icon('Mashy') which returns some jsx // that gets put into the dom + + return ( +
+ {crypPanels} +
+ ); } module.exports = CrypPanel; diff --git a/client/src/main.jsx b/client/src/main.jsx index 15c676a4..de52979f 100644 --- a/client/src/main.jsx +++ b/client/src/main.jsx @@ -9,17 +9,17 @@ const actions = require('./actions'); const fizzyText = require('../lib/fizzy-text'); const createSocket = require('./socket'); -const CrypContainer = require('./components/cryp.container'); +const CrypsContainer = require('./components/cryps.container'); const LoginContainer = require('./components/login.container'); -const Navbar = require('./components/navbar'); +// const Navbar = require('./components/navbar'); // Redux Store const store = createStore( combineReducers({ account: reducers.accountReducer, - cryp: reducers.crypReducer, + cryps: reducers.crypsReducer, ws: reducers.wsReducer, - }), + }) ); store.subscribe(() => console.log(store.getState())); @@ -36,10 +36,9 @@ jdenticon.config = { const Cryps = () => (
-
- +
); diff --git a/client/src/reducers.jsx b/client/src/reducers.jsx index 1aab82ad..94ddfaa7 100644 --- a/client/src/reducers.jsx +++ b/client/src/reducers.jsx @@ -10,10 +10,10 @@ function accountReducer(state = defaultAccount, action) { } } -const defaultCryp = null; -function crypReducer(state = defaultCryp, action) { +const defaultCryps = null; +function crypsReducer(state = defaultCryps, action) { switch (action.type) { - case actions.SET_CRYP: + case actions.SET_CRYPS: return action.value; default: return state; @@ -32,6 +32,6 @@ function wsReducer(state = defaultWs, action) { module.exports = { accountReducer, - crypReducer, + crypsReducer, wsReducer, }; diff --git a/client/src/socket.jsx b/client/src/socket.jsx index 2f14975b..dd407f48 100644 --- a/client/src/socket.jsx +++ b/client/src/socket.jsx @@ -31,14 +31,19 @@ function createSocket(store) { account = login; store.dispatch(actions.setAccount(login)); + send({ method: 'account_cryps', params: {} }); console.log(account); - return send({ method: 'cryp_spawn', params: { name: 'drake' } }); + } + + function accountCryps(response) { + const [structName, cryps] = response; + store.dispatch(actions.setCryps(cryps)); + console.log('got my cryps', cryps); } function crypSpawn(response) { const [structName, cryp] = response; console.log('got a new cryp', cryp); - return store.dispatch(actions.setCryp(cryp)); } function combatPve(response) { @@ -74,6 +79,7 @@ function createSocket(store) { combat_pve: combatPve, account_login: accountLogin, account_create: accountLogin, + account_cryps: accountCryps, }; // decodes the cbor and @@ -93,8 +99,7 @@ function createSocket(store) { // Connection opened ws.addEventListener('open', function wsOpen(event) { - // send({ method: 'account_create', params: { name: 'ntr', password: 'grepgrepgrep' }}); - // send({ method: 'account_login', params: { name: 'ntr', password: 'grepgrepgrep' } }); + send({ method: 'account_login', params: { name: 'ntr', password: 'grepgrepgrep' } }); }); // Listen for messages diff --git a/server/WORKLOG.md b/server/WORKLOG.md index c633109d..9a4dc796 100755 --- a/server/WORKLOG.md +++ b/server/WORKLOG.md @@ -34,5 +34,17 @@ items give skills gem td style attr combinations stoney + spikey = jagged + plants + animals + viruses + fungus + artificial + elementals + + + first strike + + techno artists for the soundtrack + slimey ghostly \ No newline at end of file diff --git a/server/src/account.rs b/server/src/account.rs index b18217c4..7d8c47c7 100755 --- a/server/src/account.rs +++ b/server/src/account.rs @@ -3,12 +3,16 @@ use bcrypt::{hash, verify}; use rand::{thread_rng, Rng}; use rand::distributions::Alphanumeric; use std::iter; +use serde_cbor::{from_slice, to_vec}; + use std::str; use net::Db; use rpc::{AccountCreateParams, AccountLoginParams, RpcResult}; +use cryp::Cryp; + use failure::Error; use failure::err_msg; @@ -55,7 +59,7 @@ pub fn from_token(token: String, db: &Db) -> Result { return Ok(entry); } -pub fn create(params: AccountCreateParams, db: Db) -> Result { +pub fn create(params: AccountCreateParams, db: Db) -> Result { let id = Uuid::new_v4(); if params.password.len() < PASSWORD_MIN_LEN { @@ -99,10 +103,10 @@ pub fn create(params: AccountCreateParams, db: Db) -> Result { tx.commit()?; - return Ok(RpcResult::Account(entry)); + return Ok(entry); } -pub fn login(params: AccountLoginParams, db: Db) -> Result { +pub fn login(params: AccountLoginParams, db: Db) -> Result { let query = " SELECT id, name, token, password FROM accounts @@ -142,5 +146,31 @@ pub fn login(params: AccountLoginParams, db: Db) -> Result { token: entry.token, }; - return Ok(RpcResult::Account(account)); -} \ No newline at end of file + return Ok(account); +} + +pub fn fetch_cryps(db: Db, account: Account) -> Result, Error> { + let query = " + SELECT data + FROM cryps + WHERE account = $1; + "; + + let result = db + .query(query, &[&account.id])?; + + let cryps: Result, _> = result.iter().map(|row| { + let cryp_bytes: Vec = row.get(0); + from_slice::(&cryp_bytes) + }).collect(); + + // catch any errors + if cryps.is_err() { + return Err(err_msg("could not deserialize a cryp")); + } + + // now unwrap is safe + return Ok(cryps.unwrap()); +} + + diff --git a/server/src/combat.rs b/server/src/combat.rs index 03c0ccf6..e0f3ddc0 100755 --- a/server/src/combat.rs +++ b/server/src/combat.rs @@ -121,10 +121,7 @@ where F: Fn(&Battle) -> Result<(), Error> loop { battle.next(); - match send(&battle) { - Err(e) => break Err(err_msg("could not reply")), - _ => (), - }; + send(&battle)?; if battle.finished() { break Ok(battle) diff --git a/server/src/cryp.rs b/server/src/cryp.rs index d4cad64b..109ec70a 100755 --- a/server/src/cryp.rs +++ b/server/src/cryp.rs @@ -1,7 +1,7 @@ use uuid::Uuid; use rand::prelude::*; use serde_cbor::*; -use serde_cbor::{to_vec}; +use serde_cbor::{from_slice, to_vec}; use failure::Error; use failure::err_msg; @@ -217,7 +217,6 @@ pub fn spawn(params: CrypSpawnParams, db: Db, account: Account) -> Result(&data) { Ok(v) => Ok(RpcResponse { method: v.method, - params: create(v.params, db)? + params: RpcResult::Account(create(v.params, db)?) }), Err(_e) => Err(err_msg("invalid params")), } @@ -84,12 +84,20 @@ impl Rpc { match from_slice::(&data) { Ok(v) => Ok(RpcResponse { method: v.method, - params: login(v.params, db)? + params: RpcResult::Account(login(v.params, db)?) }), Err(_e) => Err(err_msg("invalid params")), } }, - + "account_cryps" => { + match account { + Some(u) => Ok(RpcResponse { + method: v.method, + params: RpcResult::CrypList(fetch_cryps(db, u)?) + }), + None => Err(err_msg("auth required")), + } + }, _ => Err(err_msg("unknown method")), } }, @@ -108,6 +116,7 @@ pub struct RpcResponse { pub enum RpcResult { SpawnCryp(Cryp), Account(Account), + CrypList(Vec), Pve(Battle), } @@ -163,6 +172,12 @@ pub struct AccountLoginParams { pub password: String, } +#[derive(Debug,Clone,Serialize,Deserialize)] +struct AccountCrypsMsg { + method: String, + params: (), +} + // #[cfg(test)] // mod tests { // use super::*;