|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace FrankProjects\UltimateWarfare\Service; |
| 6 | + |
| 7 | +use FrankProjects\UltimateWarfare\Entity\Enum\GameUnitEnum; |
| 8 | +use FrankProjects\UltimateWarfare\Entity\Player; |
| 9 | +use FrankProjects\UltimateWarfare\Entity\World; |
| 10 | +use FrankProjects\UltimateWarfare\Entity\WorldRegion; |
| 11 | +use FrankProjects\UltimateWarfare\Entity\WorldRegionUnit; |
| 12 | +use FrankProjects\UltimateWarfare\Repository\WorldRegionRepository; |
| 13 | +use FrankProjects\UltimateWarfare\Repository\WorldRegionUnitRepository; |
| 14 | +use FrankProjects\UltimateWarfare\Util\DistanceCalculator; |
| 15 | +use RuntimeException; |
| 16 | + |
| 17 | +final class PlayerSetupService |
| 18 | +{ |
| 19 | + private WorldRegionRepository $worldRegionRepository; |
| 20 | + private WorldRegionUnitRepository $worldRegionUnitRepository; |
| 21 | + private DistanceCalculator $distanceCalculator; |
| 22 | + private NetWorthUpdaterService $netWorthUpdaterService; |
| 23 | + private IncomeUpdaterService $incomeUpdaterService; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + WorldRegionRepository $worldRegionRepository, |
| 27 | + WorldRegionUnitRepository $worldRegionUnitRepository, |
| 28 | + DistanceCalculator $distanceCalculator, |
| 29 | + NetWorthUpdaterService $netWorthUpdaterService, |
| 30 | + IncomeUpdaterService $incomeUpdaterService |
| 31 | + ) { |
| 32 | + $this->worldRegionRepository = $worldRegionRepository; |
| 33 | + $this->worldRegionUnitRepository = $worldRegionUnitRepository; |
| 34 | + $this->distanceCalculator = $distanceCalculator; |
| 35 | + $this->netWorthUpdaterService = $netWorthUpdaterService; |
| 36 | + $this->incomeUpdaterService = $incomeUpdaterService; |
| 37 | + } |
| 38 | + |
| 39 | + public function setupNewPlayer(Player $player, World $world): void |
| 40 | + { |
| 41 | + $region = $this->findStartingRegion($world); |
| 42 | + |
| 43 | + $region->setPlayer($player); |
| 44 | + $player->getWorldRegions()->add($region); |
| 45 | + $this->worldRegionRepository->save($region); |
| 46 | + |
| 47 | + $this->createStartingUnits($region); |
| 48 | + |
| 49 | + $this->netWorthUpdaterService->updateNetWorthForPlayer($player); |
| 50 | + $this->incomeUpdaterService->updateIncomeForPlayer($player); |
| 51 | + } |
| 52 | + |
| 53 | + private function findStartingRegion(World $world): WorldRegion |
| 54 | + { |
| 55 | + $waterTypes = [ |
| 56 | + WorldRegion::TYPE_DEEP_WATER, |
| 57 | + WorldRegion::TYPE_WATER, |
| 58 | + WorldRegion::TYPE_SHALLOW_WATER, |
| 59 | + ]; |
| 60 | + |
| 61 | + $unownedRegions = $this->worldRegionRepository->findByWorldAndPlayer($world, null); |
| 62 | + |
| 63 | + $landRegions = []; |
| 64 | + foreach ($unownedRegions as $region) { |
| 65 | + if (!in_array($region->getType(), $waterTypes, true)) { |
| 66 | + $landRegions[] = $region; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if ($landRegions === []) { |
| 71 | + throw new RuntimeException('No available land regions in this world!'); |
| 72 | + } |
| 73 | + |
| 74 | + $ownedRegions = []; |
| 75 | + foreach ($world->getWorldRegions() as $region) { |
| 76 | + if ($region->getPlayer() !== null) { |
| 77 | + $ownedRegions[] = $region; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if ($ownedRegions === []) { |
| 82 | + return $landRegions[0]; |
| 83 | + } |
| 84 | + |
| 85 | + $bestRegion = $landRegions[0]; |
| 86 | + $bestMinDistance = 0; |
| 87 | + |
| 88 | + foreach ($landRegions as $candidate) { |
| 89 | + $minDistance = PHP_INT_MAX; |
| 90 | + |
| 91 | + foreach ($ownedRegions as $owned) { |
| 92 | + $distance = $this->distanceCalculator->calculateDistance( |
| 93 | + $candidate->getX(), |
| 94 | + $candidate->getY(), |
| 95 | + $owned->getX(), |
| 96 | + $owned->getY() |
| 97 | + ); |
| 98 | + |
| 99 | + if ($distance < $minDistance) { |
| 100 | + $minDistance = $distance; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if ($minDistance > $bestMinDistance) { |
| 105 | + $bestMinDistance = $minDistance; |
| 106 | + $bestRegion = $candidate; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return $bestRegion; |
| 111 | + } |
| 112 | + |
| 113 | + private function createStartingUnits(WorldRegion $region): void |
| 114 | + { |
| 115 | + $startingUnits = [ |
| 116 | + [GameUnitEnum::ECONOMIC_CENTER, 1000], |
| 117 | + [GameUnitEnum::FARM, 100], |
| 118 | + [GameUnitEnum::MINE, 100], |
| 119 | + [GameUnitEnum::WOODCUTTER, 250], |
| 120 | + [GameUnitEnum::SOLDIER, 10], |
| 121 | + ]; |
| 122 | + |
| 123 | + foreach ($startingUnits as [$gameUnit, $amount]) { |
| 124 | + $worldRegionUnit = WorldRegionUnit::create($region, $gameUnit, $amount); |
| 125 | + $this->worldRegionUnitRepository->save($worldRegionUnit); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments