1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\Colony\Lib; |
6
|
|
|
|
7
|
|
|
use Override; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Stu\Component\Building\BuildingManagerInterface; |
10
|
|
|
use Stu\Lib\Transfer\Storage\StorageManagerInterface; |
11
|
|
|
use Stu\Module\Commodity\CommodityTypeConstants; |
12
|
|
|
use Stu\Orm\Entity\Building; |
13
|
|
|
use Stu\Orm\Entity\Colony; |
14
|
|
|
use Stu\Orm\Entity\PlanetField; |
15
|
|
|
use Stu\Orm\Entity\User; |
16
|
|
|
use Stu\Orm\Repository\ColonyRepositoryInterface; |
17
|
|
|
use Stu\Orm\Repository\CommodityRepositoryInterface; |
18
|
|
|
use Stu\Orm\Repository\PlanetFieldRepositoryInterface; |
19
|
|
|
|
20
|
|
|
final class PlanetColonization implements PlanetColonizationInterface |
21
|
|
|
{ |
22
|
|
|
private const int COLONY_FIELDTYPE_MEADOW = 101; |
|
|
|
|
23
|
|
|
|
24
|
1 |
|
public function __construct( |
25
|
|
|
private readonly PlanetFieldRepositoryInterface $planetFieldRepository, |
26
|
|
|
private readonly CommodityRepositoryInterface $commodityRepository, |
27
|
|
|
private readonly StorageManagerInterface $storageManager, |
28
|
|
|
private readonly ColonyLibFactoryInterface $colonyLibFactory, |
29
|
|
|
private readonly ColonyRepositoryInterface $colonyRepository, |
30
|
|
|
private readonly BuildingManagerInterface $buildingManager |
31
|
1 |
|
) {} |
32
|
|
|
|
33
|
|
|
#[Override] |
34
|
|
|
public function colonize( |
35
|
|
|
Colony $colony, |
36
|
|
|
User $user, |
37
|
|
|
Building $building, |
38
|
|
|
?PlanetField $field = null |
39
|
|
|
): void { |
40
|
|
|
if (!$colony->isFree()) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->colonyLibFactory->createColonySurface($colony)->updateSurface(); |
45
|
|
|
|
46
|
|
|
if ($field === null) { |
47
|
|
|
$list = $this->planetFieldRepository->getByColonyAndType( |
48
|
|
|
$colony->getId(), |
49
|
|
|
self::COLONY_FIELDTYPE_MEADOW |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
shuffle($list); |
53
|
|
|
|
54
|
|
|
/** @var PlanetField $field */ |
55
|
|
|
$field = current($list); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$field->setBuilding($building); |
59
|
|
|
|
60
|
|
|
$colony->getChangeable()->setWorkless($building->getHousing()); |
61
|
|
|
$colony->getChangeable()->setEps($building->getEpsStorage()); |
62
|
|
|
|
63
|
|
|
$this->buildingManager->finish($field, true); |
64
|
|
|
|
65
|
|
|
$colony->setUser($user); |
66
|
|
|
$colony->setName(_('Kolonie')); |
67
|
|
|
|
68
|
|
|
$this->colonyRepository->save($colony); |
69
|
|
|
$this->planetFieldRepository->save($field); |
70
|
|
|
|
71
|
|
|
$commodity = $this->commodityRepository->find(CommodityTypeConstants::COMMODITY_BUILDING_MATERIALS); |
72
|
|
|
if ($commodity === null) { |
73
|
|
|
throw new RuntimeException('commodity does not exist'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->storageManager->upperStorage( |
77
|
|
|
$colony, |
78
|
|
|
$commodity, |
79
|
|
|
150 |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|