Skip to content
Snippets Groups Projects
spniSelect.js 41.9 KiB
Newer Older
  • Learn to ignore specific revisions
  •     /* sort opponents */
        // Since selectableOpponents is always reloaded here with featured order,  
        // check if a different sorting mode is selected, and if yes, sort it.
        if (sortingOptionsMap.hasOwnProperty(sortingMode)) {
            selectableOpponents.sort(sortingOptionsMap[sortingMode]);
        }
        
    
        /* update max page indicator */
        $individualMaxPageIndicator.html("of "+Math.ceil(selectableOpponents.length/4));
        
        // update
        updateIndividualSelectScreen();
    }
    
    function changeSearchGender(gender) {
        chosenGender = gender;
        setActiveOption($searchGenderOptions, gender);
    
    }
    
    /************************************************************
     * Sorting Functions
     ************************************************************/
    
    /** 
     * Callback for Arrays.sort to sort an array of objects by the given field.
     * Prefixing "-" to a field will cause the sort to be done in reverse.
     * Examples:
     *   // sorts myArr by each element's first name (A-Z)
     *   myArr.sort(sortOpponentsByField("first")); 
     *   // sorts myArr by each element's last name (Z-A)
     *   myArr.sort(sortOpponentsByField("-last")); 
     */
    function sortOpponentsByField(field) {
        // check for prefix
        var order = 1; // 1 = forward, -1 = reversed
        if (field[0] === "-") { 
            order = -1;
            field = field.substr(1);
        }
        
        return function(opp1, opp2) {
            var compare = 0;
            if (opp1[field] < opp2[field]) {
                compare = -1;
            }
            else if (opp1[field] > opp2[field]) {
                compare = 1;
            }
            return order * compare;
        }
    }
    
    /**
     * Callback for Arrays.sort to sort an array of objects over multiple given fields.
     * Prefixing "-" to a field will cause the sort to be done in reverse.
     * This should allow more flexibility in the sorting order.
     * Example:
     *   // sorts myArr by each element's number of layers (low to high), 
     *   // and for elements whose layers are equivalent, sort them by first name (Z-A)
     *   myArr.sort(sortOpponentsByMultipleFields("layers", "-first")); 
     */
    function sortOpponentsByMultipleFields() {
        var fields = arguments; // retrieve the args passed in
        return function(opp1, opp2) {
            var i = 0;
            var compare = 0;
            // if both elements have the same field, check the next ones
            while (compare === 0 && i < fields.length) {
                compare = sortOpponentsByField(fields[i])(opp1, opp2);
                i++;
            }
            return compare;
        }
    }
    
    
    /** Event handler for the sort dropdown options. Fires when user clicks on a dropdown item. */
    $sortingOptionsItems.on("click", function(e) {
        sortingMode = $(this).find('a').html();
        $("#sort-dropdown-selection").html(sortingMode); // change the dropdown text to the selected option
    });
    
    
    
    /************************************************************
     * Dynamic dialogue and image counting functions
     ************************************************************/
    
    
    /** Event handler for the individual selection screen credits button. */
    $('.individual-credits-btn').on('click', function(e) {
    
        updateOpponentCountStats(shownIndividuals);
    
    /** Event handler for the group selection screen credits button. */
    $('.group-credits-btn').on('click', function(e) {
        updateOpponentCountStats(shownGroup);
    });
    
    
    /**
     * Loads and displays the number of unique dialogue lines and the number of pose images 
     * into the character's player object for those currently on the selection screen.
     * Only loads if the unique line count or image count is not known.
     */
    
    function updateOpponentCountStats(opponentArr) {
        opponentArr.forEach(function(opp) {
            // load behaviour file if line/image count is not known
    
            if (opp && (opp.uniqueLineCount === undefined || opp.posesImageCount === undefined)) {
    
                console.log("Fetching counts for " + opp.label);
                // retrieve line and image counts
    
                var countsPromise = Promise.resolve(fetchBehaviour(opp.folder));
    
                countsPromise.then(countLinesImages).then(function(response) {
                    console.log(response);
                    opp.uniqueLineCount = response.numUniqueLines;
                    opp.posesImageCount = response.numPoses;
                    
                    // show line and image counts
                    console.log(opp.label + ": " + opp.uniqueLineCount);
                });
            }
    
                if (opp)
                    console.log("Counts for " + opp.label + " already fetched: " + opp.uniqueLineCount);
                else
                    console.log("opp is null");
    
    /**
     * Fetches the behaviour.xml file of the specified opponent directory.
     */
    function fetchBehaviour(path) {
    
        return $.ajax({
            type: "GET",
            url: path + "behaviour.xml",
            dataType: "text"
        });
    }
    
    
    /**
     * Callback to parse the number of lines of dialogue and number of images
     * given a character's behaviour XML. Returns the counts as an object with 
     * properties numTotalLines, numUniqueLines, and numPoses.
     */
    
    function countLinesImages(xml) {
    
        // parse all lines of dialogue
    
        var lines = [];
        $(xml).find('state').each(function(idx, data) {
            lines.push(data.textContent);
        });
    
        
        // count only unique lines of dialogue
    
        var numUniqueDialogueLines = lines.filter(function(data, idx) {
            return idx == lines.lastIndexOf(data);
        }).length;
        
        return {
            numTotalLines : lines.length,
            numUniqueLines : numUniqueDialogueLines,
            numPoses : 0
        };
    }