From 8d25a5d140958faa790e3c245ebb16bb9616945f Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Mon, 23 Apr 2018 20:30:55 -0500 Subject: [PATCH] Added bulk spell queue (#10241) * Added bulk spell queue * Removed extra comment * Moved queue to store --- website/client/libs/spellQueue.js | 40 ++++++++++++++++++++++++++++ website/client/mixins/spells.js | 13 ++++----- website/client/store/actions/user.js | 13 +++++++-- 3 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 website/client/libs/spellQueue.js diff --git a/website/client/libs/spellQueue.js b/website/client/libs/spellQueue.js new file mode 100644 index 0000000000..3fa82c0f97 --- /dev/null +++ b/website/client/libs/spellQueue.js @@ -0,0 +1,40 @@ +let store = {}; + +let currentCount = 1; +let currentSpell = { + key: '', +}; +let timer = null; + +// @TODO: We are using this lib in actions, so we have to inject store +function setStore (storeInc) { + store = storeInc; +} + +function castSpell () { + clearTimeout(timer); + + currentSpell.quantity = currentCount; + store.dispatch('user:castSpell', currentSpell); + + currentCount = 0; +} + +function queue (spell, storeInc) { + setStore(storeInc); + + currentCount += 1; + + if (currentSpell.key && spell.key !== currentSpell.key) { + castSpell(); + } + + currentSpell = spell; + + clearTimeout(timer); + timer = setTimeout(() => { + castSpell(); + }, 1500); +} + +export default { queue }; diff --git a/website/client/mixins/spells.js b/website/client/mixins/spells.js index 6789486537..4c40934630 100644 --- a/website/client/mixins/spells.js +++ b/website/client/mixins/spells.js @@ -92,10 +92,13 @@ export default { let spellText = typeof spell.text === 'function' ? spell.text() : spell.text; - let apiResult = await this.$store.dispatch('user:castSpell', {key: spell.key, targetId}); + let apiResult = await this.$store.dispatch('user:castSpell', { + key: spell.key, + targetId, + pinType: spell.pinType, + }); let msg = ''; - switch (type) { case 'task': msg = this.$t('youCastTarget', { @@ -147,8 +150,7 @@ export default { } } - // @TODO: - if (!beforeQuestProgress) return apiResult; + if (!beforeQuestProgress) return; let questProgress = this.questProgress() - beforeQuestProgress; if (questProgress > 0) { let userQuest = this.quests[this.user.party.quest.key]; @@ -159,8 +161,7 @@ export default { } } - - return apiResult; + return; // @TOOD: User.sync(); }, castCancel () { diff --git a/website/client/store/actions/user.js b/website/client/store/actions/user.js index 145ff2a216..7c44d90e7e 100644 --- a/website/client/store/actions/user.js +++ b/website/client/store/actions/user.js @@ -1,4 +1,5 @@ import { loadAsyncResource } from 'client/libs/asyncResource'; +import spellQueue from 'client/libs/spellQueue'; import setProps from 'lodash/set'; import axios from 'axios'; @@ -117,11 +118,19 @@ export async function movePinnedItem (store, params) { } export function castSpell (store, params) { + if (params.pinType !== 'card' && !params.quantity) { + spellQueue.queue({key: params.key, targetId: params.targetId}, store); + return; + } + let spellUrl = `/api/v3/user/class/cast/${params.key}`; - if (params.targetId) spellUrl += `?targetId=${params.targetId}`; + const data = {}; - return axios.post(spellUrl); + if (params.targetId) spellUrl += `?targetId=${params.targetId}`; + if (params.quantity) data.quantity = params.quantity; + + return axios.post(spellUrl, data); } export function openMysteryItem () {