-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kasparkivistik
committed
Dec 21, 2017
1 parent
68038fa
commit adb995e
Showing
8 changed files
with
337 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "itv0110", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"express": "latest" | ||
}, | ||
"devDependencies": { | ||
"express": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,123 @@ | ||
function move(move) { | ||
var http = require('http'); | ||
var url = require('url'); | ||
var fs = require('fs'); | ||
http.createServer(function (req, res) { | ||
if (req.url.indexOf("/start") > -1) { | ||
start(req, res); | ||
} else if (req.url.indexOf("/waiting") > -1) { | ||
waiting(req, res); | ||
} else { | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.write('<head><meta charset="UTF-8"/><link rel="stylesheet" href="https://www.bootswatch.com/3/superhero/bootstrap.min.css"/></head>'); | ||
res.write('<body style="position: relative; text-align: center;"><form style="display: inline-block; vertical-align: middle;" action="/start">'); | ||
res.write('<div>see koht kus mihkel raual see saade on<br></div>'); | ||
res.write('Nimi: <input type="text" name="name" required><br>'); | ||
res.write('<input type="radio" name="value" value="rock" checked/> Kivi<br>'); | ||
res.write('<input type="radio" name="value" value="paper"/> Paber<br>'); | ||
res.write('<input type="radio" name="value" value="scissors"/> Käärid<br>'); | ||
res.write('<input type="submit" value="Alusta"/>'); | ||
res.write('</form>'); | ||
res.write('</body></html>'); | ||
res.end(); | ||
} | ||
}).listen(7938); | ||
console.log('Server running!'); | ||
|
||
} | ||
function start(req, res) { | ||
var url_parts = url.parse(req.url, true); | ||
var query = url_parts.query; | ||
fs.readFile('state.json', 'utf8', function (err, data) { | ||
if (err || !data || data === "") { | ||
fs.writeFile('state.json', '{}', 'utf8'); | ||
res.writeHead(500, {'Content-Type': 'text/html'}); | ||
res.write('<head><meta charset="UTF-8"/><link rel="stylesheet" href="https://www.bootswatch.com/3/superhero/bootstrap.min.css"/></head>'); | ||
res.write('<div>Tekkis viga proovi uuesti.</div><br>'); | ||
res.write('<a href="../">tagasi</a>'); | ||
res.end(); | ||
} else { | ||
play(query, res, data); | ||
} | ||
}); | ||
} | ||
|
||
function play(query, res, data) { | ||
var obj = JSON.parse(data); | ||
var count = Object.keys(obj).length; | ||
if (count === 0) { | ||
fs.writeFile('state.json', '{"' + query.name + '":"' + query.value + '"}', 'utf8'); | ||
res.writeHead(302, {'Location': '/waiting'}); | ||
res.end(); | ||
} else if (count === 1) { | ||
var playerValue = query.value; | ||
obj[query.name] = playerValue; | ||
fs.writeFile('state.json', JSON.stringify(obj), 'utf8'); | ||
var opponentNAme = Object.keys(obj)[0]; | ||
var opponentValue = obj[opponentNAme]; | ||
var result = checkResult(playerValue, opponentValue); | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.write('<head><meta charset="UTF-8"/><link rel="stylesheet" href="https://www.bootswatch.com/3/superhero/bootstrap.min.css"/></head>'); | ||
res.write("Sa " + result + " mängu " + opponentNAme + " vastu."); | ||
res.write('<br><a href="../">Uus mäng</a>'); | ||
res.end(); | ||
} else { | ||
fs.writeFile('state.json', '{"' + query.name + '":"' + query.value + '"}', 'utf8'); | ||
res.writeHead(302, {'Location': '/waiting'}); | ||
res.end(); | ||
} | ||
} | ||
|
||
function waiting(req, res) { | ||
fs.readFile('state.json', 'utf8', function (err, data) { | ||
if (err || !data || data === "") { | ||
fs.writeFile('state.json', '{}', 'utf8'); | ||
res.writeHead(500, {'Content-Type': 'text/html'}); | ||
res.write('<head><meta charset="UTF-8"/><link rel="stylesheet" href="https://www.bootswatch.com/3/superhero/bootstrap.min.css"/></head>'); | ||
res.write('Tekkis viga proovi uuesti.<br>'); | ||
res.write('<a href="../">tagasi</a>'); | ||
res.end(); | ||
} else { | ||
obj = JSON.parse(data); | ||
var count = Object.keys(obj).length; | ||
if (count === 2) { | ||
var playerName = Object.keys(obj)[0]; | ||
var opponentName = Object.keys(obj)[1]; | ||
var result = checkResult(obj[playerName], obj[opponentName]); | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.write('<head><meta charset="UTF-8"></head>'); | ||
res.write("mäng lõppes " + result + " mängu " + opponentName + " vastu."); | ||
res.write('<br><a href="../">Uus mäng</a>'); | ||
res.end(); | ||
} else { | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.write('<head><meta charset="UTF-8"/><link rel="stylesheet" href="https://www.bootswatch.com/3/superhero/bootstrap.min.css"/>'); | ||
res.write('<meta http-equiv="refresh" content="1" /></meta>'); | ||
res.write('oota'); | ||
res.end(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
function checkResult(playerValue, opponentValue) { | ||
console.log(playerValue, opponentValue); | ||
if (playerValue === "paper") { | ||
if (opponentValue === "rock") { | ||
return "võitsid"; | ||
} else if (opponentValue === "scissors") { | ||
return "kaotasid"; | ||
} | ||
} else if (playerValue === "rock") { | ||
if (opponentValue === "scissors") { | ||
return "võitsid"; | ||
} else if (opponentValue === "paper") { | ||
return "kaotasid"; | ||
} | ||
} else if (playerValue === "scissors") { | ||
if (opponentValue === "paper") { | ||
return "võitsid"; | ||
} else if (opponentValue === "rock") { | ||
return "kaotasid"; | ||
} | ||
} | ||
return "viik"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,29 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="x-ua-compatible" content="ie=edge"> | ||
<title>rock papa scisas</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="stylesheet" type="text/css" href="stylesheet.css"> | ||
<link rel="stylesheet" href="https://bootswatch.com/4/cosmo/bootstrap.min.css"> | ||
<script src="app.js"></script> | ||
<meta charset="UTF-8"> | ||
<title>kolmeraudne</title> | ||
<link rel="stylesheet" href="https://bootswatch.com/4/sandstone/bootstrap.min.css"> | ||
<link rel="stylesheet" href="stylesheet.css"> | ||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | ||
<script type="text/javascript" src="index.js"></script> | ||
</head> | ||
<body class="centered-wrapper"> | ||
<nav class="navbar navbar-expand-lg navbar-dark bg-primary"> | ||
<a class="navbar-brand" href="#">rock papa scisas</a> | ||
<a class="navbar-brand" href="#">see koht kus mihkel raual see saade on</a> | ||
</nav> | ||
<h1 class="centered-content">vali oma lemmik</h1> | ||
|
||
<div class="container centered-content"> | ||
<a id="playerrock"><img width="150" height="150" src="rock.png" alt="kivi"></a> | ||
<a id="playerpaper"><img width="150" height="150" src="paper.jpg" alt="paber"></a> | ||
<a id="playerscissors"><img width="150" height="150" src="scissors.jpg" alt="käärid"></a> | ||
<div class="centered-content"> | ||
<form method="post"> | ||
<br><br><input type="text" placeholder="name" id="player-name"><br><br> | ||
<button id="rock" type="submit" onclick="postMove('rock')">Rock</button> | ||
<button id="paper" type="submit" onclick="postMove('paper')">Paper</button> | ||
<button id="scissors" type="submit" onclick="postMove('scissors')">Scissors</button> | ||
</form> | ||
</div> | ||
|
||
<div id="result"> | ||
</div> | ||
<footer class="well well-sm">sau ma kaspar kivistik 164347 <br> | ||
paneme siia ruttu see 7.5 ära siis ma saan selle aine nelja ja olen õnnelik</footer> | ||
</body> | ||
</html> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
console.log("run"); | ||
|
||
if (localStorage.getItem("result")) { | ||
$("#result").text(localStorage.getItem("result")); | ||
localStorage.removeItem("result"); | ||
localStorage.removeItem("waiting"); | ||
} else if (localStorage.getItem("waiting") === "true") { | ||
refresh(); | ||
localStorage.removeItem("waiting"); | ||
} | ||
|
||
function postMove(move) { | ||
var xhr = new XMLHttpRequest(); | ||
var name = $("#player-name").val(); | ||
xhr.open("GET", "xhr://dijkstra.cs.ttu.ee:7842/?player=" + name + "&option=" + move + "&reset=false"); | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState === 4) { | ||
if (xhr.status === 200 || xhr.status === 0) { | ||
var response = xhr.responseText; | ||
if (response.length > 2) { | ||
localStorage.setItem("result", response); | ||
} else { | ||
localStorage.setItem("waiting", "true"); | ||
} | ||
} | ||
} | ||
}; | ||
xhr.send(); | ||
} | ||
|
||
function requestResult() { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open("GET", "http://dijkstra.cs.ttu.ee:7842/?player=\"none\"&option=\"none\"&reset=waiting"); | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState === 4) { | ||
if (xhr.status === 200) { | ||
var response = xhr.responseText; | ||
if (response.length > 2) { | ||
$("#result").text(response); | ||
clearGame(); | ||
} else { | ||
localStorage.setItem("waiting", "true"); | ||
refresh(); | ||
} | ||
} | ||
} | ||
}; | ||
xhr.send(); | ||
} | ||
|
||
function refresh() { | ||
$("#result").text("waiting for other player..."); | ||
setTimeout(function () { | ||
requestResult(); | ||
}, 5000); | ||
} | ||
|
||
function clearGame() { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open("GET", "http://dijkstra.cs.ttu.ee:7842/?player=\"none\"&option=\"none\"&reset=true"); | ||
xhr.send(); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.