<commit_before>/**
 * This file is part of HitTracker.
 *
 * HitTracker is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @copyright 2014 <<EMAIL>>
 * @license AGPL-3
 */
$(document).ready(function () {
    $('select[name="hittracker_game[reload_players]"]').change(function () {
        let gameId = $(this).val();
        let request = $.ajax({
            url: `${window.location.origin}/games/${gameId}`,
            headers: {
                Accept: 'application/json'
            }
        });
        request.done(function (game) {
            let teamPlayers = [];
            for (let player of game.players) {
                let pt = player.team;
                if (!teamPlayers[pt]) {
                    teamPlayers[pt] = {};
                }
                teamPlayers[pt][player.vest.id] = player.name;
            }

            let teams = Object.keys(teamPlayers);

            $('.new-game-teams').each(function () {
                let team = teams.shift();
                $(this).find('.team-no input').val(team);
                let players = teamPlayers[team];
                for (let vestId in players) {
                    $(`select[id$='_vest'] option:selected[value=${vestId}]`).
                        parent().parent().parent().parent().find("input[id$='_name']").
                        val(players[vestId]);
                }

            });
        });
    });

    $('form[name="hittracker_game"]').submit(function () {
        $('.new-game-teams').each(function () {
            let team = $(this).find('.team-no input').val().trim();
            $(this).find('input[id$="_team"]').each(function () {
                $(this).val(team);
            });
        });
    });

    if ($('body').hasClass('hittracker-game-active') || $('body').hasClass('hittracker-game-scoreboard')) {
        let source = new EventSource('/events/game');
        $(window).on('unload', (source) => {
            if (source) {
                source.close();
            }
        });

        if ($('body').hasClass('hittracker-game-scoreboard')) {
            source.addEventListener('game.start', (e) => {
                window.location.reload(true);
            });
        }

        source.addEventListener('game.end', (e) => {
            queueActivity('<li>Game Ends</li>');
        });

        source.addEventListener('game.hit', function(e) {
            let eventData = $.parseJSON(e.data);
            let hit = eventData.content;
            let targetPlayer = hit.target_player;

            //queueActivity(`<li>${player.name} hit $[targetPlayer.name} in Zone ${targetPlayer.zone}</li>`);
            pushHit(`#player-${targetPlayer.id} .zone-${targetPlayer.zone}`, targetPlayer.zone_hits);
            $(`#player-${targetPlayer.id} .player-hit-points`).text(targetPlayer.hit_points);

            let team = targetPlayer.team.replace(' ', '-').toLowerCase();
            $(`.${team} .team-total`).text(hit.target_team_hit_points);
        });

        initCountdown($('#game-time-countdown'));

    }

    $('#print-scores').click(function (event) {
        event.preventDefault();
        printScores($(this).attr('href'), $('tr[id^="player-"]').length);
    });

    $('#hit-simulator select[name="radioId"]').change(function () {
        $(this).trigger('focusout');
    });
});

function queueActivity(content) {
    if ($('.game-activity ul li').size() > 10) {
        $('.game-activity ul li:first').remove();
    }
    $('.game-activity ul').append(content);
}

/**
 * Setup the game timer countdown
 *
 * The target selector must have game-end-time, and server-time
 * data attributes to initialize the countdown.
 *
 * @param Object selector a jquery selector for the target element
 */
function initCountdown(selector) {
    let gameEnd = selector.data('game-end-time');
    let clientTime = (new Date()).getTime();
    let serverTime = new Date(parseInt(selector.data('server-time'))).getTime();
    let offset = serverTime - clientTime;
    gameEnd = gameEnd - offset;

    formatDate = function(event) {
        let format = '%M:%S';
        if (event.offset.hours > 0) {
            format = `%-H:${format}`;
        }
        return event.strftime(format);
    };
    selector.countdown(gameEnd)
        .on('update.countdown', function (event) {
            $(this).text(formatDate(event));
        }).on('finish.countdown', function(event) {
            $(this).text(formatDate(event));
        });
}

function pushHit(selector, zoneHits) {
    let value = $(selector).text();
    $(selector).animate({color: '#a50b00'}, 500, function () {
        $(this).text(zoneHits)
    }).animate({color: '#000'}, 500);
};

function printScores(url, copies) {
    let frame = document.createElement('iframe');
    frame.setAttribute('id', 'print-frame');
    frame.setAttribute('name', 'print-frame');
    frame.setAttribute('type', 'content');
    frame.setAttribute('collapsed', 'true');
    document.documentElement.appendChild(frame);

    frame.addEventListener('load', function (event) {
        jsPrintSetup.clearSilentPrint();
        jsPrintSetup.setOption('numCopies', copies);
        jsPrintSetup.setOption('orientation', jsPrintSetup.kLandscapeOrientation);
        jsPrintSetup.setOption('headerStrLeft', '');
        jsPrintSetup.setOption('headerStrCenter', '');
        jsPrintSetup.setOption('headerStrRight', '');
        jsPrintSetup.setOption('footerStrLeft', '');
        jsPrintSetup.setOption('footerStrCenter', '');
        jsPrintSetup.setOption('footerStrRight', '');
        jsPrintSetup.printWindow(frame.contentWindow);

        setTimeout(function () {
            let frame = document.getElementById('print-frame');
            frame.destroy();
        }, 10);
    }, true);

    frame.contentDocument.location.href = url;
};<commit_msg>use .remove() as .destroy() is a jquery method<commit_after>/**
 * This file is part of HitTracker.
 *
 * HitTracker is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @copyright 2014 <<EMAIL>>
 * @license AGPL-3
 */
