0

I'm trying to make a small runeword builder for diablo 2.
The user could list the runes he owns and the algorithm should show which runewords are possible to build.

I have built the algorithm and the most important part works. However, it has become very cumbersome and I wanted to ask if someone can find a simpler and clearer solution to this problem.

Here is the source of the most important part:
(full version: https://github.com/borsTiHD/diablo2-runewords-companion/blob/main/pages/runewords.vue )

calculatedRunewordList() {
    // Reducing item stock and calculates how many items the user has
    // Returns array of runeword names, that can be build with the stock
    // https://stackoverflow.com/a/6300596/7625095
    const itemsInStock = this.getInventory.reduce((acc, obj) => { return acc + obj.value }, 0)
    if (itemsInStock <= 0) {
        // Returning complete list with names, if the user dont have any item in stock
        return this.getRunewordList.map((runeword) => runeword.name)
    }

    // List with possible runewords later on
    const possibleRunewords = []
    this.getRunewordList.forEach(({ name, recipe }) => {
        let valid = true // Validation check
        const currentStock = JSON.parse(JSON.stringify(this.getInventory)) // Getting clone of current stock from the inventory - important so the clone could be manipulated without changing the real inventory
        // Looping through every rune the runeword needs
        recipe.forEach((rune) => {
            // Checking if the needed rune is in our inventory and if the value is greater than 1...
            // We will reduce on stock value - important, because some runewords have more than one of the same rune (eg. 'Ko, Ko, Mal' -> 'Sanctuary')
            const stockItem = currentStock.find((runeObj) => runeObj.name === rune)
            if (stockItem && stockItem.value > 0) {
                stockItem.value-- // Reduce stock value by one
            } else {
                // The needed rune is not in the current stock
                // TODO -> Check if the needed rune could be made by an upgrade with other runes (need to check rune recipes)
                valid = false // -> validation failed
            }
        })

        // Pushes valid runeword name
        if (valid) { possibleRunewords.push(name) }
    })

    return possibleRunewords
}

This method returns an array and is compared to display the possible runewords. Thanks :)

//Edit: An example of what the thing is doing.
The user has the following runes:

const store = [
    { name: 'Tir', value: 1 },
    { name: 'Ral', value: 1 },
    { name: 'Tal', value: 1 },
    { name: 'Sol', value: 1 },
    { name: 'Ko', value: 2 },
    { name: 'Mal', value: 1 }
]

The result should display the following runewords:

const possibleRunewords = ['Leaf', 'Prudence', 'Sanctuary', 'Insight']
/*
// Needed recipes
Leaf: Tir, Ral
Prudence: Mal, Tir
Sanctuary: Ko, Ko, Mal ('Ko' is needed twice)
Insight: Ral, Tir, Tal, Sol
*/
borsTiHD
  • 253
  • 3
  • 17
  • sounds easier to have a number of runes needed per runeword, and just compare to stock, rather than cloning and subtracting. – ASDFGerte Nov 04 '21 at 20:08
  • I have that list. I added a small example what I'm trying to achive. – borsTiHD Nov 04 '21 at 20:10
  • What i mean is instead of cloning and subtracting, just test, if `stock.ber >= runeword.ber` (something like that). For that you either need to acculumate the runeword's needed runes on the fly, or create a table for each runeword of runeName-->totalRequired – ASDFGerte Nov 04 '21 at 20:12
  • Ah ok, but I think runewords with more than one rune of the same rune name could be a problem (eg. the runeword `Sanctuary` needs two `Ko` runes -> `Ko, Ko, Mal`). // Edit: Oh... now I get it... yeah that sounds good. :D – borsTiHD Nov 04 '21 at 20:14
  • 2
    Yeah, so it would be a table `{ ko: 2, mal: 1 }`, and you check `Object.entries(table).every(([rune, amount]) => store[rune] >= amount)` – ASDFGerte Nov 04 '21 at 20:15
  • Now I understand. :) I will give this a try, thank you very much. – borsTiHD Nov 04 '21 at 20:17
  • 1
    Possible near-duplicate: https://stackoverflow.com/questions/65319596/creating-an-array-of-unique-combinations – Scott Sauyet Nov 04 '21 at 21:45
  • 1
    This is more of a code review tho. It may be better suited here: https://codereview.stackexchange.com/ – kissu Nov 05 '21 at 10:45
  • Scott Sauyet thank you for the link, I will check that question. :) kissu thanks for pointing out. Didn't know that exists. I will give it a try if no one else is commenting here. – borsTiHD Nov 05 '21 at 15:53

0 Answers0