diff --git a/client/src/actions.jsx b/client/src/actions.jsx index 99bcacc0..15f4cfb4 100644 --- a/client/src/actions.jsx +++ b/client/src/actions.jsx @@ -1,3 +1,8 @@ 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_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 new file mode 100644 index 00000000..490d1b45 --- /dev/null +++ b/client/src/components/cryp.container.js @@ -0,0 +1,10 @@ +const { connect } = require('preact-redux'); + +const InvStats = require('./invstats'); + +// Add the incident from state as a property +const addState = connect( + state => ({ cryp: state.cryp }), +); + +module.exports = addState(InvStats); diff --git a/client/src/components/invstats.jsx b/client/src/components/invstats.jsx index 6bb81263..81bc4c81 100755 --- a/client/src/components/invstats.jsx +++ b/client/src/components/invstats.jsx @@ -1,8 +1,8 @@ const preact = require('preact'); -// components all the way down +function InvStats({ cryp }) { + if (!cryp) return
not ready
; -function InvStats() { return (
@@ -23,9 +23,9 @@ function InvStats() {
- +
-

Cryp#XYZ

+

{cryp.name}

The big boy

@@ -44,6 +44,10 @@ function InvStats() {

Stat Area

+

{JSON.stringify(cryp.def)}

+

{JSON.stringify(cryp.dmg)}

+

{JSON.stringify(cryp.hp)}

+

{JSON.stringify(cryp.stam)}

diff --git a/client/src/components/login.component.jsx b/client/src/components/login.component.jsx new file mode 100644 index 00000000..ee57d6ca --- /dev/null +++ b/client/src/components/login.component.jsx @@ -0,0 +1,43 @@ +const preact = require('preact'); + +function renderLogin({ account, submitLogin }) { + if (account) return
{JSON.stringify(account)}
; + + const details = { + name: '', + password: '', + }; + + return ( +
+
+

+ details.name = e.target.value} /> + + + + + + +

+
+
+

+ details.password = e.target.value} /> + + + +

+
+
+

+ +

+
+
+ ); +} + +module.exports = renderLogin; diff --git a/client/src/components/login.container.jsx b/client/src/components/login.container.jsx new file mode 100644 index 00000000..e28077c6 --- /dev/null +++ b/client/src/components/login.container.jsx @@ -0,0 +1,16 @@ +const { connect } = require('preact-redux'); + +const Login = require('./login.component'); + +const addState = connect( + function receiveState(state) { + const { ws } = state; + function submitLogin(name, password) { + return ws.sendAccountLogin(name, password); + } + + return { account: state.account, submitLogin }; + }, +); + +module.exports = addState(Login); diff --git a/client/src/components/status.component.jsx b/client/src/components/status.component.jsx deleted file mode 100644 index 76c2b9a0..00000000 --- a/client/src/components/status.component.jsx +++ /dev/null @@ -1,6 +0,0 @@ -const preact = require('preact'); - -module.exports = ({ account }) => { - if (account) return
{JSON.stringify(account)}
; - return
not logged in
; -}; diff --git a/client/src/components/status.container.js b/client/src/components/status.container.js deleted file mode 100644 index 177ef91f..00000000 --- a/client/src/components/status.container.js +++ /dev/null @@ -1,13 +0,0 @@ -const { connect } = require('preact-redux'); - -const Status = require('./status.component'); - -// Add the incident from state as a property -const addState = connect( - state => ({ account: state.account }), - // dispatch => ({ - // closeDetails() { dispatch(openIncident()); }, - // }), -); - -module.exports = addState(Status); diff --git a/client/src/main.jsx b/client/src/main.jsx index 975fb51e..fce94aac 100644 --- a/client/src/main.jsx +++ b/client/src/main.jsx @@ -5,22 +5,27 @@ const { Provider } = require('preact-redux'); const { createStore, combineReducers } = require('redux'); const reducers = require('./reducers'); +const actions = require('./actions'); const fizzyText = require('../lib/fizzy-text'); const createSocket = require('./socket'); -const StatusContainer = require('./components/status.container'); +const CrypContainer = require('./components/cryp.container'); +const LoginContainer = require('./components/login.container'); const Navbar = require('./components/navbar'); // Redux Store const store = createStore( combineReducers({ account: reducers.accountReducer, + cryp: reducers.crypReducer, + ws: reducers.wsReducer, }), ); store.subscribe(() => console.log(store.getState())); const ws = createSocket(store); +store.dispatch(actions.setWs(ws)); // tells jdenticon to look for new svgs and render them // so we don't have to setInnerHtml or manually call update @@ -33,9 +38,9 @@ const Cryps = () => (
- + +
- ); const Main = () => ( diff --git a/client/src/reducers.jsx b/client/src/reducers.jsx index 72a483d7..1aab82ad 100644 --- a/client/src/reducers.jsx +++ b/client/src/reducers.jsx @@ -1,9 +1,29 @@ -const { SET_ACCOUNT } = require('./actions'); +const actions = require('./actions'); -const defaultAccount = {}; +const defaultAccount = null; function accountReducer(state = defaultAccount, action) { switch (action.type) { - case SET_ACCOUNT: + case actions.SET_ACCOUNT: + return action.value; + default: + return state; + } +} + +const defaultCryp = null; +function crypReducer(state = defaultCryp, action) { + switch (action.type) { + case actions.SET_CRYP: + return action.value; + default: + return state; + } +} + +const defaultWs = null; +function wsReducer(state = defaultWs, action) { + switch (action.type) { + case actions.SET_WS: return action.value; default: return state; @@ -12,4 +32,6 @@ function accountReducer(state = defaultAccount, action) { module.exports = { accountReducer, + crypReducer, + wsReducer, }; diff --git a/client/src/socket.jsx b/client/src/socket.jsx index 6893f046..d4cd05f8 100644 --- a/client/src/socket.jsx +++ b/client/src/socket.jsx @@ -37,16 +37,26 @@ function createSocket(store) { return send({ method: 'cryp_spawn', params: { name: 'drake' }}); } - function crypSpawn(cryp) { + function crypSpawn(response) { + const [structName, cryp] = response; console.log('got a new cryp', cryp); + return store.dispatch(actions.setCryp(cryp)); } + function sendAccountLogin(name, password) { + send({ method: 'account_login', params: { name, password } }); + } + + // when the server sends a reply it will have one of these message types + // this object wraps the reply types to a function const handlers = { cryp_spawn: crypSpawn, account_login: accountLogin, account_create: accountLogin, }; + // decodes the cbor and + // calls the handlers defined above based on message type function onMessage(event) { // decode binary msg from server const blob = new Uint8Array(event.data); @@ -62,7 +72,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 @@ -77,6 +87,10 @@ function createSocket(store) { console.error('WebSocket closed', event); account = null; }); + + return { + sendAccountLogin, + }; } module.exports = createSocket; diff --git a/server/WORKLOG.md b/server/WORKLOG.md index 5cc68530..40c26c5d 100755 --- a/server/WORKLOG.md +++ b/server/WORKLOG.md @@ -1,13 +1,18 @@ * Battling * Logins ✔️ - * Cryp Ownership + * Cryp Ownership ✔ * Matchmaking * Lobbies * Create * Join * Resolve * Stats + * Scrabble grid + * Items + * Grid reroll + * Colour scheme * Missions + * Bosses * Cryp Generation * diff --git a/server/src/account.rs b/server/src/account.rs index 514f577c..b18217c4 100755 --- a/server/src/account.rs +++ b/server/src/account.rs @@ -1,5 +1,5 @@ use uuid::Uuid; -use bcrypt::{DEFAULT_COST, hash, verify}; +use bcrypt::{hash, verify}; use rand::{thread_rng, Rng}; use rand::distributions::Alphanumeric; use std::iter; @@ -61,7 +61,7 @@ pub fn create(params: AccountCreateParams, db: Db) -> Result { if params.password.len() < PASSWORD_MIN_LEN { return Err(err_msg("password must be at least 12 characters")); } - let password = hash(¶ms.password, DEFAULT_COST)?; + let password = hash(¶ms.password, 4)?; let mut rng = thread_rng(); let token: String = iter::repeat(())