public function update($id, $requestData)
 {
     /** @var Dragon $dragon */
     $dragon = Dragon::find($id);
     $this->setData($dragon, $requestData);
     return $dragon;
 }
 /**
  * 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 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();
 }
 /**
  * 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');
     }
 }