/** * Set data from a request for the given dragon * * @param Dragon $dragon * @param mixed[] $requestData */ public function setData($dragon, $requestData) { $dragon->breed = $requestData['breed']; $dragon->hatching_time = $requestData['hatching-time']; $dragon->fish_start_level = zeroIsNull($requestData['fish-level']); $dragon->wood_start_level = zeroIsNull($requestData['wood-level']); $dragon->iron_start_level = zeroIsNull($requestData['iron-level']); if ($this->dragonTypeService->getIDsThatRequireNames()->contains($requestData['type'])) { $dragon->name = $requestData['name']; } else { $dragon->name = null; } $dragon->dragonType()->associate(DragonType::find($requestData['type'])); $dragon->collectTime()->associate($this->collectTimeService->getWhereMaxLevelIs($requestData['collection-time'])); $dragon->collectRateFish()->associate($this->collectRateService->getWhereMaxLevelIs($requestData['fish-rate'])); $dragon->collectRateWood()->associate($this->collectRateService->getWhereMaxLevelIs($requestData['wood-rate'])); $dragon->save(); $ironData = $dragon->collectIronData->where('level', (int) Setting::get('max-level'))->first(); if (!$ironData) { $ironData = new CollectIronData(); $ironData->level = Setting::get('max-level'); $dragon->collectIronData()->save($ironData); } $ironData->fill(['rate' => $requestData['iron-rate'], 'time' => $requestData['iron-time']]); $ironData->save(); }
/** * Update the iron information * @param Dragon $dragon * @param int $level * @param mixed[] $requestData */ public function updateIron($dragon, $level, $requestData) { $ironData = $dragon->collectIronData->where('level', $level)->first(); if (!$ironData) { $ironData = new CollectIronData(['level' => $level]); $dragon->collectIronData()->save($ironData); } foreach (['rate', 'time', 'wood', 'fish'] as $field) { $ironData->{$field} = isset($requestData['iron_collect_' . $field]) && $requestData['iron_collect_' . $field] ? $requestData['iron_collect_' . $field] : null; } $ironData->touch(); }