implemented class method for login, register, update, delete and implemented readDatabase functionality to game.js. Write funtionality is in preogress
This commit is contained in:
parent
fbb52f76c0
commit
03e5591e0a
13 changed files with 145 additions and 83 deletions
71
Database.php
71
Database.php
|
|
@ -29,4 +29,75 @@ class Database {
|
||||||
public function get() {
|
public function get() {
|
||||||
return $this->statement->fetchAll();
|
return $this->statement->fetchAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -6,7 +6,7 @@ $config = require('config.php');
|
||||||
$db = new Database($config['database'], $username, $password);
|
$db = new Database($config['database'], $username, $password);
|
||||||
|
|
||||||
session_start();
|
session_start();
|
||||||
$db->query("DELETE FROM user WHERE username = :user", ["user" => $_SESSION['username']]);
|
$db->delete($_SESSION['username']);
|
||||||
session_destroy();
|
session_destroy();
|
||||||
header("Location: /");
|
header("Location: /");
|
||||||
exit;
|
exit;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,13 @@
|
||||||
<?php
|
<?php
|
||||||
$heading = "Game";
|
$heading = "Game";
|
||||||
|
$username = 'MatheApp';
|
||||||
|
$password = 'password';
|
||||||
|
$config = require('config.php');
|
||||||
|
$db = new Database($config['database'], $username, $password);
|
||||||
|
session_start();
|
||||||
|
if(!isset($_SESSION["username"])){
|
||||||
|
header("Location: /");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
$stats = $db->getPlayerStats($_SESSION['username']);
|
||||||
require "views/game.view.php";
|
require "views/game.view.php";
|
||||||
|
|
|
||||||
|
|
@ -6,26 +6,7 @@ $config = require('config.php');
|
||||||
$db = new Database($config['database'], $username, $password);
|
$db = new Database($config['database'], $username, $password);
|
||||||
|
|
||||||
if (isset($_POST["submit"])) {
|
if (isset($_POST["submit"])) {
|
||||||
$stmt = $db->query("SELECT * FROM user WHERE username = :user", ['user' => $_POST["username"]])->get();
|
$db->login([$_POST["username"], $_POST["password"]]);
|
||||||
$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!";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
require "views/login.view.php";
|
require "views/login.view.php";
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,9 @@
|
||||||
<?php
|
<?php
|
||||||
$heading = "profile";
|
$heading = "profile";
|
||||||
|
$username = "MatheApp";
|
||||||
|
$password = "password";
|
||||||
|
$config = require('config.php');
|
||||||
|
$db = new Database($config['database'], $username, $password);
|
||||||
|
|
||||||
session_start();
|
session_start();
|
||||||
if(!isset($_SESSION["username"])){
|
if(!isset($_SESSION["username"])){
|
||||||
|
|
@ -7,4 +11,5 @@ if(!isset($_SESSION["username"])){
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$stats = $db->getPlayerStats($_SESSION['username']);
|
||||||
require "views/profile.view.php";
|
require "views/profile.view.php";
|
||||||
|
|
|
||||||
|
|
@ -6,36 +6,8 @@
|
||||||
$config = require('config.php');
|
$config = require('config.php');
|
||||||
$db = new Database($config['database'], $username, $password);
|
$db = new Database($config['database'], $username, $password);
|
||||||
|
|
||||||
if (isset($_POST["submit"])) {
|
if (isset($_POST["submit"])) {
|
||||||
$stmt = $db->query("SELECT * FROM user WHERE username = :user", ['user' => $_POST['Username']])->get();
|
$db->register($_POST);
|
||||||
$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";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
require "views/register.view.php";
|
require "views/register.view.php";
|
||||||
|
|
|
||||||
8
controllers/updateData.php
Normal file
8
controllers/updateData.php
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
$username = 'MatheApp';
|
||||||
|
$password = 'password';
|
||||||
|
$config = require('config.php');
|
||||||
|
$db = new Database($config['database'], $username, $password);
|
||||||
|
$data = json_decode(file_get_contents("php://input"), true);
|
||||||
|
dd($data);
|
||||||
|
$db->update($data);
|
||||||
|
|
@ -9,5 +9,6 @@ return[
|
||||||
'/register' => 'controllers/register.php',
|
'/register' => 'controllers/register.php',
|
||||||
'/profile' => 'controllers/profile.php',
|
'/profile' => 'controllers/profile.php',
|
||||||
'/logout' => 'controllers/logout.php',
|
'/logout' => 'controllers/logout.php',
|
||||||
'/delete' => 'controllers/delete.php'
|
'/delete' => 'controllers/delete.php',
|
||||||
|
'/updateData' => 'controllers/updateData.php'
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,11 @@
|
||||||
(function (w) {
|
(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;
|
let count = 1;
|
||||||
// game title
|
// game title
|
||||||
const gametitle = "The Math Wizard";
|
const gametitle = "The Math Wizard";
|
||||||
|
|
@ -348,9 +355,9 @@
|
||||||
_x: x,
|
_x: x,
|
||||||
_y: y,
|
_y: y,
|
||||||
character: "@",
|
character: "@",
|
||||||
name: "you",
|
name: statsOfPlayer['username'].replace('"',''),
|
||||||
// the player's stats
|
// 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
|
// the ROT.js scheduler calls this method when it is time
|
||||||
// for the player to act
|
// for the player to act
|
||||||
act: () => {
|
act: () => {
|
||||||
|
|
@ -589,13 +596,12 @@
|
||||||
const clicked = await setupButtons(answerValue);
|
const clicked = await setupButtons(answerValue);
|
||||||
let fight = checkSolution(clicked, answerValue);
|
let fight = checkSolution(clicked, answerValue);
|
||||||
if(fight) {
|
if(fight) {
|
||||||
msg.push("You hit the monster");
|
msg.push(`${Game.player.name} hit the monster.`);
|
||||||
hitter.stats.hp -= 1;
|
hitter.stats.hp -= 1;
|
||||||
sfx["hit"].play();
|
sfx["hit"].play();
|
||||||
//Game.player.stats.xp += 1;
|
|
||||||
} else {
|
} else {
|
||||||
sfx["miss"].play();
|
sfx["miss"].play();
|
||||||
msg.push("The monster hit you.");
|
msg.push(`The monster hit ${Game.player.name}.`);
|
||||||
Game.player.stats.hp -= 1;
|
Game.player.stats.hp -= 1;
|
||||||
}
|
}
|
||||||
if(msg) {
|
if(msg) {
|
||||||
|
|
@ -727,6 +733,28 @@
|
||||||
function setEndScreenValues(xp, gold) {
|
function setEndScreenValues(xp, gold) {
|
||||||
$$(".xp-stat").forEach((el) => (el.textContent = Math.floor(xp)));
|
$$(".xp-stat").forEach((el) => (el.textContent = Math.floor(xp)));
|
||||||
$$(".gold-stat").forEach((el) => (el.textContent = gold));
|
$$(".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
|
// updates the stats listed at the bottom of the screen
|
||||||
|
|
|
||||||
|
|
@ -262,7 +262,7 @@ a:hover {
|
||||||
-ms-interpolation-mode: nearest-neighbor;
|
-ms-interpolation-mode: nearest-neighbor;
|
||||||
image-rendering: pixelated;
|
image-rendering: pixelated;
|
||||||
transform: scale(8);
|
transform: scale(8);
|
||||||
background-image: url("colored_tilemap_packed.png");
|
background-image: url("./../images/16x16DungeonTileset.png");
|
||||||
margin: 80px auto;
|
margin: 80px auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
<link href="./../styles/game.css" rel="stylesheet" id="style">
|
<link href="./../styles/game.css" rel="stylesheet" id="style">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<span class="metadata" id="playerStats" style="display:none"><?php echo json_encode($stats) ?></span>
|
||||||
<!-- boilerplate splash -->
|
<!-- boilerplate splash -->
|
||||||
<div class="screen" id="plate">
|
<div class="screen" id="plate">
|
||||||
<div>
|
<div>
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,3 @@
|
||||||
<div class="isolate bg-white">
|
|
||||||
<div class="absolute inset-x-0 top-[-10rem] -z-10 transform-gpu overflow-hidden blur-3xl sm:top-[-20rem]">
|
|
||||||
<svg class="relative left-[calc(50%-11rem)] -z-10 h-[21.1875rem] max-w-none -translate-x-1/2 rotate-[30deg] sm:left-[calc(50%-30rem)] sm:h-[42.375rem]" viewBox="0 0 1155 678" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
||||||
<path fill="url(#45de2b6b-92d5-4d68-a6a0-9b9b2abad533)" fill-opacity=".3" d="M317.219 518.975L203.852 678 0 438.341l317.219 80.634 204.172-286.402c1.307 132.337 45.083 346.658 209.733 145.248C936.936 126.058 882.053-94.234 1031.02 41.331c119.18 108.451 130.68 295.337 121.53 375.223L855 299l21.173 362.054-558.954-142.079z" />
|
|
||||||
<defs>
|
|
||||||
<linearGradient id="45de2b6b-92d5-4d68-a6a0-9b9b2abad533" x1="1155.49" x2="-78.208" y1=".177" y2="474.645" gradientUnits="userSpaceOnUse">
|
|
||||||
<stop stop-color="#9089FC"></stop>
|
|
||||||
<stop offset="1" stop-color="#FF80B5"></stop>
|
|
||||||
</linearGradient>
|
|
||||||
</defs>
|
|
||||||
</svg>
|
|
||||||
</div>
|
|
||||||
<div class="px-6 pt-6 lg:px-8 mb-5">
|
<div class="px-6 pt-6 lg:px-8 mb-5">
|
||||||
<div>
|
<div>
|
||||||
<nav class="flex h-9 items-center justify-between" aria-label="Global">
|
<nav class="flex h-9 items-center justify-between" aria-label="Global">
|
||||||
|
|
@ -35,4 +23,3 @@
|
||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
<section class="message -left flex space-x-10">
|
<section class="message -left flex space-x-10">
|
||||||
<i class="nes-bcrikko mr-10"></i>
|
<i class="nes-bcrikko mr-10"></i>
|
||||||
<div class="nes-balloon from-left mx-10">
|
<div class="nes-balloon from-left mx-10">
|
||||||
<p>Hallo <?= $_SESSION['username'] ?> schön das du da bist.</p>
|
<p>Hallo <?= $stats['username'] ?> schön das du da bist.</p>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
|
|
@ -23,20 +23,19 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="nes-container with-title mt-10 space-y-4">
|
<div class="nes-container with-title mt-10 space-y-4">
|
||||||
<span class="title">Dein Fortschritt <?php $_SESSION['xp'] ?> </span>
|
<span class="title">Dein Fortschritt</span>
|
||||||
|
<span>Du bist aktuell Level: <?php echo $stats['level'] ?> </span>
|
||||||
<span>Du bist aktuell Level: <?php echo $_SESSION['level'] ?> </span>
|
|
||||||
<br>
|
<br>
|
||||||
<span>Deine Erfahrungspunkte</span>
|
<span>Deine Erfahrungspunkte</span>
|
||||||
<progress class="nes-progress is-primary" value="<?php echo $_SESSION['xp'] ?>" max="150">Erfahrung</progress>
|
<progress class="nes-progress is-primary" value="<?php echo $stats['xp'] ?>" max="150">Erfahrung</progress>
|
||||||
<span>Dein aktueller Lernfortschritt</span>
|
<span>Dein aktueller Lernfortschritt</span>
|
||||||
<progress class="nes-progress is-success" value="<?php echo $_SESSION['lesson_count'] ?>" max="10">Absolvierte Lektionen</progress>
|
<progress class="nes-progress is-success" value="<?php echo $stats['lesson_count'] ?>" max="10">Absolvierte Lektionen</progress>
|
||||||
<div class="flex flex-rows justify-center">
|
<div class="flex flex-rows justify-center">
|
||||||
<div>
|
<div>
|
||||||
<span><i class="nes-icon coin is-medium"></i></span>
|
<span><i class="nes-icon coin is-medium"></i></span>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<p><?php echo $_SESSION['coins'] ?></p>
|
<p><?php echo $stats['coins'] ?></p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue