Merge branch 'master' into rankingcss
diff --git a/admin/createtable.php b/admin/createtable.php
index 375225b..57e1802 100644
--- a/admin/createtable.php
+++ b/admin/createtable.php
@@ -22,9 +22,9 @@
 	  `assassi` int(11) NOT NULL,
 	  `curs` tinyint(1) NOT NULL,
 	  `grau` tinyint(1) NOT NULL,
-	  `data` date NOT NULL DEFAULT current_timestamp()
+	  `data` timestamp NOT NULL DEFAULT current_timestamp()
 	)"];
 	
-	foreach ($queries as $query) if (!query($query)) die('An error ocurred.');
+	foreach ($queries as $query) if (!query($query)) die('An error ocurred. ' . $query);
 	die("<script>window.location.href = './index.php?dbname=$dbname'</script>");
 ?>
diff --git a/admin/insert.php b/admin/insert.php
index 6f793a1..5099bc7 100644
--- a/admin/insert.php
+++ b/admin/insert.php
@@ -8,17 +8,19 @@
 	// Read from CSV
 	$inscrits = array_map('str_getcsv', file($csvname));
 	array_shift($inscrits); // remove header
+	shuffle($inscrits); // shuffle randomly
 	
 	$start = 1;
 	$i = $start;
 	foreach ($inscrits as $user) {
 		$i = ($i + 1) % ($start + count($inscrits));
 		if ($i == 0) $i = $i + 1;
-		$template = "INSERT INTO `".$dbname."` (`id`, `nom`, `curs`, `grau`, `quimata`, `requested`, `mort`, `password`, `bits`)" .
-					" VALUES (NULL, '".$user[0]."', '".$user[1]."', '".$user[2]."', ".$i.", 0, 0, '', ".(int)rand(1,512).")";
+		// Input values into SQL values
+		$template = "INSERT INTO `$dbname` (`id`, `nom`, `curs`, `grau`, `quimata`, `requested`, `mort`, `password`, `bits`)" .
+					" VALUES (NULL, '$user[0]', '$user[1]', '$user[2]', $i, 0, 0, '', ".(int)rand(1,512).")";
 
-		if (!query($template)) die("An error ocurred.");
+		if (!query($template)) die("An error ocurred." . $template);
 	}
 	
-	die("<script>window.location.href = './index.php'</script>");
+	die("<script>window.location.href = './index.php?successinserting'</script>");
 ?>
diff --git a/dead.php b/dead.php
index e65b073..1ef1217 100644
--- a/dead.php
+++ b/dead.php
@@ -25,6 +25,7 @@
 				<h1>Estàs mort/a!</h1>
 				<p>Torna a la pàgina principal.</p>
 				<a href="./index.php">Go back</a>
+				<a href="./ranking.php">Anar al rànquing</a>
 			</div>
 		</div>
 	</body>
diff --git a/js/utils.js b/js/utils.js
index cc6e2a6..315cf19 100644
--- a/js/utils.js
+++ b/js/utils.js
@@ -34,7 +34,7 @@
 	
 	if (info.requested) {
 		// Check for requests
-		if(info.requested) dead = confirm("El teu assassí ha dit que t'ha matat, és veritat?");
+		if (info.requested) dead = confirm("El teu assassí ha dit que t'ha matat, és veritat?");
 		
 		// Confirm/deny request
 		if (dead) send_request(user, "CONF DEAD"); // confirm death
@@ -55,16 +55,16 @@
 			let info = JSON.parse(response);
 			
 			// Check if user is dead
-			if (!user.mort) user.mort = check_requests(info, user);
+			if (!user.mort && !info.mort) user.mort = check_requests(info, user);
+			else window.location.href = "./dead.php";
 			
 			// Check if there has been a change of victim					
 			if (info.quimata != user.quimata) {
-				if (!user.mort) change_victim(info);
-				else window.location.href = "./dead.php";
+				change_victim(info);
 				user.quimata = info.quimata;
 			}
 			
-			console.log(response);
+			console.log("Response: " + response);
 	}});	
 }
 
diff --git a/main.php b/main.php
index 6197cae..c6e9ee0 100644
--- a/main.php
+++ b/main.php
@@ -2,18 +2,22 @@
 	require './credentials.php';
 	require './php/utils.php';
 	
+	$credentials = new Credentials();
+	$usersdb = $credentials->usersdb;
+	$mortsdb = $credentials->mortsdb;
+	
 	$user = (int)$_POST['user'];
 	$password = isset($_POST['password']) ? md5($_POST['password']) : '';
 
 		// Check if password is correct
