I'm trying to get an efficient way of getting an item from a loot table (which can have hundreds of items), because it has to run on my node server and most online users will run it once every minute. So having an inefficient one could mean a lot of server costs. And I'm, of course, trying to avoid wasting resources.
One way of doing it would be by filling an array by, if the chance is 10, then pushing 10 of that item.id into it and just pick one item from array with Math.random().
const lootTable = [{
id: 'stone_sword',
chance: 500 // in 1000, which is then 50%
},
{
id: 'gold_sword',
chance: 100 // in 1000, which is then 10%
},
{
id: 'iron_sword',
chance: 100 // in 1000, which is then 10%
},
{
id: 'diamond_sword',
chance: 10 // in 1000, which is then 1%
}
]
let filledLootBox = []
lootTable.forEach(item => {
// inserts item.chance amount of times item.id into lootItem
const lootItem = new Array(item.chance).fill(item.id)
filledLootBox.push(...lootItem)
})
const pickedItem = filledLootBox[Math.floor(Math.random() * filledLootBox.length)]
console.log('picked item:', pickedItem)
console.log(filledLootBox)
But, I just don't know how efficient this is in a server, where there's thousands of these requests a minute, because .fill seems to be sluggish/slow.