Skip to content

Commit

Permalink
see aine on tehtud ju
Browse files Browse the repository at this point in the history
  • Loading branch information
kasparkivistik committed Dec 21, 2017
1 parent 68038fa commit adb995e
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 39 deletions.
10 changes: 10 additions & 0 deletions package.json
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"
}
}
20 changes: 11 additions & 9 deletions prax4/src/createpost.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
session_start();
include("config.php");
include("upload.php");
//include("upload.php");

$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

Expand Down Expand Up @@ -30,30 +30,32 @@
?>
<body class="centered-wrapper">
<div class="centered-content">
<form action="upload.php" method="post" enctype="multipart/form-data">
<form method="post">
<legend>Hello create new post thanks</legend>
<label><b>The title of your new postitus</b></label><br>
<input type="text" style="width: 500px" name="title"/><br>
<label><b>The content of your new postitus</b></label><br>
<input type="text" style="width: 500px; height: 250px" name="content"/><br><br>
<input type="file" name="fileToUpload"/>
<!--<input type="file" name="fileToUpload"/>-->
<input type="submit">
<?php

if (isset($_REQUEST['title']) and isset($_REQUEST['content']) and isset($_FILES['fileToUpload'])) {
if (isset($_REQUEST['title']) and isset($_REQUEST['content'])) {
$title = escape($connection, $_REQUEST['title']);
$content = escape($connection, $_REQUEST['content']);
$name = $_SESSION['username'];
$target = "uploads/";
$target = $target . basename($_FILES['fileToUpload']);
$picture = $_FILES['fileToUpload'];
$ok = "ok";
//$target = "uploads/";
//$target = $target . basename($_FILES['fileToUpload']);
//$picture = $_FILES['fileToUpload'];
//$sql = "INSERT INTO 164347_posts (content, time, username, title, picture) VALUES ('$content', NOW(),
//'$name', '$title', '$picture')";
$sql = "INSERT INTO 164347_posts (content, time, username, title, picture) VALUES ('$content', NOW(),
'$name', '$title', '$picture')";
'$name', '$title', '$ok')";
if ($connection->query($sql) === true) {
$message = "Success!";
echo "<script type='text/javascript'>alert('$message');</script>";
header("Location: index.php");

}
}
?>
Expand Down
2 changes: 1 addition & 1 deletion prax4/src/viewpost.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
$commentQuery = "SELECT * FROM 164347_comments WHERE post_id = $id";
$commentResult = mysqli_query($connection, $commentQuery);
echo '<legend>comments</legend>';
while ($commentRow = mysqli_fetch_assoc($commentResult)) {
while ($commentRow = mysqli_fetch_assoc($commentResult )) {
$commentRow = array_values($commentRow);
echo '<div class="centered-content">
Published by ' . $commentRow[2] . ' at ' . $commentRow[3] . '<br>
Expand Down
124 changes: 122 additions & 2 deletions prax5/app.js
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";
}
34 changes: 19 additions & 15 deletions prax5/index.html
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>
62 changes: 62 additions & 0 deletions prax5/index.js
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();
}
Binary file added prax5/kivi.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit adb995e

Please sign in to comment.