<commit_before>﻿declare var swal;
declare var startTest;

/**
 * Set some form items to what were last chosen
 */
function setPreviousChoices() {
    // player 1 fams
    if (localStorage.getItem("f0") && localStorage.getItem("f0") !== "null") {
        for (var i = 0; i < 10; i++) {
            (<HTMLInputElement>document.getElementById(`f${i}`)).value = localStorage.getItem(`f${i}`);
        }
    }

    // player 2 fams
    if (localStorage.getItem("f10") && localStorage.getItem("f10") !== "null") {
        for (i = 0; i < 10; i++) {
            (<HTMLInputElement>document.getElementById(`f${i + 10}`)).value = localStorage.getItem(`f${i + 10}`);
        }
    }

    // player 1 skills
    if (localStorage.getItem("s10") && localStorage.getItem("s10") !== "null") {
        for (i = 0; i < 3; i++) {
            (<HTMLInputElement>document.getElementById(`s1${i}`)).value = localStorage.getItem(`s1${i}`);
        }
    }

    // player 2 skills
    if (localStorage.getItem("s20") && localStorage.getItem("s20") !== "null") {
        for (i = 0; i < 3; i++) {
            (<HTMLInputElement>document.getElementById(`s2${i}`)).value = localStorage.getItem(`s2${i}`);
        }
    }

    // player 1 formation
    if (localStorage.getItem("1f") && localStorage.getItem("1f") !== "null") {
        (<HTMLInputElement>document.getElementById("1f")).value = localStorage.getItem("1f");
    }

    // player 2 formation
    if (localStorage.getItem("2f") && localStorage.getItem("2f") !== "null") {
        (<HTMLInputElement>document.getElementById("2f")).value = localStorage.getItem("2f");
    }

    // debug mode
    if (localStorage.getItem("debug") === "true") {
        (<HTMLInputElement>document.getElementById("debug")).checked = true;
    }

    // battle type
    var bt = localStorage.getItem("bt");
    if (bt === "1" || bt === "2") {
        (<HTMLInputElement>document.getElementById("bt")).value = bt;
    }
}

/**
 * Disable/enable form items belonging to a player based on whether random is chosen or not
 */
function toogleDisable() {
    for (var player = 1; player <= 2; player++) {
        // is the random checkbox checked?
        var isSelected = (<HTMLInputElement>document.getElementById(`r${player}`)).checked;

        // fams, skills, formation
        var elems = document.getElementsByClassName(`p${player}`);
        for (var i = 0; i < elems.length; i++) {
            if (isSelected) {
                (<HTMLInputElement>elems[i]).disabled = true;
            }
            else {
                (<HTMLInputElement>elems[i]).disabled = false;
            }
        }

        // random modes
        var randomSelect = <HTMLInputElement>document.getElementById(player + "r");
        if (isSelected) {
            randomSelect.disabled = false;
        }
        else {
            randomSelect.disabled = true;
        }
    }
}

/**
 * Toogle on/off the reserve depending on whether the mode is Bloodclash or not
 */
function toogleReserve() {
    for (var player = 1; player <= 2; player++) {
        var isBloodclash = (<HTMLInputElement>document.getElementById("bt")).value === "1";

        var elems = document.getElementsByClassName("reserve");
        for (var i = 0; i < elems.length; i++) {
            var elem = (<HTMLInputElement>elems[i]);
            if (!isBloodclash) {
                elem.disabled = true;
                elem.style.display = 'none';
            }
            else {
                elem.disabled = false;
                elem.style.display = 'inline';
            }
        }
    }

    // need to toogle disable at the end
    toogleDisable();
}

/**
 * Prepare the form when it is loaded
 */
function onFormLoad() {
    toogleReserve();
    toogleDisable();

    // display the last tier update time
    if (localStorage.getItem("lastTierUpdateTime")) {
        var a = new Date(+localStorage.getItem("lastTierUpdateTime"));
        var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
        var year = a.getFullYear();
        var month = months[a.getMonth()];
        var date = a.getDate();
        var hour = a.getHours();
        var min = a.getMinutes();
        var sec = a.getSeconds();
        var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec;
        $("#lastTierUpdate").text(`Last tier list update: ${time}`);
    }
}

/**
 * Put any validation for the main setting form here
 */
function validateForm() {
    return true;
}

/**
 * Submits the form. Goes into debug mode if necesary.
 */
function submitForm() {
    var form = document.forms["mainForm"];
    if (form["debug"].checked === true) {
        form.action = "debug.html";
    }
    form.submit();
}

