Troop Resource Balance Calculator

Mowmaster

New Member
Reaction score
1

APPROVED: proof here


Its a very simple script that takes the players currently stored resources and figures out how much of a specific unit can be created from those resources.
I personally use this info and use the pp market to balance my resources (manually)

The script opens up a new window, the user just need to select a unit, and it will auto update and display the calculation
Unit Count: Number of units that can be made
Wood Difference/Clay Difference/Iron Difference: Positive value means this resource NEEDS more, Negative value means this resource needs to give up resources(to be converted)
Population Needed: Just to show how much pop is needed, not really needed, but maybe someone can find it helpful.

Script:
javascript: const content = ` <div class="getRes" resWood=${jQuery('#wood').text()} resStone=${jQuery('#stone').text()} resIron=${jQuery('#iron').text()}></div> <div class="ra-flex"> <div class="ra-flex-6"> <div class="ra-mb15"> <select id="unit_type"> <option value="-1" selected="selected">Select Unit Type</option> <option value="0">Spear</option> <option value="1">Sword</option> <option value="2">Axe</option> <option value="3">Scout</option> <option value="4">LC</option> <option value="5">HC</option> <option value="6">Ram</option> <option value="7">Cat</option> <option value="8">Noble</option> </select> </div> </div> <div class="ra-flex-3"> <div class="ra-mb15"> <label for="unit_count">Unit Count</label> <input id="unit_count" type="text" readonly="readonly" value=""> </div> </div> </div> <div class="ra-flex"> <div class="ra-flex-3"> <div class="ra-mb15"> <label for="wood_count">Wood Difference</label> <input id="wood_count" type="text" readonly="readonly" value=""> </div> </div> <div class="ra-flex-3"> <div class="ra-mb15"> <label for="clay_count">Clay Difference</label> <input id="clay_count" type="text" readonly="readonly" value=""> </div> </div> <div class="ra-flex-3"> <div class="ra-mb15"> <label for="iron_count">Iron Difference</label> <input id="iron_count" type="text" readonly="readonly" value=""> </div> </div> <div class="ra-flex-3"> <div class="ra-mb15"> <label for="population_count">Population Needed</label> <input id="population_count" type="text" readonly="readonly" value=""> </div> </div> </div> `; const windowContent = prepareWindowContent(content); attackPlannerWindow = window.open( '', '', 'left=10px,top=10px,width=480,height=280,toolbar=0,resizable=0,location=0,menubar=0,scrollbars=0,status=0' ); attackPlannerWindow.document.write(windowContent); function prepareWindowContent(windowBody) { const windowHeader = `<h1 class="ra-fs18 ra-fw600">Troop Resourse Balance Calculator</h1>`; const windowFooter = ``; const windowStyle = ` <style> body { background-color: #f4e4bc; font-family: Verdana, Arial, sans-serif; font-size: 14px; line-height: 1; } main { max-width: 768px; margin: 0 auto; } h1 { font-size: 27px; } a { font-weight: 700; text-decoration: none; color: #603000; } small { font-size: 10px; } input[type="text"], select { display: block; width: 100%; height: auto; line-height: 1; box-sizing: border-box; padding: 5px; outline: none; border: 1px solid #999; } input[type="text"]:focus { outline: none; box-shadow: none; border: 1px solid #603000; background-color: #eee; } label { font-weight: 600; display: block; margin-bottom: 5px; font-size: 12px; } textarea { width: 100%; height: 80px; box-sizing: border-box; padding: 5px; resize: none; } textarea:focus { box-shadow: none; outline: none; border: 1px solid #603000; background-color: #eee; } .ra-mb15 { margin-bottom: 15px; } .ra-flex { display: flex; flex-flow: row wrap; justify-content: space-between; } .ra-flex-6 { flex: 0 0 48%; } .ra-flex-4 { flex: 0 0 30%; } .button { padding: 10px 20px; background-color: #603000; font-weight: 500; color: #fff; text-align: center; display: inline-block; cursor: pointer; text-transform: uppercase; } </style> `; const html = ` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Troop Resourse Balance Calculator</title> ${windowStyle} </head> <body> <main> ${windowHeader} ${windowBody} ${windowFooter} </main> <script> function loadJS(url, callback) { var scriptTag = document.createElement('script'); scriptTag.src = url; scriptTag.onload = callback; scriptTag.onreadystatechange = callback; document.body.appendChild(scriptTag); } let units = [ ["spear", 50, 30, 10, 1], ["sword", 30, 30, 70, 1], ["axe", 60, 30, 40, 1], ["scout", 50, 50, 20, 2], ["lightcalv", 125, 100, 250, 4], ["heavycalv", 200, 150, 600, 6], ["ram", 300, 200, 200, 5], ["cat", 320, 400, 100, 8], ["noble", 40000, 50000, 50000, 100], ]; function calculateBalance(currentResArray, unitArrayCost) { let currentWood = currentResArray[0]; let currentClay = currentResArray[1]; let currentIron = currentResArray[2]; let currentResSum = parseInt(currentWood, 10) + parseInt(currentClay, 10) + parseInt(currentIron, 10); let unitWood = unitArrayCost[1]; let unitClay = unitArrayCost[2]; let unitIron = unitArrayCost[3]; let unitPopulation = unitArrayCost[4]; let unitResSum = parseInt(unitWood, 10) + parseInt(unitClay, 10) + parseInt(unitIron, 10); let unitCount = parseInt(currentResSum, 10) / parseInt(unitResSum, 10); let needWood = parseInt(unitCount, 10) * parseInt(unitWood, 10); let needClay = parseInt(unitCount, 10) * parseInt(unitClay, 10); let needIron = parseInt(unitCount, 10) * parseInt(unitIron, 10); let difResWood = parseInt(needWood, 10) - parseInt(currentWood, 10); let difResClay = parseInt(needClay, 10) - parseInt(currentClay, 10); let difResIron = parseInt(needIron, 10) - parseInt(currentIron, 10); let needPopulation = parseInt(unitPopulation, 10) * parseInt(unitCount, 10); let returnArray = [parseInt(difResWood, 10),parseInt(difResClay, 10),parseInt(difResIron, 10),parseInt(needPopulation, 10),parseInt(unitCount, 10)]; return returnArray; }; loadJS('https://code.jquery.com/jquery-3.6.0.min.js', function() { $("#unit_type").change(function(){ let selectedValue = this.value; console.log("selectedValue: " + selectedValue); let currentRes = [jQuery('.getRes').attr('resWood'), jQuery('.getRes').attr('resStone'), jQuery('.getRes').attr('resIron')]; let calcArray = calculateBalance(currentRes,units[selectedValue]); $("#wood_count").val(parseInt(calcArray[0], 10)); $("#clay_count").val(parseInt(calcArray[1], 10)); $("#iron_count").val(parseInt(calcArray[2], 10)); $("#population_count").val(parseInt(calcArray[3], 10)); $("#unit_count").val(parseInt(calcArray[4], 10)); }); }); </script> </body> </html> `; return html; } ;

