Passed
Push — master ( 4cbcfa...1ee8de )
by Nico
53:18 queued 29:25
created

BuildingAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\Lib;
6
7
use Stu\Component\Building\BuildingManagerInterface;
8
use Stu\Component\Colony\Storage\ColonyStorageManagerInterface;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Orm\Entity\ColonyInterface;
11
use Stu\Orm\Entity\ColonySandboxInterface;
12
use Stu\Orm\Entity\PlanetFieldInterface;
13
14
final class BuildingAction implements BuildingActionInterface
15
{
16
    private ColonyStorageManagerInterface $colonyStorageManager;
17
18
    private BuildingManagerInterface $buildingManager;
19
20 1
    public function __construct(
21
        ColonyStorageManagerInterface $colonyStorageManager,
22
        BuildingManagerInterface $buildingManager
23
    ) {
24 1
        $this->colonyStorageManager = $colonyStorageManager;
25 1
        $this->buildingManager = $buildingManager;
26
    }
27
28
    public function activate(PlanetFieldInterface $field, GameControllerInterface $game): void
29
    {
30
        if (!$field->hasBuilding()) {
31
            return;
32
        }
33
        if (!$field->isActivateAble()) {
34
            return;
35
        }
36
        if ($field->isActive()) {
37
            return;
38
        }
39
        if ($field->hasHighDamage()) {
40
            $game->addInformationf(
41
                _('Das Gebäude (%s) kann aufgrund zu starker Beschädigung nicht aktiviert werden'),
42
                $field->getBuilding()->getName()
43
            );
44
            return;
45
        }
46
47
        $host = $field->getHost();
48
        if (
49
            $host instanceof ColonyInterface
50
            && $host->getWorkless() < $field->getBuilding()->getWorkers()
51
        ) {
52
            $game->addInformationf(
53
                _('Zum Aktivieren des Gebäudes (%s) werden %s Arbeiter benötigt'),
54
                $field->getBuilding()->getName(),
55
                $field->getBuilding()->getWorkers()
56
            );
57
            return;
58
        }
59
60
        $this->buildingManager->activate($field);
61
62
        $game->addInformationf(
63
            _('%s auf Feld %s wurde aktiviert'),
64
            $field->getBuilding()->getName(),
65
            $field->getFieldId()
66
        );
67
    }
68
69
    public function deactivate(PlanetFieldInterface $field, GameControllerInterface $game): void
70
    {
71
        if (!$field->hasBuilding()) {
72
            return;
73
        }
74
        if (!$field->isActivateAble()) {
75
            return;
76
        }
77
        if (!$field->isActive()) {
78
            return;
79
        }
80
81
        $this->buildingManager->deactivate($field);
82
83
        $game->addInformationf(
84
            _('%s auf Feld %s wurde deaktiviert'),
85
            $field->getBuilding()->getName(),
86
            $field->getFieldId()
87
        );
88
    }
89
90 1
    public function remove(
91
        PlanetFieldInterface $field,
92
        GameControllerInterface $game,
93
        bool $isDueToUpgrade = false
94
    ): void {
95
96 1
        if (!$field->hasBuilding()) {
97
            return;
98
        }
99
100 1
        $building = $field->getBuilding();
101
102 1
        if (!$isDueToUpgrade && !$building->isRemovable()) {
103
            return;
104
        }
105
106 1
        $this->buildingManager->remove($field, $isDueToUpgrade);
107
108 1
        $game->addInformationf(
109 1
            _('%s auf Feld %d wurde demontiert'),
110 1
            $building->getName(),
111 1
            $field->getFieldId()
112 1
        );
113
114
115 1
        $host = $field->getHost();
116 1
        if ($host instanceof ColonySandboxInterface) {
117 1
            return;
118
        }
119
120
        $game->addInformation(_('Es konnten folgende Waren recycled werden'));
121
122
        foreach ($building->getCosts() as $value) {
123
            $halfAmount = $value->getHalfAmount();
124
            if ($host->getStorageSum() + $halfAmount > $host->getMaxStorage()) {
125
                $amount = $host->getMaxStorage() - $host->getStorageSum();
126
            } else {
127
                $amount = $halfAmount;
128
            }
129
            if ($amount <= 0) {
130
                $game->addInformation(_('[b][color=#ff2626]Keine weiteren Lagerkapazitäten vorhanden![/color][/b]'));
131
                break;
132
            }
133
            $this->colonyStorageManager->upperStorage($host, $value->getCommodity(), $amount);
134
135
            $game->addInformationf('%d %s', $amount, $value->getCommodity()->getName());
136
        }
137
    }
138
}
139