<commit_before><!DOCTYPE html>
<!--
Copyright (c) 2014, tibbitts
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
-->

<button id="start-btn" class="btn btn-lg btn-primary">New game</button>

<hr>

<div id="letter-btns"></div>

<script>
    $("#start-btn").click(function() {
        sendRandomWord();
    });
    
    $.each("ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""), function(index, value) {
        $("#letter-btns").append("<button class='letter-btn btn btn-lg btn-default' style='margin: 3px' data-value='"+value+"' disabled='disabled'>" + value + "</button>");
    });
    
    $(".letter-btn").click(function() {
        var value = $(this).attr("data-value");
        sendToDisplay(JSON.stringify({
            responseType: "guess",
            letter: value
        }));
        $(this).prop("disabled", "disabled");
    });
    
    var people;
    settings.local = {
        verbs: {
            people: function(obj) {
                people = obj.people;
                sendRandomWord();
            }
        }
    }
    
    var usedWords = [];
    function sendRandomWord() {
        var person, word;
        var tries = 0, maxTries = 50;
        do {
            person = getRandomElement(people);
            var names = splitWords(person.name);
            if (names && names.length > 0) {
                word = getRandomElement(names);
            }
        } while ((word.length > 12 || 
                word.length < 3 || 
                word.toLowerCase() === "living" || 
                usedWords[word]) && tries++ < maxTries);
        if (tries >= maxTries) {
            usedWords = [];
        }
        
        usedWords[word] = word;
        var imgUrl;
        if (person.images) {
            imgUrl = getRandomElement(person.images);
        }
        sendToDisplay(JSON.stringify({
            responseType: "start",
            word: word,
            name: person.name,
            imgUrl: imgUrl
        }));
        $(".letter-btn").removeProp("disabled");
    }
    
    ws.socketSend("get-ancestors " + token + " " + settings.userId);
</script><commit_msg>Add configurations to hangman game<commit_after><!DOCTYPE html>
<!--
Copyright (c) 2014, tibbitts
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
-->

<ul class="nav nav-tabs" id="tabs" role="tablist">
    <li class="active" id="game-tab" ><a href="#game-tab" role="tab" data-toggle="tab">Game</a></li>
    <li id="settings-tab" ><a href="#settings-tab" role="tab" data-toggle="tab">Settings</a></li>
</ul>


<div id="game">
    <button id="start-btn" class="btn btn-lg btn-primary">New game</button>

    <hr>

    <div id="letter-btns"></div>
</div>

<div id="settings" style="display: none">
    <div class="btn-group btn-group-lg" data-toggle="buttons">
        <label class="btn btn-default">
            <input type="radio" name="difficulty" value="easy">Easy
        </label>
        <label class="btn btn-default active">
            <input type="radio" name="difficulty" value="medium" checked="">Medium
        </label>
        <label class="btn btn-default">
            <input type="radio" name="difficulty" value="hard">Hard
        </label>
        <label class="btn btn-default">
            <input type="radio" name="difficulty" value="insane">Insane
        </label>
        <label class="btn btn-warning">
            <input type="radio" name="difficulty" value="custom">Custom
        </label>
    </div>
    <div id="customControls" style="display: none;">
        <dl class="dl-horizontal">
            <dt></dt>
            <dd>
                <div class="btn-group btn-group-lg" data-toggle="buttons">
                    <label class="btn btn-default active">
                        <input type="checkbox" name="type" value="name">Name
                    </label>
                    <label class="btn btn-default active">
                        <input type="checkbox" name="type" value="relationship">Relationship
                    </label>
                    <label class="btn btn-default">
                        <input type="checkbox" name="type" value="birthPlace">Birth place
                    </label>
                    <label class="btn btn-default">
                        <input type="checkbox" name="type" value="deathPlace">Death place
                    </label>
                </div>
            </dd>
        </dl>
    </div>
</div>


