const toast = require('izitoast');
const eachSeries = require('async/eachSeries');
const actions = require('./actions');
const { TIMES } = require('./constants');
const { getCombatSequence } = require('./utils');
function registerEvents(store) {
function setCryps(cryps) {
console.log('EVENT ->', 'cryps', cryps);
}
function setCrypList(cryps) {
store.dispatch(actions.setCryps(cryps));
}
function setWs(ws) {
store.dispatch(actions.setWs(ws));
}
function setGame(game) {
const { game: currentGame, ws } = store.getState();
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.event);
return eachSeries(sequence, (stage, 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));
// 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));
}
function clearCombiner() {
store.dispatch(actions.setCombiner([null, null, null]));
}
function setActiveSkill(skill) {
store.dispatch(actions.setActiveSkill(skill));
}
function setActiveVar(v) {
store.dispatch(actions.setActiveVar(v));
}
function clearInfo(info) {
store.dispatch(actions.setInfo([]));
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 setInstanceList(v) {
return store.dispatch(actions.setInstances(v));
}
function setPlayer(v) {
return store.dispatch(actions.setInstance(v));
}
function setZone(zone) {
console.log('EVENT ->', 'zone', zone);
}
function setGameList(gameList) {
console.log('EVENT ->', 'gameList', gameList);
}
function setCrypStatusUpdate(id, skill, target) {
console.log('EVENT ->', 'crypStatusUpdate', { id, skill, target });
}
// events.on('SET_PLAYER', setPlayer);
// events.on('SEND_SKILL', function skillActive(gameId, crypId, targetCrypId, skill) {
// ws.sendGameSkill(gameId, crypId, targetCrypId, skill);
// setCrypStatusUpdate(crypId, skill, targetCrypId);
// });
// events.on('CRYP_ACTIVE', function crypActiveCb(cryp) {
// for (let i = 0; i < cryps.length; i += 1) {
// if (cryps[i].id === cryp.id) cryps[i].active = !cryps[i].active;
// }
// return setCryps(cryps);
// });
const errMessages = {
select_cryps: 'Select your cryps before battle using the numbered buttons next to the cryp avatar',
complete_nodes: 'You need to complete the previously connected nodes first',
max_skills: 'Your cryp can only learn a maximum of 4 skills',
};
function errorPrompt(type) {
const message = errMessages[type];
const 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,
});
}
// function loginPrompt() {
// const USER_INPUT = '';
// const PASSWORD_INPUT = '';
// const LOGIN_BUTTON = '';
// const REGISTER_BUTTON = '';
// const DEMO_BUTTON = '';
// const ws = registry.get('ws');
// function submitLogin(instance, thisToast, button, e, inputs) {
// const USERNAME = inputs[0].value;
// const PASSWORD = inputs[1].value;
// ws.sendAccountLogin(USERNAME, PASSWORD);
// }
// function submitRegister(instance, thisToast, button, e, inputs) {
// const USERNAME = inputs[0].value;
// const PASSWORD = inputs[1].value;
// ws.sendAccountCreate(USERNAME, PASSWORD);
// }
// function submitDemo() {
// ws.sendAccountDemo();
// }
// const existing = document.querySelector('#login'); // Selector of your toast
// if (existing) toast.hide({}, existing, 'reconnect');
// toast.question({
// id: 'login',
// theme: 'dark',
// color: 'black',
// timeout: false,
// // overlay: true,
// drag: false,
// close: false,
// title: 'LOGIN',
// position: 'center',
// inputs: [
// [USER_INPUT, 'change', () => true, true], // true to focus
// [PASSWORD_INPUT, 'change', () => true],
// ],
// buttons: [
// [LOGIN_BUTTON, submitLogin], // true to focus
// [REGISTER_BUTTON, submitRegister], // true to focus
// [DEMO_BUTTON, submitDemo], // true to focus
// ],
// });
// console.log('ACCOUNT', function closeLoginCb() {
// const prompt = document.querySelector('#login'); // Selector of your toast
// if (prompt) toast.hide({ transitionOut: 'fadeOut' }, prompt, 'EVENT ->');
// });
// }
// events.on('CRYP_SPAWN', function spawnPrompt() {
// const NAME_INPUT = '';
// const SPAWN_BUTTON = '';
// const ws = registry.get('ws');
// function submitSpawn(instance, thisToast, button, e, inputs) {
// const NAME = inputs[0].value;
// ws.sendCrypSpawn(NAME);
// instance.hide({ transitionOut: 'fadeOut' }, thisToast, 'button');
// }
// toast.question({
// theme: 'dark',
// color: 'black',
// timeout: false,
// // overlay: true,
// drag: false,
// close: true,
// title: 'SPAWN CRYP',
// position: 'center',
// inputs: [
// [NAME_INPUT, 'change', null, true], // true to focus
// ],
// buttons: [
// [SPAWN_BUTTON, submitSpawn], // true to focus
// ],
// });
// });
return {
errorPrompt,
// loginPrompt,
clearCombiner,
setAccount,
setActiveSkill,
setActiveVar,
setCryps,
setCrypList,
setGame,
clearInfo,
setMenu,
setPlayer,
setInstanceList,
setVbox,
setWs,
setGameList,
setZone,
setScores,
};
}
module.exports = registerEvents;