Passed
Push — dev ( 392257...472d6e )
by Janko
10:08
created

EpsBarProvider::getEpsBarTitleString()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.1158

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 10
nop 2
dl 0
loc 27
ccs 15
cts 18
cp 0.8333
crap 5.1158
rs 9.3554
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Colony\Lib\Gui\Component;
4
5
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Stu\Lib\Colony\PlanetFieldHostInterface;
7
use Stu\Module\Control\GameControllerInterface;
8
use Stu\Module\Template\StatusBarColorEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Template\StatusBarColorEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Stu\Orm\Entity\ColonyInterface;
10
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
11
12
final class EpsBarProvider implements GuiComponentProviderInterface
13
{
14 2
    public function __construct(private PlanetFieldRepositoryInterface $planetFieldRepository) {}
15
16 4
    #[Override]
17
    public function setTemplateVariables(
18
        PlanetFieldHostInterface $host,
19
        GameControllerInterface $game
20
    ): void {
21 4
        $energyProduction = $this->planetFieldRepository->getEnergyProductionByHost($host);
22
23 4
        $game->setTemplateVar('EPS_STATUS_BAR', $this->getEpsStatusBar($host, $energyProduction));
24 4
        $game->setTemplateVar('EPS_PRODUCTION', $energyProduction);
25 4
        $game->setTemplateVar('EPS_BAR_TITLE_STRING', $this->getEpsBarTitleString($host, $energyProduction));
26
    }
27
28 4
    public static function getEpsStatusBar(PlanetFieldHostInterface $host, int $energyProduction, int $width = 360): string
29
    {
30 4
        $currentEps = $host instanceof ColonyInterface ? $host->getEps() : 0;
31
32 4
        $bars = [];
33 4
        $epsBar = '';
34 4
        if ($energyProduction < 0) {
35
            $prod = abs($energyProduction);
36
            if ($currentEps - $prod < 0) {
37
                $bars[StatusBarColorEnum::STATUSBAR_RED] = $currentEps;
38
                $bars[StatusBarColorEnum::STATUSBAR_GREY] = $host->getMaxEps() - $currentEps;
39
            } else {
40
                $bars[StatusBarColorEnum::STATUSBAR_YELLOW] = $currentEps - $prod;
41
                $bars[StatusBarColorEnum::STATUSBAR_RED] = $prod;
42
                $bars[StatusBarColorEnum::STATUSBAR_GREY] = $host->getMaxEps() - $currentEps;
43
            }
44
        }
45 4
        if ($energyProduction > 0) {
46 4
            if ($currentEps + $energyProduction > $host->getMaxEps()) {
47 3
                $bars[StatusBarColorEnum::STATUSBAR_YELLOW] = $currentEps;
48 3
                if ($currentEps < $host->getMaxEps()) {
49
                    $bars[StatusBarColorEnum::STATUSBAR_GREEN] = $host->getMaxEps() - $currentEps;
50
                }
51
            } else {
52 1
                $bars[StatusBarColorEnum::STATUSBAR_YELLOW] = $currentEps;
53 1
                $bars[StatusBarColorEnum::STATUSBAR_GREEN] = $energyProduction;
54 1
                $bars[StatusBarColorEnum::STATUSBAR_GREY] = $host->getMaxEps() - $currentEps - $energyProduction;
55
            }
56
        }
57 4
        if ($energyProduction == 0) {
58
            $bars[StatusBarColorEnum::STATUSBAR_YELLOW] = $currentEps;
59
            $bars[StatusBarColorEnum::STATUSBAR_GREY] = $host->getMaxEps() - $currentEps;
60
        }
61 4
        foreach ($bars as $color => $value) {
62 4
            if ($host->getMaxEps() < $value) {
63
                $value = $host->getMaxEps();
64
            }
65 4
            if ($value <= 0) {
66
                continue;
67
            }
68 4
            $epsBar .= sprintf(
69 4
                '<img src="/assets/bars/balken.png" style="background-color: #%s;height: 12px; width: %dpx;" title="%s" />',
70 4
                $color,
71 4
                round($width / 100 * (100 / $host->getMaxEps() * $value)),
72 4
                'Energieproduktion'
73 4
            );
74
        }
75
76 4
        return $epsBar;
77
    }
78
79 4
    private function getEpsBarTitleString(PlanetFieldHostInterface $host, int $energyProduction): string
80
    {
81 4
        if ($host instanceof ColonyInterface) {
82 4
            $forecast = $host->getEps() + $energyProduction;
83 4
            if ($host->getEps() + $energyProduction < 0) {
84
                $forecast = 0;
85
            }
86 4
            if ($host->getEps() + $energyProduction > $host->getMaxEps()) {
87 3
                $forecast = $host->getMaxEps();
88
            }
89
90 4
            $eps = $host->getEps();
91
        } else {
92
            $eps = 0;
93
            $forecast = $energyProduction;
94
        }
95
96 4
        if ($energyProduction > 0) {
97 4
            $energyProduction = sprintf('+%d', $energyProduction);
98
        }
99
100 4
        return sprintf(
101 4
            _('Energie: %d/%d (%s/Runde = %d)'),
102 4
            $eps,
103 4
            $host->getMaxEps(),
104 4
            $energyProduction,
105 4
            $forecast
106 4
        );
107
    }
108
}
109