|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VideoGamesRecords\CoreBundle\Controller\Player\Gamercard; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use League\Flysystem\FilesystemException; |
|
9
|
|
|
use League\Flysystem\FilesystemOperator; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
11
|
|
|
use Symfony\Component\HttpKernel\Attribute\Cache; |
|
12
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
|
13
|
|
|
use VideoGamesRecords\CoreBundle\Entity\Badge; |
|
14
|
|
|
use VideoGamesRecords\CoreBundle\Entity\Player; |
|
15
|
|
|
use VideoGamesRecords\CoreBundle\File\PictureCreatorFactory; |
|
16
|
|
|
use VideoGamesRecords\CoreBundle\Traits\GetOrdinalSuffixTrait; |
|
17
|
|
|
use VideoGamesRecords\CoreBundle\Traits\NumberFormatTrait; |
|
18
|
|
|
|
|
19
|
|
|
class Mini extends AbstractController |
|
20
|
|
|
{ |
|
21
|
|
|
use GetOrdinalSuffixTrait; |
|
22
|
|
|
use NumberFormatTrait; |
|
23
|
|
|
|
|
24
|
|
|
private FilesystemOperator $appStorage; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(FilesystemOperator $appStorage) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->appStorage = $appStorage; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
#[Route( |
|
32
|
|
|
'/gamercard/{id}/mini', |
|
33
|
|
|
name: 'vgr_core_player_gamercard_mini', |
|
34
|
|
|
methods: ['GET'], |
|
35
|
|
|
requirements: ['id' => '[1-9]\d*'] |
|
36
|
|
|
)] |
|
37
|
|
|
#[Cache(public: true, maxage: 3600, mustRevalidate: true)] |
|
38
|
|
|
public function __invoke(Player $player): void |
|
39
|
|
|
{ |
|
40
|
|
|
chdir(__DIR__); |
|
41
|
|
|
$gamercard = PictureCreatorFactory::fromFile('../../../Resources/img/gamercard/mini.png'); |
|
42
|
|
|
|
|
43
|
|
|
// Ranking Points |
|
44
|
|
|
$fontSize = 8; |
|
45
|
|
|
$gamercard |
|
46
|
|
|
->addColor('lightBrown', 255, 218, 176) |
|
47
|
|
|
->addFont('segoeUISemiBold', '../../../Resources/fonts/seguisb.ttf') |
|
48
|
|
|
->write($this->numberFormat($player->getPointGame()) . ' Pts', $fontSize, 40, 20) |
|
49
|
|
|
->write('/', $fontSize, 124, 20) |
|
50
|
|
|
->addColor('darkYellow', 255, 191, 1) |
|
51
|
|
|
->write($player->getRankPointGame() . ' ' . $this->getOrdinalSuffix($player->getRankPointGame()), $fontSize, 130, 20); |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
// Ranking Medals |
|
55
|
|
|
$sprite = PictureCreatorFactory::fromFile('../../../Resources/img/sprite.png'); |
|
56
|
|
|
$gamercard |
|
57
|
|
|
->copyResized($sprite, 164, 8, 126, 160, 16, 16, 16, 16) |
|
58
|
|
|
->copyResized($sprite, 211, 8, 108, 160, 16, 16, 16, 16) |
|
59
|
|
|
->copyResized($sprite, 258, 8, 92, 160, 16, 16, 16, 16) |
|
60
|
|
|
->copyResized($sprite, 305, 8, 74, 160, 16, 16, 16, 16); |
|
61
|
|
|
|
|
62
|
|
|
$gamercard->getColor('lightBrown'); |
|
63
|
|
|
$gamercard |
|
64
|
|
|
->write($player->getChartRank0(), $fontSize, 180, 20) |
|
65
|
|
|
->write($player->getChartRank1(), $fontSize, 227, 20) |
|
66
|
|
|
->write($player->getChartRank2(), $fontSize, 274, 20) |
|
67
|
|
|
->write($player->getChartRank3(), $fontSize, 321, 20); |
|
68
|
|
|
$gamercard->write('/', $fontSize, 350, 20); |
|
69
|
|
|
$gamercard->getColor('darkYellow'); |
|
70
|
|
|
$rank = $player->getRankMedal(); |
|
71
|
|
|
if ($rank <= 99) { |
|
72
|
|
|
$rank .= $this->getOrdinalSuffix($rank); |
|
73
|
|
|
} |
|
74
|
|
|
$gamercard->write($rank, $fontSize, 356, 20); |
|
75
|
|
|
|
|
76
|
|
|
// Add avatar |
|
77
|
|
|
$avatar = PictureCreatorFactory::fromStream($this->getAvatar($player)); |
|
78
|
|
|
$gamercard->copyResized($avatar, 4, 2, 0, 0, 26, 26); |
|
79
|
|
|
|
|
80
|
|
|
try { |
|
81
|
|
|
$gamercard->downloadPicture('png', 'VGR-GamerCard-Mini-' . $player->getSlug() . '.png'); |
|
82
|
|
|
} catch (Exception $e) { |
|
83
|
|
|
exit; |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
exit; |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param $value |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
private function numberFormat($value): string |
|
94
|
|
|
{ |
|
95
|
|
|
return number_format($value); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param Player $player |
|
100
|
|
|
* @return string |
|
101
|
|
|
* @throws FilesystemException |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getAvatar(Player $player): string |
|
104
|
|
|
{ |
|
105
|
|
|
$path = 'user' . DIRECTORY_SEPARATOR . $player->getAvatar(); |
|
106
|
|
|
if (!$this->appStorage->fileExists($path)) { |
|
107
|
|
|
$path = 'user' . DIRECTORY_SEPARATOR . 'default.png'; |
|
108
|
|
|
} |
|
109
|
|
|
return $this->appStorage->read($path); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param Badge $badge |
|
115
|
|
|
* @return string |
|
116
|
|
|
* @throws FilesystemException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getBadge(Badge $badge): string |
|
119
|
|
|
{ |
|
120
|
|
|
$path = 'badge' . DIRECTORY_SEPARATOR . $badge->getType() . DIRECTORY_SEPARATOR . $badge->getPicture(); |
|
|
|
|
|
|
121
|
|
|
if (!$this->appStorage->fileExists($path)) { |
|
122
|
|
|
$path = 'badge' . DIRECTORY_SEPARATOR . 'default.gif'; |
|
123
|
|
|
} |
|
124
|
|
|
return $this->appStorage->read($path); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.