diff --git a/client/src/actions.jsx b/client/src/actions.jsx index 52a062c3..911de48b 100644 --- a/client/src/actions.jsx +++ b/client/src/actions.jsx @@ -13,5 +13,8 @@ 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 }); +export const SET_ACTIVE_ITEM = 'SET_ACTIVE_ITEM'; +export const setActiveItem = (value) => ({ type: SET_ACTIVE_ITEM, value }); + export const SET_WS = 'SET_WS'; export const setWs = (value) => ({ type: SET_WS, value }); diff --git a/client/src/components/cryp.list.container.js b/client/src/components/cryp.list.container.js index 291f4e45..89f98009 100644 --- a/client/src/components/cryp.list.container.js +++ b/client/src/components/cryp.list.container.js @@ -4,12 +4,19 @@ const CrypList = require('./cryp.list'); const addState = connect( function receiveState(state) { - const { ws, cryps } = state; + const { ws, cryps, activeItem } = state; function sendCombatPve(crypId) { return ws.sendCombatPve(crypId); } - return { cryps, sendCombatPve }; + function sendItemUse(targetId) { + if (activeItem) { + return ws.sendItemUse(activeItem, targetId); + } + return false; + } + + return { cryps, sendCombatPve, activeItem, sendItemUse }; } ); diff --git a/client/src/components/cryp.list.jsx b/client/src/components/cryp.list.jsx index 531a735a..843bfdeb 100644 --- a/client/src/components/cryp.list.jsx +++ b/client/src/components/cryp.list.jsx @@ -1,11 +1,14 @@ +// eslint-disable-next-line const preact = require('preact'); - -function CrypList({ cryps, sendCombatPve }) { +function CrypList({ cryps, activeItem, sendCombatPve, sendItemUse }) { if (!cryps) return
not ready
; const crypPanels = cryps.map(cryp => ( -
+
sendItemUse(cryp.id)} >
@@ -25,14 +28,14 @@ function CrypList({ cryps, sendCombatPve }) {
)); return ( - //
-
+
{crypPanels}
); diff --git a/client/src/components/item.list.container.js b/client/src/components/item.list.container.js index d035102a..bfa8731d 100644 --- a/client/src/components/item.list.container.js +++ b/client/src/components/item.list.container.js @@ -1,15 +1,19 @@ const { connect } = require('preact-redux'); +const actions = require('../actions'); const ItemList = require('./item.list'); const addState = connect( function receiveState(state) { - const { ws, items } = state; - function sendItemUse(crypId) { - return ws.sendItemUse(crypId); + const { items } = state; + return { items }; + }, + function receiveDispatch(dispatch) { + function setActiveItem(id) { + dispatch(actions.setActiveItem(id)) } - return { items, sendItemUse }; + return { setActiveItem }; } ); diff --git a/client/src/components/item.list.jsx b/client/src/components/item.list.jsx index 3fb705e4..d0cd6199 100644 --- a/client/src/components/item.list.jsx +++ b/client/src/components/item.list.jsx @@ -1,6 +1,7 @@ +// eslint-disable-next-line const preact = require('preact'); -function ItemList({ items, sendItemUse }) { +function ItemList({ items, setActiveItem }) { if (!items) return
...
; const itemPanels = items.map(item => ( @@ -21,7 +22,7 @@ function ItemList({ items, sendItemUse }) {
diff --git a/client/src/main.jsx b/client/src/main.jsx index 11aa1a69..08c2fff1 100755 --- a/client/src/main.jsx +++ b/client/src/main.jsx @@ -15,6 +15,7 @@ const Body = require('./components/body.component'); // Redux Store const store = createStore( combineReducers({ + activeItem: reducers.activeItemReducer, account: reducers.accountReducer, battle: reducers.battleReducer, cryps: reducers.crypsReducer, diff --git a/client/src/reducers.jsx b/client/src/reducers.jsx index 41f9c3e1..1158eb54 100644 --- a/client/src/reducers.jsx +++ b/client/src/reducers.jsx @@ -30,6 +30,16 @@ function itemsReducer(state = defaultItems, action) { } } +const defaultActiveItem = null; +function activeItemReducer(state = defaultActiveItem, action) { + switch (action.type) { + case actions.SET_ACTIVE_ITEM: + return action.value; + default: + return state; + } +} + const defaultBattle = null; function battleReducer(state = defaultBattle, action) { switch (action.type) { @@ -51,6 +61,7 @@ function wsReducer(state = defaultWs, action) { } module.exports = { + activeItemReducer, battleReducer, accountReducer, crypsReducer, diff --git a/client/src/socket.jsx b/client/src/socket.jsx index 6aa5018f..a9367800 100644 --- a/client/src/socket.jsx +++ b/client/src/socket.jsx @@ -109,6 +109,12 @@ function createSocket(store) { send({ method: 'combat_pve', params: { id } }); } + function sendItemUse(item, target) { + console.log(item, target); + send({ method: 'item_use', params: { item, target } }); + store.dispatch(actions.setActiveItem(null)); + } + // ------------- // Handling @@ -144,6 +150,7 @@ function createSocket(store) { sendAccountLogin, sendCombatPve, sendCrypSpawn, + sendItemUse, connect, }; } diff --git a/server/src/item.rs b/server/src/item.rs index b158e7a5..3d46bf4b 100644 --- a/server/src/item.rs +++ b/server/src/item.rs @@ -70,10 +70,10 @@ pub fn item_create(item: Item, tx: &mut Transaction, account: &Account) -> Resul pub fn item_use(params: ItemUseParams, tx: &mut Transaction, account: &Account) -> Result<(), Error> { let query = " SELECT data - FOR UPDATE FROM items WHERE id = $1 - AND account = $2; + AND account = $2 + FOR UPDATE; "; let result = tx diff --git a/server/src/rpc.rs b/server/src/rpc.rs index 9d45998e..0414ef4a 100755 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -142,18 +142,21 @@ impl Rpc { } fn item_use(data: Vec, tx: &mut Transaction, account: Option, _client: &mut WebSocket) -> Result { - match from_slice::(&data) { - Ok(v) => { - match account { - Some(a) => Ok(RpcResponse { - method: v.method, - params: RpcResult::ItemUse(item_use(v.params, tx, &a)?) - }), - None => Err(err_msg("auth required")), - } - } - Err(_e) => Err(err_msg("invalid params")), - } + let a = match account { + Some(a) => a, + None => return Err(err_msg("auth required")), + }; + + let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; + + item_use(msg.params, tx, &a)?; + + let cryps_list = RpcResponse { + method: "account_cryps".to_string(), + params: RpcResult::CrypList(account_cryps(tx, &a)?) + }; + + return Ok(cryps_list); }