mnml/client/src/events.jsx
2020-01-23 11:19:33 +10:00

257 lines
7.2 KiB
JavaScript

const LogRocket = require('logrocket');
const querystring = require('query-string');
const eachSeries = require('async/eachSeries');
const sample = require('lodash/sample');
const actions = require('./actions');
const { setAnimations, clearAnimations } = require('./animations.utils');
const { infoToast, errorToast } = require('./utils');
const { tutorialVbox } = require('./tutorial.utils');
function registerEvents(store) {
function notify(msg) {
if (window.Notification && window.Notification.permission === 'granted') {
const n = new Notification('MNML', {
body: msg,
tag: 'MNML',
});
}
return infoToast(msg);
}
function error(msg) {
return errorToast(msg);
}
function clearTutorial() {
store.dispatch(actions.setTutorial(null));
}
function setPing(ping) {
store.dispatch(actions.setPing(ping));
}
function setPvp(v) {
store.dispatch(actions.setPvp(v));
}
function setNav(v) {
store.dispatch(actions.setNav(v));
}
function setTeam(team) {
store.dispatch(actions.setTeam(team));
const { nav } = store.getState();
if (nav !== 'reshape') setNav('play');
}
function setSubscription(sub) {
const { subscription } = store.getState();
if (subscription && sub.cancel_at_period_end) {
notify('Your subscription has been cancelled. Thank you for your support.');
}
store.dispatch(actions.setSubscription(sub));
}
function setConstructList(constructs) {
store.dispatch(actions.setConstructs(constructs));
}
function setNewConstruct(construct) {
const { constructs } = store.getState();
constructs.push(construct);
store.dispatch(actions.setConstructs(constructs));
}
function setWs(ws) {
store.dispatch(actions.setWs(ws));
}
function setGame(game) {
const { game: currentGame, ws, animating } = store.getState();
if (animating) return false;
if (game && currentGame) {
if (game.resolutions.length !== currentGame.resolutions.length) {
// stop fetching the game state til animations are done
store.dispatch(actions.setAnimating(true));
store.dispatch(actions.setGameSkillInfo(null));
const newTurns = game.resolutions.slice(currentGame.resolutions.length);
return eachSeries(newTurns, (turn, turnCb) => {
return eachSeries(turn, (r, cb) => {
if (r.delay === 0) return cb(); // TargetKo etc
setAnimations(r, store);
return setTimeout(cb, r.delay);
}, turnCb);
}, err => {
if (err) return console.error(err);
clearAnimations(store);
// set the game state so resolutions don't fire twice
store.dispatch(actions.setGame(game));
ws.sendGameState(game.id);
return false;
});
}
}
return store.dispatch(actions.setGame(game));
}
function setAccount(account) {
store.dispatch(actions.setAccount(account));
}
function setAuthenticated(account) {
if (account && window.Notification) {
window.Notification.requestPermission();
}
if (process.env.NODE_ENV !== 'development') {
LogRocket.identify(account.id, account);
}
store.dispatch(actions.setAccount(account));
store.dispatch(actions.setAuthenticated(true));
}
function setEmail(email) {
store.dispatch(actions.setEmail(email));
}
function setShop(v) {
store.dispatch(actions.setShop(v));
}
function clearConstructRename() {
store.dispatch(actions.setConstructRename(null));
}
function clearMtxActive() {
store.dispatch(actions.setMtxActive(null));
}
function setActiveSkill(skill) {
store.dispatch(actions.setActiveSkill(skill));
}
function setActiveItem(v) {
store.dispatch(actions.setActiveItem(v));
}
function clearInfo() {
store.dispatch(actions.setInfo(null));
console.log('event clear item');
}
function clearInstance() {
store.dispatch(actions.setActiveSkill(null));
store.dispatch(actions.setInfo(null));
store.dispatch(actions.setItemUnequip([]));
store.dispatch(actions.setVboxCombiner(null));
store.dispatch(actions.setVboxHighlight(null));
store.dispatch(actions.setVboxInfo(null));
store.dispatch(actions.setVboxSelected({ storeSelect: [], stashSelect: [] }));
}
function setAccountInstances(v) {
store.dispatch(actions.setMtxActive(null));
return store.dispatch(actions.setInstances(v));
}
function setInvite(code) {
if (!code) return store.dispatch(actions.setInvite(null));
const link = `${document.location.origin}#join=${code}`;
navigator.clipboard.writeText(link).then(() => {
notify('Invite link copied to clipboard.');
}, () => {});
return store.dispatch(actions.setInvite(code));
}
function setInstance(v) {
const { account, ws, tutorial } = store.getState();
if (v) {
setInvite(null);
setPvp(false);
const player = v.players.find(p => p.id === account.id);
store.dispatch(actions.setPlayer(player));
if (tutorial && v.rounds.length === 1 && v.time_control === 'Practice') tutorialVbox(player, store, tutorial);
if (v.phase === 'Finished') {
ws.sendAccountInstances();
}
}
return store.dispatch(actions.setInstance(v));
}
function setInstanceChat(v) {
return store.dispatch(actions.setInstanceChat(v));
}
function setChatWheel(v) {
return store.dispatch(actions.setChatWheel(v));
}
function setItemInfo(v) {
return store.dispatch(actions.setItemInfo(v));
}
function urlHashChange() {
const { ws } = store.getState();
const cmds = querystring.parse(location.hash);
if (cmds.join) ws.sendInstanceJoin(cmds.join);
return true;
}
function promptRegister() {
store.dispatch(actions.setTutorial(null));
store.dispatch(actions.setInstance(null));
}
window.addEventListener('hashchange', urlHashChange, false);
return {
clearConstructRename,
clearInfo,
clearInstance,
clearMtxActive,
clearTutorial,
setAccount,
setAuthenticated,
setAccountInstances,
setActiveItem,
setActiveSkill,
setChatWheel,
setConstructList,
setNewConstruct,
setGame,
setEmail,
setInstance,
setInstanceChat,
setItemInfo,
setInvite,
setPing,
setPvp,
setShop,
setTeam,
setSubscription,
setWs,
promptRegister,
urlHashChange,
notify,
};
}
module.exports = registerEvents;