diff --git a/client/src/actions.jsx b/client/src/actions.jsx index 5d1990bf..524561a1 100644 --- a/client/src/actions.jsx +++ b/client/src/actions.jsx @@ -4,6 +4,9 @@ export const setAccount = (value) => ({ type: SET_ACCOUNT, value }); export const SET_CRYPS = 'SET_CRYPS'; export const setCryps = (value) => ({ type: SET_CRYPS, value }); +export const SET_BATTLE = 'SET_BATTLE'; +export const setBattle = (value) => ({ type: SET_BATTLE, value }); + export const SET_ACTIVE_CRYP = 'SET_ACTIVE_CRYP'; export const setActiveCryp = (value) => ({ type: SET_ACTIVE_CRYP, value }); diff --git a/client/src/components/battle.container.js b/client/src/components/battle.container.js new file mode 100644 index 00000000..aef38566 --- /dev/null +++ b/client/src/components/battle.container.js @@ -0,0 +1,16 @@ +const { connect } = require('preact-redux'); + +const Battle = require('./battle'); + +const addState = connect( + function receiveState(state) { + const { battle } = state; + // function sendCombatPve(crypId) { + // return ws.sendCombatPve(crypId); + // } + + return { battle }; + } +); + +module.exports = addState(Battle); diff --git a/client/src/components/battle.jsx b/client/src/components/battle.jsx new file mode 100644 index 00000000..229294f5 --- /dev/null +++ b/client/src/components/battle.jsx @@ -0,0 +1,11 @@ +const preact = require('preact'); + +function CrypPanel({ battle }) { + return ( +
+ {JSON.stringify(battle)} +
+ ); +} + +module.exports = CrypPanel; diff --git a/client/src/components/body.component.jsx b/client/src/components/body.component.jsx new file mode 100644 index 00000000..159f8c41 --- /dev/null +++ b/client/src/components/body.component.jsx @@ -0,0 +1,20 @@ +// eslint-disable-next-line +const preact = require('preact'); + +const CrypListContainer = require('./cryp.list.container'); +const BattleContainer = require('./battle.container'); + +function renderBody() { + return ( +
+
+ +
+
+ +
+
+ ); +} + +module.exports = renderBody; diff --git a/client/src/components/cryp.battle.jsx b/client/src/components/cryp.battle.jsx deleted file mode 100644 index b388afc2..00000000 --- a/client/src/components/cryp.battle.jsx +++ /dev/null @@ -1,100 +0,0 @@ -const preact = require('preact'); - -function CrypPanel() { - return ( -
-
-
-
-

Future Animation / Fizzy Area

-
-
-
-
-
- -
-
-
-
-
-

Drake

-

Level 1

-
-
-

Flying

-

Fire

-

Stone

-
-
-
    5
-
    5
-
    5
-
-
-
5 / 5 HP
- -
-
-
-
-
-
-
-
-
-
-
-
-
    10
-
    1
-
    40
-
-
-

Walking

-

Water

-

Rock

-
-
-

BB

-

Level 1

-
-
-
5 / 5 HP
- -
-
-
- -
-
-
-
-
-
-
- Ability 1 -
- Ability 3 -
-
- Ability 2 -
- Ability 4 -
-
-

Combat Log

-

"bamboo basher" deals 0 dmg to "drake" (0 blocked / 1 hp remaining)

-

"bamboo basher" deals 0 dmg to "drake" (0 blocked / 1 hp remaining)

-
-
-
-
-
- -
-
- ); -} - -module.exports = CrypPanel; diff --git a/client/src/components/cryps.container.js b/client/src/components/cryp.list.container.js similarity index 77% rename from client/src/components/cryps.container.js rename to client/src/components/cryp.list.container.js index 7888b0b9..291f4e45 100644 --- a/client/src/components/cryps.container.js +++ b/client/src/components/cryp.list.container.js @@ -1,6 +1,6 @@ const { connect } = require('preact-redux'); -const CrypsPanel = require('./cryps.panel'); +const CrypList = require('./cryp.list'); const addState = connect( function receiveState(state) { @@ -13,4 +13,4 @@ const addState = connect( } ); -module.exports = addState(CrypsPanel); +module.exports = addState(CrypList); diff --git a/client/src/components/cryp.list.jsx b/client/src/components/cryp.list.jsx new file mode 100644 index 00000000..072ea116 --- /dev/null +++ b/client/src/components/cryp.list.jsx @@ -0,0 +1,23 @@ +const preact = require('preact'); + +function CrypPanel({ cryps, sendCombatPve }) { + if (!cryps) return
not ready
; + const crypPanels = cryps.map(cryp => ( +
+
{cryp.name}
+ +
+ )); + return ( +
+ {crypPanels} +
+ ); +} + +module.exports = CrypPanel; diff --git a/client/src/components/cryp.panel.jsx b/client/src/components/cryp.panel.jsx new file mode 100644 index 00000000..99694d6e --- /dev/null +++ b/client/src/components/cryp.panel.jsx @@ -0,0 +1,37 @@ +const preact = require('preact'); + +function renderCrypPanel(cryp) { + return ( +
+
+
+
+
+ +
+
+
+
+
+

Drake

+

Level 1

+
+
+

Flying

+

Fire

+

Stone

+
+
+
    5
+
    5
+
    5
+
+
+
5 / 5 HP
+ +
+
+
+
+ ) +} diff --git a/client/src/components/cryps.panel.jsx b/client/src/components/cryps.panel.jsx deleted file mode 100644 index a8fe1e84..00000000 --- a/client/src/components/cryps.panel.jsx +++ /dev/null @@ -1,88 +0,0 @@ -const preact = require('preact'); - -function CrypPanel({ cryps, sendCombatPve }) { - if (!cryps) return
not ready
; - const crypPanels = cryps.map(cryp => ( -
-
-
-
-

Helm Slot

-
-
-

Body Armor

-
-
-

Gloves

-
-
-

Boots

-
-
-
-
-
- -
-

{cryp.name}

-

The big boy

-
-
-
-
-

Weapon

-
-
-

Jewellery

-
-
-

Artifact

-
-
-
-
-
-

Cryp Stats

-

Level - {cryp.lvl}

-
-
-
    Hit Points {cryp.hp.value}
-
    Attack Damage {cryp.dmg.value}
-
-
-
    Stamina {cryp.stam.value}
-
    Defense {cryp.def.value}
-
-
-
- Progress to next level -
-
- -
-
- {cryp.xp} / 4 XP -
-
- -
-
-
- )); - // 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/components/header.component.jsx b/client/src/components/header.component.jsx new file mode 100644 index 00000000..9257349b --- /dev/null +++ b/client/src/components/header.component.jsx @@ -0,0 +1,19 @@ +// eslint-disable-next-line +const preact = require('preact'); + +const LoginContainer = require('./login.container'); + +function renderHeader() { + return ( +
+
+
+

cryps.gg

+ +
+
+
+ ) +} + +module.exports = renderHeader; diff --git a/client/src/components/sidebar.component.jsx b/client/src/components/sidebar.component.jsx new file mode 100644 index 00000000..78f50ebe --- /dev/null +++ b/client/src/components/sidebar.component.jsx @@ -0,0 +1,19 @@ +// eslint-disable-next-line +const preact = require('preact'); + +const LoginContainer = require('./login.container'); + +function renderHeader() { + return ( +
+
+
+

