Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- abstract class Model
- {
- /** @var DatabazovaTrida */
- protected $db;
- /**
- * @param DatabazovaTrida
- */
- public function __construct($db)
- {
- $this->db = $db;
- }
- }
- class Player extends Model
- {
- /** @var int */
- private $id;
- /** @var int|NULL */
- private $money = NULL;
- /**
- * @param int
- * @param DatabazovaTrida
- */
- public function __construct($id, $db)
- {
- parent::__construct($db);
- $this->id = $id;
- }
- /**
- * @return int
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * @return int
- */
- public function getMoney()
- {
- if ($this->money === NULL) {
- $this->money = $this->db->fetchColumn("
- SELECT money
- FROM player
- WHERE id = ?
- LIMIT 1
- ", $this->id);
- }
- return $this->money;
- }
- /**
- * @param int
- * @throws Exception
- */
- public function subtractMoney($num)
- {
- if ($this->getMoney() - $num < 0) {
- throw new Exception("nemáš dostatek peněz");
- }
- $this->db->query("
- UPDATE player
- SET money = money - ?
- WHERE id = ?
- ", $num, $this->id);
- $this->money -= $num;
- }
- /**
- * @param int
- */
- public function addMoney($num)
- {
- $this->db->query("
- UPDATE player
- SET money = money + ?
- WHERE id = ?
- ", $num, $this->id);
- $this->money += $num;
- }
- }
- class Item extends Model
- {
- /** @var int */
- private $id;
- /** @var array */
- private $data = array();
- /**
- * @param int
- * @param DatabazovaTrida
- */
- public function __construct($id, $db)
- {
- parent::__construct($db);
- $this->id = $id;
- }
- /**
- * @return array
- */
- public function getData()
- {
- if (empty($this->data)) {
- $this->data = $this->db->fetch("
- SELECT ownerId, name, price
- FROM item
- WHERE id = ?
- LIMIT 1
- ", $this->id);
- }
- return $this->data;
- }
- /**
- * @param string
- * @return int|string
- */
- public function __get($key)
- {
- return $this->getData()[$key];
- }
- /**
- * @param Player
- */
- public function changeOwner(Player $owner)
- {
- $newOwnerId = $owner->getId();
- $this->db->query("
- UPDATE item
- SET ownerId = ?
- WHERE id = ? AND ownerId = ?
- ", $newOwnerId, $this->id, $this->data["ownerId"]);
- $this->data["owner"] = $newOwnerId;
- }
- }
- class PlayerFactory
- {
- /** @var DatabazovaTrida */
- private $db;
- /**
- * @param DatabazovaTrida
- */
- public function __construct($db)
- {
- $this->db = $db;
- }
- /**
- * @param int
- */
- function create($id)
- {
- return new Player($id, $this->db);
- }
- }
- class ItemFactory
- {
- /** @var DatabazovaTrida */
- private $db;
- /**
- * @param DatabazovaTrida
- */
- public function __construct($db)
- {
- $this->db = $db;
- }
- /**
- * @param int
- */
- public function create($id)
- {
- return new Item($id, $this->db);
- }
- }
- abstract class Relation
- {
- /** @var array */
- private $classes = array();
- /** @var DatabazovaTrida */
- protected $db;
- /**
- * @param DatabazovaTrida
- */
- public function __construct($db)
- {
- $this->db = $db;
- }
- /**
- * @param string
- * @param mixed
- */
- function __set($k, $v)
- {
- $this->classes[$k] = $v;
- }
- /**
- * @param string
- * @return mixed
- */
- function __get($k)
- {
- return $this->classes[$k];
- }
- }
- class RelationPlayerItem extends Relation
- {
- /**
- * Provede transakci při koupi předmětu
- */
- function buyItem()
- {
- $this->db->beginTransaction();
- $this->player->subtractMoney($item->price); // vyhodí výjimku, pokud nemá hráč dostatek peněz
- $this->item->changeOwner($this->player);
- $this->owner->addMoney($item->price);
- $this->db->commit();
- }
- }
- class Presenter
- {
- // objekty $playerFactory a $itemFactory umí Nette automaticky vytvořit, když je potřeba,
- // a předat jim databázové spojení podle konfiguračního souboru
- /** @var PlayerFactory */
- private $playerFactory;
- /** @var ItemFactory */
- private $itemFactory;
- /** @var Player */
- protected $player;
- // startup se zavolá jako první a slouží pro inicializaci vlastnostní
- public function startup()
- {
- // $this->user->id je v Nette ID přihlášeného uživatele
- $this->player = $this->playerFactory->create($this->user->id);
- }
- /**
- * @param string
- * @return object
- */
- private function createRelation($name)
- {
- $class = "Relation" . ucFirst($name);
- // $this->context je instance DI containeru
- // metoda getService() vrátí objekt reprezentující databázové spojení
- return new $class($this->context->getService("db"));
- }
- /**
- * @param int
- */
- public function handleBuyItem($id)
- {
- $item = $this->itemFactory->create($id);
- $ownerId = $item->getData()->ownerId;
- // relace nyní obsahuje objekty kupujícího hráče, vlastníka předmětu a samotný předmět
- $relation = $this->createRelation("playerItem");
- $relation->player = $this->player;
- $relation->item = $item;
- $relation->owner = $this->playerFactory->create($ownerId);
- try {
- $relation->buyItem();
- } catch (Exception $e) {
- $this->flashMessage($e->getMessage()); // zpráva, která se poté objeví na stránce
- $this->redirect("default");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement