Inheritance: extends Controller
Example #1
0
 public function getWall($id)
 {
     TaskController::checkTasks();
     $city = City::where('id', $id)->first();
     if (!$this->validateOwner($city)) {
         return redirect('/home')->withErrors('Nem a te városod');
     }
     $wall = $city->building_slot->wall;
     $wall = Building::find($wall);
     $production = ResourceController::processProduction($city);
     return view('wall', ['city' => $city, 'wall' => $wall, 'production' => $production]);
 }
 /**
  * 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}");
 }
Example #3
0
 /**
  * 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;
 }
Example #4
0
 /**
  *
  */
 public function getHome()
 {
     $user = Auth::user();
     $username = $user->name;
     $cities = Auth::user()->cities;
     TaskController::checkTasks();
     $productions = [];
     if (!count($cities)) {
         // TODO if the user has not got any city.
     }
     foreach ($cities as $city) {
         $productions[$city->id] = ResourceController::processProduction($city);
     }
     $armies = $user->armies;
     $coords = [];
     foreach ($armies as $army) {
         $coords[$army->id] = ['x' => $army->currentHex->x, 'y' => $army->currentHex->y];
     }
     return view('home', ['username' => $username, 'cities' => $cities, 'help' => '/help/home', 'productions' => $productions, 'armies' => $armies, 'coords' => $coords]);
 }
Example #5
0
 public function manageupdate(Request $request)
 {
     $info = TaskController::getnumber($request->infoprob);
     $gain = TaskController::getnumber($request->gainprob);
     $adjust_common = TaskController::getnumber($request->adjustprob);
     $adjust_list = TaskController::getnumber($request->adjustlprob);
     $adjust_cheating = TaskController::getnumber($request->adjustcprob);
     $total = $info + $gain + $adjust_common + $adjust_list + $adjust_cheating;
     if ($total != 0) {
         TaskController::viewtodb(compact('msg', 'info', 'gain', 'adjust_common', 'adjust_list', 'adjust_cheating'));
     }
     return redirect()->action('TaskController@manage');
 }
Example #6
0
 public function getArmyData(Request $request)
 {
     TaskController::checkTasks();
     $army = Army::where('id', $request->input('army_id'))->first()->toArray();
     $hex = Grid::find($army['current_hex_id']);
     // if there's a city on the current hex
     if ($hex->city > 0) {
         $city = City::find($hex->city);
         $army['city_nation'] = intval($city->nation);
         $army['city_name'] = $city->name;
     }
     $army['hex_type'] = $hex->type;
     $army['hex_owner'] = User::find($hex->owner)->name;
     if (Auth::user()->id === $army['user_id']) {
         return $army;
     } else {
         $units = ['unit1' => 0, 'unit2' => 0, 'unit3' => 0, 'unit4' => 0, 'unit5' => 0, 'unit6' => 0, 'unit7' => 0];
         $army = array_diff_key($army, $units);
         $user = User::find($army['user_id']);
         $army['army_owner'] = $user->name;
         $army['nation'] = intval($user->nation);
         return $army;
     }
 }
 /**
  * Test destroyTask.
  */
 public function testDestroyTask()
 {
     $controller = new TaskController();
     $task = factory(\App\Task::class)->create($this->taskData);
     $this->expectsEvents('App\\Events\\TaskDeletedEvent');
     $this->response = $controller->destroyTask($task->id);
     $this->assertEquals(200, $this->response->status());
     // Check that the task cannot be found.
     $deletedTask = Task::find($task->id);
     $this->assertNull($deletedTask);
     // Check that task was soft-deleted.
     $deletedTask = Task::withTrashed()->find($task->id);
     $this->assertNotNull($deletedTask);
 }
Example #8
0
 /**
  * Obtain the user information from social media.
  *
  * @param Request $request
  * @param $provider
  * @return \Illuminate\Http\Response
  */
 public function handleProviderCallback(Request $request, $provider)
 {
     //notice we are not doing any validation, you should do it
     if ($request->has('error')) {
         return redirect('/');
     }
     $user = Socialite::driver($provider)->user();
     // storing data to our users table
     $data = ['name' => $user->getName(), 'email' => $user->getEmail(), 'verified' => 1];
     // login the user
     try {
         Auth::login(User::firstOrCreate($data));
     } catch (QueryException $e) {
         //        throw new UserCreateException();
         return redirect('/')->withErrors([Lang::get('auth.user')]);
     }
     TaskController::checkTasks();
     //after login redirecting to home page
     return redirect('home');
 }
 /**
  * @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}");
 }
 /**
  * deletes the selected building
  *
  * @param $city_id
  * @param $slot_num
  */
 public function getDeleteBuilding($city_id, $slot_num, $building_id)
 {
     $city = City::find($city_id);
     if (!$this->validateOwner($city)) {
         return redirect('/home')->withErrors('Nem a te városod');
     }
     if ($building = $this->buildingCompleted($building_id)) {
         $city->human_resources->add(['workers' => $building->workers]);
         $slot = "slot{$slot_num}";
         $city->building_slot->{$slot} = 0;
         $city->building_slot->save();
         $building->task->each(function ($task) {
             TaskController::undoTask($task);
             $task->delete();
         });
         $building->delete();
         return redirect("/city/{$city_id}");
     }
     return redirect("/city/{$city_id}")->withErrors(['not_yet' => 'Az épület még nincs kész']);
 }