Possible Improvements, (that i have no idea how to do as ive very new to TW's code.)
- Soft-coding Unit costs, so i dont have to update this if things change
- Language support
- Adding more unit types (i faintly remember archer calv, but im not sure what else there is/was)
 
Last edited:

Kawoni

Community Manager TW US
Staff member
Community Manager
Reaction score
9
rumor has it, these are approved on .net forums, so i posted it there too .net Forums
That is correct they are only approved on .net. if you get it approved there then you can bring it back here and let us know that is was approved with the screenshot saying it was approved.
 

Mowmaster

New Member
Reaction score
1
That is correct they are only approved on .net. if you get it approved there then you can bring it back here and let us know that is was approved with the screenshot saying it was approved.
I read over the rules again this morning, could you confirm if the script falls under the rule "Scripts that only fill in a form (without submitting the form/data) will be approved by default and do not require the script to be submitted for approval." ? This just grabs the stored resource info from the page with jquery and then uses a new window to run some client side calculations which fill out custom text fields. this basically doesnt effect the game in any way as its all handled client side.
 

RedAlert

Member
Reaction score
38
@Mowmaster ,

Script approvals are handled on the .net forum and if approved, you can use the script here on the US server too.

I would recommend you to not share un-approved scripts like this here on the forum, someone might use the script without knowledge if it's approved or not.

All other questions will be answered on the .net forum.
 

Mowmaster

New Member
Reaction score
1
Looks like it got approved, and ill keep this in mind in the future, thankyou.
 

Kawoni

Community Manager TW US
Staff member
Community Manager
Reaction score
9
Looks like it got approved, and ill keep this in mind in the future, thankyou.
Perfect, Thank you.
 
Top