Fibinger Ádám
2020-06-04 ffa7c6f5c007dfc826aef0ca27d23bb69bbeed7e
EOG/Models/Stripe.php
@@ -4,123 +4,233 @@
class Stripe
{
    const TEAM_BLUE = 'blue';
    const TEAM_ORANGE = 'orange';
   const TEAM_BLUE = 'blue';
   const TEAM_ORANGE = 'orange';
    private $allowedColors = [self::TEAM_BLUE, self::TEAM_ORANGE];
    private $allowedStripeClasses = ['team', 'simple'];
    private $allowedOperators = [
        'alibi', 'amaru', 'ash', 'bandit', 'blackbeard', 'blitz', 'buck', 'capitao', 'castle',
        'caveira', 'clash', 'doc', 'dokkaebi', 'echo', 'ela', 'finka', 'frost', 'fuze',
        'glaz', 'goyo', 'gridlock', 'hibana', 'iq', 'jackal', 'jager', 'kaid', 'kapkan',
        'lesion', 'lion', 'maestro', 'maverick', 'mira', 'montagne', 'mozzie', 'mute', 'nokk',
        'nomad', 'pulse', 'rook', 'sledge', 'smoke', 'tachanka', 'thatcher', 'thermite', 'twitch',
        'valkyrie', 'vigil', 'warden', 'ying', 'zofia'];
    protected $state = [
        'stripe' => [
            'class' => 'team',
        ],
        'cup' => [
            'number' => '#75',
            'name' => '5on5 Open Cup',
        ],
        'team' => [
            'orange' =>
                [
                    'name' => 'Narancs Csapat',
                    'ban' => [
                        'mira',
                        'jackal'
                    ]
                ],
            'blue' => [
                'name' => 'Kék csapat',
                'ban' => [
                    'rook',
                    'blitz'
                ]
            ]
        ]
    ];
   private $allowedColors = [self::TEAM_BLUE, self::TEAM_ORANGE];
   private $allowedStripeClasses = ['team', 'simple', 'team-ban'];
   private $allowedOperators = [
      'alibi', 'amaru', 'ash', 'bandit', 'blackbeard', 'blitz', 'buck', 'capitao', 'castle',
      'caveira', 'clash', 'doc', 'dokkaebi', 'echo', 'ela', 'finka', 'frost', 'fuze',
      'glaz', 'goyo', 'gridlock', 'hibana', 'iq', 'jackal', 'jager', 'kaid', 'kapkan',
      'lesion', 'lion', 'maestro', 'maverick', 'mira', 'montagne', 'mozzie', 'mute', 'nokk',
      'nomad', 'pulse', 'rook', 'sledge', 'smoke', 'tachanka', 'thatcher', 'thermite', 'twitch',
      'valkyrie', 'vigil', 'warden', 'ying', 'zofia', 'wamai', 'kali', 'iana', 'oryx', 'no-ban', 'no-ban2'];
   protected $state = [
      'stripe' => [
         'class' => 'team',
      ],
      'cup'    => [
         'number' => '#76',
         'name'   => '5on5 Open Cup',
         'bestof'   => '5',
      ],
      'team'   => [
         'orange' =>
            [
               'name'  => 'Narancs Csapat',
               'ban'   => [
                  'mira',
                  'jackal'
               ],
               'score' => [
                  true,
                  true,
                  true
               ]
    public function setClass(string $class): self
    {
        if (!in_array($class, $this->allowedStripeClasses)) {
            throw new \InvalidArgumentException("Given class " . $class . " not allowed. Allowed classes: " . implode(', ', $this->allowedStripeClasses));
        }
            ],
         'blue'   => [
            'name'  => 'Kék csapat',
            'ban'   => [
               'rook',
               'blitz'
            ],
            'score' => [
               true,
               true,
               true
            ]
         ]
      ]
   ];
        if ($class === 'simple') {
            $this->setSimpleStripe();
        }
   public function setClass(string $class): self
   {
      if (!in_array($class, $this->allowedStripeClasses))
      {
         throw new \InvalidArgumentException("Given class " . $class . " not allowed. Allowed classes: " . implode(', ', $this->allowedStripeClasses));
      }
        $this->state['stripe']['class'] = $class;
      $this->state['stripe']['class'] = $class;
        return $this;
    }
      return $this;
   }
    private function setSimpleStripe()
    {
        unset($this->state['team']);
    }
   private function setSimpleStripe()
   {
      unset($this->state['team']);
   }
    public function setCup(string $number, string $name)
    {
        $this->state['cup']['number'] = $number;
        $this->state['cup']['name'] = $name;
    }
   public function setScore(string $team, int $number, bool $score)
   {
      $this->state['team'][$team]['score'][$number] = $score;
   }
    /**
     * @param string $color Csapat szín validáció
     *
     * Dob egy Exception-t ha nem orange / blue
     */
    private function testColor(string $color)
    {
        if (!in_array($color, $this->allowedColors)) {
            throw new \InvalidArgumentException("Given colour " . $team_color . " not allowed. Allowed colours: " . implode(', ', $this->allowedColors));
        }
    }
   public function setCup(string $number, string $name, int $bestOf = 1)
   {
      $this->state['cup']['number'] = $number;
      $this->state['cup']['name'] = $name;
      $this->state['cup']['bestof'] = $bestOf;
   }
    public function setTeamName(string $team_color = self::TEAM_BLUE, $name)
    {
        $this->testColor($team_color);
        $this->state['team'][$team_color]['name'] = $name;
    }
   /**
    * @param string $color Csapat szín validáció
    *
    * Dob egy Exception-t ha nem orange / blue
    */
   private function testColor(string $color)
   {
      if (!in_array($color, $this->allowedColors))
      {
         throw new \InvalidArgumentException("Given colour " . $color . " not allowed. Allowed colours: " . implode(', ', $this->allowedColors));
      }
   }
    public function addTeamBan(string $team_color = self::TEAM_BLUE, $operator)
    {
        $this->testColor($team_color);
        if (!in_array($operator, $this->allowedOperators)) {
            //FIXME: védő és támadó operátorok külön
            throw new \InvalidArgumentException("Given operator not allowed:  " . $operator . " Allowed operators: " . implode(', ', $this->allowedOperators));
        }
    }
   public function setTeamName(string $team_color = self::TEAM_BLUE, $name = '')
   {
      $this->testColor($team_color);
      $this->state['team'][$team_color]['name'] = $name;
   }
    public function __get($name)
    {
        if ($this->__isset($name)) {
            return $this->state[$name];
        }
   public function addTeamBan(string $team_color = self::TEAM_BLUE, $operator = '')
   {
      $this->testColor($team_color);
      if (!in_array($operator, $this->allowedOperators))
      {
         //FIXME: védő és támadó operátorok külön
         throw new \InvalidArgumentException("Given operator not allowed:  " . $operator . " Allowed operators: " . implode(', ', $this->allowedOperators));
      }
        return null;
    }
      if (isset($this->state['team'][$team_color]['ban']) && count($this->state['team'][$team_color]['ban']) > 1)
      {
         throw new \InvalidArgumentException("Team " . $team_color . " already has 2 operators.");
      }
    public function __isset($name)
    {
        return isset($this->state[$name]);
    }
      $this->state['team'][$team_color]['ban'][] = $operator;
   }
    public function loadJson(string $json)
    {
        $state = json_decode($json);
   public function __get($name)
   {
      if ($this->__isset($name))
      {
         return $this->state[$name];
      }
        if (!empty($state['stripe']["class"])) {
            $this->setClass($state['stripe']["class"]);
        }
      return null;
   }
        if (!empty($state['cup']['name']) && !empty($state['cup']['number'])) {
            $this->setCup($state['cup']['number'], $state['cup']['name']);
        }
   public function __isset($name)
   {
      return isset($this->state[$name]);
   }
    }
   public function loadFromJson(string $json)
   {
      $state = json_decode($json);
      $this->loadFromArray($state);
   }
   public function loadFromArray(array $state)
   {
      unset($this->state);
      $this->state = [];
      if (!empty($state['team']['orange']['score'][0]))
      {
         $this->setScore('orange', 0, true);
      }
      if (!empty($state['team']['orange']['score'][1]))
      {
         $this->setScore('orange', 1, true);
      }
      if (!empty($state['team']['orange']['score'][2]))
      {
         $this->setScore('orange', 2, true);
      }
      if (!empty($state['team']['blue']['score'][0]))
      {
         $this->setScore('blue', 0, true);
      }
      if (!empty($state['team']['blue']['score'][1]))
      {
         $this->setScore('blue', 1, true);
      }
      if (!empty($state['team']['blue']['score'][2]))
      {
         $this->setScore('blue', 2, true);
      }
      if (!empty($state['stripe']["class"]))
      {
         $this->setClass($state['stripe']["class"]);
      }
      if (!empty($state['cup']['name']) || !empty($state['cup']['number']) || !empty($state['cup']['bestof']))
      {
         $cupNum = $state['cup']['number'] ?? '';
         $cupName = $state['cup']['name'] ?? '';
         $bestOf = (int) $state['cup']['bestof'] ?? 1;
         $this->setCup($cupNum, $cupName, $bestOf);
      }
      if (!empty($state['team']))
      {
         foreach ([self::TEAM_BLUE, self::TEAM_ORANGE] as $color)
         {
            if (!empty($state['team'][$color]['name']))
            {
               $this->setTeamName($color, $state['team'][$color]['name']);
            }
            if (!empty($state['team'][$color]['ban']) && is_array($state['team'][$color]['ban']))
            {
               foreach ($state['team'][$color]['ban'] as $operator)
               {
                  $this->addTeamBan($color, $operator);
               }
            }
         }
      }
   }
   public function getJson()
   {
      return json_encode($this->state);
   }
   public function getOperators()
   {
      return $this->allowedOperators;
   }
   public function getState()
   {
      return $this->state;
   }
   public function getType()
   {
      if (empty($this->state['stripe']['class']))
      {
         return null;
      }
      return $this->state['stripe']['class'];
   }
}