<commit_before>/*global $, _, document, window */
$(function () {
    'use strict';

    // Die verfügbaren Bilder.
    var imageData = [];

    var $gameBoard = $('#gameBoard');

    // CSS-Klassen
    var foundClass = 'found';
    var peekClass = 'peek';
    var timeoutClass = 'timeout';
    var lastSelectionClass = 'lastSelection';
    var selectionInfoClass = 'selectionInfo';

    // Namen für Datenfelder and DOM-Elementen
    var identifierDataName = 'identifier';
    var descriptionDataName = 'description';
    var moveCountDataName = 'moveCount';
    var startTimeDataName = 'startTime';
    var timerIdDataName = 'timerId';

    /**
     * Die Anzahl der Memory-Karten auf dem Spielfeld.
     */
    var getBoardDimensions = function () {
        return JSON.parse($('.sizeSelection').val());
    };

    /**
     * Brettgröße aus Auswahl im Menü ermittlen.
     */
    var getNumberOfPairs = function (boardDimensions) {
        return boardDimensions[0] * boardDimensions[1] / 2;
    };

    /**
     * Filterfunktion für Münzdaten aus Auswahl im Menü ermitteln.
     */
    var getCenturyFilter = function () {
        var range = JSON.parse($('.centurySelection').val());
        return function (imageRecord) {
            var year = imageRecord.year;
            return range[0] <= year && year < range[1];
        };
    };

    /**
     * Ist der schwierige Spielmodus, bei dem die Vorder- und
     * Rückseite derselben Münze zusammengefunden werden müssen,
     * aktiviert?
     */
    var isDifficult = function () {
        return $('.difficult').prop('checked');
    };

    /**
     * Pfad-URL zum Bild für die Münze.
     * useBack: false/true -> Vorderseite/Rückseite
     */
    var createCoinSourcePath = function (coin, useBack) {
        var prefix = 'file:///opt/digiverso/kenom_viewer/data/' + coin.magic + '/media/';
        var folderName = 'record_' + coin.id;
        var sideSuffix = useBack ? '_rs' : '_vs';
        var fileName = folderName + sideSuffix + '.jpg';
        return prefix + folderName + '/' + fileName;
    };

    /**
     * Erzeugt die vollständige Bild-URL für die gegebene
     * Münze und Seite.
     */
    var createImageUrl = function (coin, useBack) {
        var imageSize = 300;
        var imageParameters = {
            action: 'image',
            sourcepath: createCoinSourcePath(coin, useBack),
            width: imageSize,
            height: imageSize,
            rotate: 0,
            resolution: 72,
            thumbnail: true,
            ignoreWatermark: true
        };
        return 'http://www.kenom.de/content/?' + $.param(imageParameters);
    };

    /**
     * Zufällig geordnete Liste von Bildern für die Brettgröße erzeugen.
     */
    var shuffleCoins = function (boardDimensions) {
        var numberOfPairs = getNumberOfPairs(boardDimensions);
        var filteredCoins = _.filter(imageData, getCenturyFilter());
        var selectedCoins = _.sample(filteredCoins, numberOfPairs);
        var shuffled = _.flatten(
            _.map(selectedCoins, function (coin) {
                var firstCopy = _.clone(coin);
                firstCopy.back = false;
                var secondCopy = _.clone(coin);
                secondCopy.back = isDifficult();
                return [firstCopy, secondCopy];
            })
        );

        return _.sample(shuffled, shuffled.length);
    };

    var setBoardSize = function ($board, boardDimensions) {
        $board.removeClass();

        var w = window.innerWidth;
        var h = window.innerHeight - 50;
        var ratio = w / h;

        var columnCount = ratio > 1 ? boardDimensions[0] : boardDimensions[1];

        $board.addClass('columns-' + columnCount);
        $board.css({
            width: w - 25
        });
    };

    /**
     * Frisches Spielfeld mit den übergebenen Bildern erzeugen.
     */
    var fillBoard = function () {
        var boardDimensions = getBoardDimensions();
        var shuffledCoins = shuffleCoins(boardDimensions);

        // alte Bilder löschen
        $gameBoard.empty();
        setBoardSize($gameBoard, boardDimensions);

        // neue Bilder einfügen
        _.each(shuffledCoins, function (coin) {
            var li = document.createElement('li');
            var imageUrl = createImageUrl(coin, coin.back);
            var identifyingString = imageUrl.replace(/.*record_/, '').replace(/_[vr]s.*/, '');
            li.setAttribute('data-' + identifierDataName, identifyingString);
            li.setAttribute('data-' + descriptionDataName, coin.title);
            li.setAttribute('class', 'cell');

            var img = document.createElement('img');
            img.setAttribute('src', imageUrl);
            img.setAttribute('alt', 'Spielstein');
            li.appendChild(img);

            $gameBoard.append(li);
        });
    };

    var createHighscoreKey = function () {
        return JSON.stringify(getBoardDimensions());
    };

    var readHighscore = function () {
        if (_.isObject(localStorage)) {
            return JSON.parse(localStorage.getItem(createHighscoreKey()));
        }
        return;
    };

    var writeHighscore = function (newValues) {
        if (_.isObject(localStorage)) {
            localStorage.setItem(createHighscoreKey(), JSON.stringify(newValues));<commit_msg>Getrennte Highscores für Spiele mit »schwierig« Einstellung<commit_after>/*global $, _, document, window */
$(function () {
    'use strict';

    // Die verfügbaren Bilder.
    var imageData = [];

    var $gameBoard = $('#gameBoard');

    // CSS-Klassen
    var foundClass = 'found';
    var peekClass = 'peek';
    var timeoutClass = 'timeout';
    var lastSelectionClass = 'lastSelection';
    var selectionInfoClass = 'selectionInfo';

    // Namen für Datenfelder and DOM-Elementen
    var identifierDataName = 'identifier';
    var descriptionDataName = 'description';
    var moveCountDataName = 'moveCount';
    var startTimeDataName = 'startTime';
    var timerIdDataName = 'timerId';

    /**
     * Die Anzahl der Memory-Karten auf dem Spielfeld.
     */
    var getBoardDimensions = function () {
        return JSON.parse($('.sizeSelection').val());
    };

    /**
     * Brettgröße aus Auswahl im Menü ermittlen.
     */
    var getNumberOfPairs = function (boardDimensions) {
        return boardDimensions[0] * boardDimensions[1] / 2;
    };

    /**
     * Filterfunktion für Münzdaten aus Auswahl im Menü ermitteln.
     */
    var getCenturyFilter = function () {
        var range = JSON.parse($('.centurySelection').val());
        return function (imageRecord) {
            var year = imageRecord.year;
            return range[0] <= year && year < range[1];
        };
    };

    /**
     * Ist der schwierige Spielmodus, bei dem die Vorder- und
     * Rückseite derselben Münze zusammengefunden werden müssen,
     * aktiviert?
     */
    var isDifficult = function () {
        return $('.difficult').prop('checked');
    };

    /**
     * Pfad-URL zum Bild für die Münze.
     * useBack: false/true -> Vorderseite/Rückseite
     */
    var createCoinSourcePath = function (coin, useBack) {
        var prefix = 'file:///opt/digiverso/kenom_viewer/data/' + coin.magic + '/media/';
        var folderName = 'record_' + coin.id;
        var sideSuffix = useBack ? '_rs' : '_vs';
        var fileName = folderName + sideSuffix + '.jpg';
        return prefix + folderName + '/' + fileName;
    };

    /**
     * Erzeugt die vollständige Bild-URL für die gegebene
     * Münze und Seite.
     */
    var createImageUrl = function (coin, useBack) {
        var imageSize = 300;
        var imageParameters = {
            action: 'image',
            sourcepath: createCoinSourcePath(coin, useBack),
            width: imageSize,
            height: imageSize,
            rotate: 0,
            resolution: 72,
            thumbnail: true,
            ignoreWatermark: true
        };
        return 'http://www.kenom.de/content/?' + $.param(imageParameters);
    };

    /**
     * Zufällig geordnete Liste von Bildern für die Brettgröße erzeugen.
     */
    var shuffleCoins = function (boardDimensions) {
        var numberOfPairs = getNumberOfPairs(boardDimensions);
        var filteredCoins = _.filter(imageData, getCenturyFilter());
        var selectedCoins = _.sample(filteredCoins, numberOfPairs);
        var shuffled = _.flatten(
            _.map(selectedCoins, function (coin) {
                var firstCopy = _.clone(coin);
                firstCopy.back = false;
                var secondCopy = _.clone(coin);
                secondCopy.back = isDifficult();
                return [firstCopy, secondCopy];
            })
        );

        return _.sample(shuffled, shuffled.length);
    };

    var setBoardSize = function ($board, boardDimensions) {
        $board.removeClass();

        var w = window.innerWidth;
        var h = window.innerHeight - 50;
        var ratio = w / h;

        var columnCount = ratio > 1 ? boardDimensions[0] : boardDimensions[1];

        $board.addClass('columns-' + columnCount);
        $board.css({
            width: w - 25
        });
    };

    /**
     * Frisches Spielfeld mit den übergebenen Bildern erzeugen.
     */
    var fillBoard = function () {
        var boardDimensions = getBoardDimensions();
        var shuffledCoins = shuffleCoins(boardDimensions);

        // alte Bilder löschen
        $gameBoard.empty();
        setBoardSize($gameBoard, boardDimensions);

        // neue Bilder einfügen
        _.each(shuffledCoins, function (coin) {
            var li = document.createElement('li');
            var imageUrl = createImageUrl(coin, coin.back);
            var identifyingString = imageUrl.replace(/.*record_/, '').replace(/_[vr]s.*/, '');
            li.setAttribute('data-' + identifierDataName, identifyingString);
            li.setAttribute('data-' + descriptionDataName, coin.title);
            li.setAttribute('class', 'cell');

            var img = document.createElement('img');
            img.setAttribute('src', imageUrl);
            img.setAttribute('alt', 'Spielstein');
            li.appendChild(img);

            $gameBoard.append(li);
        });
    };

    var createHighscoreKey = function () {
        return 'highscore-'
                + JSON.stringify(getBoardDimensions()) + '-'
                + JSON.stringify(isDifficult());
    };

    var readHighscore = function () {
        if (_.isObject(localStorage)) {
            return JSON.parse(localStorage.getItem(createHighscoreKey()));
        }
        return;
    };

    var writeHighscore = function (newValues) {
        if (_.isObject(localStorage)) {
            localStorage.setItem(createHighscoreKey(), JSON.stringify(newValues));
{'fraction_non_alphanumeric': 0.12184913031345922, 'fraction_numerical': 0.0033485257185378106, 'mean_word_length': 2.1679434295816145, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5588772', 'n_tokens_mistral': 3116, 'n_tokens_neox': 3065, 'n_words': 711}