$(document).ready(function () {
    $('select[name="hittracker_game[reload_players]"]').change(function () {
        let gameId = $(this).val();
        let request = $.ajax({
            url: `${window.location.origin}/games/${gameId}`,
            headers: {
                Accept: 'application/json'
            }
        });
        request.done(function (game) {
            let teamPlayers = [];
            for (let player of game.players) {
                let pt = player.team;
                if (!teamPlayers[pt]) {
                    teamPlayers[pt] = {};
                }
                teamPlayers[pt][player.vest.id] = player.name;
            }

            let teams = Object.keys(teamPlayers);

            $('.new-game-teams').each(function () {
                let team = teams.shift();
                $(this).find('.team-no input').val(team);
                let players = teamPlayers[team];
                for (let vestId in players) {
                    $(`select[id$='_vest'] option:selected[value=${vestId}]`).
                        parent().parent().parent().parent().find("input[id$='_name']").
                        val(players[vestId]);
                }

            });
        });
    });

    $('form[name="hittracker_game"]').submit(function () {
        $('.new-game-teams').each(function () {
            let team = $(this).find('.team-no input').val().trim();
            $(this).find('input[id$="_team"]').each(function () {
                $(this).val(team);
            });
        });
    });

    if ($('body').hasClass('hittracker-game-active') || $('body').hasClass('hittracker-game-scoreboard')) {
        let source = new EventSource('/events/game');
        $(window).on('unload', (source) => {
            if (source) {
                source.close();
            }
        });

        if ($('body').hasClass('hittracker-game-scoreboard')) {
            source.addEventListener('game.start', (e) => {
                window.location.reload(true);
            });
        }

        source.addEventListener('game.end', (e) => {
            queueActivity('<li>Game Ends</li>');
        });

        source.addEventListener('game.hit', function(e) {
            let eventData = $.parseJSON(e.data);
            let hit = eventData.content;
            let targetPlayer = hit.target_player;

            //queueActivity(`<li>${player.name} hit $[targetPlayer.name} in Zone ${targetPlayer.zone}</li>`);
            pushHit(`#player-${targetPlayer.id} .zone-${targetPlayer.zone}`, targetPlayer.zone_hits);
            $(`#player-${targetPlayer.id} .player-hit-points`).text(targetPlayer.hit_points);

            let team = targetPlayer.team.replace(' ', '-').toLowerCase();
            $(`.${team} .team-total`).text(hit.target_team_hit_points);
        });

        initCountdown($('#game-time-countdown'));

    }

    $('#print-scores').click(function (event) {
        event.preventDefault();
        printScores($(this).attr('href'), $('tr[id^="player-"]').length);
    });

    $('#hit-simulator select[name="radioId"]').change(function () {
        $(this).trigger('focusout');
    });
});

function queueActivity(content) {
    if ($('.game-activity ul li').size() > 10) {
        $('.game-activity ul li:first').remove();
    }
    $('.game-activity ul').append(content);
}

/**
 * Setup the game timer countdown
 *
 * The target selector must have game-end-time, and server-time
 * data attributes to initialize the countdown.
 *
 * @param Object selector a jquery selector for the target element
 */
function initCountdown(selector) {
    let gameEnd = selector.data('game-end-time');
    let clientTime = (new Date()).getTime();
    let serverTime = new Date(parseInt(selector.data('server-time'))).getTime();
    let offset = serverTime - clientTime;
    gameEnd = gameEnd - offset;

    formatDate = function(event) {
        let format = '%M:%S';
        if (event.offset.hours > 0) {
            format = `%-H:${format}`;
        }
        return event.strftime(format);
    };
    selector.countdown(gameEnd)
        .on('update.countdown', function (event) {
            $(this).text(formatDate(event));
        }).on('finish.countdown', function(event) {
            $(this).text(formatDate(event));
        });
}

function pushHit(selector, zoneHits) {
    let value = $(selector).text();
    $(selector).animate({color: '#a50b00'}, 500, function () {
        $(this).text(zoneHits)
    }).animate({color: '#000'}, 500);
};

function printScores(url, copies) {
    let frame = document.createElement('iframe');
    frame.setAttribute('id', 'print-frame');
    frame.setAttribute('name', 'print-frame');
    frame.setAttribute('type', 'content');
    frame.setAttribute('collapsed', 'true');
    document.documentElement.appendChild(frame);

    frame.addEventListener('load', function (event) {
        jsPrintSetup.clearSilentPrint();
        jsPrintSetup.setOption('numCopies', copies);
        jsPrintSetup.setOption('orientation', jsPrintSetup.kLandscapeOrientation);
        jsPrintSetup.setOption('headerStrLeft', '');
        jsPrintSetup.setOption('headerStrCenter', '');
        jsPrintSetup.setOption('headerStrRight', '');
        jsPrintSetup.setOption('footerStrLeft', '');
        jsPrintSetup.setOption('footerStrCenter', '');
        jsPrintSetup.setOption('footerStrRight', '');
        jsPrintSetup.printWindow(frame.contentWindow);

        setTimeout(function () {
            let frame = document.getElementById('print-frame');
            frame.remove();
        }, 10);
    }, true);

    frame.contentDocument.location.href = url;
};
{'fraction_non_alphanumeric': 0.16589002795899346, 'fraction_numerical': 0.003727865796831314, 'mean_word_length': 2.45042872454448, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2097811', 'n_tokens_mistral': 3667, 'n_tokens_neox': 3549, 'n_words': 735}