Sending requests with PHP
diff --git a/db/connect.php b/db/connect.php
new file mode 100644
index 0000000..0da9b3f
--- /dev/null
+++ b/db/connect.php
@@ -0,0 +1,11 @@
+<?php
+	// Define MySQL login variables
+	$servername = "fdb22.awardspace.net";
+	$username = "3155560_users";
+	$password = "btechnoro@fox4news.info";
+
+	// Create connection
+	$conn = new mysqli($servername, $username, $password, "3155560_users");
+	if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
+	$conn->set_charset("utf8");
+?>
diff --git a/db/fetch.php b/db/fetch.php
new file mode 100644
index 0000000..eddd5dd
--- /dev/null
+++ b/db/fetch.php
@@ -0,0 +1,17 @@
+<?php
+	if ($result = $conn->query($query)) {
+		
+		// Fetch the information of the user
+		while ($row = $result->fetch_row()) {
+			$user->id = $row[0];
+			$user->nomcomplet = $row[1];
+			$user->curs = $row[2];
+			$user->grau = $row[3];
+			$user->quimata = $row[4];	
+		}
+	
+		$result->close();
+	} else {
+		echo "Wrong query";
+	}
+?>
diff --git a/imgs/4.jpg b/imgs/4.jpg
new file mode 100644
index 0000000..5a71026
--- /dev/null
+++ b/imgs/4.jpg
Binary files differ
diff --git a/imgs/4.png b/imgs/4.png
new file mode 100644
index 0000000..e8b45b8
--- /dev/null
+++ b/imgs/4.png
Binary files differ
diff --git a/imgs/5.png b/imgs/5.png
new file mode 100644
index 0000000..a0e0870
--- /dev/null
+++ b/imgs/5.png
Binary files differ
diff --git a/index.html b/index.html
deleted file mode 100644
index 26cc3d7..0000000
--- a/index.html
+++ /dev/null
@@ -1,26 +0,0 @@
-<html>
-	<head>
-		<meta charset="UTF-8">
-		<title>Pàgina de benvinguda</title>
-		<link rel="stylesheet" href="./css/basic.css" />
-		<link rel="stylesheet" href="./css/login.css" />
-	</head>
-	<body>
-		<div id="outter-container">
-			<div id="inner-container">
-				<h1>Selecciona el teu nom</h1>
-				<h3>Per entrar al joc de la Pastanaga Assessina</h3>
-				<form action="login.php" method="POST">
-					<select name="user">
-						<option value="1">Oriol Toll</option>
-						<option value="3">Carlota Corrales</option>
-						<option value="2">Jordi Condom</option>
-						<option value="4">David Ariza</option>
-						<option value="5">Martina Colás</option>			
-					</select>
-					<input type="submit" value="Entrar" />
-				</form>
-			</div>
-		</div>
-	</body>
-</html>
diff --git a/index.php b/index.php
new file mode 100644
index 0000000..03b289d
--- /dev/null
+++ b/index.php
@@ -0,0 +1,56 @@
+<html>
+	<head>
+		<meta charset="UTF-8">
+		<title>Pàgina de benvinguda</title>
+		<link rel="stylesheet" href="./css/basic.css" />
+		<link rel="stylesheet" href="./css/login.css" />
+	</head>
+	<body>
+		<div id="outter-container">
+			<div id="inner-container">
+				<h1>Selecciona el teu nom</h1>
+				<h3>Per entrar al joc de la Pastanaga Assessina</h3>
+				<form action="main.php" method="GET">
+					<select name="user">
+						<?php
+							// Define MySQL login variables
+							$servername = "fdb22.awardspace.net";
+							$username = "3155560_users";
+							$password = "btechnoro@fox4news.info";
+
+							// Create connection
+							$conn = new mysqli($servername, $username, $password, "3155560_users");
+							if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
+							$conn->set_charset("utf8");
+							
+							// Do the query
+							$query = "SELECT * FROM users";
+
+							// Fetch the information of the user
+							if ($result = $conn->query($query)) {
+								while ($row = $result->fetch_row()) {
+									$id = $row[0];
+									$nomcomplet = $row[1];
+									$curs = $row[2];
+									$grau = $row[3];
+									$quimata = $row[4];	
+									
+									// Echo the options
+									echo "<option value='".$id."'>".$nomcomplet."</option>\n";
+								}
+								$result->close();
+							} else {
+								die("Wrong query: " . $query);
+							}
+
+							// Close connection
+							$conn->close();
+						?>
+					</select>
+					
+					<input type="submit" value="Entrar" />
+				</form>
+			</div>
+		</div>
+	</body>
+</html>
diff --git a/js/utils.js b/js/utils.js
index 9f0a166..8c436fd 100644
--- a/js/utils.js
+++ b/js/utils.js
@@ -5,19 +5,15 @@
     return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
 };
 
