This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
fun-with-binary/app/input-frontend/index.html

165 lines
7.1 KiB
HTML

<!--
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/>.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Fun with Binary</title>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<!-- Roboto -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:400,500,700">
<link rel="stylesheet" href="css/ratchet.min.css">
<link rel="stylesheet" href="css/ratchet-theme-android.min.css">
<link rel="stylesheet" href="css/app.css">
<script src="js/ratchet.min.js"></script>
</head>
<body onload="start_game()">
<header class="bar bar-nav">
<a class="icon icon-compose pull-right" href="#networkModal"></a>
<h1 class="title">
Have fun with Binary
</h1>
</header>
<div class="content">
<style>
.led {
width: auto;
height: auto;
max-width: 50%;
max-height: 50%;
}
</style>
<div id="statement"></div>
<img class="led" id="led0" onclick="change_state(0)" src="pic_bulboff.gif">
<img class="led" id="led1" onclick="change_state(1)" src="pic_bulboff.gif">
<img class="led" id="led2" onclick="change_state(2)" src="pic_bulboff.gif">
<img class="led" id="led3" onclick="change_state(3)" src="pic_bulboff.gif">
<img class="led" id="led4" onclick="change_state(4)" src="pic_bulboff.gif">
<img class="led" id="led5" onclick="change_state(5)" src="pic_bulboff.gif">
<div id="result"></div>
<input type="button" onclick="end_game()" value="That's it!" id="play_button">
<p>Made with love by <a href="https://www.diogo.site/">Diogo Cordeiro</a></p>
</div>
<script>
// URL for server communication
URL = "http://localhost";
/**
* Turn current answer bits accordingly
*/
function change_state(led_id)
{
let image = document.getElementById("led"+led_id);
if (image.src.match("bulbon"))
image.src = "pic_bulboff.gif";
else
image.src = "pic_bulbon.gif";
let xhttp = new XMLHttpRequest();
xhttp.open("GET", URL+"?change_state="+led_id+"&t=" + Math.random(), true);
xhttp.send();
}
/**
* Starts game
*/
function start_game()
{
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
document.getElementById("statement").innerHTML = "Write "+this.responseText+" in binary :)";
};
xhttp.open("GET", URL+"?start&t=" + Math.random(), true);
xhttp.send();
}
/**
* Prepares everything for a new game
*/
function restart_game()
{
document.getElementById("result").innerHTML = "";
start_game();
for (let led_id = 0; led_id <= 5; led_id++)
{
let image = document.getElementById("led"+led_id);
image.src = "pic_bulboff.gif";
}
document.getElementById("play_button").onclick=end_game;
document.getElementById("play_button").value="That's it!";
}
/**
* Compares the given answer with the right answer and tells the result
*/
function end_game()
{
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
document.getElementById("result").innerHTML = this.responseText;
};
xhttp.open("GET", URL+"?end&t=" + Math.random(), true);
xhttp.send();
document.getElementById("play_button").onclick=restart_game;
document.getElementById("play_button").value="Play again!";
}
/**
* Sets the URL for server communication
*/
function setURL()
{
URL = document.getElementById("new_url").value;
}
</script>
<!-- Network modal -->
<div id="networkModal" class="modal">
<header class="bar bar-nav">
<a class="icon icon-left pull-left" href="#networkModal"></a>
<h1 class="title">Settings</h1>
</header>
<div class="content">
<form class="content-padded">
<label for="new_url">Insert the URL for server communication</label>
<input type="text" value="http://localhost" id="new_url">
<a class="btn btn-positive btn-block" href="#networkModal" onclick="setURL()">Save</a>
</form>
</div>
</div><!-- /.modal -->
</body>
</html>