/**
 * Populates the familiar selects options
 */
function setFamOptions() {
    var famSelects = document.getElementsByClassName("famSelect");

    // create an array of fam id and sort it based on the fam's full name
    var famIdArray = [];
    for (var key in famDatabase) {
        if (famDatabase.hasOwnProperty(key)) {
            famIdArray.push(key);
        }
    }
    famIdArray.sort((a, b) => famDatabase[a].fullName.localeCompare(famDatabase[b].fullName));

    for (var i = 0; i < famSelects.length; i++) {
        for (var index = 0; index < famIdArray.length; index++) {
            key = famIdArray[index];
            var option = document.createElement("option");
            option.value = key;
            option.text = famDatabase[key].fullName;
            (<HTMLSelectElement>famSelects[i]).add(option);
        }
    };
}

/**
 * Populates the skill selects options
 */
function setSkillOptions() {
    var skillSelects = document.getElementsByClassName("skillSelect");

    // create an array of skill id and sort it based on the skill's name
    var skillIdArray = SkillProvider.getAvailableSkillsForSelect();
    skillIdArray.sort((a, b) => SkillDatabase[a].name.localeCompare(SkillDatabase[b].name));

    for (var i = 0; i < skillSelects.length; i++) {
        for (var index = 0; index < skillIdArray.length; index++) {
            var key = skillIdArray[index];
            var option = document.createElement("option");
            option.value = key + "";
            option.text = SkillDatabase[key].name;
            (<HTMLSelectElement>skillSelects[i]).add(option);
        }
    };
}

/**
 * Get the battle data and option from the URL. Also saves the settings to localStorage for later retrieval
 */
