using items
This commit is contained in:
parent
dcdd9a232b
commit
901dc28541
@ -13,5 +13,8 @@ export const setBattle = (value) => ({ type: SET_BATTLE, value });
|
|||||||
export const SET_ACTIVE_CRYP = 'SET_ACTIVE_CRYP';
|
export const SET_ACTIVE_CRYP = 'SET_ACTIVE_CRYP';
|
||||||
export const setActiveCryp = (value) => ({ type: SET_ACTIVE_CRYP, value });
|
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 SET_WS = 'SET_WS';
|
||||||
export const setWs = (value) => ({ type: SET_WS, value });
|
export const setWs = (value) => ({ type: SET_WS, value });
|
||||||
|
|||||||
@ -4,12 +4,19 @@ const CrypList = require('./cryp.list');
|
|||||||
|
|
||||||
const addState = connect(
|
const addState = connect(
|
||||||
function receiveState(state) {
|
function receiveState(state) {
|
||||||
const { ws, cryps } = state;
|
const { ws, cryps, activeItem } = state;
|
||||||
function sendCombatPve(crypId) {
|
function sendCombatPve(crypId) {
|
||||||
return ws.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 };
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,14 @@
|
|||||||
|
// eslint-disable-next-line
|
||||||
const preact = require('preact');
|
const preact = require('preact');
|
||||||
|
|
||||||
|
function CrypList({ cryps, activeItem, sendCombatPve, sendItemUse }) {
|
||||||
function CrypList({ cryps, sendCombatPve }) {
|
|
||||||
if (!cryps) return <div>not ready</div>;
|
if (!cryps) return <div>not ready</div>;
|
||||||
const crypPanels = cryps.map(cryp => (
|
const crypPanels = cryps.map(cryp => (
|
||||||
|
|
||||||
<div key={cryp.id} className="tile is-vertical box">
|
<div key={cryp.id}
|
||||||
|
className="tile is-vertical box"
|
||||||
|
style={activeItem ? { cursor: 'pointer' } : {}}
|
||||||
|
onClick={() => sendItemUse(cryp.id)} >
|
||||||
<div className="tile is-vertical is-child">
|
<div className="tile is-vertical is-child">
|
||||||
<div className="columns" >
|
<div className="columns" >
|
||||||
<div className="column is-10">
|
<div className="column is-10">
|
||||||
@ -25,14 +28,14 @@ function CrypList({ cryps, sendCombatPve }) {
|
|||||||
<button
|
<button
|
||||||
className="button is-dark"
|
className="button is-dark"
|
||||||
type="submit"
|
type="submit"
|
||||||
|
disabled={cryp.hp.value === 0}
|
||||||
onClick={() => sendCombatPve(cryp.id)}>
|
onClick={() => sendCombatPve(cryp.id)}>
|
||||||
Start PVE
|
Start PVE
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
));
|
));
|
||||||
return (
|
return (
|
||||||
// <div className="tile is-parent is-vertical" >
|
<div className="tile is-parent is-vertical" >
|
||||||
<div>
|
|
||||||
{crypPanels}
|
{crypPanels}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,15 +1,19 @@
|
|||||||
const { connect } = require('preact-redux');
|
const { connect } = require('preact-redux');
|
||||||
|
const actions = require('../actions');
|
||||||
|
|
||||||
const ItemList = require('./item.list');
|
const ItemList = require('./item.list');
|
||||||
|
|
||||||
const addState = connect(
|
const addState = connect(
|
||||||
function receiveState(state) {
|
function receiveState(state) {
|
||||||
const { ws, items } = state;
|
const { items } = state;
|
||||||
function sendItemUse(crypId) {
|
return { items };
|
||||||
return ws.sendItemUse(crypId);
|
},
|
||||||
|
function receiveDispatch(dispatch) {
|
||||||
|
function setActiveItem(id) {
|
||||||
|
dispatch(actions.setActiveItem(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
return { items, sendItemUse };
|
return { setActiveItem };
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
|
// eslint-disable-next-line
|
||||||
const preact = require('preact');
|
const preact = require('preact');
|
||||||
|
|
||||||
function ItemList({ items, sendItemUse }) {
|
function ItemList({ items, setActiveItem }) {
|
||||||
if (!items) return <div>...</div>;
|
if (!items) return <div>...</div>;
|
||||||
const itemPanels = items.map(item => (
|
const itemPanels = items.map(item => (
|
||||||
|
|
||||||
@ -21,7 +22,7 @@ function ItemList({ items, sendItemUse }) {
|
|||||||
<button
|
<button
|
||||||
className="button is-dark"
|
className="button is-dark"
|
||||||
type="submit"
|
type="submit"
|
||||||
onClick={() => sendItemUse(item.id)}>
|
onClick={() => setActiveItem(item.id)}>
|
||||||
Use
|
Use
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -15,6 +15,7 @@ const Body = require('./components/body.component');
|
|||||||
// Redux Store
|
// Redux Store
|
||||||
const store = createStore(
|
const store = createStore(
|
||||||
combineReducers({
|
combineReducers({
|
||||||
|
activeItem: reducers.activeItemReducer,
|
||||||
account: reducers.accountReducer,
|
account: reducers.accountReducer,
|
||||||
battle: reducers.battleReducer,
|
battle: reducers.battleReducer,
|
||||||
cryps: reducers.crypsReducer,
|
cryps: reducers.crypsReducer,
|
||||||
|
|||||||
@ -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;
|
const defaultBattle = null;
|
||||||
function battleReducer(state = defaultBattle, action) {
|
function battleReducer(state = defaultBattle, action) {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
@ -51,6 +61,7 @@ function wsReducer(state = defaultWs, action) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
activeItemReducer,
|
||||||
battleReducer,
|
battleReducer,
|
||||||
accountReducer,
|
accountReducer,
|
||||||
crypsReducer,
|
crypsReducer,
|
||||||
|
|||||||
@ -109,6 +109,12 @@ function createSocket(store) {
|
|||||||
send({ method: 'combat_pve', params: { id } });
|
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
|
// Handling
|
||||||
@ -144,6 +150,7 @@ function createSocket(store) {
|
|||||||
sendAccountLogin,
|
sendAccountLogin,
|
||||||
sendCombatPve,
|
sendCombatPve,
|
||||||
sendCrypSpawn,
|
sendCrypSpawn,
|
||||||
|
sendItemUse,
|
||||||
connect,
|
connect,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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> {
|
pub fn item_use(params: ItemUseParams, tx: &mut Transaction, account: &Account) -> Result<(), Error> {
|
||||||
let query = "
|
let query = "
|
||||||
SELECT data
|
SELECT data
|
||||||
FOR UPDATE
|
|
||||||
FROM items
|
FROM items
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
AND account = $2;
|
AND account = $2
|
||||||
|
FOR UPDATE;
|
||||||
";
|
";
|
||||||
|
|
||||||
let result = tx
|
let result = tx
|
||||||
|
|||||||
@ -142,18 +142,21 @@ impl Rpc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn item_use(data: Vec<u8>, tx: &mut Transaction, account: Option<Account>, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
|
fn item_use(data: Vec<u8>, tx: &mut Transaction, account: Option<Account>, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
|
||||||
match from_slice::<ItemUseMsg>(&data) {
|
let a = match account {
|
||||||
Ok(v) => {
|
Some(a) => a,
|
||||||
match account {
|
None => return Err(err_msg("auth required")),
|
||||||
Some(a) => Ok(RpcResponse {
|
};
|
||||||
method: v.method,
|
|
||||||
params: RpcResult::ItemUse(item_use(v.params, tx, &a)?)
|
let msg = from_slice::<ItemUseMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
}),
|
|
||||||
None => Err(err_msg("auth required")),
|
item_use(msg.params, tx, &a)?;
|
||||||
}
|
|
||||||
}
|
let cryps_list = RpcResponse {
|
||||||
Err(_e) => Err(err_msg("invalid params")),
|
method: "account_cryps".to_string(),
|
||||||
}
|
params: RpcResult::CrypList(account_cryps(tx, &a)?)
|
||||||
|
};
|
||||||
|
|
||||||
|
return Ok(cryps_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user