Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function giveTo(Character $p, array $datas)
- {
- if (!$this->Effects->count())
- return;
- if ($this->is_lottery)
- {
- if (!isset($_SESSION['buy_index'][$this->id]))
- exit('cheating buy_index giveTo');
- $effectIndex = $_SESSION['buy_index'][$this->id];
- $effect = $this->Effects[$effectIndex];
- $hasEffect = $effect->giveTo($p, $datas); //you can not "disable" a lottery result, that'd of course be cheating.
- unset($_SESSION['buy_index'][$this->id]);
- return array($effect, $hasEffect);
- }
- else
- {
- $hasEffect = false;
- foreach ($this->Effects as $effect)
- {
- if ($effect->giveTo($p, $datas))
- $hasEffect = true;
- }
- return $hasEffect;
- }
- }
- public function hasConfirmation()
- {
- foreach ($this->Effects as $effect)
- {
- if ($effect->getConfirmation(array()) != NULL)
- return true;
- }
- return false;
- }
- public function getConfirmationForm($datas)
- {
- if (!$this->hasConfirmation())
- return '';
- $form = array();
- if ($this->is_lottery)
- {
- if (!isset($_SESSION['buy_index'][$this->id]))
- exit('cheating buy_index getConfirmationForm');
- $effect = $this->Effects[$_SESSION['buy_index'][$this->id]];
- $form[$effect->render()] = $effect->getConfirmation($datas); //should never be empty !!
- }
- else
- {
- foreach ($this->Effects as $effect)
- {
- $inputs = $effect->getConfirmation($datas);
- if (empty($inputs))
- continue;
- $form[$effect->render()] = $inputs;
- }
- }
- return make_form($form);
- }
- public function confirm($datas)
- {
- if (!$this->hasConfirmation())
- {
- if ($this->is_lottery)
- return $this->getRandomIndex();
- return true;
- }
- global $errors;
- if ($this->is_lottery)
- {
- if (empty($_SESSION['buy_index']))
- $_SESSION['buy_index'] = array();
- $index = $this->getRandomIndex();
- if (!isset($_SESSION['buy_index'][$this->id]))
- {
- if (empty($datas)) //not submitted yet
- $_SESSION['buy_index'][$this->id] = $index;
- else
- { //I SEE YOU CHEATING
- $errors[] = lang('shop.not_buying');
- return false;
- }
- }
- }
- if (empty($datas)) //show up da form anyway
- return false;
- if (empty($errors))
- { //if errors are here ...
- if ($this->is_lottery)
- $this->Effects[$index]->confirm($datas);
- else
- {
- foreach ($this->Effects as $effect)
- $effect->confirm($datas);
- }
- }
- return empty($errors);
- }
- public function getRandomIndex()
- {
- do
- {
- $rand = rand(0, $this->Effects->count()-1);
- }
- while (!$this->Effects->has($rand));
- return $rand;
- }
Add Comment
Please, Sign In to add comment