function getBattleDataOption() {
    // fam: player 1: f0 -> f4, f5 -> f9
    //      player 2: f10 -> f14, f15 -> f19
    // skills: player 1: s10 -> s12<commit_msg>Only populate one select for familiar list and one for skill list, then clone those 2 to others. Massive speed up in load time.<commit_after>﻿declare var swal;
declare var startTest;

/**
 * Set some form items to what were last chosen
 */
function setPreviousChoices() {
    // player 1 fams
    if (localStorage.getItem("f0") && localStorage.getItem("f0") !== "null") {
        for (var i = 0; i < 10; i++) {
            (<HTMLInputElement>document.getElementById(`f${i}`)).value = localStorage.getItem(`f${i}`);
        }
    }

    // player 2 fams
    if (localStorage.getItem("f10") && localStorage.getItem("f10") !== "null") {
        for (i = 0; i < 10; i++) {
            (<HTMLInputElement>document.getElementById(`f${i + 10}`)).value = localStorage.getItem(`f${i + 10}`);
        }
    }

    // player 1 skills
    if (localStorage.getItem("s10") && localStorage.getItem("s10") !== "null") {
        for (i = 0; i < 3; i++) {
            (<HTMLInputElement>document.getElementById(`s1${i}`)).value = localStorage.getItem(`s1${i}`);
        }
    }

    // player 2 skills
    if (localStorage.getItem("s20") && localStorage.getItem("s20") !== "null") {
        for (i = 0; i < 3; i++) {
            (<HTMLInputElement>document.getElementById(`s2${i}`)).value = localStorage.getItem(`s2${i}`);
        }
    }

    // player 1 formation
    if (localStorage.getItem("1f") && localStorage.getItem("1f") !== "null") {
        (<HTMLInputElement>document.getElementById("1f")).value = localStorage.getItem("1f");
    }

    // player 2 formation
    if (localStorage.getItem("2f") && localStorage.getItem("2f") !== "null") {
        (<HTMLInputElement>document.getElementById("2f")).value = localStorage.getItem("2f");
    }

    // debug mode
    if (localStorage.getItem("debug") === "true") {
        (<HTMLInputElement>document.getElementById("debug")).checked = true;
    }

    // battle type
    var bt = localStorage.getItem("bt");
    if (bt === "1" || bt === "2") {
        (<HTMLInputElement>document.getElementById("bt")).value = bt;
    }
}

/**
 * Disable/enable form items belonging to a player based on whether random is chosen or not
 */
function toogleDisable() {
    for (var player = 1; player <= 2; player++) {
        // is the random checkbox checked?
        var isSelected = (<HTMLInputElement>document.getElementById(`r${player}`)).checked;

        // fams, skills, formation
        var elems = document.getElementsByClassName(`p${player}`);
        for (var i = 0; i < elems.length; i++) {
            if (isSelected) {
                (<HTMLInputElement>elems[i]).disabled = true;
            }
            else {
                (<HTMLInputElement>elems[i]).disabled = false;
            }
        }

        // random modes
        var randomSelect = <HTMLInputElement>document.getElementById(player + "r");
        if (isSelected) {
            randomSelect.disabled = false;
        }
        else {
            randomSelect.disabled = true;
        }
    }
}

/**
 * Toogle on/off the reserve depending on whether the mode is Bloodclash or not
 */
function toogleReserve() {
    for (var player = 1; player <= 2; player++) {
        var isBloodclash = (<HTMLInputElement>document.getElementById("bt")).value === "1";

        var elems = document.getElementsByClassName("reserve");
        for (var i = 0; i < elems.length; i++) {
            var elem = (<HTMLInputElement>elems[i]);
            if (!isBloodclash) {
                elem.disabled = true;
                elem.style.display = 'none';
            }
            else {
                elem.disabled = false;
                elem.style.display = 'inline';
            }
        }
    }

    // need to toogle disable at the end
    toogleDisable();
}

/**
 * Prepare the form when it is loaded
 */
function onFormLoad() {
    toogleReserve();
    toogleDisable();

    // display the last tier update time
    if (localStorage.getItem("lastTierUpdateTime")) {
        var a = new Date(+localStorage.getItem("lastTierUpdateTime"));
        var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
        var year = a.getFullYear();
        var month = months[a.getMonth()];
        var date = a.getDate();
        var hour = a.getHours();
        var min = a.getMinutes();
        var sec = a.getSeconds();
        var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec;
        $("#lastTierUpdate").text(`Last tier list update: ${time}`);
    }
}

/**
 * Put any validation for the main setting form here
 */
function validateForm() {
    return true;
}

/**
 * Submits the form. Goes into debug mode if necesary.
 */
function submitForm() {
    var form = document.forms["mainForm"];
    if (form["debug"].checked === true) {
        form.action = "debug.html";
    }
    form.submit();
}

/**
 * Populates the familiar selects options
 */
function setFamOptions() {
    var famSelects = document.getElementsByClassName("famSelect");

    // create an array of fam id and sort it based on the fam's full name
    var famIdArray = [];
    for (var key in famDatabase) {
        if (famDatabase.hasOwnProperty(key)) {
            famIdArray.push(key);
        }
    }
    famIdArray.sort((a, b) => famDatabase[a].fullName.localeCompare(famDatabase[b].fullName));

    // populate one select
    for (var index = 0; index < famIdArray.length; index++) {
        key = famIdArray[index];
        var option = document.createElement("option");
        option.value = key;
        option.text = famDatabase[key].fullName;
        (<HTMLSelectElement>famSelects[0]).add(option);
    }

    // and clone it
    for (var i = 1; i < famSelects.length; i++) {
        (<HTMLSelectElement>famSelects[i]).innerHTML = (<HTMLSelectElement>famSelects[0]).innerHTML;
    };
}

/**
 * Populates the skill selects options
 */
function setSkillOptions() {
    var skillSelects = document.getElementsByClassName("skillSelect");

    // create an array of skill id and sort it based on the skill's name
    var skillIdArray = SkillProvider.getAvailableSkillsForSelect();
    skillIdArray.sort((a, b) => SkillDatabase[a].name.localeCompare(SkillDatabase[b].name));

    // populate one select
    for (var index = 0; index < skillIdArray.length; index++) {
        var key = skillIdArray[index];
        var option = document.createElement("option");
        option.value = key + "";
        option.text = SkillDatabase[key].name;
        (<HTMLSelectElement>skillSelects[0]).add(option);
    }

    // and clone it
    for (var i = 1; i < skillSelects.length; i++) {
        (<HTMLSelectElement>skillSelects[i]).innerHTML = (<HTMLSelectElement>skillSelects[0]).innerHTML;
    };
}

/**
 * Get the battle data and option from the URL. Also saves the settings to localStorage for later retrieval
 */
function getBattleDataOption() {
    // fam: player 1: f0 -> f4, f5 -> f9
    //      player 2: f10 -> f14, f15 -> f19
    // skills: player 1: s10 -> s12
{'fraction_non_alphanumeric': 0.1550915750915751, 'fraction_numerical': 0.011794871794871795, 'mean_word_length': 2.477075904228222, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7168908', 'n_tokens_mistral': 4203, 'n_tokens_neox': 4078, 'n_words': 1030}