Passed
Push — master ( 4c294d...73245e )
by Nico
31:23
created

ShowResearchTree::addEdge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Admin\View\ResearchTree;
6
7
use Fhaculty\Graph\Graph;
8
use Fhaculty\Graph\Vertex;
9
use Graphp\GraphViz\GraphViz;
10
use request;
11
use Stu\Module\Commodity\CommodityTypeEnum;
12
use Stu\Module\Control\GameControllerInterface;
13
use Stu\Module\Control\ViewControllerInterface;
14
use Stu\Orm\Entity\ResearchDependencyInterface;
15
use Stu\Orm\Entity\ResearchInterface;
16
use Stu\Orm\Repository\FactionRepositoryInterface;
17
use Stu\Orm\Repository\ResearchDependencyRepositoryInterface;
18
19
final class ShowResearchTree implements ViewControllerInterface
20
{
21
    public const VIEW_IDENTIFIER = 'SHOW_RESEARCH_TREE';
22
23
    private FactionRepositoryInterface $factionRepository;
24
25
    private ResearchDependencyRepositoryInterface $researchDependencyRepository;
26
27
    public function __construct(
28
        FactionRepositoryInterface $factionRepository,
29
        ResearchDependencyRepositoryInterface $researchDependencyRepository,
30
    ) {
31
        $this->factionRepository = $factionRepository;
32
        $this->researchDependencyRepository = $researchDependencyRepository;
33
    }
34
35
    public function handle(GameControllerInterface $game): void
36
    {
37
        // only Admins can show it
38
        if (!$game->isAdmin()) {
39
            $game->addInformation(_('[b][color=#ff2626]Aktion nicht möglich, Spieler ist kein Admin![/color][/b]'));
40
            return;
41
        }
42
43
        $graph = new Graph();
44
45
        $factionId = request::postIntFatal('factionid');
46
        $faction = $this->factionRepository->find($factionId);
47
        if ($faction === null) {
48
            $game->addInformationf('Faction with following id does not exist: %d', $factionId);
49
            return;
50
        }
51
52
        $vertexes = [];
53
        $points = 0;
54
55
        $startResearch = $faction->getStartResearch();
56
        if ($startResearch !== null) {
57
            $this->addResearch($startResearch, $graph, $vertexes, $points);
58
        }
59
60
        $graphviz = new GraphViz();
61
62
        $game->appendNavigationPart(
63
            sprintf(
64
                '/admin/?%s=1',
65
                static::VIEW_IDENTIFIER
66
            ),
67
            _('Forschungsbaum')
68
        );
69
        $game->setTemplateFile('html/admin/researchTree.twig');
70
        $game->setPageTitle(_('Forschungsbaum'));
71
        $game->setTemplateVar('POINTS', $points);
72
        $game->setTemplateVar('TREE', $graphviz->createImageHtml($graph));
73
    }
74
75
    /** @param array<Vertex> $vertexes */
76
    private function addResearch(ResearchInterface $research, Graph $graph, array &$vertexes, int &$points): void
77
    {
78
        $researchId = $research->getId();
79
        if (array_key_exists($research->getId(), $vertexes)) {
80
            return;
81
        }
82
83
        // create node
84
        $vertex = $graph->createVertex($researchId);
85
        $vertex->setAttribute('graphviz.label', $research->getName());
86
        $vertexes[$researchId] = $vertex;
87
88
        // compute dependencies
89
        foreach ($this->researchDependencyRepository->getByDependingResearch($researchId) as $dependency) {
90
            $this->addResearch($dependency->getResearch(), $graph, $vertexes, $points);
91
            $this->addEdge($dependency, $vertexes);
92
        }
93
94
        $commodityId = $research->getCommodityId();
95
96
        if ($commodityId === CommodityTypeEnum::COMMODITY_RESEARCH_LVL1) {
97
            $points += $research->getPoints() * 1;
98
        }
99
        if ($commodityId === CommodityTypeEnum::COMMODITY_RESEARCH_LVL2) {
100
            $points += $research->getPoints() * 2;
101
        }
102
        if ($commodityId === CommodityTypeEnum::COMMODITY_RESEARCH_LVL3) {
103
            $points += $research->getPoints() * 3;
104
        }
105
        if (in_array($commodityId, CommodityTypeEnum::COMMODITY_RESEARCH_LVL4)) {
106
            $points += $research->getPoints() * 4;
107
        }
108
    }
109
110
    /** @param array<Vertex> $vertexes */
111
    private function addEdge(ResearchDependencyInterface $researchDependency, array $vertexes): void
112
    {
113
        $edge = $vertexes[$researchDependency->getDependsOn()]
114
            ->createEdgeTo($vertexes[$researchDependency->getResearchId()]);
115
116
        $color = $researchDependency->getMode()->getTechtreeEdgeColor();
117
        if ($color !== null) {
118
            $edge->setAttribute('graphviz.color', $color);
119
        }
120
    }
121
}
122