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

EpsBarProvider   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 61
dl 0
loc 98
ccs 0
cts 60
cp 0
rs 10
c 1
b 0
f 0
wmc 17

3 Methods

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