Skip to content

Commit

Permalink
Merge pull request #132 from lanedirt/97-add-fleet-send-error-handlin…
Browse files Browse the repository at this point in the history
…g-in-frontend

Add fleet send error handling in frontend
Add colonisation mission
  • Loading branch information
lanedirt authored May 4, 2024
2 parents 7feb2ea + d22deb4 commit c02bbc1
Show file tree
Hide file tree
Showing 35 changed files with 2,345 additions and 812 deletions.
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ If you have an idea or suggestion, feel free to start a discussion on the [Discu
## Pull Requests
If you would like to contribute via pull requests, a good way to get started is to filter the issues list by the [good first issues](https://github.com/lanedirt/OGameX/labels/good%20first%20issue) label. This label is used for issues that are easy to fix and a good starting point for new contributors.

Please see the [Installation section](https://github.com/lanedirt/OGameX#installation) in the main README.md for how to get your local development environment setup.
[![good first issues open](https://img.shields.io/github/issues/lanedirt/OGameX/good%20first%20issue.svg?logo=github)](https://github.com/lanedirt/OGameX/issues?q=is%3Aopen+is%3Aissue+label%3A"good+first+issue")

Refer to the [Installation section](https://github.com/lanedirt/OGameX#installation) in the main README.md for how to get your local development environment setup.

When submitting a pull request, please make sure to follow these guidelines:

Expand Down
60 changes: 60 additions & 0 deletions app/Factories/GameMissionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace OGame\Factories;

use Illuminate\Contracts\Container\BindingResolutionException;
use OGame\GameMissions\Abstracts\GameMission;
use OGame\GameMissions\ColonisationMission;
use OGame\GameMissions\DeploymentMission;
use OGame\GameMissions\TransportMission;

class GameMissionFactory
{
/**
* @return array<GameMission>
* @throws BindingResolutionException
*/
public static function getAllMissions(): array
{
/*
{
"1": "Attack",
"2": "ACS Attack",
"3": "Transport",
"4": "Deployment",
"5": "ACS Defend",
"6": "Espionage",
"7": "Colonisation",
"8": "Recycle Debris Field",
"9": "Moon Destruction",
"15": "Expedition"
}
*/
return [
3 => app()->make(TransportMission::class),
4 => app()->make(DeploymentMission::class),
7 => app()->make(ColonisationMission::class),
];
}

/**
* @param int $missionId
* @param array<string,mixed> $dependencies
*
* @return GameMission
* @throws BindingResolutionException
*/
public static function getMissionById(int $missionId, array $dependencies): GameMission
{
switch ($missionId) {
case 3:
return app()->make(TransportMission::class, $dependencies);
case 4:
return app()->make(DeploymentMission::class, $dependencies);
case 7:
return app()->make(ColonisationMission::class, $dependencies);
}
$missions = self::getAllMissions();
return $missions[$missionId];
}
}
50 changes: 37 additions & 13 deletions app/Factories/PlanetServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OGame\Factories;

use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Carbon;
use OGame\Models\Planet;
Expand Down Expand Up @@ -92,7 +93,7 @@ public function makeForPlayer(PlayerService $player, int $planetId): PlanetServi
* @return ?PlanetService
* @throws BindingResolutionException
*/
public function makeForCoordinate(Planet\Coordinate $coordinate): ?PlanetService
public function makeForCoordinate(Coordinate $coordinate): ?PlanetService
{
if (!isset($this->instancesByCoordinate[$coordinate->asString()])) {
// Get the planet ID from the database.
Expand All @@ -116,10 +117,10 @@ public function makeForCoordinate(Planet\Coordinate $coordinate): ?PlanetService
/**
* Determine next available new planet position.
*
* @return Planet\Coordinate
* @throws \Exception
* @return Coordinate
* @throws Exception
*/
public function determineNewPlanetPosition(): Planet\Coordinate
public function determineNewPlanetPosition(): Coordinate
{
$lastAssignedGalaxy = (int)$this->settings->get('last_assigned_galaxy', 1);
$lastAssignedSystem = (int)$this->settings->get('last_assigned_system', 1);
Expand Down Expand Up @@ -162,29 +163,56 @@ public function determineNewPlanetPosition(): Planet\Coordinate
}

// If more than 100 tries have been done with no success, give up.
throw new \Exception('Unable to determine new planet position.');
throw new Exception('Unable to determine new planet position.');
}

/**
* Creates a new random planet and then return the planetService instance for it.
* Creates a new random initial planet for a player and then return the planetService instance for it.
*
* @param PlayerService $player
* @param string $planetName
* @return PlanetService
* @throws BindingResolutionException
*/
public function createForPlayer(PlayerService $player, string $planetName = 'Colony'): PlanetService
public function createInitialForPlayer(PlayerService $player, string $planetName): PlanetService
{
$new_position = $this->determineNewPlanetPosition();
if (empty($new_position->galaxy) || empty($new_position->system) || empty($new_position->position)) {
// Failed to get a new position for the to be created planet. Throw exception.
throw new \Exception('Unable to determine new planet position.');
throw new Exception('Unable to determine new planet position.');
}

$createdPlanet = $this->createPlanet($player, $new_position, $planetName);

// Update settings with the last assigned galaxy and system if they changed.
$this->settings->set('last_assigned_galaxy', $createdPlanet->getPlanetCoordinates()->galaxy);
$this->settings->set('last_assigned_system', $createdPlanet->getPlanetCoordinates()->system);

return $createdPlanet;
}

/**
* Creates a new planet for a player at the given coordinate and then return the planetService instance for it.
*
* @param PlayerService $player
* @param Coordinate $coordinate
* @return PlanetService
* @throws BindingResolutionException
*/
public function createAdditionalForPlayer(PlayerService $player, Coordinate $coordinate): PlanetService
{
return $this->createPlanet($player, $coordinate, 'Colony');
}

/**
* @throws BindingResolutionException
*/
private function createPlanet(PlayerService $player, Coordinate $new_position, string $planet_name): PlanetService
{
// Position is available
$planet = new Planet();
$planet->user_id = $player->getId();
$planet->name = $planetName;
$planet->name = $planet_name;
$planet->galaxy = $new_position->galaxy;
$planet->system = $new_position->system;
$planet->planet = $new_position->position;
Expand All @@ -211,10 +239,6 @@ public function createForPlayer(PlayerService $player, string $planetName = 'Col
$planet->time_last_update = (int)Carbon::now()->timestamp;
$planet->save();

// Update settings with the last assigned galaxy and system if they changed.
$this->settings->set('last_assigned_galaxy', $new_position->galaxy);
$this->settings->set('last_assigned_system', $new_position->system);

// Now make and return the planetService.
return $this->makeForPlayer($player, $planet->id);
}
Expand Down
Loading

0 comments on commit c02bbc1

Please sign in to comment.