Widget:PriceCalculator: Difference between revisions
Jump to navigation
Jump to search
(Tablify) |
(Add calculation) |
||
Line 1: | Line 1: | ||
<includeonly> | <includeonly> | ||
< | <table id="bg3wiki-price-calculator-form"> | ||
<tr> | <tr> | ||
<th><label for="item-value">Item value</label></th><td><input type="number" id="item-value" size="4"/></td> | <th><label for="item-value">Item value</label></th><td><input type="number" id="item-value" size="4" value="0"/></td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<th><label for="persuasion-mod">Persuasion modifier</label></th><td><input type="number" id="persuasion-mod" size="2"/></td> | <th><label for="persuasion-mod">Persuasion modifier</label></th><td><input type="number" id="persuasion-mod" size="2" value="0"/></td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
Line 19: | Line 18: | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<th></th><td><input type="button" value="Submit" /></td> | <th></th><td><input type="button" value="Submit" id="submit" /></td> | ||
</tr> | </tr> | ||
</table> | </table> | ||
<div id="bg3wiki-price-calculator-result"></div> | |||
<script> | <script> | ||
$("bg3wiki-price-calculator-form #submit").on(click, do_calculation); | |||
function sanitize(input) { | |||
// a null-y input or a blank string should be treated as 0 | |||
if (input == "" || input == null) { | |||
return 0; | |||
} | |||
return +input; | |||
} | |||
function get_difficulty_mod(difficulty) { | |||
switch (difficulty) { | |||
case "explorer": return 0.5; | |||
case "balanced": return 0; | |||
case "tactician": return -0.5; | |||
default: return 0; | |||
} | |||
} | |||
function create_output_row(header, content) { | |||
let row = document.createElement('tr'); | |||
row.appendChild('th').textContent(header); | |||
row.appendChild('td').textContent(textContent); | |||
return row; | |||
} | |||
let output = document.querySelector('#bg3wiki-price-calculator-result'); | |||
function do_calculation() { | |||
let get_value = (selector) => document.querySelector(selector).getAttribute("value"); | |||
let item_value = sanitize(get_value('#item-value')); | |||
let persuasion_mod = sanitize(get_value('#persuasion-mod')) * 0.05; | |||
let attitude = sanitize(get_value('#attitude')) * 0.025; | |||
let difficulty = document.querySelectorAll('input[name="difficulty"]:checked').value; | |||
let price_mod = max(1.0, 2.5 - persuasion_mod - attitude - get_difficulty_mod(difficulty)); | |||
let price_buy = item_value * price_mod; | |||
let price_sell = item_value / price_mod; | |||
output.textContent = ''; | |||
let table = document.createElement('table'); | |||
table.appendChild(create_output_row('Base item price', item_value)); | |||
table.appendChild(create_output_row('Item price when selling to a trader', price_sell)); | |||
table.appendChild(create_output_row('Item price when buying from a trader', price_buy)); | |||
output.appendChild(table); | |||
} | |||
</script> | </script> | ||
</includeonly> | </includeonly> | ||
<noinclude>{{#widget:PriceCalculator}}</noinclude> | <noinclude>{{#widget:PriceCalculator}}</noinclude> |