-		$query_password = "SELECT password FROM users WHERE id=".$user;
+		$query_password = "SELECT password FROM $usersdb WHERE id=$user";
 		$real_password = query($query_password)->fetch_row()[0];
 		if ($real_password != $password) die("<script>window.location.href = './index.php?wrongpassword=1'</script>");
 
 	if (!isset($_POST['user']) or $_POST['user'] == '') {
 		die("<script>window.location.href = './index.php'</script>");
 	} else if (isset($_POST['password'])) {
-		$query_password = "SELECT password FROM users WHERE id=$user";
+		$query_password = "SELECT password FROM $usersdb WHERE id=$user";
 		if (query($query_password)->fetch_row()[0] != $password) {
 			// Unset variables
 			setcookie('user', '', -1, "/");
diff --git a/php/change_password.php b/php/change_password.php
index d02f389..d8bcc19 100644
--- a/php/change_password.php
+++ b/php/change_password.php
@@ -2,12 +2,16 @@
 	require '../credentials.php';
 	require 'utils.php';
 
+	$credentials = new Credentials();
+	$usersdb = $credentials->usersdb;
+	$mortsdb = $credentials->mortsdb;
+
 	// Check if confirmation is the same
 	if ($_POST['password'] != $_POST['confirmation']) {
 		die("<script>window.location.href = '../index.php?wrongconfirmation=1'</script>");
 	} else {
 		// Execute query to change password
-		$update_password = "UPDATE users SET password=\"".md5($_POST['password'])."\" WHERE id=".$_POST['userid'];
+		$update_password = "UPDATE $usersdb SET password=\"".md5($_POST['password'])."\" WHERE id=".$_POST['userid'];
 		if(!$result = query($update_password)) die("<script>window.location.href = '../index.php?errordb=1'</script>");
 		
 		// Save 'password' to cookies
diff --git a/php/login.php b/php/login.php
index abaac74..45575e3 100644
--- a/php/login.php
+++ b/php/login.php
@@ -1,7 +1,11 @@
 <?php
 	require '../credentials.php';
 	require 'utils.php';
-	
+
+	$credentials = new Credentials();
+	$usersdb = $credentials->usersdb;
+	$mortsdb = $credentials->mortsdb;
+
 	// Set the 'user' POST and COOKIE variable
 	$user = '';
 	if (isset($_POST['user'])) $user = $_POST['user'];
@@ -11,7 +15,7 @@
 	}
 	
 	// Check if password is correct
-	$query_password = "SELECT password FROM users WHERE id=".$user;
+	$query_password = "SELECT password FROM $usersdb WHERE id=".$user;
 	$real_password = query($query_password)->fetch_row()[0];
 	
 	// Prioritize input rather than memory
diff --git a/php/request.php b/php/request.php
index 845cdb1..ac2c8cf 100644
--- a/php/request.php
+++ b/php/request.php
@@ -2,17 +2,21 @@
 	require '../credentials.php';
 	require 'utils.php';
 
+	$credentials = new Credentials();
+	$usersdb = $credentials->usersdb;
+	$mortsdb = $credentials->mortsdb;
+
 	// Do the query
 	$queries = [""];
-	$victimid = "ANY (SELECT quimata FROM (SELECT * FROM users) AS victims WHERE id=".(int)$_POST['user_id'].")";
+	$victimid = "ANY (SELECT quimata FROM (SELECT * FROM $usersdb) AS victims WHERE id=".(int)$_POST['user_id'].")";
 	
-	if ($_POST['msg'] == "REQ KILL") $queries = ["UPDATE users SET requested=1 WHERE id=".$victimid]; 						// request kill
-	if ($_POST['msg'] == "REQ DEAD") $queries = ["UPDATE users SET requested=2 WHERE quimata=".(int)$_POST['user_id']]; 	// request dead
-	if ($_POST['msg'] == "DENY REQ") $queries = ["UPDATE users SET requested=0 WHERE id=".(int)$_POST['user_id']]; 			// deny request
+	if ($_POST['msg'] == "REQ KILL") $queries = ["UPDATE $usersdb SET requested=1 WHERE id=".$victimid]; 						// request kill
+	if ($_POST['msg'] == "REQ DEAD") $queries = ["UPDATE $usersdb SET requested=2 WHERE quimata=".(int)$_POST['user_id']]; 	// request dead
+	if ($_POST['msg'] == "DENY REQ") $queries = ["UPDATE $usersdb SET requested=0 WHERE id=".(int)$_POST['user_id']]; 			// deny request
 	if ($_POST['msg'] == "CONF DEAD") {
-		$queries = ["INSERT INTO morts (id, quimatava, assassi, curs, grau) (SELECT id, quimata, (SELECT id FROM users WHERE quimata=".(int)$_POST['user_id']."), curs, grau FROM users WHERE id=".(int)$_POST['user_id'].")", 	// add to 'morts'
-					"UPDATE users SET requested=0, quimata=".(int)$_POST['user_quimata'].", bits=".(int)rand(1,512)." WHERE quimata=".(int)$_POST['user_id'], 	// assign new victim to killer
-					"UPDATE users SET quimata=0, mort=1 WHERE id=".(int)$_POST['user_id']];		// confirm victim dead/killed										
+		$queries = ["INSERT INTO $mortsdb (id, quimatava, assassi, curs, grau) (SELECT id, quimata, (SELECT id FROM $usersdb WHERE quimata=".(int)$_POST['user_id']."), curs, grau FROM $usersdb WHERE id=".(int)$_POST['user_id'].")", 	// add to 'morts'
+					"UPDATE $usersdb SET requested=0, quimata=".(int)$_POST['user_quimata'].", bits=".(int)rand(1,512)." WHERE quimata=".(int)$_POST['user_id'], 	// assign new victim to killer
+					"UPDATE $usersdb SET quimata=0, mort=1 WHERE id=".(int)$_POST['user_id']];		// confirm victim dead/killed										
 	}
 	// Fetch the information of the user
 	foreach ($queries as $query) {
diff --git a/php/utils.php b/php/utils.php
index abd519e..49ccbb7 100644
--- a/php/utils.php
+++ b/php/utils.php
@@ -1,4 +1,5 @@
 <?php
+
 	function nomcurs($curs) {
 		if ($curs == 1) return "1r";
 		if ($curs == 2) return "2n";
@@ -53,8 +54,12 @@
 	function get_users($id = 0, $getAsObjects = true) {
 		$users = [];
 		
+		$credentials = new Credentials();
+		$usersdb = $credentials->usersdb;
+		$mortsdb = $credentials->mortsdb;
+		
 		// Prepare the query
-		$query = "SELECT * FROM users";
+		$query = "SELECT * FROM $usersdb";
 		if ($id > 0) $query .= " WHERE id=".$id;
 
 		// Fetch the information of the user
diff --git a/ranking.php b/ranking.php
index 3d90f1d..fb0aef3 100644
--- a/ranking.php
+++ b/ranking.php
@@ -10,21 +10,21 @@
 <?php
 	require './credentials.php';
 	require './php/utils.php';
-
-	// $getranking = "SELECT morts.assassi AS id, users.grau, users.curs, count(morts.assassi) AS kills FROM morts INNER JOIN users ON morts.assassi = users.id GROUP BY morts.assassi";
-	// $getranking = "SELECT morts.assassi AS id, users.grau, users.curs, count(morts.assassi) AS kills, sum(CASE WHEN morts.grau = users.grau THEN 1 ELSE 2 END) AS kills_pondered FROM morts INNER JOIN users ON morts.assassi = users.id GROUP BY morts.assassi ORDER BY kills_pondered DESC";
-	// Mateix grau i curs => 1, Mateix grau diferent curs => 2, Diferent grau => 3
-	// $getranking = "SELECT morts.assassi AS id, users.grau, users.curs, count(morts.assassi) AS kills, sum(CASE WHEN morts.grau = users.grau AND morts.curs = users.curs THEN 1 WHEN morts.grau = users.grau AND morts.curs != users.curs THEN 2 ELSE 3 END) AS kills_pondered FROM morts INNER JOIN users ON morts.assassi = users.id GROUP BY morts.assassi ORDER BY kills_pondered DESC";
-
+	
+	$credentials = new Credentials();
+	$usersdb = $credentials->usersdb;
+	$mortsdb = $credentials->mortsdb;
+	
 	// Mateix grau i curs => 100, Mateix grau diferent curs => 150, Diferent grau => 200
-	$getranking = "SELECT morts.assassi AS id, users.nom, users.grau, users.curs, count(morts.assassi) AS kills,
-					sum(morts.grau = users.grau AND morts.curs = users.curs) AS companys_classe,
-					sum(morts.grau = users.grau AND morts.curs != users.curs) AS companys_grau,
-					sum(morts.grau != users.grau) AS companys_facu,
-					sum(CASE WHEN morts.grau = users.grau AND morts.curs = users.curs THEN 100 WHEN morts.grau = users.grau AND morts.curs != users.curs THEN 150 ELSE 200 END) AS score
-					FROM morts INNER JOIN users ON morts.assassi = users.id GROUP BY morts.assassi ORDER BY score DESC";
-
+	$getranking = "SELECT $mortsdb.assassi AS id, $usersdb.nom, $usersdb.grau, $usersdb.curs, count($mortsdb.assassi) AS kills,
+					sum($mortsdb.grau = $usersdb.grau AND $mortsdb.curs = $usersdb.curs) AS companys_classe,
+					sum($mortsdb.grau = $usersdb.grau AND $mortsdb.curs != $usersdb.curs) AS companys_grau,
+					sum($mortsdb.grau != $usersdb.grau) AS companys_facu,
+					sum(CASE WHEN $mortsdb.grau = $usersdb.grau AND $mortsdb.curs = $usersdb.curs THEN 100 WHEN $mortsdb.grau = $usersdb.grau AND $mortsdb.curs != $usersdb.curs THEN 150 ELSE 200 END) AS score
+					FROM $mortsdb INNER JOIN $usersdb ON $mortsdb.assassi = $usersdb.id GROUP BY $mortsdb.assassi ORDER BY score DESC";
+	
 	$results = query($getranking);
+	// die($getranking);
 ?>
 
 <div id="outter-container">