/**
  * Sets up the user and the city for the game
  *
  * @param Request $request
  * @return \Illuminate\View\View
  */
 public function postSetup(Request $request)
 {
     $validator = $this->validator($request->all());
     if ($validator->fails()) {
         $this->throwValidationException($request, $validator);
     }
     $user = Auth::user();
     $nation = $request->input('nation');
     $user->setNation($nation);
     $hex_id = Grid::randomHex()->id;
     $name = $request->input('name');
     City::create(['name' => $name, 'nation' => $user->nation, 'capital' => true, 'owner' => $user->id, 'hex_id' => $hex_id]);
     return redirect('home');
 }
Example #2
0
 /**
  * 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']);
 }
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 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;
     }
 }
Example #5
0
 /**
  * returns an array containing the current hex neighbors.
  *
  * @return array
  */
 private function neighbors()
 {
     $x = $this->x;
     $y = $this->y;
     $directions = [0 => [1 => ['x' => +1, 'y' => 0], 2 => ['x' => +1, 'y' => -1], 3 => ['x' => 0, 'y' => -1], 4 => ['x' => -1, 'y' => -1], 5 => ['x' => -1, 'y' => 0], 6 => ['x' => 0, 'y' => +1]], 1 => [1 => ['x' => +1, 'y' => +1], 2 => ['x' => +1, 'y' => 0], 3 => ['x' => 0, 'y' => -1], 4 => ['x' => -1, 'y' => 0], 5 => ['x' => -1, 'y' => +1], 6 => ['x' => 0, 'y' => +1]]];
     $parity = $x & 1;
     $neighbors = [];
     foreach ($directions[$parity] as $dir) {
         $nx = $x + $dir['x'];
         $ny = $y + $dir['y'];
         /** @var Grid $neighbor */
         $neighbor = Grid::where('x', $nx)->where('y', $ny)->first();
         if ($neighbor !== null) {
             array_push($neighbors, $neighbor);
         }
     }
     return $neighbors;
 }