/** * creates a city and the belonging models (BuildingSlot, Resource, HumanResource) * saves the city in the map * * @param User $user The user to whom the city belongs * @param bool $capital Is the city a capital * @param int $hex_id the id of the hex on which the city is located * @param string $name the name of the city */ public static function create(array $attributes) { /** @var City $city */ $city = parent::create($attributes); /** @var BuildingSlot $slot */ $slot = BuildingSlot::create(['city_id' => $city->id]); Resource::create(['city_id' => $city->id]); HumanResource::create(['city_id' => $city->id]); $city->building_slot = $slot->id; $city->save(); /** @var Building $wall */ $wall = Building::create(['city_id' => $city->id, 'slot' => $slot->id, 'nation' => $attributes['nation'], 'type' => 9, 'finished_at' => Carbon::now()]); $slot->wall = $wall->id; $slot->save(); /** @var Grid $hex */ $hex = Grid::find($hex_id); $hex->update(['owner_id' => $attributes['owner'], 'city_id' => $city->id]); $hex->setNeighborsOwner($attributes['owner']); }
/** * Store a csv created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function import(Request $request) { if ($request->file('csv-file')->isValid()) { $csv_file = $request->file('csv-file'); if ("text/csv" == $csv_file->getClientMimeType()) { $dest_path = storage_path('temp'); $file_name = time() . '_' . str_replace(" ", "_", $csv_file->getClientOriginalName()); $csv_file->move($dest_path, $file_name); $fname = $dest_path . '/' . $file_name; $file = fopen($fname, "r"); $flash_message = []; $flash_error = 0; while (!feof($file)) { $tmp_data = fgetcsv($file); $item['code'] = !empty($tmp_data[0]) ? $tmp_data[0] : ''; $item['name'] = !empty($tmp_data[1]) ? $tmp_data[1] : ''; $v = Validator::make($item, ['code' => 'required|unique:buildings|max:20', 'name' => 'required|max:254']); if (!$v->fails()) { Building::create($item); $flash_message['success'][] = '[' . $item['code'] . '] ' . $item['name']; } else { $flash_error++; $flash_message['error'] = $flash_error; } } \Session::flash('flash_message', $flash_message); fclose($file); chmod($fname, 0777); unlink($fname); } } return redirect('/buildings'); }
/** * Creates a new building * * @param $type @int The type of the building. * @param City $city @int The id of the city in which the building is built. * @param $slot_num @int The number of the slot on which the building going to be built. * @param BuildingSlot $slot @int The id of the slot on which the building is built. * @return $this */ private function createBuilding($type, City $city, $slot_num) { $price = Building::$building_prices[$city->nation][$type]; $lack_resource = $city->hasEnoughResources($price); if (empty($lack_resource)) { $finished = Carbon::now()->addSeconds(Building::$building_times[$city->nation][$type]); $slot = $city->building_slot; $building = Building::create(['type' => $type, 'city_id' => $city->id, 'slot' => $slot->id, 'nation' => $city->nation, 'finished_at' => $finished]); $slot->update(["slot{$slot_num}" => $building->id]); $city->resources->subtract($price); } else { $messages = []; $resources = ['stone' => 'kő', 'lumber' => 'fa', 'food' => 'élelmiszer', 'iron' => 'vas']; foreach ($lack_resource as $key => $value) { $messages["not_enough_{$key}"] = "Még {$value} {$resources[$key]} hiányzik"; } return redirect("city/{$city->id}")->withErrors($messages); } }