public static function processSiege(Army $army, City $city) { // TODO check if the army has catapult // TODO message users about the result $attack_point = $army->calculateAttackingPoints(); $defense_point = $city->calculateDefensePoints(); if ($attack_point > $defense_point) { // attacker wins // the city becomes the attacker's city $city->update(['owner' => $army->user->id, 'capital' => 0, 'nation' => $army->user->nation]); $city->hex->update(['owner' => $army->user->id]); } elseif ($attack_point < $defense_point) { // defender wins $army->destroyArmy(); } }
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; } }
/** * @param Task $task */ public static function finishTask(Task $task) { switch ($task->type) { case 1: // create worker $task->building->city->human_resources->workers += 1; $task->building->city->human_resources->save(); $task->delete(); break; case 2: // create settler $task->building->city->human_resources->settlers += 1; $task->building->city->human_resources->save(); $task->delete(); break; case 3: // create a general if ($task->building->city->hex->army_id == 0) { $army = Army::create(['user_id' => $task->building->city->owner, 'current_hex_id' => $task->building->city->hex->id]); $task->building->city->hex->army_id = $army->id; $task->building->city->hex->save(); } if ($task->building->city->hex->army->general) { break; } $task->building->city->hex->army->update(['general' => true]); $task->delete(); break; case 11: // create unit // create unit case 12: case 13: case 14: case 15: case 16: case 17: if ($task->building->city->hex->army_id == 0) { $army = Army::create(['user_id' => $task->building->city->owner, 'current_hex_id' => $task->building->city->hex->id]); $task->building->city->hex->army_id = $army->id; $task->building->city->hex->save(); } $unit_type = 'unit' . ($task->type - 10); $task->building->city->hex->army->{$unit_type} += 1; $task->building->city->hex->army->save(); $task->delete(); break; case 20: // move army ArmyController::pathProgress($task); break; } }
public function move($id, Request $request) { $delay = 30; $army = Army::find($id); $army->move_to_x = $request->input('move_to_x'); $army->move_to_y = $request->input('move_to_y'); $job = (new \App\Jobs\MoveArmy($army))->delay($delay)->onQueue('moves'); $this->dispatch($job); $army->move_at = round(microtime(true)) + $delay; $army->save(); }
/** * @param Path $second * @param Army $attacking */ public static function processFriendlyArmiesMeeting(Path $second, Army $attacking) { $path = $second->path; $previous = $second; if ($second->id !== $path->min('id')) { $previous_id = $second->id - 1; $previous = Path::where('id', '=', $previous_id)->first(); } $previous->hex->army_id = $attacking->id; $previous->deletePath(); $attacking->task->delete(); $attacking->current_hex_id = $previous->hex->id; $attacking->task_id = 0; $attacking->path_id = 0; $attacking->currentHex->update(['army_id' => 0]); $attacking->save(); }
/** * Display a listing of the resource. * * @return Response */ public function index() { return 'Hello'; $armies = Army::lists('Army_name', 'Id'); return view('test', ['armies' => $armies]); }
public function getUnfriendlyArmies($position) { $result = array(); if ($position->player == null) { array_push($result, $this->getNeutralArmy()); return $result; } $armies = Army::where('position_id', $position->id); foreach ($armies as $army) { if ($this->isArmyFriendly($army)) { array_push($result, $army); } } return $result; }
/** * Chooses the winner army based on attack and defense points * * @param Army $attacking_army * @return void */ public function processBattle(Army $attacking_army) { /** * attacking army's attack points * * @var int $attacking_attack */ $attacking_attack = $attacking_army->calculateAttackingPoints(); /** * attacked army's defense points * * @var int $attacked_defense */ $attacked_defense = $this->calculateDefensePoints(); // TODO include hex modifier in the calculation /** @var int $point */ $point = $attacking_attack - $attacked_defense; if ($point === 0) { // it's a tie $point += rand(-1, 1); // randomly add or remove 1 so the $point will be 1 or -1 } if ($point > 0) { // the attacking army wins /** @var Army $winner */ $winner = $attacking_army; /** @var Army $loser */ $loser = $this; } elseif ($point < 0) { // the attacked army wins /** @var Army $winner */ $winner = $this; /** @var Army $loser */ $loser = $attacking_army; } // TODO $this::calculateCasualties(); /** @var Army $loser */ $loser->destroyArmy(); // TODO a pontok alapján kiszámolni a veszteségeket és ennek megfelelően módosítani a seregeket // jelenleg a vesztes sereg teljesen megsemmisül, a győztesnek nincs vesztesége és folytatja az útját // TODO a csata eredményét megjeleníteni a felhasználónak }