const preact = require('preact');
const { connect } = require('preact-redux');
const actions = require('../actions');
// dunno if this is a good idea
// bashing together instance and game to save having two panels
const addState = connect(
function receiveState(state) {
const {
ws,
game,
account,
animating,
} = state;
function sendReady() {
document.activeElement.blur();
return ws.sendGameReady(game.id);
}
function getInstanceState() {
return ws.sendInstanceState(game.instance);
}
return {
game,
sendReady,
account,
getInstanceState,
animating,
};
},
function receiveDispatch(dispatch) {
function quit() {
dispatch(actions.setNav('transition'));
dispatch(actions.setGame(null));
dispatch(actions.setInstance(null));
}
return { quit };
}
);
function scoreboard(game, player, isOpponent) {
const text = game.phase === 'Finished'
? player.wins > game.rounds / 2 && 'Winner'
: '';
const classes =`scoreboard ${player.ready ? 'ready' : ''}`;
const body = isOpponent
? (
| {player.name} |
| {player.wins} / {player.losses} |
| {player.ready ? 'ready' : ''} |
) : (
| {player.ready ? 'ready' : ''} |
| {player.wins} / {player.losses} |
| {player.name} |
);
return (
);
}
function Controls(args) {
const {
account,
game,
animating,
sendReady,
quit,
getInstanceState,
} = args;
if (!game) return false;
const opponent = game.players.find(t => t.id !== account.id);
const player = game.players.find(t => t.id === account.id);
function quitClick() {
getInstanceState();
quit();
}
const zero = Date.parse(game.phase_start);
const now = Date.now();
const end = Date.parse(game.phase_end);
const timerPct = game.phase_end
? ((now - zero) / (end - zero) * 100)
: 100;
const displayColour = !game.phase_end
? '#222'
: player.ready
? 'forestgreen'
: timerPct > 80
? 'red'
: 'whitesmoke';
const timerStyles = {
height: `${timerPct > 100 ? 100 : timerPct}%`,
background: displayColour,
};
const timer = (
);
const readyBtn = ;
const quitBtn = ;
return (
);
}
module.exports = addState(Controls);