Passed
Pull Request — master (#1696)
by Nico
49:43 queued 22:34
created

ShieldingProvider::buildShieldBar()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 21
nc 20
nop 2
dl 0
loc 34
ccs 0
cts 22
cp 0
crap 42
rs 8.9617
c 1
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Colony\Lib\Gui\Component;
4
5
use Stu\Component\Colony\Shields\ColonyShieldingManagerInterface;
6
use Stu\Lib\Colony\PlanetFieldHostInterface;
7
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
8
use Stu\Module\Control\GameControllerInterface;
9
use Stu\Module\Tal\StatusBarColorEnum;
10
use Stu\Orm\Entity\ColonyInterface;
11
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
12
13
final class ShieldingProvider implements GuiComponentProviderInterface
14
{
15
    private ColonyLibFactoryInterface $colonyLibFactory;
16
17
    private PlanetFieldRepositoryInterface $planetFieldRepository;
18
19
    public function __construct(
20
        ColonyLibFactoryInterface $colonyLibFactory,
21
        PlanetFieldRepositoryInterface $planetFieldRepository
22
    ) {
23
        $this->colonyLibFactory = $colonyLibFactory;
24
        $this->planetFieldRepository = $planetFieldRepository;
25
    }
26
27
    public function setTemplateVariables(
28
        PlanetFieldHostInterface $host,
29
        GameControllerInterface $game
30
    ): void {
31
        $shieldingManager = $this->colonyLibFactory->createColonyShieldingManager($host);
32
33
        $game->setTemplateVar('SHIELDING_MANAGER', $shieldingManager);
34
35
        if ($shieldingManager->hasShielding()) {
36
            $game->setTemplateVar(
37
                'SHIELD_STATUS_BAR',
38
                $this->buildShieldBar($shieldingManager, $host)
39
            );
40
41
            $game->setTemplateVar('SHIELD_BAR_TITLE_STRING', $this->getShieldBarTitleString($host));
42
        }
43
    }
44
45
    private function getShieldBarTitleString(PlanetFieldHostInterface $host): string
46
    {
47
        return sprintf(
48
            'Schildstärke: %d/%d',
49
            $host instanceof ColonyInterface ? $host->getShields() : 0,
50
            $this->planetFieldRepository->getMaxShieldsOfHost($host)
51
        );
52
    }
53
54
    private function buildShieldBar(
55
        ColonyShieldingManagerInterface $colonyShieldingManager,
56
        PlanetFieldHostInterface $host
57
    ): string {
58
        $shieldBar = '';
59
        $bars = [];
60
        $width = 360;
61
62
        $currentShields = $host instanceof ColonyInterface ? $host->getShields() : 0;
63
        $maxShields = $colonyShieldingManager->getMaxShielding();
64
65
        if ($colonyShieldingManager->isShieldingEnabled()) {
66
            $bars[StatusBarColorEnum::STATUSBAR_SHIELD_ON] = $currentShields;
67
        } else {
68
            $bars[StatusBarColorEnum::STATUSBAR_SHIELD_OFF] = $currentShields;
69
        }
70
        $bars[StatusBarColorEnum::STATUSBAR_GREY] = $maxShields - $currentShields;
71
72
        foreach ($bars as $color => $value) {
73
            if ($maxShields < $value) {
74
                $value = $maxShields;
75
            }
76
            if ($value <= 0) {
77
                continue;
78
            }
79
            $shieldBar .= sprintf(
80
                '<img src="/assets/bars/balken.png" style="background-color: #%s;height: 12px; width: %dpx;" title="%s" />',
81
                $color,
82
                round($width / 100 * (100 / $maxShields * $value)),
83
                'Schildstärke'
84
            );
85
        }
86
87
        return $shieldBar;
88
    }
89
}
90