Add backend classes for cards

This commit is contained in:
Robbie Antenesse 2019-08-01 16:37:50 -06:00
parent 31832dcab9
commit fba6748f89
5 changed files with 181 additions and 0 deletions

58
backend/classes/Card.php Normal file
View File

@ -0,0 +1,58 @@
<?php
class Card {
public $color;
public $shape;
public $plus;
public $minus;
public $is_shadow;
public $shape2;
public function __construct($options = []) {
if (!isset($options['color'])) {
throw new Exception('A Color is Required to construct a Card');
}
if (!isset($options['shape'])) {
throw new Exception('A Shape is Required to construct a Card');
}
if (!isset($options['plus'])) {
throw new Exception('A Plus value is Required to construct a Card');
}
if (!isset($options['minus'])) {
throw new Exception('A Minus value is Required to construct a Card');
}
$this->color = $options['color'];
$this->shape = $options['shape'];
$this->plus = $options['plus'];
$this->minus = $options['minus'];
if (isset($options['id'])) {
$this->id = $options['id'];
}
if (isset($options['is_shadow'])) {
$this->is_shadow = $options['is_shadow'];
}
if (isset($options['shape2'])) {
$this->shape2 = $options['shape2'];
}
}
public function getWorth() {
if ($this->is_shadow) {
if (isset($this->color2)) {
return 1;
}
return 0;
}
if (isset($this->color2)) {
if ($this->plus > $this->minus) {
return ceil($this->plus / 2);
}
return ceil($this->minus / 2);
}
return ceil(($this->plus + $this->minus) / 2);
}
public function getCost() {
return floor($this->getWorth() / 2);
}
}

View File

@ -0,0 +1,75 @@
<?php
require_once(realpath(dirname(__FILE__) . '/../config.php'));
require_once(realpath(dirname(__FILE__) . '/./Card.php'));
class CardFactory {
public static $card_colors = [
'red',
'blue',
'yellow',
'black',
'white',
];
public static $card_shapes = [
'triangle',
'square',
'pentagon',
];
private $db;
private $max_value;
public function __construct() {
$this->db = new Db();
$this->max_value = 10;
}
public function createCardData($rare_chance = 33) {
$color = self::$card_colors[mt_rand(0, (count(self::$card_colors) - (mt_rand(0, 100) < $rareChance ? 0 : 2)))];
$shape = self::$card_shapes[mt_rand(0, (count(self::$card_shapes) - (mt_rand(0, 100) < $rareChance ? 0 : 1)))];
$plus = mt_rand(1, $this->max_value);
$minus = mt_rand(1, $this->max_value);
return [
'color' => $color,
'shape' => $shape,
'plus' => $plus,
'minus' => $minus,
];
}
private function getMaxId() {
return $this->db->query('SELECT TOP 1 `id` FROM `base_cards` ORDER BY `id` DESC;')->fetch()['id'];
}
public function getCard($id = false) {
if ($id === false) {
$id = mt_rand(1, $this->getMaxId());
}
$query = 'SELECT * FROM `base_cards` WHERE `id`=?';
$card_data = $this->db->query($query, [$id])->result();
if ($card_data) {
if (!is_null($card_data['owned'])) {
$card_data['is_shadow'] = true;
}
return new Card($card_data);
}
return false;
}
public function getPack() {
$pack = [
'cost' => 0,
'cards' => [],
];
for ($i = 0; $i < 4; $i++) {
$card = $this->getCard();
$pack['cards'][] = $card;
if (!$card->is_shadow) {
$this->db->execute('UPDATE `base_cards` SET `is_packed` WHERE `id`=?', [$card->id]);
}
}
$pack['cost'] = array_reduce($pack['cards'], function($total, $card) { return $total + $card->getCost(); }, 0);
return $pack;
}
}

39
backend/classes/Db.php Normal file
View File

@ -0,0 +1,39 @@
<?php
require_once(realpath(dirname(__FILE__) . '/./config.php'));
class Db {
private $dbh;
public $last_error_info;
function __construct() {
$this->dbh = new PDO('mysql:host=localhost;dbname=' . DB_NAME . ';charset=utf8', DB_USER, DB_PASSWORD);
$this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$this->last_error_info = null;
}
public function execute ($query, $params = array()) {
// Run a query that doesn't require a result to be returned
$stmt = $this->dbh->prepare($query);
if ($stmt->execute($params)) {
$this->last_error_info = null;
return true;
}
$this->last_error_info = $stmt->errorInfo();
return false;
}
public function query ($query, $params = array()) {
// Run a query that returns results
$stmt = $this->dbh->prepare($query);
$stmt->execute($params);
$this->last_error_info = $stmt->errorInfo();
return $stmt;
}
public function lastInsertId () {
return $this->dbh->lastInsertId();
}
public function errorInfo () {
return $this->dbh->errorInfo();
}
}

View File

@ -0,0 +1,4 @@
<?php
class Marketplace {
}

View File

@ -0,0 +1,5 @@
<?php
define('DB_NAME', 'db_name');
define('DB_USER', 'db_username');
define('DB_PASSWORD', 'user_password');