From c76212b285c1e10a2ef824f594f05b4847fd81f2 Mon Sep 17 00:00:00 2001 From: ntr Date: Sun, 24 Nov 2019 21:47:29 +1100 Subject: [PATCH] combiner --- client/src/components/vbox.combiner.jsx | 104 ++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 client/src/components/vbox.combiner.jsx diff --git a/client/src/components/vbox.combiner.jsx b/client/src/components/vbox.combiner.jsx new file mode 100644 index 00000000..93051a33 --- /dev/null +++ b/client/src/components/vbox.combiner.jsx @@ -0,0 +1,104 @@ +const preact = require('preact'); +const { connect } = require('preact-redux'); +const countBy = require('lodash/countBy'); + +const addState = connect( + function receiveState(state) { + const { + ws, + itemInfo, + instance, + player, + vboxSelected, + } = state; + + function sendVboxAccept(group, index) { + document.activeElement.blur(); + return ws.sendVboxAccept(instance.id, group, index); + } + + function sendVboxCombine() { + return ws.sendVboxCombine(instance.id, vboxSelected.stashSelect, vboxSelected.storeSelect); + } + + return { + itemInfo, + player, + vboxSelected, + sendVboxAccept, + sendVboxCombine, + }; + } +); + +function Combiner(args) { + const { + // Variables that will change + vboxHighlight, + vboxSelected, + // Static + player, + itemInfo, + // functions + sendVboxAccept, + sendVboxCombine, + } = args; + + const { vbox } = player; + const { stashSelect, storeSelect } = vboxSelected; + + const vboxCombo = () => { + if (!(vboxHighlight && vboxHighlight.length === 0)) return false; + // The selected items can't be combined with additional items therefore valid combo + const stashItems = stashSelect.map(j => vbox.bound[j]); + const shopItems = storeSelect.map(j => vbox.free[j[0]][j[1]]); + const selectedItems = stashItems.concat(shopItems); + const combinerCount = countBy(selectedItems, co => co); + + const comboItemObj = itemInfo.combos.find(combo => selectedItems.every(c => { + if (!combo.components.includes(c)) return false; + const comboCount = countBy(combo.components, co => co); + if (combinerCount[c] > comboCount[c]) return false; + return true; + })); + return comboItemObj.item; + }; + + const combinerCombo = vboxCombo(); + + function vboxBuySelected() { + if (!(storeSelect.length === 1 && stashSelect.length === 0)) return false; + document.activeElement.blur(); + sendVboxAccept(storeSelect[0][0], storeSelect[0][1]); + return true; + } + let text = ''; + let mouseEvent = false; + if (combinerCombo) { + const combinerComboText = combinerCombo.replace('Plus', '+'); + let bits = 0; + storeSelect.forEach(item => bits += item[0] + 1); + text = bits + ? `Buy ${combinerComboText} ${bits}b` + : `Combine ${combinerComboText}`; + if (vbox.bits >= bits) mouseEvent = sendVboxCombine; + } else if (stashSelect.length === 0 && storeSelect.length === 1) { + const item = storeSelect[0]; + text = `Buy ${vbox.free[item[0]][item[1]]} ${item[0] + 1}b`; + mouseEvent = vboxBuySelected; + } else { + return false; + } + + return ( + + ); +} + +module.exports = addState(Combiner);