Exemplo n.º 1
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;
 }
Exemplo n.º 2
0
 public function run()
 {
     DB::table('paths')->delete();
     Path::create(['id' => 1, 'floor_level' => 1, 'svg_json' => '']);
 }