Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.29 KB | None | 0 0
  1. <?php
  2.  
  3. abstract class Model
  4. {
  5.  
  6.     /** @var DatabazovaTrida */
  7.     protected $db;
  8.    
  9.     /**
  10.      * @param DatabazovaTrida
  11.      */
  12.     public function __construct($db)
  13.     {
  14.         $this->db = $db;
  15.     }
  16.  
  17. }
  18.  
  19. class Player extends Model
  20. {
  21.  
  22.     /** @var int */
  23.     private $id;
  24.    
  25.     /** @var int|NULL */
  26.     private $money = NULL;
  27.    
  28.    
  29.     /**
  30.      * @param int
  31.      * @param DatabazovaTrida
  32.      */
  33.     public function __construct($id, $db)
  34.     {
  35.         parent::__construct($db);
  36.         $this->id = $id;
  37.     }
  38.    
  39.    
  40.     /**
  41.      * @return int
  42.      */
  43.     public function getId()
  44.     {
  45.         return $this->id;
  46.     }
  47.    
  48.    
  49.     /**
  50.      * @return int
  51.      */
  52.     public function getMoney()
  53.     {
  54.         if ($this->money === NULL) {
  55.             $this->money = $this->db->fetchColumn("
  56.                 SELECT money
  57.                 FROM player
  58.                 WHERE id = ?
  59.                 LIMIT 1
  60.             ", $this->id);
  61.         }
  62.        
  63.         return $this->money;
  64.     }
  65.  
  66.    
  67.     /**
  68.      * @param int
  69.      * @throws Exception
  70.      */
  71.     public function subtractMoney($num)
  72.     {
  73.         if ($this->getMoney() - $num < 0) {
  74.             throw new Exception("nemáš dostatek peněz");
  75.         }
  76.        
  77.         $this->db->query("
  78.             UPDATE player
  79.             SET money = money - ?
  80.             WHERE id = ?
  81.         ", $num, $this->id);
  82.         $this->money -= $num;
  83.     }
  84.    
  85.    
  86.     /**
  87.      * @param int
  88.      */
  89.     public function addMoney($num)
  90.     {
  91.         $this->db->query("
  92.             UPDATE player
  93.             SET money = money + ?
  94.             WHERE id = ?
  95.         ", $num, $this->id);
  96.         $this->money += $num;
  97.     }
  98.    
  99. }
  100.  
  101. class Item extends Model
  102. {
  103.  
  104.     /** @var int */
  105.     private $id;
  106.    
  107.     /** @var array */
  108.     private $data = array();
  109.    
  110.    
  111.     /**
  112.      * @param int
  113.      * @param DatabazovaTrida
  114.      */
  115.     public function __construct($id, $db)
  116.     {
  117.         parent::__construct($db);
  118.         $this->id = $id;
  119.     }
  120.    
  121.    
  122.     /**
  123.      * @return array
  124.      */
  125.     public function getData()
  126.     {
  127.         if (empty($this->data)) {
  128.             $this->data = $this->db->fetch("
  129.                 SELECT ownerId, name, price
  130.                 FROM item
  131.                 WHERE id = ?
  132.                 LIMIT 1
  133.             ", $this->id);
  134.         }
  135.        
  136.         return $this->data;
  137.     }
  138.    
  139.    
  140.     /**
  141.      * @param string
  142.      * @return int|string
  143.      */
  144.     public function __get($key)
  145.     {
  146.         return $this->getData()[$key];
  147.     }
  148.  
  149.    
  150.     /**
  151.      * @param Player
  152.      */
  153.     public function changeOwner(Player $owner)
  154.     {
  155.         $newOwnerId = $owner->getId();
  156.         $this->db->query("
  157.             UPDATE item
  158.             SET ownerId = ?
  159.             WHERE id = ? AND ownerId = ?
  160.         ", $newOwnerId, $this->id, $this->data["ownerId"]);
  161.        
  162.         $this->data["owner"] = $newOwnerId;
  163.     }
  164.    
  165. }
  166.  
  167. class PlayerFactory
  168. {
  169.  
  170.     /** @var DatabazovaTrida */
  171.     private $db;
  172.    
  173.    
  174.     /**
  175.      * @param DatabazovaTrida
  176.      */
  177.     public function __construct($db)
  178.     {
  179.         $this->db = $db;
  180.     }
  181.    
  182.    
  183.     /**
  184.      * @param int
  185.      */
  186.     function create($id)
  187.     {
  188.         return new Player($id, $this->db);
  189.     }
  190.  
  191. }
  192.  
  193. class ItemFactory
  194. {
  195.  
  196.     /** @var DatabazovaTrida */
  197.     private $db;
  198.    
  199.    
  200.     /**
  201.      * @param DatabazovaTrida
  202.      */
  203.     public function __construct($db)
  204.     {
  205.         $this->db = $db;
  206.     }
  207.    
  208.    
  209.     /**
  210.      * @param int
  211.      */
  212.     public function create($id)
  213.     {
  214.         return new Item($id, $this->db);
  215.     }
  216.  
  217. }
  218.  
  219. abstract class Relation
  220. {
  221.  
  222.     /** @var array */
  223.     private $classes = array();
  224.    
  225.     /** @var DatabazovaTrida */
  226.     protected $db;
  227.    
  228.    
  229.     /**
  230.      * @param DatabazovaTrida
  231.      */
  232.     public function __construct($db)
  233.     {
  234.         $this->db = $db;
  235.     }
  236.    
  237.    
  238.     /**
  239.      * @param string
  240.      * @param mixed
  241.      */
  242.     function __set($k, $v)
  243.     {
  244.         $this->classes[$k] = $v;
  245.     }
  246.    
  247.    
  248.     /**
  249.      * @param string
  250.      * @return mixed
  251.      */
  252.     function __get($k)
  253.     {
  254.         return $this->classes[$k];
  255.     }
  256.  
  257.  
  258. }
  259.  
  260.  
  261. class RelationPlayerItem extends Relation
  262. {
  263.    
  264.    
  265.     /**
  266.      * Provede transakci při koupi předmětu
  267.      */
  268.     function buyItem()
  269.     {
  270.         $this->db->beginTransaction();
  271.         $this->player->subtractMoney($item->price);  // vyhodí výjimku, pokud nemá hráč dostatek peněz
  272.         $this->item->changeOwner($this->player);
  273.         $this->owner->addMoney($item->price);
  274.         $this->db->commit();
  275.     }
  276.  
  277.  
  278. }
  279.  
  280.  
  281. class Presenter
  282. {
  283.  
  284.     // objekty $playerFactory a $itemFactory umí Nette automaticky vytvořit, když je potřeba,
  285.     // a předat jim databázové spojení podle konfiguračního souboru
  286.    
  287.     /** @var PlayerFactory */
  288.     private $playerFactory;
  289.    
  290.     /** @var ItemFactory */
  291.     private $itemFactory;
  292.  
  293.     /** @var Player */
  294.     protected $player;
  295.    
  296.     // startup se zavolá jako první a slouží pro inicializaci vlastnostní
  297.     public function startup()
  298.     {
  299.         // $this->user->id je v Nette ID přihlášeného uživatele
  300.         $this->player = $this->playerFactory->create($this->user->id);
  301.     }
  302.  
  303.    
  304.     /**
  305.      * @param string
  306.      * @return object
  307.      */
  308.     private function createRelation($name)
  309.     {
  310.         $class = "Relation" . ucFirst($name);
  311.        
  312.         // $this->context je instance DI containeru
  313.         // metoda getService() vrátí objekt reprezentující databázové spojení
  314.         return new $class($this->context->getService("db"));
  315.     }
  316.  
  317.    
  318.     /**
  319.      * @param int
  320.      */
  321.     public function handleBuyItem($id)
  322.     {
  323.         $item = $this->itemFactory->create($id);
  324.         $ownerId = $item->getData()->ownerId;
  325.        
  326.         // relace nyní obsahuje objekty kupujícího hráče, vlastníka předmětu a samotný předmět
  327.         $relation = $this->createRelation("playerItem");
  328.         $relation->player = $this->player;
  329.         $relation->item = $item;
  330.         $relation->owner = $this->playerFactory->create($ownerId);
  331.        
  332.         try {
  333.             $relation->buyItem();
  334.         } catch (Exception $e) {
  335.             $this->flashMessage($e->getMessage());  // zpráva, která se poté objeví na stránce
  336.             $this->redirect("default");
  337.         }
  338.     }
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement