75 lines
1.9 KiB
PHP
75 lines
1.9 KiB
PHP
|
<?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;
|
||
|
}
|
||
|
}
|