diff --git a/application/models/Model_database.php b/application/models/Model_database.php index 8d23e7b..58a5680 100644 --- a/application/models/Model_database.php +++ b/application/models/Model_database.php @@ -51,7 +51,7 @@ protected function setTable($table) { $this->table = $table; } protected function setConnection() { - $this->connection = new DatabaseConnection('localhost', null, 'marvel', 'securepass', 'marvel_heroes'); + $this->connection = new DatabaseConnection('localhost', 3306, 'root', '', 'marvel_heroes'); } } diff --git a/assets/css/board.css b/assets/css/board.css index 7c5b5ed..c1fc6d5 100644 --- a/assets/css/board.css +++ b/assets/css/board.css @@ -10,17 +10,22 @@ main { .entity_card { width: 140px; height: 190px; - z-index: 1; display: flex; margin-left: 20px; } +.enemyUsedCards { + z-index: 2; +} +.playerUsedCards { + z-index: 3; +} .entity_card:first-child { margin-left: 0; } .entity_card img { width: 100%; height: 100%; - z-index: 1; + z-index: 3; } .entity_card span#card_currentAttack { color: white; @@ -29,7 +34,7 @@ main { position: absolute; margin-top: 158px; margin-left: 16px; - z-index: 2; + z-index: 4; } .entity_card span#card_currentDefense { color: white; @@ -38,7 +43,7 @@ main { position: absolute; margin-top: 158px; margin-left: 115px; - z-index: 2; + z-index: 4; } .cardCanAttack { animation: cardGlow 2s infinite; diff --git a/server/GameSession.php b/server/GameSession.php index 326be27..07a8ae3 100644 --- a/server/GameSession.php +++ b/server/GameSession.php @@ -1,7 +1,7 @@ connection = new PDO("mysql:host=$host;dbname=$database", $username, $password); + $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } catch(PDOException $e) { + echo "::Connection failed: ".$e->getMessage().'\n'; + } + } + public function __destruct() { + $this->connection = null; + } + public function getConnectionStatus() + { + if(isset($this->connection)) + return true; + return false; + } +} + +abstract class Model_database +{ + public $connection; + public $table; + + abstract public function findUsername($username); + abstract public function findEmail($email); + abstract public function save(); + abstract public function delete(); + abstract public function checkPass($username, $password); + abstract public function renew(); + abstract public function update($username, $name, $email, $password); + abstract public function getAvatar($username); + abstract public function getTotalGames($username); + abstract public function getTotalWins($username); + abstract public function getTotalLoses($username); + abstract public function incrementTotalGames($username); + abstract public function incrementTotalWins($username); + abstract public function incrementTotalLoses($username); + abstract public function updateAvatar($username, $avatar_name); + + public function __construct($table) { + $this->setConnection(); + $this->setTable($table); + } + + protected function setTable($table) { + $this->table = $table; + } + protected function setConnection() { + $this->connection = new DatabaseConnection('localhost', 3306, 'root', '', 'marvel_heroes'); + } +} + +?> \ No newline at end of file diff --git a/server/controller_database.php b/server/controller_database.php new file mode 100644 index 0000000..9279fa9 --- /dev/null +++ b/server/controller_database.php @@ -0,0 +1,194 @@ +connection = null; + } + + public function findUsername($username) + { + if ($this->connection->getConnectionStatus()) { + $result = $this->connection->connection->query("SELECT * FROM $this->table WHERE username='$username'"); + $pdo = $result->fetch(PDO::FETCH_ASSOC); + if ($pdo) { + if($username == $pdo["username"]) + return true; + else return false; + } + } + } + public function findEmail($email) + { + if ($this->connection->getConnectionStatus()) { + $result = $this->connection->connection->query("SELECT * FROM $this->table WHERE email='$email'"); + $pdo = $result->fetch(PDO::FETCH_ASSOC); + if ($pdo) { + if($email == $pdo["email"]) + return true; + else return false; + } + } + } + public function delete() + { + if ($this->connection->getConnectionStatus()) { + $result = $this->connection->connection->query("SELECT id FROM " . $this->table . " WHERE id = " . $this->id . ";"); + $pdo = $result->fetch(PDO::FETCH_ASSOC); + if ($pdo["id"]) { + $sql = "DELETE FROM $this->table WHERE id=$this->id"; + $stmt = $this->connection->connection->prepare($sql); + $stmt->execute(); + $this->id = null; + $this->username = null; + $this->password = null; + $this->name = null; + $this->email = null; + $this->avatar_name = null; + } + } + } + public function save() + { + if ($this->connection->getConnectionStatus()) { + $sql = "INSERT INTO `users` (username, name, email, password, avatar_name) VALUES (:username, :name, :email, :password, :avatar_name)"; + $stmt = $this->connection->connection->prepare($sql); + $stmt->bindParam(":username", $this->username); + $stmt->bindParam(":name", $this->name); + $stmt->bindParam(":email", $this->email); + $stmt->bindParam(":password", $this->password); + $stmt->bindParam(":avatar_name", $this->avatar_name); + $stmt->execute(); + } + } + public function renew() + { + if ($this->connection->getConnectionStatus()) { + $result = $this->connection->connection->query("SELECT * FROM $this->table WHERE username='$this->username'"); + $pdo = $result->fetch(PDO::FETCH_ASSOC); + $email = $pdo['email']; + $pass = substr($pdo['password'], 0, 9); + $passHash = md5($pass); + $text = "\nYou have requested a renewal of your password.\nDo not give your password to anyone!\n\nYour new password is $pass\n\nDon't lose it anymore;)\n"; + mail($email, "Password reminder.", $text); + $this->update($pdo['username'], $pdo['name'], $pdo['email'], $passHash); + } + } + public function update($username, $name, $email, $password) + { + if ($this->connection->getConnectionStatus()) { + $sql = "UPDATE users SET name=:name, email=:email, password=:password WHERE username=:username"; + $stmt = $this->connection->connection->prepare($sql); + $stmt->bindParam(":username", $username); + $stmt->bindParam(":name", $name); + $stmt->bindParam(":email", $email); + $stmt->bindParam(":password", $password); + $stmt->execute(); + } + } + public function checkPass($username, $password) + { + if ($this->connection->getConnectionStatus()) { + $result = $this->connection->connection->query("SELECT * FROM $this->table WHERE username='$username'"); + $pdo = $result->fetch(PDO::FETCH_ASSOC); + if($pdo['password'] == $password) + return true; + else + return false; + } + } + public function getAvatar($username) { + if ($this->connection->getConnectionStatus()) { + $result = $this->connection->connection->query("SELECT avatar_name FROM $this->table WHERE username='$username'"); + $pdo = $result->fetch(PDO::FETCH_ASSOC); + return $pdo['avatar_name']; + } + } + public function getTotalGames($username) { + if ($this->connection->getConnectionStatus()) { + $result = $this->connection->connection->query("SELECT total_games FROM $this->table WHERE username='$username'"); + $pdo = $result->fetch(PDO::FETCH_ASSOC); + if(isset($pdo['total_games'])) + return $pdo['total_games']; + } + } + public function getTotalWins($username) { + if ($this->connection->getConnectionStatus()) { + $result = $this->connection->connection->query("SELECT total_wins FROM $this->table WHERE username='$username'"); + $pdo = $result->fetch(PDO::FETCH_ASSOC); + if(isset($pdo['total_wins'])) + return $pdo['total_wins']; + } + } + public function getTotalLoses($username) { + if ($this->connection->getConnectionStatus()) { + $result = $this->connection->connection->query("SELECT total_loses FROM $this->table WHERE username='$username'"); + $pdo = $result->fetch(PDO::FETCH_ASSOC); + if(isset($pdo['total_loses'])) + return $pdo['total_loses']; + } + } + public function incrementTotalGames($username) { + if ($this->connection->getConnectionStatus()) { + $result = $this->connection->connection->query("SELECT total_games FROM $this->table WHERE username='$username'"); + $pdo = $result->fetch(PDO::FETCH_ASSOC); + $totalGames = $pdo['total_games']; + $totalGames += 1; + + $sql = "UPDATE $this->table SET total_games=:total_games WHERE username=:username"; + $stmt = $this->connection->connection->prepare($sql); + $stmt->bindParam(":username", $username); + $stmt->bindParam(":total_games", $totalGames); + $stmt->execute(); + } + } + public function incrementTotalWins($username) { + if ($this->connection->getConnectionStatus()) { + $result = $this->connection->connection->query("SELECT total_wins FROM $this->table WHERE username='$username'"); + $pdo = $result->fetch(PDO::FETCH_ASSOC); + $totalWins = $pdo['total_wins']; + $totalWins += 1; + + $sql = "UPDATE $this->table SET total_wins=:total_wins WHERE username=:username"; + $stmt = $this->connection->connection->prepare($sql); + $stmt->bindParam(":username", $username); + $stmt->bindParam(":total_wins", $totalWins); + $stmt->execute(); + } + } + public function incrementTotalLoses($username) { + if ($this->connection->getConnectionStatus()) { + $result = $this->connection->connection->query("SELECT total_loses FROM $this->table WHERE username='$username'"); + $pdo = $result->fetch(PDO::FETCH_ASSOC); + $totalLoses = $pdo['total_loses']; + $totalLoses += 1; + + $sql = "UPDATE $this->table SET total_loses=:total_loses WHERE username=:username"; + $stmt = $this->connection->connection->prepare($sql); + $stmt->bindParam(":username", $username); + $stmt->bindParam(":total_loses", $totalLoses); + $stmt->execute(); + } + } + public function updateAvatar($username, $avatar_name) { + if ($this->connection->getConnectionStatus()) { + $sql = "UPDATE $this->table SET avatar_name=:avatar_name WHERE username=:username"; + $stmt = $this->connection->connection->prepare($sql); + $stmt->bindParam(":username", $username); + $stmt->bindParam(":avatar_name", $avatar_name); + $stmt->execute(); + } + } +} + +?> \ No newline at end of file