示例#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}");
 }
示例#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;
 }
示例#4
0
 /**
  * @param $city_id
  * @param $slot_num
  */
 public function getMakeSettler($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)) {
         TaskController::checkTasks();
         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::$settler_price[$city->nation]);
                 if (empty($lack_resource)) {
                     if ($city->resources->workers >= 5) {
                         if ($city->resources->population >= 10) {
                             $city->resources->workers -= 5;
                             $city->resources->population -= 10;
                             $city->resources->save();
                             $city->resources->subtract(HumanResource::$settler_price[$city->nation]);
                             TaskController::createTask($building, 2, HumanResource::$settler_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']);
                     }
                     return redirect("/city/{$city_id}/slot/{$slot_num}/building/{$building_id}")->withErrors(['not_enough_worker' => 'Nincs elég munkás']);
                 } 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 telepest 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}");
 }
示例#5
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]);
 }
示例#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;
     }
 }
示例#7
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');
 }
 /**
  * sets the number of workers in the selected building
  *
  * @param Request $request
  * @param $city_id
  * @param $slot_num
  * @param $building_id
  * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
  */
 public function postSetWorkers(Request $request, $city_id, $slot_num, $building_id)
 {
     $this->validate($request, ['workers' => 'required|integer']);
     $city = City::find($city_id);
     if (!$this->validateOwner($city)) {
         return redirect('/home')->withErrors('Nem a te városod');
     }
     if ($building = $this->buildingCompleted($building_id)) {
         TaskController::checkTasks();
         return $this->setWorkers($city, $building, $request);
     }
     return redirect("/city/{$city_id}");
 }