<script>
    console.info("Loading controller-hangman");
    var configurations = {
        easy: {
            types: [
                "name"
            ]
        },
        medium: {
            types: [
                "name",
                "relationship"
            ]
        },
        hard: {
            types: [
                "name",
                "relationship",
                "birthPlace"
            ]
        },
        insane: {
            types: [
                "name",
                "relationship",
                "birthPlace",
                "deathPlace"
            ]
        }
    };
    
    var templates = {
        name: {
            path: "name"
        },
        relationship: {
            path: "relationship",
            contextPrefix: " "
        },
        birthPlace: {
            path: "facts.*type=Birth.place",
            contextPrefix: "Birth: "
        },
        deathPlace: {
            path: "facts.*type=Death.place",
            contextPrefix: "Death: "
        }
    }
    
    $('input[name=difficulty]:radio').change(function () {
        $("#customControls").hide();

        settings.config = cloneObject(configurations[$(this).val()]);
    });

    $("input[name=difficulty][value='custom']:radio").unbind().change(function () {
        if (!settings.config) {
            settings.config = cloneObject(configurations.medium);
        }
        initCheckboxes("[name=type]", settings.config.types);
        $("#customControls").show();
    });
    
    $("[name=type]").change(function () {
        settings.config.types = getCheckboxValues("[name=type]");
    });

    $("#game-tab").click(function() {
        $("#game").show();
        $("#settings").hide();
    });

    $("#settings-tab").click(function() {
        $("#game").hide();
        $("#settings").show();
    });

    
    $("#start-btn").click(function() {
        sendRandomWord();
    });
    
    $.each("ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""), function(index, value) {
        $("#letter-btns").append("<button class='letter-btn btn btn-lg btn-default' style='margin: 3px' data-value='"+value+"' disabled='disabled'>" + value + "</button>");
    });
    
    $(".letter-btn").click(function() {
        var value = $(this).attr("data-value");
        sendToDisplay(JSON.stringify({
            responseType: "guess",
            letter: value
        }));
        $(this).prop("disabled", "disabled");
    });
    
    var people;
    settings.local = {
        verbs: {
            people: function(obj) {
                people = filterArray(obj.people, "living!=true");
                sendRandomWord();
            }
        }
    }
    
    var usedWords = [];
    function sendRandomWord() {
        var person, word;
        var tries = 0, maxTries = 50;
        var context;
        do {
            //TODO: place selectors not working
            person = getRandomElement(people);
            var type = getRandomElement(settings.config.types);
            var template = templates[type];
            var element = resolveChildProperty(person, template.path);
            var words = splitWords(element);
            if (words && words.length > 0) {
                word = getRandomElement(words).replace(/[^a-zA-Z]/g, "");
            }
            context = (template.contextPrefix ? "<hr>"+ template.contextPrefix + element : "");
        } while ((!word || word.length > 12 || 
                word.length < 3 || 
                usedWords[word]) && tries++ < maxTries);
        if (tries >= maxTries) {
            usedWords = [];
        }
        
        usedWords[word] = word;
        var imgUrl;
        if (person.images) {
            imgUrl = getRandomElement(person.images);
        }
        sendToDisplay(JSON.stringify({
            responseType: "start",
            word: word,
            name: person.name + context,
            imgUrl: imgUrl
        }));
        $(".letter-btn").removeProp("disabled");
    }
    
    function cloneObject(obj) {
        var serialized = window.JSON.stringify(obj);
        var clone = window.JSON.parse(serialized);
        return clone;
    }
    
    function initCheckboxes(selector, values) {
        $(selector).removeProp('checked').parent().removeClass("active");
        if (values) {
            $.each(values, function(index, value) {
                console.info("Checking checkbox " + value);
                $(selector + "[value="+value+"]").prop('checked',true).parent().addClass("active");
            });
        }
    }
    
    function initRadio(selector, value) {
        $(selector).removeProp('checked').parent().removeClass("active");
        $(selector + "[value="+value+"]").prop('checked',true).parent().addClass("active");
    }
    
    function getCheckboxValues(selector) {
        var values = [];
        $(selector + ":checked").each(function() {
            values.push($(this).val());
        });
        return values;
    }
    
    settings.config = cloneObject(configurations.medium);
    ws.socketSend("get-ancestors " + token + " " + settings.userId);
</script>
{'fraction_non_alphanumeric': 0.13502180476350217, 'fraction_numerical': 0.0020127474002012745, 'mean_word_length': 2.1522601110229975, 'pattern_counts': {'":': 1, '<': 83, '<?xml version=': 0, '>': 85, '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': '4045003', 'n_tokens_mistral': 3328, 'n_tokens_neox': 3137, 'n_words': 851}