/** * creates the unit training task based on the submitted POST request. * * @param Request $request * @param $city_id * @param $slot_num * @param $building_id * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function getTrainUnit($city_id, $slot_num, $building_id, $type) { $city = City::find($city_id); if (!$this->validateOwner($city)) { return redirect('/home')->withErrors('Nem a te városod'); } if ($building = $this->buildingCompleted($building_id)) { // check if the building is completed TaskController::checkTasks(); // check if there any pending tasks and complete the finished ones if ($building->workers > 0) { // check if there's at least one worker in the building if ($building->type == 5) { // check if the type of the building is 'barrack' $lack_resource = $city->hasEnoughResources(Army::$unit_prices[$city->nation][$type]); if (empty($lack_resource)) { // check if the city has enough resources to train the unit if ($city->human_resources->population > 0) { // check if the city has enough population (i.e. at least 1) $city->human_resources->population -= 1; $city->human_resources->save(); // if everything is set, remove one from the population of the city and save the new population $city->resources->subtract(Army::$unit_prices[$city->nation][$type]); // remove the amount of resources needed by the training of the unit TaskController::createTask($building, $type + 10, Army::$unit_times[$city->nation][$type]); // create the task return redirect("/city/{$city_id}/slot/{$slot_num}/building/{$building_id}"); } return redirect("/city/{$city_id}/slot/{$slot_num}/building/{$building_id}")->withErrors(['not_enough_population' => 'Nincs elég népesség']); } $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}/slot/{$slot_num}/building/{$building_id}")->withErrors($messages); } return redirect("/city/{$city_id}/slot/{$slot_num}/building/{$building_id}")->withErrors(['not_a_forum' => 'Az épület nem tud munkást képezni']); } return redirect("/city/{$city_id}/slot/{$slot_num}/building/{$building_id}")->withErrors(['not_enough_worker' => 'Az épületben nem dolgozik munkás']); } return redirect("/city/{$city_id}"); }
/** * creates the path and task based on the request sent from the map * * @param Request $request * @return $this */ public function postMoveArmy(Request $request) { TaskController::checkTasks(); $path = $request->input('path'); $army_id = $request->input('army'); $army = Army::where('id', $army_id)->first(); if (!$army->general) { return false; } if (Auth::user()->id != $army->user_id) { return redirect('/home')->withErrors('Nem a te sereged'); } // calculate the speed of the army. // it is the speed of the slowest unit (i.e. the bigger number in speed table) $units = [1 => intval($army->unit1), 2 => intval($army->unit2), 3 => intval($army->unit3), 4 => intval($army->unit4), 5 => intval($army->unit5), 6 => intval($army->unit6), 7 => intval($army->unit7)]; $speed = []; foreach ($units as $key => $value) { if ($value > 0) { $speed[] = Army::$unit_speeds[$army->user->nation][$key]; } } $speed = max($speed); $path_id = DB::table('paths')->max('path_id') + 1; $time = 0; $finished = Carbon::now(); $l = sizeof($path); for ($i = 1; $i < $l; $i++) { $hex = Grid::where("x", $path[$i]['x'])->where('y', $path[$i]['y'])->select('id', 'type')->first(); $time += Grid::$price[intval($hex->type)] * $speed; $path_hex = Path::create(['path_id' => $path_id, 'hex_id' => $hex->id, 'started_at' => $finished]); $finished = Carbon::now()->addSeconds($time); $path_hex->finished_at = $finished; $path_hex->save(); } $task = TaskController::createTask($army, 20, $time); $task->path_id = $path_id; $task->save(); $army->task_id = $task->id; $army->path_id = $path_id; $army->save(); return $army->id; }
/** * @param $city_id * @param $slot_num */ public function getMakeGeneral($city_id, $slot_num, $building_id) { // TaskController::checkTasks(); // $city = City::where('id', $city_id)->first(); if ($city->army() && $city->army()->general) { return redirect("/city/{$city_id}/slot/{$slot_num}/building/{$building_id}")->withErrors(['already_has_general' => 'Már van egy tábornok a városban. Egyszerre csak egy lehet egy városban']); } if (!$this->validateOwner($city)) { return redirect('/home')->withErrors('Nem a te városod'); } if ($building = $this->buildingCompleted($building_id)) { if ($building->task->where('building_id', $building->id)->first()) { return redirect("/city/{$city_id}/slot/{$slot_num}/building/{$building_id}")->withErrors(['already_training' => 'Az épület használatban van']); } if ($building->workers > 0) { if ($building->type == 7) { $lack_resource = $city->hasEnoughResources(HumanResource::$general_price[$city->nation]); if (empty($lack_resource)) { if ($city->human_resources->population > 0) { $city->human_resources->population -= 1; $city->human_resources->save(); $city->resources->subtract(HumanResource::$general_price[$city->nation]); TaskController::createTask($building, 3, HumanResource::$general_time[$city->nation]); return redirect("/city/{$city_id}/slot/{$slot_num}/building/{$building_id}"); } return redirect("/city/{$city_id}/slot/{$slot_num}/building/{$building_id}")->withErrors(['not_enough_population' => 'Nincs elég népesség']); } 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}/slot/{$slot_num}/building/{$building_id}")->withErrors($messages); } } return redirect("/city/{$city_id}/slot/{$slot_num}/building/{$building_id}")->withErrors(['not_a_forum' => 'Az épület nem tud tábornokot képezni']); } return redirect("/city/{$city_id}/slot/{$slot_num}/building/{$building_id}")->withErrors(['not_enough_worker' => 'Az épületben nem dolgozik munkás']); } return redirect("/city/{$city_id}"); }