219 lines
6.8 KiB
JavaScript
219 lines
6.8 KiB
JavaScript
const toast = require('izitoast');
|
|
const eachSeries = require('async/eachSeries');
|
|
|
|
const actions = require('./actions');
|
|
const { TIMES } = require('./constants');
|
|
const { getCombatSequence } = require('./utils');
|
|
|
|
function registerEvents(store) {
|
|
|
|
// timeout handlers
|
|
store.subscribe(() => {
|
|
const { game, instance, constructs, ws } = store.getState();
|
|
|
|
if (!game) ws.clearGameStateTimeout();
|
|
if (!instance) ws.clearInstanceStateTimeout();
|
|
});
|
|
|
|
function setPing(ping) {
|
|
store.dispatch(actions.setPing(ping));
|
|
}
|
|
|
|
function setConstructs(constructs) {
|
|
console.log('EVENT ->', 'constructs', constructs);
|
|
}
|
|
|
|
function setConstructList(constructs) {
|
|
store.dispatch(actions.setConstructs(constructs));
|
|
}
|
|
|
|
function setInstanceList(list) {
|
|
store.dispatch(actions.setInstanceList(list));
|
|
}
|
|
|
|
function setWs(ws) {
|
|
store.dispatch(actions.setWs(ws));
|
|
}
|
|
|
|
function setGame(game) {
|
|
const { game: currentGame, ws } = store.getState();
|
|
|
|
if (game) ws.startGameStateTimeout(game.id);
|
|
if (game && currentGame) {
|
|
if (game.resolved.length !== currentGame.resolved.length) {
|
|
// stop fetching the game state til animations are done
|
|
ws.clearGameStateTimeout();
|
|
const newRes = game.resolved.slice(currentGame.resolved.length);
|
|
return eachSeries(newRes, (r, cb) => {
|
|
if (['Disable', 'TargetKo'].includes(r.event[0])) return cb();
|
|
// Create sub events for combat animations
|
|
const sequence = getCombatSequence(r);
|
|
return eachSeries(sequence, (stage, sCb) => {
|
|
const { skip } = store.getState();
|
|
if (skip) return sCb();
|
|
const stagedR = Object.create(r);
|
|
stagedR.stage = stage;
|
|
store.dispatch(actions.setResolution(stagedR));
|
|
|
|
return setTimeout(sCb, TIMES[stage]);
|
|
}, err => {
|
|
if (err) return console.error(err);
|
|
// Finished this resolution
|
|
return cb();
|
|
});
|
|
}, err => {
|
|
if (err) return console.error(err);
|
|
store.dispatch(actions.setResolution(null));
|
|
// stop skipping resolutions
|
|
store.dispatch(actions.setSkip(false));
|
|
// update the game
|
|
store.dispatch(actions.setGame(game));
|
|
// get the latest state and restart polling
|
|
ws.sendGameState(currentGame.id);
|
|
return true;
|
|
});
|
|
}
|
|
}
|
|
|
|
return store.dispatch(actions.setGame(game));
|
|
return console.log('EVENT ->', 'game', game);
|
|
}
|
|
|
|
function setAccount(account) {
|
|
store.dispatch(actions.setAccount(account));
|
|
store.dispatch(actions.setNav('team'));
|
|
}
|
|
|
|
function clearCombiner() {
|
|
store.dispatch(actions.setInfo([]));
|
|
store.dispatch(actions.setCombiner([null, null, null]));
|
|
}
|
|
|
|
function setActiveSkill(skill) {
|
|
store.dispatch(actions.setActiveSkill(skill));
|
|
}
|
|
|
|
function setActiveItem(v) {
|
|
store.dispatch(actions.setActiveItem(v));
|
|
}
|
|
|
|
function clearInfo() {
|
|
store.dispatch(actions.setInfo(null));
|
|
store.dispatch(actions.setActiveConstruct(null));
|
|
console.log('event clear item');
|
|
}
|
|
|
|
function setMenu() {
|
|
console.log('EVENT ->', 'menu', true);
|
|
}
|
|
|
|
function setVbox(items) {
|
|
console.log('EVENT ->', 'vbox', items);
|
|
}
|
|
|
|
function setScores(scores) {
|
|
console.log('EVENT ->', 'scores', scores);
|
|
}
|
|
|
|
function setAccountInstances(v) {
|
|
return store.dispatch(actions.setInstances(v));
|
|
}
|
|
|
|
function setInstance(v) {
|
|
const { account, ws, instance } = store.getState();
|
|
if (v) {
|
|
ws.startInstanceStateTimeout(v.id);
|
|
|
|
const player = v.players.find(p => p.id === account.id);
|
|
store.dispatch(actions.setPlayer(player));
|
|
|
|
if (!instance || v.id !== instance.id) {
|
|
store.dispatch(actions.setNav('vbox'));
|
|
const first = player.constructs[0];
|
|
store.dispatch(actions.setActiveConstruct(first));
|
|
}
|
|
}
|
|
return store.dispatch(actions.setInstance(v));
|
|
}
|
|
|
|
function setZone(zone) {
|
|
console.log('EVENT ->', 'zone', zone);
|
|
}
|
|
|
|
function setGameList(gameList) {
|
|
console.log('EVENT ->', 'gameList', gameList);
|
|
}
|
|
|
|
function setConstructStatusUpdate(id, skill, target) {
|
|
console.log('EVENT ->', 'constructStatusUpdate', { id, skill, target });
|
|
}
|
|
|
|
function setItemInfo(v) {
|
|
return store.dispatch(actions.setItemInfo(v));
|
|
}
|
|
|
|
// events.on('SET_PLAYER', setInstance);
|
|
|
|
// events.on('SEND_SKILL', function skillActive(gameId, constructId, targetConstructId, skill) {
|
|
// ws.sendGameSkill(gameId, constructId, targetConstructId, skill);
|
|
// setConstructStatusUpdate(constructId, skill, targetConstructId);
|
|
// });
|
|
|
|
// events.on('CONSTRUCT_ACTIVE', function constructActiveCb(construct) {
|
|
// for (let i = 0; i < constructs.length; i += 1) {
|
|
// if (constructs[i].id === construct.id) constructs[i].active = !constructs[i].active;
|
|
// }
|
|
// return setConstructs(constructs);
|
|
// });
|
|
|
|
const errMessages = {
|
|
select_constructs: 'Select your constructs before battle using the numbered buttons next to the construct avatar',
|
|
complete_nodes: 'You need to complete the previously connected nodes first',
|
|
max_skills: 'Your construct can only learn a maximum of 4 skills',
|
|
|
|
};
|
|
|
|
function errorPrompt(type) {
|
|
const message = errMessages[type];
|
|
const OK_BUTTON = '<button type="submit">OK</button>';
|
|
toast.info({
|
|
theme: 'dark',
|
|
color: 'black',
|
|
timeout: false,
|
|
drag: false,
|
|
position: 'center',
|
|
maxWidth: window.innerWidth / 2,
|
|
close: false,
|
|
buttons: [
|
|
[OK_BUTTON, (instance, thisToast) => instance.hide({ transitionOut: 'fadeOut' }, thisToast)],
|
|
],
|
|
message,
|
|
});
|
|
}
|
|
|
|
return {
|
|
errorPrompt,
|
|
clearCombiner,
|
|
setAccount,
|
|
setActiveSkill,
|
|
setActiveItem,
|
|
setConstructs,
|
|
setConstructList,
|
|
setGame,
|
|
clearInfo,
|
|
setMenu,
|
|
setInstance,
|
|
setInstanceList,
|
|
setAccountInstances,
|
|
setVbox,
|
|
setWs,
|
|
setGameList,
|
|
setZone,
|
|
setPing,
|
|
setScores,
|
|
setItemInfo,
|
|
};
|
|
}
|
|
|
|
module.exports = registerEvents;
|