/modifiers/land/Ground.php

https://bitbucket.org/Sett/evolution · PHP · 71 lines · 42 code · 12 blank · 17 comment · 6 complexity · 2becf65264b5d02d5a92a4f824381a18 MD5 · raw file

  1. <?php
  2. namespace app\modifiers\land;
  3. use app\components\LocationModifier;
  4. use app\interfaces\ILocationObject;
  5. use app\models\Water;
  6. /**
  7. * Class Ground
  8. * @package app\modifiers\land
  9. * @author Funcraft
  10. */
  11. class Ground extends LocationModifier
  12. {
  13. const ATTRIBUTE = 'landGround';
  14. const RELATION_LOCATION = 'landGround';
  15. const PROBABILITY_NEW = 20;//percentage
  16. const PROBABILITY_INC = 5;
  17. const P_NEW = 5;
  18. /**
  19. * @var string
  20. */
  21. protected $name = 'ground';
  22. /**
  23. * @return \app\models\Location
  24. */
  25. public function apply()
  26. {
  27. $location = $this->getLocation();
  28. if($ground = $location->getLocationAttribute(static::ATTRIBUTE)){
  29. if($this->getProbability(static::PROBABILITY_INC) == static::P_NEW){
  30. $ground->inc($location);
  31. }
  32. } elseif($this->getProbability(static::PROBABILITY_NEW) == static::P_NEW) {
  33. $location->setLocationAttribute($this->create());
  34. }
  35. return $location;
  36. }
  37. /**
  38. * @return ILocationObject
  39. */
  40. public function create()
  41. {
  42. $class = \Yii::$container->get(\app\models\Ground::className());
  43. $ground = new $class();
  44. $ground->land_id = $this->getLocation()->getId();
  45. $ground->max_square = $this->getLocation()->getSquare();
  46. /**
  47. * @var Water $water
  48. */
  49. $water = $this->getLocation()->getLocationAttribute(\app\modifiers\land\Water::ATTRIBUTE);
  50. if($water && ($water->getCurrentSquare() < $ground->max_square)){
  51. $ground->max_square = $ground->max_square - $water->getCurrentSquare();
  52. }
  53. $ground->current_square = mt_rand(1, $ground->max_square);
  54. $ground->created = time();
  55. $ground->changed = time();
  56. $ground->save();
  57. return $ground;
  58. }
  59. }