/**
  * Update the given dragon at the given level - collection, levelling up
  * @param int $id
  * @param int $level
  * @param mixed[] $requestData
  * @return Dragon
  */
 public function update($id, $level, $requestData)
 {
     /* @var Dragon $dragon */
     $dragon = Dragon::with('levelUp.data', 'collectTime.data', 'collectRateFish.data', 'collectRateWood.data', 'collectIronData')->find($id);
     $this->updateLevelUpData($dragon, $level, $requestData);
     // Only update the fish/wood collection if the dragon is going to collect them
     if ($this->startCollectingLevel($dragon) !== false && $this->startCollectingLevel($dragon) <= $level) {
         $this->updateCollectTime($dragon, $level, $requestData);
         if ($dragon->wood_id == $dragon->fish_id) {
             // We know one of the levels is fine, so update it
             $this->updateRate($dragon, $level, 'collectRateFish', $requestData['both_collect']);
         } else {
             if ($dragon->fish_start_level !== null && $dragon->fish_start_level <= $level) {
                 $this->updateRate($dragon, $level, 'collectRateFish', $requestData['fish_collect']);
             }
             if ($dragon->wood_start_level !== null && $dragon->wood_start_level <= $level) {
                 $this->updateRate($dragon, $level, 'collectRateWood', $requestData['wood_collect']);
             }
         }
         $this->percentageService->updatePercentages($level);
     }
     // Only update iron collection if the dragon is going to collect it, and the user can see the information
     if ($dragon->iron_start_level !== null && $dragon->iron_start_level <= $level && Auth::user()->collect_iron) {
         $this->updateIron($dragon, $level, $requestData);
     }
     return $dragon;
 }
 public function presentAllLevels()
 {
     $levels = [];
     foreach ($this->data as $time) {
         $levels[$time->level] = ['time' => $time->time];
     }
     for ($i = 1; $i <= Setting::get('max-level'); $i++) {
         if (!isset($levels[$i])) {
             $levels[$i] = ['time' => isset($levels[Setting::get('max-level')]) ? $this->percentageService->estimateTime($i, $levels[Setting::get('max-level')]->time) : null, 'time_estimate' => true];
         }
     }
     ksort($levels);
     return $levels;
 }
 /**
  * If values are missing, estimate them
  * @param object $dragon
  */
 protected function setEstimatesForMax(&$dragon)
 {
     if ($dragon['collect_time'] === null) {
         if ($dragon['max_collect_time']) {
             $dragon['collect_time'] = $this->percentageService->estimateTime(Auth::user()->academyLevel->max_dragon_level, $dragon['max_collect_time']);
             $dragon['collect_time_estimate'] = true;
         }
     }
     if ($dragon['wood_rate'] === null) {
         if ($dragon['max_wood_rate']) {
             $dragon['wood_rate'] = $this->percentageService->estimateRate(Auth::user()->academyLevel->max_dragon_level, $dragon['max_wood_rate']);
             $dragon['wood_rate_estimate'] = true;
             $dragon['wood_user_collect_estimate'] = true;
         }
     }
     if ($dragon['fish_rate'] === null) {
         if ($dragon['max_fish_rate']) {
             $dragon['fish_rate'] = $this->percentageService->estimateRate(Auth::user()->academyLevel->max_dragon_level, $dragon['max_fish_rate']);
             $dragon['fish_rate_estimate'] = true;
             $dragon['fish_user_collect_estimate'] = true;
         }
     }
 }
 /**
  * Update the time at the maximum level for the given CollectTime
  * @param integer $id
  * @param integer $newMax
  */
 public function updateMax($id, $newMax)
 {
     /** @var CollectRates|CollectTimes $collectorClass */
     $collectorClass = $this->collector;
     /** @var CollectRates|CollectTimes $collector */
     /** @noinspection PhpUndefinedMethodInspection */
     $collector = $collectorClass::with('data')->find($id);
     /** @var CollectRateData|CollectTimeData $data */
     $data = $collector->data->where('level', (int) Setting::get('max-level'))->first();
     if (!$data) {
         $data = new $this->collectData(['level' => Setting::get('max-level')]);
         $collector->data()->save($data);
     }
     $data->{$this->dataField} = $newMax;
     $data->save();
     $this->percentageService->updateAllPercentages();
 }
 public function allLevelsRate(&$levels, $attribute, $key, $startLevel)
 {
     foreach ($attribute->data as $wRate) {
         $levels[$wRate->level][$key] = $wRate->rate;
         $levels[$wRate->level][$key . '_updated_at'] = $wRate->updated_at;
     }
     $fillTo = $startLevel !== null ? $startLevel : Setting::get('max-level') + 1;
     for ($i = 1; $i < $fillTo; $i++) {
         $levels[$i][$key] = 0;
         $levels[$i][$key . '_updated_at'] = null;
     }
     foreach ($levels as $level => &$data) {
         if ($data[$key] === null) {
             $data[$key] = $this->percentageService->estimateRate($level, $levels[Setting::get('max-level')][$key]);
             $data[$key . '_estimate'] = true;
         }
     }
 }
 public function updatePercentages()
 {
     $this->percentages->updateAllPercentages();
     Notification::add('success', 'Updated all percentages');
     return Redirect::back();
 }