kfmodrandomloadout/random.js

176 lines
6.8 KiB
JavaScript

var perks = ["medic", "support", "sharpshooter", "commando", "berserker", "firebug"];
var vanillaWeapons = [
{name: "Broken Pipe", weight: 3, price: 133, category: "Melee"},
{name: "Fire Axe", weight: 5, price: 200, category: "Melee"},
{name: "Chainsaw", weight: 8, price: 666, category: "Melee"},
{name: "Drawf Axe", weight: 6, price: 1337, category: "Melee"},
{name: "Handcannon", weight: 5, price: 266, category: "Power"},
{name: "Shotgun", weight: 8, price: 400, category: "Power"},
{name: "A Landmine", weight: 10, price: 560, category: "Power"},
{name: "Hunting Shotgun", weight: 10, price: 600, category: "Power"},
{name: "LAW", weight: 15, price: 800, category: "Power"},
{name: "Dual 9mms", weight: 4, price: 200, category: "Speed"},
{name: "Bullpup", weight: 6, price: 400, category: "Speed"},
{name: "FlameThrower", weight: 10, price: 533, category: "Speed"},
{name: "Machine Pistol", weight: 4, price: 533, category: "Speed"},
{name: "Winchester", weight: 6, price: 400, category: "Range"},
{name: "Crossbow", weight: 10, price: 533, category: "Range"},
]
var KFSWeapons = [
{name: "Hammer", weight: 1, price: 133, category: "Melee"},
{name: "Sword", weight: 5, price: 533, category: "Melee"},
{name: "Taurus Revolver", weight: 5, price: 400, category: "Power"},
{name: "Blunderbuss", weight: 10, price: 560, category: "Power"},
{name: "A Landmine", weight: 10, price: 560, category: "Power"},
{name: "RPG-7", weight: 15, price: 800, category: "Power"},
{name: "M202A1", weight: 16, price: 1200, category: "Power"},
{name: "Ultimax 100 MK2", weight: 8, price: 1733, category: "Power"},
{name: "Dual Glock 17s", weight: 4, price: 200, category: "Speed"},
{name: "BD3008", weight: 6, price: 400, category: "Speed"},
{name: "Molotov", weight: 6, price: 400, category: "Speed"},
{name: "MAC M10", weight: 3, price: 600, category: "Speed"},
{name: "MP5A", weight: 6, price: 653, category: "Speed"},
{name: "Dual MAC M10s", weight: 4, price: 1200, category: "Speed"},
{name: "Winchester 1873", weight: 6, price: 400, category: "Range"},
{name: "Pepper Spray", weight: 6, price: 400, category: "Range"},
{name: "MSG-90", weight: 6, price: 1266, category: "Range"},
]
function areDistinct(arr) {
let n = arr.length;
let s = new Set();
for (let i = 0; i < n; i++) {
s.add(arr[i]);
}
return (s.size == n);
}
function getSupportLvlLimit() {
switch (localStorage.getItem("supportlvl")) {
case '1':
return 21;
break;
case '2':
return 23;
break;
default:
return 25;
}
}
function genFromWpnCategories(set) {
var str = "";
document.querySelectorAll('#categories>[type="checkbox"]:checked').forEach(e=>{str+="|"+e.value})
return set.filter((e)=>e.category.match(str.slice(1)));
}
function getRandomElement(array) {
return array[Math.floor(Math.random() * array.length)];
}
function getRandomWeapons(weaponset, howmany, perk) {
var weightmax = (perk == "support" ? getSupportLvlLimit() : 16);
var isfirebug = (perk == "firebug" ? true : false);
var chosenweapons = [];
var newset = genFromWpnCategories(weaponset);
var totalweight = 0;
var totalprice = 0;
var containsFireAxe = false;
var i = 0;
do {
containsFireAxe = false;
i = i + 1;
chosenweapons = [];
totalweight = 0;
console.log(newset);
for (let i = 0; i < howmany; i++) { chosenweapons.push(getRandomElement(newset)) };
chosenweapons.forEach(e => { totalweight += e.weight });
if (i < 75000) {
if (isfirebug) containsFireAxe = chosenweapons.some(e=>e === vanillaWeapons[1]);
totalling = !(areDistinct(chosenweapons) && totalweight < weightmax && !containsFireAxe);
} else {
totalling = false;
return {};
}
} while (totalling);
chosenweapons.forEach(e => { totalprice += e.price });
return {chosenweapons, totalweight, totalprice};
}
// webui functions
window.onload = function() {///////////////////////////////////////////
var selperk = document.getElementById("perk");
var selset = document.getElementById("set");
var selnum = document.getElementById("numofweapons");
var selsuplvl = document.getElementById("supportlvl");
var supportlvl = localStorage.getItem("supportlvl");
if (supportlvl != null) selsuplvl.value = supportlvl;
document.getElementById("rollperk").addEventListener("click", () => {
document.getElementById("perk").value = getRandomElement(perks);
changePerkAndOrSetFunction();
});
function changePerkAndOrSetFunction() {
if (selperk.value == "support") {
if (selset.value == "kfs" && getSupportLvlLimit() == 25) selnum.max = 7;
else if (selset.value == "kfs" && getSupportLvlLimit() < 25) selnum.max = 6;
else if (getSupportLvlLimit() < 23) selnum.max = 4;
else selnum.max = 5;
selsuplvl.style.display = "";
} else {
if (selset.value == "kfs") selnum.max = 5;
else selnum.max = 3;
selsuplvl.style.display = "none";
}
if (selnum.value > selnum.max) selnum.value = selnum.max;
}
selperk.addEventListener("change", changePerkAndOrSetFunction);
selset.addEventListener("change", changePerkAndOrSetFunction);
selsuplvl.addEventListener("change", () => {
localStorage.setItem("supportlvl", selsuplvl.value);
changePerkAndOrSetFunction();
});
selnum.addEventListener("keyup", () => {
if (selnum.value != "") {
if (parseInt(selnum.value) < parseInt(selnum.min)) {
selnum.value = selnum.min;
}
if (parseInt(selnum.value) > parseInt(selnum.max)) {
selnum.value = selnum.max;
}
}
});
document.getElementById("random").addEventListener("click", () => {
document.querySelectorAll(".row").forEach(e=>{e.remove()});
var set = (selset.value == "kfs" ? KFSWeapons : vanillaWeapons)
if (selnum.value == 0) selnum.value = 2;
var randomWeapon = getRandomWeapons(set, selnum.value, selperk.value)
if (Object.keys(randomWeapon) != 0) {
randomWeapon.chosenweapons.forEach(e=>{
document.getElementById("tbody").insertAdjacentHTML('beforeend', `<tr class="row">
<td>${e.name}</td>
<td>${e.price}</td>
<td>${e.weight}</td>
<td>${e.category}</td>
</tr>`);
})
document.getElementById("tbody").insertAdjacentHTML('beforeend',
`<tr class="row"><th colspan="4">Total price: ${randomWeapon.totalprice} | Total weight: <b>${randomWeapon.totalweight}</th></tr>`);
} else { alert("shit failed, hit the button again or try lowering the number of weapons") }
})
}//////////////////////////////////////////////////////////////////////