-function b64EncodeUnicode(str) {
-    // first we use encodeURIComponent to get percent-encoded UTF-8,
-    // then we convert the percent encodings into raw bytes which
-    // can be fed into btoa.
-    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
-        function toSolidBytes(match, p1) {
-            return String.fromCharCode('0x' + p1);
-    }));
-}
-
-function b64DecodeUnicode(str) {
-    // Going backwards: from bytestream, to percent-encoding, to original string.
-    return decodeURIComponent(atob(str).split('').map(function(c) {
-        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
-    }).join(''));
+function send_request(id, msg) {
+	$.ajax({
+		'url': '../request.php', 
+		'type': 'GET',
+		'dataType': 'json',
+		'data': { 'id': id, 'msg': msg }, 
+		'success': function(data) { alert("S'ha avisat a l'altre jugador"); },
+		'error': function(xhr, status, error) {
+			alert(xhr.responseText);
+		 }
+	});
 }
diff --git a/login.php b/login.php
index 3440136..ee64f27 100644
--- a/login.php
+++ b/login.php
@@ -1,49 +1,56 @@
-<html>
-<head>
-<meta http-equiv="content-type" content="text/html; charset=utf-8">
-<script src="./js/utils.js"></script>
 <?php
-        $servername = "fdb22.awardspace.net";
-        $username = "3155560_users";
-        $password = "btechnoro@fox4news.info";
-        
-        // Create connection
-        $conn = new mysqli($servername, $username, $password, "3155560_users");
-        if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
-        $conn->set_charset("utf8");
-        
-        $id = $_POST['user'];
-        $query = "SELECT * FROM users WHERE id=".$id;
+	class User{
+		public $id;
+		public $nomcomplet;
+		public $curs;
+		public $grau;
+		public $quimata;
+		
+		public function nom() {
+			$noms = explode(" ", $this->nomcomplet);
+			return $noms[0];
+		}
+	}
+	
+	function get_user($id) {
+		$user = new User();
+		
+		// Define MySQL login variables
+		$servername = "fdb22.awardspace.net";
+		$username = "3155560_users";
+		$password = "btechnoro@fox4news.info";
 
-        if ($result = $conn->query($query)) {
-            
-            while ($row = $result->fetch_row()) {
-                echo "<script>let db_info = {'id': ".$row[0];
-                echo ", 'nom':'".$row[1]."', 'curs':".$row[2].", 'grau':".$row[3];
-                echo ", 'quimata':".$row[4].", 'quielmata':".$row[5];
-                echo "}; console.log(db_info);</script>";
-            }
-        
-            $result->close();
-        } else {
-            echo "Wrong query";
-        }
-        
-        // Close connection
-        $conn->close(); 
-?>
-</head>
-<body>
-<?php
-// Rebut l'SQL, l'enviem com a JSON a main.html
-echo '
-<script>
-let infoString = JSON.stringify(db_info);
-let infoBase64 = b64EncodeUnicode(infoString);
+		// Create connection
+		$conn = new mysqli($servername, $username, $password, "3155560_users");
+		if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
+		$conn->set_charset("utf8");
+		
+		// Do the query
+		$query = "SELECT * FROM users WHERE id=".$id;
 
-window.location.href = "./main.html?info=" + infoBase64;
-</script>
-';
+		// Fetch the information of the user
+		if ($result = $conn->query($query)) {
+			while ($row = $result->fetch_row()) {
+				$user->id = $row[0];
+				$user->nomcomplet = $row[1];
+				$user->curs = $row[2];
+				$user->grau = $row[3];
+				$user->quimata = $row[4];
+				$user->quielmata = $row[5];
+				$user->requested = $row[6];
+				$user->mort = $row[7];
+			}
+			$result->close();
+		} else {
+			die("Wrong query: " . $query);
+		}
+
+		// Close connection
+		$conn->close();
+		
+		return $user;
+	}
+	
+	$user = get_user($_GET['user']);
+	$victim = get_user($user->quimata);
 ?>
-</body>
-</html>
diff --git a/main.html b/main.html
deleted file mode 100644
index 567cfd2..0000000
--- a/main.html
+++ /dev/null
@@ -1,33 +0,0 @@
-<html>
-	<head>
-		<meta charset="UTF-8">
-		<title>Pàgina de l'usuari</title>
-		<link rel="stylesheet" href="./css/basic.css" />
-		<link rel="stylesheet" href="./css/main.css" />
-		
-		<script src="./js/utils.js"></script>
-	</head>
-	<body>
-		<div id="outter-container">
-			<div id="inner-container">
-				<h2>Hola <name id="user_name">Sabina</name>,</h2>
-				<h3>La teva víctima és:</h3>
-				
-				<div class="victima">
-					<img width="300px" src="https://images.squarespace-cdn.com/content/v1/55d66363e4b0b9bb40814594/1563604453236-E5W0AMO0G48FCM315P38/ke17ZwdGBToddI8pDm48kOTrVJ1u5-lShfatTnqwpvJ7gQa3H78H3Y0txjaiv_0fDoOvxcdMmMKkDsyUqMSsMWxHk725yiiHCCLfrh8O1z5QHyNOqBUUEtDDsRWrJLTmHHRMqniMJbuwH8EZRFFu5Xniw0XOPZocUpOLtZKAgc3IeAs9gv2T1pupIMoxMYeP/Monica-Leon-Mugshot-Danger-VH1-Star-For-the-Love-of-Ray-J.jpg?format=2500w" />
-					<h2 id="victim_name">Vera Pujadas</h2>
-					<h3><span id="victim_curs">3er</span>-<span id="victim_grau">MAT</span></h3>
-				</div>
-			</div>
-		</div>
-		<div id="butons">
-			<button id="win">L'he matat</button>
-			<button id="lose">M'han matat</button>
-		</div>
-		
-		<script>
-			var info = JSON.parse(b64DecodeUnicode(getUrlParameter("info")));
-			console.log(info);
-		</script>
-	</body>
-</html>
diff --git a/main.php b/main.php
new file mode 100644
index 0000000..4dd8d4a
--- /dev/null
+++ b/main.php
@@ -0,0 +1,46 @@
+<html>
+	<head>
+		<meta charset="UTF-8">
+		<title>Pàgina de l'usuari</title>
+		<link rel="stylesheet" href="./css/basic.css" />
+		<link rel="stylesheet" href="./css/main.css" />
+		
+		<script src="./js/utils.js"></script>
+		<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
+		
+		<?php require 'login.php'; ?>
+		<script>
+			if (<?=$user->mort?>) alert("Venga niño, pitjor que el Condom, MORT.");
+			else {				
+				let dead = false
+				let killed = false
+				if(<?=$user->requested?> == 1) dead = confirm("El teu assassí ha dit que t'ha matat, és veritat?");
+				if(<?=$user->requested?> == 2) killed = confirm("En/na <?=$victim->nom()?> ha dit que l'has matat, és veritat?");
+				if (dead) send_request(<?=$user->id?>, 3);
+				if (killed) send_request(<?=$victim->id?>, 4);
+			}
+		</script>
+	</head>
+	<body>
+		<div id="outter-container">
+			<div id="inner-container">
+				<h2>Hola <name id="user_name"><?=$user->nom()?></name>,</h2>
+				<h3>La teva víctima és:</h3>
+				
+				<div class="victima">
+					<img width="300px" src="./imgs/<?=$victim->id?>.png" />
+					<h2 id="victim_name"><?=$victim->nomcomplet?></h2>
+					<h3><span id="victim_curs"><?=$victim->curs?></span>-<span id="victim_grau"><?=$victim->grau?></span></h3>
+				</div>
+			</div>
+		</div>
+		<div id="butons">
+			<button id="win" onclick="js: send_request(<?=$victim->id?>,1);">L'he matat</button>
+			<button id="lose" onclick="js: send_request(<?=$user->id?>,2);">M'han matat</button>
+		</div>
+		
+		<script>
+			
+		</script>
+	</body>
+</html>
diff --git a/request.php b/request.php
new file mode 100644
index 0000000..39fa91c
--- /dev/null
+++ b/request.php
@@ -0,0 +1,29 @@
+<?php
+	// Define MySQL login variables
+	$servername = "fdb22.awardspace.net";
+	$username = "3155560_users";
+	$password = "btechnoro@fox4news.info";
+
+	// Create connection
+	$conn = new mysqli($servername, $username, $password, "3155560_users");
+	if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
+	$conn->set_charset("utf8");
+	
+	// Do the query
+	$query = "";
+	if ($_GET['msg'] == 1) $query = "UPDATE users SET requested=".$_GET['msg']." WHERE id=".$_GET['id']; // request kill
+	if ($_GET['msg'] == 2) $query = "UPDATE users SET requested=".$_GET['msg']." WHERE quimata=".$_GET['id']; // request dead
+	if ($_GET['msg'] == 3) $query = "UPDATE users SET mort=1 WHERE id=".$_GET['id']; // confirm dead
+	if ($_GET['msg'] == 4) $query = "UPDATE users SET mort=1 WHERE quimata=".$_GET['id']; // confirm killed
+
+	// Fetch the information of the user
+	if ($result = $conn->query($query)) {
+		echo 'Success!';
+		// $result->close();
+	} else {
+		die("Wrong query: " . $query);
+	}
+
+	// Close connection
+	$conn->close();
+?>