Guest User

Untitled

a guest
Apr 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1.  
  2.     public function giveTo(Character $p, array $datas)
  3.     {
  4.         if (!$this->Effects->count())
  5.             return;
  6.  
  7.         if ($this->is_lottery)
  8.         {
  9.             if (!isset($_SESSION['buy_index'][$this->id]))
  10.                 exit('cheating buy_index giveTo');
  11.  
  12.             $effectIndex = $_SESSION['buy_index'][$this->id];
  13.             $effect = $this->Effects[$effectIndex];
  14.             $hasEffect = $effect->giveTo($p, $datas); //you can not "disable" a lottery result, that'd of course be cheating.
  15.             unset($_SESSION['buy_index'][$this->id]);
  16.             return array($effect, $hasEffect);
  17.         }
  18.         else
  19.         {
  20.             $hasEffect = false;
  21.             foreach ($this->Effects as $effect)
  22.             {
  23.                 if ($effect->giveTo($p, $datas))
  24.                     $hasEffect = true;
  25.             }
  26.             return $hasEffect;
  27.         }
  28.     }
  29.  
  30.  
  31.     public function hasConfirmation()
  32.     {
  33.         foreach ($this->Effects as $effect)
  34.         {
  35.             if ($effect->getConfirmation(array()) != NULL)
  36.                 return true;
  37.         }
  38.         return false;
  39.     }
  40.     public function getConfirmationForm($datas)
  41.     {
  42.         if (!$this->hasConfirmation())
  43.             return '';
  44.  
  45.         $form = array();
  46.         if ($this->is_lottery)
  47.         {
  48.             if (!isset($_SESSION['buy_index'][$this->id]))
  49.                 exit('cheating buy_index getConfirmationForm');
  50.  
  51.             $effect = $this->Effects[$_SESSION['buy_index'][$this->id]];
  52.             $form[$effect->render()] = $effect->getConfirmation($datas); //should never be empty !!
  53.         }
  54.         else
  55.         {
  56.             foreach ($this->Effects as $effect)
  57.             {
  58.                 $inputs = $effect->getConfirmation($datas);
  59.                 if (empty($inputs))
  60.                     continue;
  61.  
  62.                 $form[$effect->render()] = $inputs;
  63.             }
  64.         }
  65.  
  66.         return make_form($form);
  67.     }
  68.     public function confirm($datas)
  69.     {
  70.         if (!$this->hasConfirmation())
  71.         {
  72.             if ($this->is_lottery)
  73.                 return $this->getRandomIndex();
  74.             return true;
  75.         }
  76.  
  77.         global $errors;
  78.  
  79.         if ($this->is_lottery)
  80.         {
  81.             if (empty($_SESSION['buy_index']))
  82.                 $_SESSION['buy_index'] = array();
  83.  
  84.             $index = $this->getRandomIndex();
  85.  
  86.             if (!isset($_SESSION['buy_index'][$this->id]))
  87.             {
  88.                 if (empty($datas)) //not submitted yet
  89.                     $_SESSION['buy_index'][$this->id] = $index;
  90.                 else
  91.                 { //I SEE YOU CHEATING
  92.                     $errors[] = lang('shop.not_buying');
  93.                     return false;
  94.                 }
  95.             }
  96.         }
  97.  
  98.         if (empty($datas)) //show up da form anyway
  99.             return false;
  100.  
  101.         if (empty($errors))
  102.         { //if errors are here ...
  103.             if ($this->is_lottery)
  104.                 $this->Effects[$index]->confirm($datas);
  105.             else
  106.             {
  107.                 foreach ($this->Effects as $effect)
  108.                     $effect->confirm($datas);
  109.             }
  110.         }
  111.  
  112.         return empty($errors);
  113.     }
  114.     public function getRandomIndex()
  115.     {
  116.         do
  117.         {
  118.             $rand = rand(0, $this->Effects->count()-1);
  119.         }
  120.         while (!$this->Effects->has($rand));
  121.  
  122.         return $rand;
  123.     }
Add Comment
Please, Sign In to add comment