108 lines
2.6 KiB
JavaScript
108 lines
2.6 KiB
JavaScript
const preact = require('preact');
|
|
const { connect } = require('preact-redux');
|
|
|
|
const actions = require('../actions');
|
|
|
|
const addState = connect(
|
|
function receiveState(state) {
|
|
const {
|
|
ws,
|
|
game,
|
|
account,
|
|
authenticated,
|
|
instance,
|
|
chatShow,
|
|
animating,
|
|
} = state;
|
|
|
|
function sendReady() {
|
|
document.activeElement.blur();
|
|
return ws.sendGameReady(game.id);
|
|
}
|
|
|
|
function getInstanceState() {
|
|
return ws.sendInstanceState(game.instance);
|
|
}
|
|
|
|
function sendGameSkillClear() {
|
|
return ws.sendGameSkillClear(game.id);
|
|
}
|
|
|
|
function sendAbandon() {
|
|
return ws.sendInstanceAbandon(game.instance);
|
|
}
|
|
|
|
return {
|
|
game,
|
|
account,
|
|
authenticated,
|
|
instance,
|
|
chatShow,
|
|
sendAbandon,
|
|
sendGameSkillClear,
|
|
sendReady,
|
|
getInstanceState,
|
|
animating,
|
|
};
|
|
},
|
|
|
|
function receiveDispatch(dispatch) {
|
|
function quit() {
|
|
dispatch(actions.setNav('transition'));
|
|
dispatch(actions.setGame(null));
|
|
}
|
|
|
|
function setChatShow(v) {
|
|
dispatch(actions.setChatShow(v));
|
|
}
|
|
|
|
return {
|
|
setChatShow,
|
|
quit,
|
|
};
|
|
}
|
|
);
|
|
|
|
function GameCtrlBtns(args) {
|
|
const {
|
|
game,
|
|
animating,
|
|
account,
|
|
chatShow,
|
|
authenticated,
|
|
instance,
|
|
|
|
getInstanceState,
|
|
sendGameSkillClear,
|
|
sendReady,
|
|
setChatShow,
|
|
quit,
|
|
} = args;
|
|
|
|
if (!game) return false;
|
|
const finished = game.phase === 'Finished';
|
|
|
|
function quitClick() {
|
|
if (authenticated) {
|
|
getInstanceState();
|
|
}
|
|
quit();
|
|
}
|
|
|
|
const firstRoundTutorial = instance && instance.time_control === 'Practice' && !game.resolutions.length;
|
|
const noTargets = firstRoundTutorial && game.stack.length === 0;
|
|
|
|
const readyBtn = <button disabled={animating || noTargets} class="ready" onClick={() => sendReady()}>Ready</button>;
|
|
const quitBtn = <button disabled={animating} class="quit" onClick={quitClick}>Back</button>;
|
|
|
|
return (
|
|
<div class="game-ctrl-btns">
|
|
<button disabled={!account.subscribed} onClick={() => setChatShow(!chatShow)}>Chat</button>
|
|
<button disabled={animating} onClick={sendGameSkillClear}>Clear</button>
|
|
{finished ? quitBtn : readyBtn}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
module.exports = addState(GameCtrlBtns);
|