1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\Database\View\ColonyWorthRanking; |
6
|
|
|
|
7
|
|
|
use Stu\Module\Control\GameControllerInterface; |
8
|
|
|
use Stu\Module\Control\ViewControllerInterface; |
9
|
|
|
use Stu\Module\Database\Lib\DatabaseTopListArchitects; |
10
|
|
|
use Stu\Module\Database\Lib\DatabaseUiFactoryInterface; |
11
|
|
|
use Stu\Orm\Repository\ColonyRepositoryInterface; |
12
|
|
|
|
13
|
|
|
final class ColonyWorthRanking implements ViewControllerInterface |
14
|
|
|
{ |
15
|
|
|
public const VIEW_IDENTIFIER = 'SHOW_COLONY_WORTH'; |
16
|
|
|
|
17
|
|
|
private DatabaseUiFactoryInterface $databaseUiFactory; |
18
|
|
|
|
19
|
|
|
private ColonyRepositoryInterface $colonyRepository; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
DatabaseUiFactoryInterface $databaseUiFactory, |
23
|
|
|
ColonyRepositoryInterface $colonyRepository |
24
|
|
|
) { |
25
|
|
|
$this->databaseUiFactory = $databaseUiFactory; |
26
|
|
|
$this->colonyRepository = $colonyRepository; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function handle(GameControllerInterface $game): void |
30
|
|
|
{ |
31
|
|
|
$game->setNavigation([ |
32
|
|
|
[ |
33
|
|
|
'url' => 'database.php', |
34
|
|
|
'title' => 'Datenbank' |
35
|
|
|
], |
36
|
|
|
[ |
37
|
|
|
'url' => sprintf('database.php?%s=1', static::VIEW_IDENTIFIER), |
38
|
|
|
'title' => 'Die Top 10 der Architekten' |
39
|
|
|
] |
40
|
|
|
]); |
41
|
|
|
$game->setPageTitle('/ Datenbank / Die Top 10 der Architekten'); |
42
|
|
|
//$game->showMacro('html/database/top_architects.twig', true); |
43
|
|
|
$game->showMacro('html/database.xhtml/top_architects'); |
44
|
|
|
|
45
|
|
|
$netWorthPerUserArray = $this->getNetWorthPerUser($game); |
46
|
|
|
|
47
|
|
|
$game->setTemplateVar( |
48
|
|
|
'ARCHITECTS_LIST', |
49
|
|
|
array_map( |
50
|
|
|
fn (int $userId, float $points): DatabaseTopListArchitects => $this->databaseUiFactory->createDatabaseTopListArchitects( |
51
|
|
|
$userId, |
52
|
|
|
$this->floatPointsToPercentageString($points) |
53
|
|
|
), |
54
|
|
|
array_keys($netWorthPerUserArray), |
55
|
|
|
array_values($netWorthPerUserArray) |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array<int, int> |
62
|
|
|
*/ |
63
|
|
|
private function getNetWorthPerUser(GameControllerInterface $game): array |
64
|
|
|
{ |
65
|
|
|
$currentUser = $game->getUser(); |
66
|
|
|
|
67
|
|
|
$resultSet = $this->colonyRepository->getColoniesNetWorth(); |
68
|
|
|
|
69
|
|
|
$commodityAmountArray = []; |
70
|
|
|
|
71
|
|
|
$userArray = []; |
72
|
|
|
|
73
|
|
|
foreach ($resultSet as $entry) { |
74
|
|
|
$userId = (int)$entry['user_id']; |
75
|
|
|
$commodityId = (int)$entry['commodity_id']; |
76
|
|
|
$sum = (int)$entry['sum']; |
77
|
|
|
|
78
|
|
|
if (!array_key_exists($commodityId, $commodityAmountArray)) { |
79
|
|
|
$commodityAmountArray[$commodityId] = 0; |
80
|
|
|
} |
81
|
|
|
$commodityAmountArray[$commodityId] += $sum; |
82
|
|
|
|
83
|
|
|
if (!array_key_exists($userId, $userArray)) { |
84
|
|
|
$userArray[$userId] = []; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$userArray[$userId][$commodityId] = $sum; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$userNetWorthArray = []; |
91
|
|
|
|
92
|
|
|
foreach ($userArray as $userId => $data) { |
93
|
|
|
$userNetWorthArray[$userId] = 0; |
94
|
|
|
|
95
|
|
|
foreach ($data as $commodityId => $sum) { |
96
|
|
|
$userNetWorthArray[$userId] += $sum / $commodityAmountArray[$commodityId]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$userNetWorthArray[$userId] *= 100; |
100
|
|
|
|
101
|
|
|
if ($userId === $currentUser->getId()) { |
102
|
|
|
$game->setTemplateVar('USER_POINTS', $this->floatPointsToPercentageString($userNetWorthArray[$userId])); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
arsort($userNetWorthArray); |
107
|
|
|
|
108
|
|
|
return array_slice($userNetWorthArray, 0, 10, true); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function floatPointsToPercentageString(float $points): string |
112
|
|
|
{ |
113
|
|
|
return sprintf('%01.2f', $points); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|