diff --git a/backend/classes/Card.php b/backend/classes/Card.php new file mode 100644 index 0000000..078ad4a --- /dev/null +++ b/backend/classes/Card.php @@ -0,0 +1,58 @@ +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); + } +} \ No newline at end of file diff --git a/backend/classes/CardFactory.php b/backend/classes/CardFactory.php new file mode 100644 index 0000000..f0766c4 --- /dev/null +++ b/backend/classes/CardFactory.php @@ -0,0 +1,75 @@ +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; + } +} \ No newline at end of file diff --git a/backend/classes/Db.php b/backend/classes/Db.php new file mode 100644 index 0000000..b753eaa --- /dev/null +++ b/backend/classes/Db.php @@ -0,0 +1,39 @@ +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(); + } +} \ No newline at end of file diff --git a/backend/classes/Marketplace.php b/backend/classes/Marketplace.php new file mode 100644 index 0000000..24bd231 --- /dev/null +++ b/backend/classes/Marketplace.php @@ -0,0 +1,4 @@ +