Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from PAXANDDOS/dev
Browse files Browse the repository at this point in the history
Small front-end fixes. Database path fixed, aimed to my pc server
  • Loading branch information
PAXANDDOS authored Apr 19, 2021
2 parents 7052512 + b685624 commit 6c60ba7
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 6 deletions.
2 changes: 1 addition & 1 deletion application/models/Model_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

Expand Down
13 changes: 9 additions & 4 deletions assets/css/board.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -38,7 +43,7 @@ main {
position: absolute;
margin-top: 158px;
margin-left: 115px;
z-index: 2;
z-index: 4;
}
.cardCanAttack {
animation: cardGlow 2s infinite;
Expand Down
2 changes: 1 addition & 1 deletion server/GameSession.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

include_once "GameClasses.php";

include_once "controller_database.php";

class GameSession
{
Expand Down
58 changes: 58 additions & 0 deletions server/Model_database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

class DatabaseConnection {
public $connection;
public function __construct($host, $port, $username, $password, $database) {
try {
$this->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');
}
}

?>
194 changes: 194 additions & 0 deletions server/controller_database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?php

require_once 'Model_database.php';
class User extends Model_database {
public $id;
public $username;
public $password;
public $name;
public $email;
public $avatar_name;

public function __construct() {
parent::__construct("users");
}
public function __destruct() {
$this->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();
}
}
}

?>

0 comments on commit 6c60ba7

Please sign in to comment.