Passed
Pull Request — master (#1896)
by Janko
55:48 queued 26:18
created

EpsBarProvider::setTemplateVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 1
rs 10
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 1
    public function __construct(private PlanetFieldRepositoryInterface $planetFieldRepository) {}
15
16 1
    #[Override]
17
    public function setTemplateVariables(
18
        PlanetFieldHostInterface $host,
19
        GameControllerInterface $game
20
    ): void {
21 1
        $energyProduction = $this->planetFieldRepository->getEnergyProductionByHost($host);
22
23 1
        $game->setTemplateVar('EPS_STATUS_BAR', $this->getEpsStatusBar($host, $energyProduction));
24 1
        $game->setTemplateVar('EPS_PRODUCTION', $energyProduction);
25 1
        $game->setTemplateVar('EPS_BAR_TITLE_STRING', $this->getEpsBarTitleString($host, $energyProduction));
26
    }
27
28 1
    public static function getEpsStatusBar(PlanetFieldHostInterface $host, int $energyProduction, int $width = 360): string
29
    {
30 1
        $currentEps = $host instanceof ColonyInterface ? $host->getEps() : 0;
31
32 1
        $bars = [];
33 1
        $epsBar = '';
34 1
        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 1
        if ($energyProduction > 0) {
46 1
            if ($currentEps + $energyProduction > $host->getMaxEps()) {
47
                $bars[StatusBarColorEnum::STATUSBAR_YELLOW] = $currentEps;
48
                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 1
        if ($energyProduction == 0) {
58
            $bars[StatusBarColorEnum::STATUSBAR_YELLOW] = $currentEps;
59
            $bars[StatusBarColorEnum::STATUSBAR_GREY] = $host->getMaxEps() - $currentEps;
60
        }
61 1
        foreach ($bars as $color => $value) {
62 1
            if ($host->getMaxEps() < $value) {
63
                $value = $host->getMaxEps();
64
            }
65 1
            if ($value <= 0) {
66
                continue;
67
            }
68 1
            $epsBar .= sprintf(
69 1
                '<img src="/assets/bars/balken.png" style="background-color: #%s;height: 12px; width: %dpx;" title="%s" />',
70 1
                $color,
71 1
                round($width / 100 * (100 / $host->getMaxEps() * $value)),
72 1
                'Energieproduktion'
73 1
            );
74
        }
75
76 1
        return $epsBar;
77
    }
78
79 1
    private function getEpsBarTitleString(PlanetFieldHostInterface $host, int $energyProduction): string
80
    {
81 1
        if ($host instanceof ColonyInterface) {
82 1
            $forecast = $host->getEps() + $energyProduction;
83 1
            if ($host->getEps() + $energyProduction < 0) {
84
                $forecast = 0;
85
            }
86 1
            if ($host->getEps() + $energyProduction > $host->getMaxEps()) {
87
                $forecast = $host->getMaxEps();
88
            }
89
90 1
            $eps = $host->getEps();
91
        } else {
92
            $eps = 0;
93
            $forecast = $energyProduction;
94
        }
95
96 1
        if ($energyProduction > 0) {
97 1
            $energyProduction = sprintf('+%d', $energyProduction);
98
        }
99
100 1
        return sprintf(
101 1
            _('Energie: %d/%d (%s/Runde = %d)'),
102 1
            $eps,
103 1
            $host->getMaxEps(),
104 1
            $energyProduction,
105 1
            $forecast
106 1
        );
107
    }
108
}
109