Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.09 KB | None | 0 0
  1. <?php
  2.  
  3. class Model
  4. {
  5.  
  6.     protected $db;
  7.    
  8.     function __construct($db)
  9.     {
  10.         $this->db = $db;
  11.     }
  12.  
  13. }
  14.  
  15. class Player extends Model
  16. {
  17.  
  18.     private $id;
  19.  
  20.     function __construct($id, $db)
  21.     {
  22.         parent::__construct($db);
  23.         $this->id = $id;
  24.     }
  25.  
  26. }
  27.  
  28. class Item extends Model
  29. {
  30.  
  31.     private $id;
  32.     private $data = array();
  33.    
  34.     function __construct($id, $db)
  35.     {
  36.         parent::__construct($db);
  37.         $this->id = $id;
  38.     }
  39.  
  40. }
  41.  
  42. class PlayerFactory
  43. {
  44.  
  45.     private $db;
  46.    
  47.     function create($id)
  48.     {
  49.         return new Player($id, $this->db);
  50.     }
  51.  
  52. }
  53.  
  54. class ItemFactory
  55. {
  56.  
  57.     private $db;
  58.    
  59.     function create($id)
  60.     {
  61.         return new Item($id, $this->db);
  62.     }
  63.  
  64. }
  65.  
  66. class Relation
  67. {
  68.  
  69.     private $classes = array();
  70.     protected $db;
  71.    
  72.     function __construct($db)
  73.     {
  74.         $this->db = $db;
  75.     }
  76.    
  77.     function __set($k, $v)
  78.     {
  79.         $this->classes[$k] = $v;
  80.     }
  81.    
  82.     function __get($k)
  83.     {
  84.         return $this->classes[$k];
  85.     }
  86.  
  87.  
  88. }
  89.  
  90.  
  91. class RelationPlayerItem extends Service
  92. {
  93.    
  94.     function buyItem()
  95.     {
  96.         if ($this->player->money < $item->price) {
  97.             throw new Exception("not enough money");
  98.         }
  99.        
  100.         $this->db->beginTransaction();
  101.         $this->player->subtractMoney();
  102.         $this->item->changeOwner($this->player);
  103.         $this->db->commit();
  104.     }
  105.  
  106.  
  107. }
  108.  
  109.  
  110. class Presenter
  111. {
  112.  
  113.     // obě proměnné umí Nette automaticky vytvořit
  114.     private $playerFactory;
  115.     private $itemFactory;
  116.  
  117.     protected $player;
  118.    
  119.     // startup se zavolá jako první a slouží pro inicializaci vlastnostní
  120.     public function startup()
  121.     {
  122.         // $this->user->id je v Nette ID přihlášeného uživatele
  123.         $this->player = $this->playerFactory->create($this->user->id);
  124.     }
  125.  
  126.     private function createRelation($name)
  127.     {
  128.         $class = "Relation" . ucFirst($name);
  129.        
  130.         return new $class($this->context->getService("db"));
  131.     }
  132.  
  133.     function handleBuyItem($id)
  134.     {
  135.         $relation = $this->createRelation("playerItem");
  136.         $relation->player = $this->player;
  137.         $relation->item = $this->itemFactory->create($id);
  138.        
  139.         try {
  140.             $relation->buyItem();
  141.         } catch (Exception $e) {
  142.             $this->flashMessage($e->getMessage());
  143.             $this->redirect("default");
  144.         }
  145.     }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement