diff --git a/Database.php b/Database.php index 8f1a1e6..bcf9fd4 100644 --- a/Database.php +++ b/Database.php @@ -29,4 +29,75 @@ class Database { public function get() { return $this->statement->fetchAll(); } -} \ No newline at end of file + + public function delete($params) { + $this->query("DELETE FROM user WHERE username = :user", ["user" => $params]); + } + + public function update($params = []) { + dd($params); + $this->query("UPDATE user SET level= :level, xp= :xp, coins = :coins WHERE username = :user", [ + "level" => $params[1], + "xp" => $params[3], + "coins" => $params[4], + "user" => $params[0], + ]); + } + + public function getPlayerStats ($params) { + return $this->query( + "SELECT username, level, lesson_count, xp, coins FROM user WHERE username= :user", + ["user" => $params])->get()[0]; + } + + public function login($params = []) { + $stmt = $this->query("SELECT * FROM user WHERE username = :user", ['user' => $params[0]])->get(); + $count = sizeof($stmt); + if ($count == 1) { + $stmt = $stmt[0];; + + if (password_verify($params[1], $stmt["password"])) { + session_start(); + $_SESSION["username"] = $stmt['username']; + + header("Location: /profile"); + } else { + echo "Anmeldung fehlgeschlagen!"; + } + } else { + echo "Anmeldung fehlgeschlagen!"; + } + } + + public function register($params = []) { + $stmt = $this->query("SELECT * FROM user WHERE username = :user", ['user' => $params['Username']])->get(); + $count = sizeof($stmt); + if($count == 0 && Validator::string($params['Username'], 1, 255)){ + $checkEmail = $this->query("SELECT * FROM user WHERE email = :email", ['email' => $params['Email-Adresse']])->find(); + if(!$checkEmail && Validator::string($params['Email-Adresse'], 1, 255)) { + if($params["Passwort"] == $params["pw2"] && Validator::string($params['Passwort'], 8, 255)) { + //Username ist frei + //User anlegen + $hash = password_hash($params["Passwort"], PASSWORD_BCRYPT); + $this->query("INSERT INTO user (username, vorname, nachname, email, password, lesson_count, level, xp, coins) VALUES ( + :username, :vorname, :nachname, :email, :password, :lesson_count, :level, :xp, :coins )", [ + 'username' => $params['Username'], + 'vorname' => $params['Vorname'], + 'nachname' => $params['Nachname'], + 'email' => $params['Email-Adresse'], + 'password' => $hash, + 'lesson_count' => 0, + 'level' => 1, + 'xp' => 0, + 'coins' => 0 + ]); + header("Location: /login"); + } else { + echo "Die Passwörter stimmen nicht überein"; + } + } else { + echo "Der Username ist bereits vergeben"; + } +} + } +} diff --git a/controllers/delete.php b/controllers/delete.php index b138c79..03ba3bb 100644 --- a/controllers/delete.php +++ b/controllers/delete.php @@ -6,7 +6,7 @@ $config = require('config.php'); $db = new Database($config['database'], $username, $password); session_start(); -$db->query("DELETE FROM user WHERE username = :user", ["user" => $_SESSION['username']]); +$db->delete($_SESSION['username']); session_destroy(); header("Location: /"); exit; diff --git a/controllers/game.php b/controllers/game.php index 82a554a..fc2d801 100644 --- a/controllers/game.php +++ b/controllers/game.php @@ -1,3 +1,13 @@ getPlayerStats($_SESSION['username']); + require "views/game.view.php"; diff --git a/controllers/login.php b/controllers/login.php index 9ad4c88..30782db 100644 --- a/controllers/login.php +++ b/controllers/login.php @@ -6,26 +6,7 @@ $config = require('config.php'); $db = new Database($config['database'], $username, $password); if (isset($_POST["submit"])) { - $stmt = $db->query("SELECT * FROM user WHERE username = :user", ['user' => $_POST["username"]])->get(); - $count = sizeof($stmt); - if ($count == 1) { - $stmt = $stmt[0];; - - if (password_verify($_POST['password'], $stmt["password"])) { - session_start(); - $_SESSION["username"] = $stmt['username']; - $_SESSION["level"] = $stmt['level']; - $_SESSION["xp"] = $stmt['xp']; - $_SESSION["coins"] = $stmt['coins']; - $_SESSION["lesson_count"] = $stmt['lesson_count']; - - header("Location: /profile"); - } else { - echo "Anmeldung fehlgeschlagen!"; - } - } else { - echo "Anmeldung fehlgeschlagen!"; - } + $db->login([$_POST["username"], $_POST["password"]]); } require "views/login.view.php"; diff --git a/controllers/profile.php b/controllers/profile.php index 39c52be..816befe 100644 --- a/controllers/profile.php +++ b/controllers/profile.php @@ -1,5 +1,9 @@ getPlayerStats($_SESSION['username']); require "views/profile.view.php"; diff --git a/controllers/register.php b/controllers/register.php index 082a33b..f2ab550 100644 --- a/controllers/register.php +++ b/controllers/register.php @@ -6,36 +6,8 @@ $config = require('config.php'); $db = new Database($config['database'], $username, $password); - if (isset($_POST["submit"])) { - $stmt = $db->query("SELECT * FROM user WHERE username = :user", ['user' => $_POST['Username']])->get(); - $count = sizeof($stmt); - if($count == 0 && Validator::string($_POST['Username'], 1, 255)){ - $checkEmail = $db->query("SELECT * FROM user WHERE email = :email", ['email' => $_POST['Email-Adresse']])->find(); - if(!$checkEmail && Validator::string($_POST['Email-Adresse'], 1, 255)) { - if($_POST["Passwort"] == $_POST["pw2"] && Validator::string($_POST['Passwort'], 8, 255)) { - //Username ist frei - //User anlegen - $hash = password_hash($_POST["Passwort"], PASSWORD_BCRYPT); - $db->query("INSERT INTO user (username, vorname, nachname, email, password, lesson_count, level, xp, coins) VALUES ( - :username, :vorname, :nachname, :email, :password, :lesson_count, :level, :xp, :coins )", [ - 'username' => $_POST['Username'], - 'vorname' => $_POST['Vorname'], - 'nachname' => $_POST['Nachname'], - 'email' => $_POST['Email-Adresse'], - 'password' => $hash, - 'lesson_count' => 0, - 'level' => 1, - 'xp' => 0, - 'coins' => 0 - ]); - header("Location: /login"); - } else { - echo "Die Passwörter stimmen nicht überein"; - } - } else { - echo "Der Username ist bereits vergeben"; - } -} +if (isset($_POST["submit"])) { + $db->register($_POST); } require "views/register.view.php"; diff --git a/controllers/updateData.php b/controllers/updateData.php new file mode 100644 index 0000000..8c88660 --- /dev/null +++ b/controllers/updateData.php @@ -0,0 +1,8 @@ +update($data); diff --git a/routes.php b/routes.php index 4bce1e5..7f75a56 100644 --- a/routes.php +++ b/routes.php @@ -9,5 +9,6 @@ return[ '/register' => 'controllers/register.php', '/profile' => 'controllers/profile.php', '/logout' => 'controllers/logout.php', - '/delete' => 'controllers/delete.php' + '/delete' => 'controllers/delete.php', + '/updateData' => 'controllers/updateData.php' ]; diff --git a/scripts/game.js b/scripts/game.js index 1ff9aef..725eb4f 100644 --- a/scripts/game.js +++ b/scripts/game.js @@ -1,4 +1,11 @@ (function (w) { + var playerStats = document.getElementById("playerStats").innerHTML; + playerStats = playerStats.replace("'","").replace("{","").replace("}", "").split(","); + statsOfPlayer = {}; + for (var i = 0; i < playerStats.length; i++) { + var parts = playerStats[i].split(":") + statsOfPlayer[parts[0].trim().replace("'","").replace('"','').replace('"','')] = parts[1].trim().replace("'",""); + } let count = 1; // game title const gametitle = "The Math Wizard"; @@ -348,9 +355,9 @@ _x: x, _y: y, character: "@", - name: "you", + name: statsOfPlayer['username'].replace('"',''), // the player's stats - stats: { hp: 10, xp: 0, gold: 0 }, + stats: { hp: 10, xp: Number(statsOfPlayer['xp']), gold: Number(statsOfPlayer['coins']) }, // the ROT.js scheduler calls this method when it is time // for the player to act act: () => { @@ -589,13 +596,12 @@ const clicked = await setupButtons(answerValue); let fight = checkSolution(clicked, answerValue); if(fight) { - msg.push("You hit the monster"); + msg.push(`${Game.player.name} hit the monster.`); hitter.stats.hp -= 1; sfx["hit"].play(); - //Game.player.stats.xp += 1; } else { sfx["miss"].play(); - msg.push("The monster hit you."); + msg.push(`The monster hit ${Game.player.name}.`); Game.player.stats.hp -= 1; } if(msg) { @@ -727,6 +733,28 @@ function setEndScreenValues(xp, gold) { $$(".xp-stat").forEach((el) => (el.textContent = Math.floor(xp))); $$(".gold-stat").forEach((el) => (el.textContent = gold)); + statsOfPlayer["coins"] = gold; + statsOfPlayer["username"] = Game.player.name.trim().replace('"',''); + statsOfPlayer["lesson_count"] = Number(statsOfPlayer["lesson_count"]); + if (xp > 150) { + statsOfPlayer["level"] = Number(statsOfPlayer["level"]) + 1; + xp -= 150; + statsOfPlayer["xp"] = xp; + } else { + statsOfPlayer["level"] = Number(statsOfPlayer["level"]); + statsOfPlayer["xp"] = xp; + } + var json = JSON.stringify(statsOfPlayer); + var xhr = new XMLHttpRequest(); + xhr.open("POST", "/updateData", true); + xhr.setRequestHeader("Content-Type", "application/json"); + xhr.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + console.log(this.responseText); + } + } + console.log(json); + xhr.send(json); } // updates the stats listed at the bottom of the screen diff --git a/styles/game.css b/styles/game.css index 277b15e..99d51b2 100644 --- a/styles/game.css +++ b/styles/game.css @@ -262,7 +262,7 @@ a:hover { -ms-interpolation-mode: nearest-neighbor; image-rendering: pixelated; transform: scale(8); - background-image: url("colored_tilemap_packed.png"); + background-image: url("./../images/16x16DungeonTileset.png"); margin: 80px auto; } diff --git a/views/game.view.php b/views/game.view.php index 90f0751..f88a5f0 100644 --- a/views/game.view.php +++ b/views/game.view.php @@ -14,7 +14,7 @@
- +