1c59afa216
Translated interface to portuguese as this is a portuguese event
412 lines
17 KiB
JavaScript
412 lines
17 KiB
JavaScript
/*
|
|
* Fun with Binary - a fun way of introducing binary
|
|
* Copyright (C) 2018, Diogo Cordeiro.
|
|
*
|
|
* This program 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/>.
|
|
*
|
|
* @category UI interaction
|
|
* @package Fun with Binary
|
|
* @author Diogo Cordeiro <up201705417@fc.up.pt>
|
|
* @copyright 2018 Diogo Cordeiro.
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
|
* @link https://www.diogo.site/projects/fun_with_binary/
|
|
*/
|
|
|
|
/***********************************
|
|
* EXTEND JAVASCRIPT FUNCTIONALITY *
|
|
***********************************/
|
|
|
|
/**
|
|
* Returns a random integer between min(inclusive) and max(inclusive)
|
|
* Using Math.round() would give a non-uniform distribution!
|
|
*/
|
|
function getRandomInt(min, max)
|
|
{
|
|
return Math.floor(Math.random() *(max - min + 1)) + min;
|
|
}
|
|
|
|
/**
|
|
* Replace char at given position
|
|
*/
|
|
String.prototype.replaceAt=function(index, replacement)
|
|
{
|
|
return this.substr(0, index) + replacement+ this.substr(index +
|
|
replacement.length);
|
|
}
|
|
|
|
/**
|
|
* Checks if session storage is available
|
|
*/
|
|
function is_session_storage_available()
|
|
{
|
|
return window.sessionStorage != null;
|
|
}
|
|
|
|
/***********************************
|
|
* GAME SETTINGS *
|
|
***********************************/
|
|
// Online Mode (only used in Online Mode)
|
|
ONLINEMODE = true;
|
|
|
|
// Box IP
|
|
ONLINEURL = "http://42.42.42.42/game/";
|
|
|
|
// Number of leds
|
|
NLEDS = 6;
|
|
|
|
// Activate the Show 2 powers Labels feature
|
|
HAVELABELS = true;
|
|
|
|
// Default Show 2 powers Labels feature mode
|
|
show_labels = true;
|
|
|
|
// Easter Egg
|
|
EASTEREGG = true;
|
|
|
|
// ON and OFF leds srcs
|
|
LED_OFF = "data:image/png;base64,R0lGODlhZAC0ANX/AMDAwP//zP/M///MzP/Mmf/MZv/MM/+Zmf+ZZv+ZM8z//8z/zMzM/8zMzMyZZsyZM5mZmZmZZplmZplmM2aZmWZmmWZmZmYzZmYzMzNmZjNmMzMzZjMzMzMzADMAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAABkALQAQAb/QIBwSCwaj8ikcslsOp/FRgMgpVavUix1O9VmreBvFUo2Zrvo8NWaxrbf3vdQXjar2fczPs6Go+VxWlFfTXB9fYCJeW5dYYuPU2aEg3+MlY58mZpcipuaY5BnonufdZKhqGJrQm6pnopJYoiutKB+nbitr6yNu76gUWq/pH+ki7q4tU/Dsp+iyJXJm6acocVxEFnZzw3be5jS1EqGqrPP2+jogMriZG15wlLb2urlu+3U715q2fPa8tIiAcNnKhejet0AluJFsGEgeRAi+uOWUKLFZ7wiNXQIwKLHjyA/aiTSLeLAjfgQIChAgCWCljBfuiQgc+XKAioRJNjJk8AA/wI7dSYogLIozAIFhtIkcIBly5U0cbqUqjLBy58DhL7USaDoRqBLCzRtSgBm05xCrfJcq1Zp2bRdvcoNkLOsU7EtxTpteeDo0rQB5ApmYgEECA4YNHDYwKGx4ccgLAyeLMkexTX2wFB2l+haL32dhCFKI9eztUOZjrkSlFHzMsvmQPuCN4rZEdK3H16qHTBebGYUOTUaBO6a6FfDl+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o0MQjnKQTYC535HKYJcGcuVywg+uTyoBcrlvP3RkZJmJ4wmJc4Bh0kg9qyiPOLxCAAOmByReOeA4ckOzwBw3gFU2BcUSsZ+8ttTB6m3PKGIj4AN+HDu2KfDRTnwTvI74ADXQr5HR0XS2EtKXv/KMmoA5iUtQJThBvVnFUOvkNRkQQyk/4KlL0HmKDJkAAoDwKXkNQZQyuMK9bxCJfPdsHyXHjWguCIUvtB6LYmpn1/krDhOX5qG0iZTrgNDv/B1RSjouyEHraKUX5tF0SwUdf3MgpWpaFvXN7wTtk2Nbgcib9O5nl9gUHgAbp9pMb/OtvxiXb+8uBB/UAF1qMcHpP8JHODKljdQ6rQWMAJAS99btPxmnerzNUC4insK+PRipYDfaYdggrfFdddupCgGL42hIZYQuHIldAADNAQ4ZJh9vAZkAAQ1b0IImqhE0HwGBSpgomZQEHQnTGDpnlHiEY+o9BVgselOEM0USaAXRCuSButr6IAIPlACEZhABFpHQZVlFAQAOw==";
|
|
|
|
/**********************************
|
|
* ACTUAL GAME CODE *
|
|
**********************************/
|
|
|
|
/**
|
|
* Global variables
|
|
*/
|
|
// Empty Answer
|
|
empty_answer = '';
|
|
while (empty_answer.length < NLEDS)
|
|
{
|
|
empty_answer += '0';
|
|
}
|
|
|
|
// correct answer
|
|
correct_answer = empty_answer;
|
|
|
|
// current answer
|
|
current_answer = empty_answer;
|
|
|
|
// Generate a token for this session
|
|
sessionStorage.token = Math.random().toString(36).substr(2, 15+2);
|
|
|
|
/**
|
|
* Create leds
|
|
*/
|
|
for (let i = 0; i < NLEDS; ++i)
|
|
{
|
|
// Led holder
|
|
let figure = document.createElement("figure");
|
|
figure.setAttribute("class", "box");
|
|
|
|
// Label
|
|
if (HAVELABELS)
|
|
{
|
|
let c_label = Math.pow(2, NLEDS-i-1);
|
|
let figcaption = document.createElement("figcaption");
|
|
figcaption.setAttribute("value", c_label);
|
|
figcaption.setAttribute("class", "text labels");
|
|
figcaption.id = "label" + i;
|
|
figcaption.innerHTML = c_label;
|
|
figure.appendChild(figcaption);
|
|
}
|
|
else
|
|
{
|
|
document.getElementById('label_switch_button').setAttribute("style", "display:none");
|
|
}
|
|
|
|
// Led
|
|
let img = document.createElement("img");
|
|
|
|
// Set img src
|
|
if (is_session_storage_available()
|
|
&& sessionStorage.playing == 'true'
|
|
&& sessionStorage.current_answer.charAt(i) == '1')
|
|
{
|
|
img.src = LED_ON;
|
|
img.setAttribute("value", "ON");
|
|
}
|
|
else
|
|
{
|
|
img.src = LED_OFF;
|
|
img.setAttribute("value", "OFF");
|
|
}
|
|
|
|
img.id = "led" + i;
|
|
img.setAttribute("class", "led");
|
|
img.setAttribute("onclick", "switch_led("+i+")");
|
|
figure.appendChild(img);
|
|
|
|
document.getElementById("leds").appendChild(figure);
|
|
}
|
|
|
|
// If initial value for label is false, handle it
|
|
if (HAVELABELS && !show_labels)
|
|
{
|
|
show_labels = true;
|
|
document.getElementById("label_switch").click();
|
|
}
|
|
|
|
// Let the game begin
|
|
get_game();
|
|
|
|
/**
|
|
* Switch labels
|
|
*/
|
|
function switch_labels()
|
|
{
|
|
if (show_labels)
|
|
{
|
|
for (let i = 0; i < NLEDS; ++i)
|
|
{
|
|
document.getElementById("label"+i).setAttribute
|
|
("style", "visibility:hidden");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (let i = 0; i < NLEDS; ++i)
|
|
{
|
|
document.getElementById("label"+i).setAttribute
|
|
("style", "");
|
|
}
|
|
}
|
|
|
|
show_labels = !show_labels;
|
|
cache_current_game();
|
|
}
|
|
|
|
/**
|
|
* Turn current answer bits accordingly
|
|
*/
|
|
function switch_led(led_id)
|
|
{
|
|
let image = document.getElementById("led"+led_id);
|
|
if (image.getAttribute("value") == "ON")
|
|
{
|
|
image.setAttribute("value", "OFF");
|
|
image.src = LED_OFF;
|
|
|
|
current_answer = current_answer.replaceAt(led_id, "0");
|
|
}
|
|
else
|
|
{
|
|
image.setAttribute("value", "ON");
|
|
image.src = LED_ON;
|
|
|
|
current_answer = current_answer.replaceAt(led_id, "1");
|
|
}
|
|
|
|
cache_current_game();
|
|
|
|
// Easter egg
|
|
if (EASTEREGG && current_answer == (empty_answer.substr(6) + "101010"))
|
|
{
|
|
alert("That's the answer to life, the universe and everything!");
|
|
}
|
|
|
|
// If we are connected to the box
|
|
if (ONLINEMODE)
|
|
{
|
|
let xhttp = new XMLHttpRequest();
|
|
xhttp.open("GET", ONLINEURL+"switch_state?led=" + led_id +
|
|
"&token=" + sessionStorage.token, true);
|
|
xhttp.send();
|
|
}
|
|
|
|
// If right answer, finish game, restart a new one
|
|
if (current_answer == correct_answer)
|
|
{
|
|
end_game();
|
|
restart_game();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sets problem statement's decimal number
|
|
*/
|
|
function set_statement(n_dec)
|
|
{
|
|
document.getElementById("statement_value").innerHTML = n_dec;
|
|
}
|
|
|
|
/**
|
|
* Recovers game from page reload
|
|
*/
|
|
function get_game()
|
|
{
|
|
if (!is_session_storage_available() || sessionStorage.playing != 'true')
|
|
{
|
|
start_game();
|
|
}
|
|
else
|
|
{
|
|
restore_from_cache();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Restore current game from cache
|
|
*/
|
|
function restore_from_cache()
|
|
{
|
|
correct_answer = sessionStorage.correct_answer;
|
|
current_answer = sessionStorage.current_answer;
|
|
set_statement(Number(sessionStorage.number_dec));
|
|
|
|
if (HAVELABELS && sessionStorage.show_label != show_labels.toString())
|
|
{
|
|
show_labels = true;
|
|
document.getElementById("label_switch").click();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Caches current game
|
|
*/
|
|
function cache_current_game()
|
|
{
|
|
if (!is_session_storage_available())
|
|
{
|
|
return;
|
|
}
|
|
|
|
sessionStorage.current_answer = current_answer;
|
|
sessionStorage.show_label = show_labels;
|
|
}
|
|
|
|
/**
|
|
* Starts game
|
|
*/
|
|
function start_game()
|
|
{
|
|
let n_dec = getRandomInt(1, Math.pow(2, NLEDS)-1);
|
|
let n_bin = n_dec.toString(2);
|
|
correct_answer = empty_answer.substr(n_bin.length) + n_bin;
|
|
|
|
set_statement(n_dec);
|
|
|
|
if (is_session_storage_available())
|
|
{
|
|
sessionStorage.playing = true;
|
|
sessionStorage.correct_answer = correct_answer;
|
|
sessionStorage.number_dec = n_dec;
|
|
|
|
cache_current_game();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Prepares everything for a new game
|
|
*/
|
|
function reset_game()
|
|
{
|
|
correct_answer = empty_answer;
|
|
current_answer = empty_answer;
|
|
turn_all_leds_images_off();
|
|
for (let i = 0; i < NLEDS; ++i)
|
|
{
|
|
document.getElementById("led"+i).setAttribute("value", "OFF");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Start a new game
|
|
*/
|
|
function restart_game()
|
|
{
|
|
reset_game();
|
|
start_game();
|
|
}
|
|
|
|
/**
|
|
* Finishes the game
|
|
*/
|
|
function end_game()
|
|
{
|
|
// If we are connected to the box
|
|
if (ONLINEMODE)
|
|
{
|
|
let xhttp = new XMLHttpRequest();
|
|
xhttp.open("GET", ONLINEURL+"won" +
|
|
"?token=" + sessionStorage.token, true);
|
|
xhttp.send();
|
|
}
|
|
|
|
// Blink correct answer leds 3 times
|
|
blink_led_effect(3, correct_answer);
|
|
|
|
// Reset cache
|
|
if (is_session_storage_available())
|
|
{
|
|
sessionStorage.clear();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Turn every led on(without turning value attribute)
|
|
*/
|
|
function turn_leds_images_on(which)
|
|
{
|
|
// Turn all on
|
|
for (let i = 0; i < NLEDS; ++i)
|
|
{
|
|
if (which.charAt(i) == '1')
|
|
{
|
|
document.getElementById("led"+i).src = LED_ON;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Turn every led off(without turning value attribute)
|
|
*/
|
|
function turn_all_leds_images_off()
|
|
{
|
|
// Turn all off
|
|
for (let i = 0; i < NLEDS; ++i)
|
|
{
|
|
document.getElementById("led"+i).src = LED_OFF;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Makes leds blink i times
|
|
*/
|
|
function blink_led_effect(i, which)
|
|
{
|
|
// Because the recursive function fulfills an action every two iterations
|
|
blink_led_effect_helper(i*2, which);
|
|
}
|
|
|
|
/**
|
|
* Makes leds blink i/2 times
|
|
*/
|
|
function blink_led_effect_helper(i, which)
|
|
{
|
|
if (i % 2 == 1)
|
|
{
|
|
turn_leds_images_on(which);
|
|
}
|
|
else
|
|
{
|
|
turn_all_leds_images_off();
|
|
}
|
|
|
|
if (--i > -1)
|
|
{
|
|
setTimeout(function() { blink_led_effect_helper(i, which); }, 500);
|
|
}
|
|
}
|