/**
  * Get iron collection fields
  * @param int $dragonID
  * @param int $level
  * @return bool[]
  */
 protected function getIronCollectionFields($dragonID, $level)
 {
     $dragon = Dragon::find($dragonID);
     $fields = ['iron_collect' => false, 'iron_fish' => false, 'iron_wood' => false];
     // Only show the field if the user can collect iron
     if (Auth::user()->collect_iron && $dragon->iron_start_level !== null && $dragon->iron_start_level <= $level) {
         $fields['iron_collect'] = true;
         // See if we can show just wood or fish for the cost
         $result = $this->getIronCollectionFromDB($dragonID);
         $nonEmptyValues = count(array_filter($result));
         // If all are empty, or none are empty, show all fields
         switch ($nonEmptyValues) {
             case 0:
             case 2:
                 $fields['iron_fish'] = true;
                 $fields['iron_wood'] = true;
                 break;
             default:
                 foreach ($result as $key => $value) {
                     if ($value) {
                         $fields['iron_' . $key] = true;
                     }
                 }
         }
     }
     return $fields;
 }
 public function getListForSelects()
 {
     $dragonList = [];
     foreach (Dragon::with('dragonType')->get() as $dragon) {
         $dragonList[$dragon->id] = $dragon->getPresenter()->fullName;
     }
     asort($dragonList);
     return $dragonList;
 }
 public function edit($id, $level, Request $request, DragonLevelFieldService $dragonLevelFieldService)
 {
     if ($level < 1 || $level > Setting::get('max-level')) {
         Notification::add('error', 'The level requested for updating (' . $level . ') is invalid');
         return Redirect::to($request->get('from', Session::get('table', 'my-dragon')));
     }
     /** @var Dragon $dragon */
     $dragon = Dragon::find($id);
     if ($request->get('from')) {
         Session::flash('level-update-redirect', $request->get('from'));
     }
     /** @noinspection PhpUndefinedMethodInspection */
     return view('dragonLevel.edit')->with('dragon', $dragon)->with('level', $level)->with('fields', $dragonLevelFieldService->getFields($id, $level))->with('levelDetail', $dragon->getPresenter()->level((int) $level));
 }
 /**
  * Set the data for my dragon based on the request data
  * @param MyDragon $myDragon
  * @param mixed[] $requestData
  */
 protected function setData($myDragon, $requestData)
 {
     // Check the dragon is valid
     $dragon = Dragon::find($requestData['dragon_id']);
     if (!$dragon) {
         return;
     }
     $myDragon->dragon()->associate($dragon);
     // Fills name, level, highlight, flagged, training, collecting_iron
     $myDragon->fill($requestData);
     // Check if name can even be set, or nullify if empty
     if ($dragon->name || !$requestData['name']) {
         $myDragon->name = null;
     }
     // Training must be verified
     if (!$myDragon->getPresenter()->thisLevel->level_up_time) {
         $myDragon->training = false;
     }
     // Should also verify iron collection is possible
     if ($myDragon->dragon->iron_start_level === null || $myDragon->dragon->iron_start_level > $myDragon->level || !Auth::user()->collect_iron) {
         $myDragon->collecting_iron = false;
     }
     $myDragon->save();
 }
 public function isUpToDate($dragonID, $level)
 {
     $dragon = Dragon::with('levelUp.data', 'collectTime.data', 'collectRateFish.data', 'collectRateWood.data')->find($dragonID);
     // See if the dragon can collect wood at this level
     if ($dragon->wood_start_level !== null && $dragon->wood_start_level <= $level) {
         // Check there is data and it's up to date
         $woodC = $dragon->collectRateWood->data->where('level', (int) $level)->first();
         if (!$woodC || \OldData::isModelOld($woodC)) {
             return false;
         }
     }
     // See if the dragon can collect fish at this level
     if ($dragon->fish_start_level !== null && $dragon->fish_start_level <= $level) {
         // Check there is data and it's up to date
         $fishC = $dragon->collectRateFish->data->where('level', (int) $level)->first();
         if (!$fishC || \OldData::isModelOld($fishC)) {
             return false;
         }
     }
     // See if the dragon can collect either fish or wood at this level
     if ($this->startCollectingLevel($dragon) !== false && $this->startCollectingLevel($dragon) <= $level) {
         // Check there is data and it's up to date
         $rateC = $dragon->collectTime->data->where('level', (int) $level)->first();
         if (!$rateC || \OldData::isModelOld($rateC)) {
             return false;
         }
     }
     // See if the dragon can collect iron at this level (and the user can see iron details)
     if (Auth::user()->collect_iron && $dragon->iron_start_level !== null && $dragon->iron_start_level <= $level) {
         // Check there is data and it's up to date
         $ironC = $dragon->collectIronData->where('level', (int) $level)->first();
         if (!$ironC || \OldData::isModelOld($ironC)) {
             return false;
         }
     }
     // Get the level up data
     $levelUpData = $dragon->levelUp->data->where('level', (int) $level)->first();
     // Check there is data and it's up to date
     if (!$levelUpData || \OldData::isModelOld($levelUpData)) {
         return false;
     }
     // We've got this far, so we're good.
     return true;
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     /** @var Dragon $dragon */
     $dragon = Dragon::find($id);
     if ($dragon->owned->count()) {
         Notification::add('error', $dragon->getPresenter()->fullName . ' cannot be deleted; it is owned by users');
         return Redirect::route('dragon.show', ['id' => $id]);
     } else {
         $dragon->delete();
         Notification::add('success', $dragon->getPresenter()->fullName . ' deleted');
         return Redirect::route('dragon.index');
     }
 }