From 77e85e8c45034a2b524aaf438fae5dc528a034f8 Mon Sep 17 00:00:00 2001 From: naknode Date: Fri, 29 May 2020 01:20:20 -0500 Subject: [PATCH] Smith ore and turn to bar! --- .eslintrc.js | 1 + server/core/skills/smithing.js | 35 ++++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 75fee302..33e0453c 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -9,6 +9,7 @@ module.exports = { ], rules: { 'import/extensions': 0, + 'no-restricted-syntax': 0, 'import/first': 0, 'import/no-cycle': 'off', 'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off', diff --git a/server/core/skills/smithing.js b/server/core/skills/smithing.js index 57b64eb7..21e6c04b 100644 --- a/server/core/skills/smithing.js +++ b/server/core/skills/smithing.js @@ -2,11 +2,14 @@ import world from '@server/core/world'; import Skill from './index'; export default class Smithing extends Skill { - constructor(playerIndex, ores) { + constructor(playerIndex, resourceId, type) { super(playerIndex); this.player = world.players[playerIndex]; - this.ores = ores; + this.resourceId = resourceId; + this.type = type; // bar | ore this.columnId = 'smithing'; + + this.inventory = this.player.inventory; } static ores() { @@ -27,31 +30,43 @@ export default class Smithing extends Skill { }; } - static smelt(inventory, bar) { - console.log('Smelting', bar); + smelt(inventory) { + console.log('Smelting', this.resourceId); - const barToSmelt = this.ores()[bar]; + const barToSmelt = Smithing.ores()[this.resourceId]; const hasEnoughOre = () => { - // eslint-disable-next-line for (const ore of Object.keys(barToSmelt.requires)) { const oreFound = inventory.filter(inv => inv.id === ore); - if (barToSmelt.requires[ore] > oreFound.length) { - return false; - } + if (barToSmelt.requires[ore] > oreFound.length) { return false; } } return true; }; if (hasEnoughOre) { - console.log('We have enough ore!'); + // Let's take away the needed ores from inventory + for (const ore of Object.keys(barToSmelt.requires)) { + for (let i = 0; i < barToSmelt.requires[ore]; i + 1) { + // Going through every ore requirement, getting the value + // and filtering the ore needed one by one. + // There's probably a better way to do this... + const getIndexOfOre = this.inventory.findIndex(inv => inv.id === ore); + this.inventory.splice(getIndexOfOre, 1); + } + } + + // Add bar to inventory + // WORLD PLAYER ADD BAR TO INVENTORY (inventory.add() from Player) + // AND RETURN THEIR NEW INVENTORY } else { console.log('Not enough ore.'); } } static bars() { + // The bars available to smith and their level needed. + // Is this better suited in a config file? return { 'bronze-bar': 1, 'iron-bar': 19,