cryps.gg

+ +
+
+
+ ); +} + +module.exports = renderHeader; diff --git a/client/src/main.jsx b/client/src/main.jsx index 90828917..15b1bfb9 100755 --- a/client/src/main.jsx +++ b/client/src/main.jsx @@ -6,17 +6,16 @@ const { createStore, combineReducers } = require('redux'); const reducers = require('./reducers'); const actions = require('./actions'); -const fizzyText = require('../lib/fizzy-text'); +// const fizzyText = require('../lib/fizzy-text'); const createSocket = require('./socket'); -const CrypsContainer = require('./components/cryps.container'); -const LoginContainer = require('./components/login.container'); -const CrypBattle = require('./components/cryp.battle'); -// const Navbar = require('./components/navbar'); +const Header = require('./components/header.component'); +const Body = require('./components/body.component'); // Redux Store const store = createStore( combineReducers({ + battle: reducers.battleReducer, account: reducers.accountReducer, cryps: reducers.crypsReducer, ws: reducers.wsReducer, @@ -37,11 +36,8 @@ jdenticon.config = { const Cryps = () => (
- -
- -
- +
+
); @@ -53,4 +49,4 @@ const Main = () => ( preact.render(
, document.body); -fizzyText('cryps.gg'); +// fizzyText('cryps.gg'); diff --git a/client/src/reducers.jsx b/client/src/reducers.jsx index 94ddfaa7..517e1f96 100644 --- a/client/src/reducers.jsx +++ b/client/src/reducers.jsx @@ -20,6 +20,16 @@ function crypsReducer(state = defaultCryps, action) { } } +const defaultBattle = null; +function battleReducer(state = defaultBattle, action) { + switch (action.type) { + case actions.SET_BATTLE: + return action.value; + default: + return state; + } +} + const defaultWs = null; function wsReducer(state = defaultWs, action) { switch (action.type) { @@ -31,6 +41,7 @@ function wsReducer(state = defaultWs, action) { } module.exports = { + battleReducer, accountReducer, crypsReducer, wsReducer, diff --git a/client/src/socket.jsx b/client/src/socket.jsx index dd407f48..e8b9f199 100644 --- a/client/src/socket.jsx +++ b/client/src/socket.jsx @@ -41,6 +41,11 @@ function createSocket(store) { console.log('got my cryps', cryps); } + function battleState(response) { + const [structName, battle] = response; + store.dispatch(actions.setBattle(battle)); + } + function crypSpawn(response) { const [structName, cryp] = response; console.log('got a new cryp', cryp); @@ -77,6 +82,7 @@ function createSocket(store) { const handlers = { cryp_spawn: crypSpawn, combat_pve: combatPve, + battle_state: battleState, account_login: accountLogin, account_create: accountLogin, account_cryps: accountCryps, diff --git a/server/WORKLOG.md b/server/WORKLOG.md index 9a4dc796..1ad4ff5b 100755 --- a/server/WORKLOG.md +++ b/server/WORKLOG.md @@ -1,4 +1,7 @@ * Battling + * QOL + * auto login + * ws reconnect * Levelling * Global rolls * Logins ✔️ diff --git a/server/src/combat.rs b/server/src/combat.rs index e0f3ddc0..1c4e7edf 100755 --- a/server/src/combat.rs +++ b/server/src/combat.rs @@ -10,7 +10,7 @@ use net::Db; use account::Account; use rpc::{CombatPveParams}; -use cryp::Cryp; +use cryp::{Cryp, write_cryp}; use battle::Battle; use skill::Skill; @@ -113,7 +113,7 @@ where F: Fn(&Battle) -> Result<(), Error> // tells from_slice to cast into a cryp let cryp_bytes: Vec = returned.get("data"); - let plr: Cryp = from_slice::(&cryp_bytes)?; + let mut plr: Cryp = from_slice::(&cryp_bytes)?; let mob = generate_mob(&plr); let mut battle = Battle::new(&plr, &mob); @@ -124,7 +124,17 @@ where F: Fn(&Battle) -> Result<(), Error> send(&battle)?; if battle.finished() { - break Ok(battle) + let success = match battle.winner() { + Some(c) => c.id == plr.id, + None => false, + }; + + if success { + plr = plr.add_xp(); + write_cryp(plr, db, account)?; + } + + break Ok(battle) } } } diff --git a/server/src/cryp.rs b/server/src/cryp.rs index 109ec70a..bb16c9d3 100755 --- a/server/src/cryp.rs +++ b/server/src/cryp.rs @@ -217,6 +217,31 @@ pub fn spawn(params: CrypSpawnParams, db: Db, account: Account) -> Result Result { + let cryp_bytes = to_vec(&cryp)?; + + let query = " + UPDATE cryps + SET data = $1 + WHERE id = $2 + AND account = $3 + RETURNING id, account, data; + "; + + let tx = db.transaction()?; + + let result = tx + .query(query, &[&cryp_bytes, &cryp.id, &account.id])?; + + let _returned = result.iter().next().expect("no row returned"); + + println!("{:?} wrote cryp {:}", account.id, cryp.id); + + tx.commit()?; + + return Ok(cryp); +} + #[cfg(test)] mod tests { use cryp::*; diff --git a/server/src/rpc.rs b/server/src/rpc.rs index 2472abc7..4feed4be 100755 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -48,7 +48,7 @@ impl Rpc { "combat_pve" => { let send = |b: &Battle| -> Result<(), Error> { let reply = RpcResponse { - method: "combat_pve".to_string(), + method: "battle_state".to_string(), params: RpcResult::Pve(b.clone()) }; let response = to_vec(&reply)?;