WT Calculate Optimal Stone use for 45 Set

by gdarussian

HTML

<p>
    <h1>Calculate Optimal Stone use for 45 Set</h1>
    <hr />
</p>

<p>
    <label>Best stone in shop:</label>
    <select id="bestStone" size="1" onChange="calc()">
        <option value="1">Level 1</option>
        <option value="2">Level 2</option>
        <option value="3">Level 3</option>
        <option value="4">Level 4</option>
        <option value="5" selected="selected">Level 5</option>
    </select>
</p>

<p>
    <hr />
    <br />
    <label>Available Stones</label>
    <table>
        <tr>
            <th class="stoneColor1">Level1</th>
            <th class="stoneColor2">Level2</th>
            <th class="stoneColor3">Level3</th>
            <th class="stoneColor4">Level4</th>
            <th class="stoneColor5">Level5</th>
            <th class="stoneColor6">Level6</th>
            <th class="stoneColor7">Level7</th>
            <th class="stoneColor8">Level8</th>
            <th class="stoneColor9">Level9</th>
        </tr>
        <tr>
            <td><input name="stones1" value="0" /></td>
            <td><input name="stones2" value="0" /></td>
            <td><input name="stones3" value="0" /></td>
            <td><input name="stones4" value="0" /></td>
            <td><input name="stones5" value="0" /></td>
            <td><input name="stones6" value="0" /></td>
            <td><input name="stones7" value="0" /></td>
            <td><input name="stones8" value="0" /></td>
            <td><input name="stones9" value="0" /></td>
        </tr>
    </table>
</p>
        
<p>
    <hr />
    <br />
    <label>Current Set Levels</label>
    <table>
        <tr>
            <th>Weapon</th>
            <th>Armor</th>
            <th>Hat</th>
            <th>Neck</th>
        </tr>
        <tr>
            <td><input name="weaponLevel" value="0" /></td>
            <td><input name="armorLevel" value="0" /></td>
            <td><input name="hatLevel" value="0" /></td>
            <td><input name="necklaceLevel" value="0" /></td>
        </tr>
   ...

CSS

body { 
    margin: 1em; 
    font-size: 85%;
}

* {
    font-family:arial, helvetica, sans-serif;
    margin:0;
    padding:0;
}

p {
    font-size:1em;
    margin: 0 0 .8em 0;
}

.result {
    font-weight: bold;
    font-size: 100%;
}

.stoneColor0 { color: black; }
.stoneColor1 { color: black; }
.stoneColor2 { color: green; }
.stoneColor3 { color: deepskyblue; }
.stoneColor4 { color: lightgreen; }
.stoneColor5 { color: blue; }
.stoneColor6 { color: red; }
.stoneColor7 { color: violet; }
.stoneColor8 { color: orange; }
.stoneColor9 { color: fuchsia; }

table {
    margin: 1em 0 0 0
}

html>body {
    font-size: 12px;
}
h1 {
    font-weight: bold;
    font-size: big;
    margin: 0 1em 1em 0;
}

label {
    display: inline-block; 
    width: 110px; 
}

select {
    width: 105px;
}
input {
    width: 45px;
}
th {
    text-align: center;
    padding: 1px 5px 0 0;
}
td {
    padding: 1px 5px 0 0;
    text-align: center;
}


.button {
    width: 80px;
}

JavaScript

// *** Calculate Optimal Stone for Enchanting *** //
// By Asaka, based on Myrkr's template. Fix this when more costs are known for guild shop.
    
var tableFormat = 'Lvl {0} stone costs {1}, has {2}% success, with avg cost of {3}. {4}<br/>'
var answerFormat = '<span class="result result{0}">{1} stone</span> with an average cost of {2} gold'

function calc()
{
    var bestStoneInShop = parseInt($('bestStone').value)
    var resultsTable = $('resultsTable')
    
    if (isNaN(successRate) || isNaN(cost) || isNaN(bestStoneInShop))
    {
        answer = 'NaN'
    }
    else if (cost < 1)
    {
        answer = 'Invalid cost'
    }
    else
    {
        var bestStone = 0
        var minCost = Number.MAX_VALUE
        var prevCostOfStone = 0
        for (var stone = 0; stone <= 9; stone++)
        {
            var costOfStone = 0
            if (stone <= bestStoneInShop)
                costOfStone = stoneCosts[stone]
            else
            {
                costOfStone = prevCostOfStone * 2 + stoneCosts[stone - 1]
                stoneCosts
            }
            prevCostOfStone = costOfStone
    
            var successRateWithStone = successRate + stone / 10.0
            var costWithStone = cost + costOfStone
            var avgCostWithStone = costWithStone / successRateWithStone
            var isNewBest = false
            if (avgCostWithStone < minCost)
            {
                isNewBest = true
                minCost = avgCostWithStone
                bestStone = stone
            }
    
            var outputLine = tableFormat.format(stone, costOfStone,
                        Math.round(successRateWithStone * 100),
                        Math.round(avgCostWithStone), 
                        isNewBest ? '←' : '')
            var row = outputTable.insertRow(0)
            var cell = row.insertCell(0)
            cell.innerHTML= outputLine
        }
            
        answer = answerFormat.format(bestStone,
                    (bestStone...