Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiabasone committed Sep 9, 2020
1 parent 518f3e4 commit e224814
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 55 deletions.
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Kernel extends ConsoleKernel

/**
* Define the application's command schedule.
*
* @param Schedule $schedule
*/
protected function schedule(Schedule $schedule)
{
Expand Down
10 changes: 6 additions & 4 deletions app/Http/Controllers/Api/AvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ public function serveUuid(Request $request, string $uuid, $size = 0): Response
*/
public function serveDefault($size = 0): Response
{
return $this->pngResponse(
(string) $this->rendering->avatar(
$image = $this->cache()->remember('rendering.system.default_avatar', 3600, function () use ($size) {
return (string) $this->rendering->avatar(
null,
(int) $size
)
);
);
});

return $this->pngResponse($image);
}
}
24 changes: 19 additions & 5 deletions app/Http/Controllers/Api/BaseApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Minepic\Http\Controllers\Api;

use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laravel\Lumen\Http\ResponseFactory;
Expand All @@ -13,9 +15,6 @@
use Minepic\Resolvers\UsernameResolver;
use Minepic\Resolvers\UuidResolver;

/**
* Class BaseApiController.
*/
abstract class BaseApiController extends BaseController
{
/**
Expand All @@ -34,6 +33,10 @@ abstract class BaseApiController extends BaseController
* @var Rendering
*/
protected Rendering $rendering;
/**
* @var Dispatcher
*/
protected Dispatcher $eventDispatcher;

/**
* Api constructor.
Expand All @@ -42,17 +45,20 @@ abstract class BaseApiController extends BaseController
* @param ResponseFactory $responseFactory Response Factory
* @param UsernameResolver $usernameResolver
* @param Rendering $rendering
* @param Dispatcher $eventDispatcher
*/
public function __construct(
UuidResolver $uuidResolver,
ResponseFactory $responseFactory,
UsernameResolver $usernameResolver,
Rendering $rendering
Rendering $rendering,
Dispatcher $eventDispatcher
) {
$this->uuidResolver = $uuidResolver;
$this->responseFactory = $responseFactory;
$this->usernameResolver = $usernameResolver;
$this->rendering = $rendering;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand Down Expand Up @@ -104,6 +110,14 @@ public function pngResponse(string $image): Response
*/
protected function dispatchAccountImageServedEvent(): void
{
\Event::dispatch(new AccountImageServedEvent($this->uuidResolver->getAccount()));
$this->eventDispatcher->dispatch(new AccountImageServedEvent($this->uuidResolver->getAccount()));
}

/**
* @return CacheRepository
*/
protected function cache(): CacheRepository
{
return \Cache::driver('file');
}
}
8 changes: 5 additions & 3 deletions app/Http/Controllers/Api/IsometricAvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public function serveUuid(Request $request, $uuid, $size = 0): Response
*/
public function serveDefault($size = 0): Response
{
return $this->pngResponse(
(string) $this->rendering->isometricAvatar(null, (int) $size)
);
$image = $this->cache()->remember('rendering.system.default_isometric_avatar', 3600, function () use ($size) {
return (string) $this->rendering->isometricAvatar(null, (int) $size);
});

return $this->pngResponse($image);
}
}
13 changes: 6 additions & 7 deletions app/Http/Controllers/Api/SkinBackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use Illuminate\Http\Response;
use Minepic\Image\ImageSection;

/**
* Class BaseApiController.
*/
class SkinBackController extends BaseApiController
{
/**
Expand Down Expand Up @@ -43,12 +40,14 @@ public function serveUuid(Request $request, $uuid, $size = 0): Response
*/
public function serveDefault($size = 0): Response
{
return $this->pngResponse(
(string) $this->rendering->skin(
$image = $this->cache()->remember('rendering.system.default_skin_back', 3600, function () use ($size) {
return (string) $this->rendering->skin(
null,
(int) $size,
ImageSection::BACK
)
);
);
});

return $this->pngResponse($image);
}
}
13 changes: 6 additions & 7 deletions app/Http/Controllers/Api/SkinFrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use Illuminate\Http\Response;
use Minepic\Image\ImageSection;

/**
* Class SkinFrontController.
*/
class SkinFrontController extends BaseApiController
{
/**
Expand Down Expand Up @@ -44,12 +41,14 @@ public function serveUuid(Request $request, $uuid, $size = 0): Response
*/
public function serveDefault($size = 0): Response
{
return $this->pngResponse(
(string) $this->rendering->skin(
$image = $this->cache()->remember('rendering.system.default_skin_front', 3600, function () use ($size) {
return (string) $this->rendering->skin(
null,
(int) $size,
ImageSection::FRONT
)
);
);
});

return $this->pngResponse($image);
}
}
1 change: 0 additions & 1 deletion app/Image/Rendering.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Rendering
*
* @throws Exceptions\ImageCreateFromPngFailedException
* @throws Exceptions\ImageTrueColorCreationFailedException
* @throws Exceptions\InvalidSectionSpecifiedException
*
* @return Avatar
*/
Expand Down
65 changes: 43 additions & 22 deletions app/Image/Sections/Skin.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,61 +63,82 @@ public function render(int $skin_height = 256, $type = self::FRONT): void
$skin_height = $this->checkHeight($skin_height);

$image = $this->createImageFromPng($this->skinPath);
$scale = $skin_height / 32;
if ($scale === 0) {
$scale = 1;
}
$this->imgResource = \imagecreatetruecolor(16 * $scale, 32 * $scale);
if ($this->imgResource === false) {
throw new ImageResourceCreationFailedException('imagecreatetruecolor() failed');
}
\imagealphablending($this->imgResource, false);
\imagesavealpha($this->imgResource, true);
$transparent = \imagecolorallocatealpha($this->imgResource, 255, 255, 255, 127);
\imagefilledrectangle($this->imgResource, 0, 0, 16 * $scale, 32 * $scale, $transparent);

$tmpImageResource = $this->emptyBaseImage(16, 32);

$tmpAvatar = new Avatar($this->skinPath);
$tmpAvatar->render(8, $type);
// Front
if ($type === self::FRONT) {
// Head
\imagecopyresized($this->imgResource, $tmpAvatar->getResource(), 4 * $scale, 0 * $scale, 0, 0, 8 * $scale, 8 * $scale, 8, 8);
\imagecopyresized($tmpImageResource, $tmpAvatar->getResource(), 4, 0, 0, 0, 8, 8, 8, 8);
// Body Front
\imagecopyresized($this->imgResource, $image, 4 * $scale, 8 * $scale, 20, 20, 8 * $scale, 12 * $scale, 8, 12);
\imagecopyresized($tmpImageResource, $image, 4, 8, 20, 20, 8, 12, 8, 12);
// Right Arm (left on img)
$r_arm = \imagecreatetruecolor(4, 12);
\imagecopy($r_arm, $image, 0, 0, 44, 20, 4, 12);
\imagecopyresized($this->imgResource, $r_arm, 0 * $scale, 8 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12);
\imagecopyresized($tmpImageResource, $r_arm, 0, 8, 0, 0, 4, 12, 4, 12);
// Right leg (left on img)
$r_leg = \imagecreatetruecolor(4, 20);
\imagecopy($r_leg, $image, 0, 0, 4, 20, 4, 12);
\imagecopyresized($this->imgResource, $r_leg, 4 * $scale, 20 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12);
\imagecopyresized($tmpImageResource, $r_leg, 4, 20, 0, 0, 4, 12, 4, 12);
} else {
// Head
\imagecopyresized($this->imgResource, $tmpAvatar->getResource(), 4 * $scale, 0 * $scale, 0, 0, 8 * $scale, 8 * $scale, 8, 8);
\imagecopyresized($tmpImageResource, $tmpAvatar->getResource(), 4, 0, 0, 0, 8, 8, 8, 8);
// Body Back
\imagecopyresized($this->imgResource, $image, 4 * $scale, 8 * $scale, 32, 20, 8 * $scale, 12 * $scale, 8, 12);
\imagecopyresized($tmpImageResource, $image, 4, 8, 32, 20, 8, 12, 8, 12);
// Right Arm Back (left on img)
$r_arm = \imagecreatetruecolor(4, 12);
\imagecopy($r_arm, $image, 0, 0, 52, 20, 4, 12);
\imagecopyresized($this->imgResource, $r_arm, 0 * $scale, 8 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12);
\imagecopyresized($tmpImageResource, $r_arm, 0, 8, 0, 0, 4, 12, 4, 12);
// Right leg Back (left on img)
$r_leg = \imagecreatetruecolor(4, 20);
\imagecopy($r_leg, $image, 0, 0, 12, 20, 4, 12);
\imagecopyresized($this->imgResource, $r_leg, 4 * $scale, 20 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12);
\imagecopyresized($tmpImageResource, $r_leg, 4, 20, 0, 0, 4, 12, 4, 12);
}

// Left Arm (right flipped)
$l_arm = \imagecreatetruecolor(4, 12);
for ($x = 0; $x < 4; ++$x) {
\imagecopy($l_arm, $r_arm, $x, 0, 4 - $x - 1, 0, 1, 12);
}
\imagecopyresized($this->imgResource, $l_arm, 12 * $scale, 8 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12);
\imagecopyresized($tmpImageResource, $l_arm, 12, 8, 0, 0, 4, 12, 4, 12);
// Left leg (right flipped)
$l_leg = \imagecreatetruecolor(4, 20);
for ($x = 0; $x < 4; ++$x) {
\imagecopy($l_leg, $r_leg, $x, 0, 4 - $x - 1, 0, 1, 20);
}
\imagecopyresized($this->imgResource, $l_leg, 8 * $scale, 20 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12);
\imagecopyresized($tmpImageResource, $l_leg, 8, 20, 0, 0, 4, 12, 4, 12);

$scale = $skin_height / 32;
if ($scale === 0) {
$scale = 1;
}
$skin_width = (int) \round($scale * (16));

$this->imgResource = $this->emptyBaseImage($skin_width, $skin_height);
\imagecopyresized($this->imgResource, $tmpImageResource, 0, 0, 0, 0, $skin_width, $skin_height, 16, 32);
}

/**
* @param int $width
* @param int $height
*
* @throws ImageResourceCreationFailedException
*
* @return resource
*/
private function emptyBaseImage(int $width, int $height)
{
$tmpImageResource = \imagecreatetruecolor($width, $height);
if ($tmpImageResource === false) {
throw new ImageResourceCreationFailedException('imagecreatetruecolor() failed');
}
\imagealphablending($tmpImageResource, false);
\imagesavealpha($tmpImageResource, true);
$transparent = \imagecolorallocatealpha($tmpImageResource, 255, 255, 255, 127);
\imagefilledrectangle($tmpImageResource, 0, 0, $width, $height, $transparent);

return $tmpImageResource;
}
}
4 changes: 4 additions & 0 deletions app/Resolvers/UsernameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class UsernameResolver
*/
private MojangClient $mojangClient;

/**
* @param AccountRepository $accountRepository
* @param MojangClient $mojangClient
*/
public function __construct(
AccountRepository $accountRepository,
MojangClient $mojangClient
Expand Down
19 changes: 14 additions & 5 deletions app/Resolvers/UuidResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Minepic\Resolvers;

use Event;
use Illuminate\Contracts\Events\Dispatcher;
use Log;
use Minepic\Cache\UserNotFoundCache;
use Minepic\Events\Account\AccountCreatedEvent;
Expand Down Expand Up @@ -55,14 +55,21 @@ class UuidResolver
* @var MojangClient
*/
private MojangClient $mojangClient;
/**
* @var Dispatcher
*/
private Dispatcher $eventDispatcher;

/**
* @param MojangClient $mojangClient Client for Mojang API
* @param MojangClient $mojangClient Client for Mojang API
* @param Dispatcher $eventDispatcher
*/
public function __construct(
MojangClient $mojangClient
MojangClient $mojangClient,
Dispatcher $eventDispatcher
) {
$this->mojangClient = $mojangClient;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand Down Expand Up @@ -141,7 +148,7 @@ public function insertNewUuid(): bool
$this->saveRemoteSkin();

$this->uuid = $this->account->uuid;
Event::dispatch(new AccountCreatedEvent($this->account));
$this->eventDispatcher->dispatch(new AccountCreatedEvent($this->account));

return true;
}
Expand Down Expand Up @@ -249,7 +256,9 @@ public function userDataUpdated(): bool
private function logUsernameChange(Account $account, string $previousUsername): void
{
if ($account->username !== $previousUsername && $previousUsername !== '') {
Event::dispatch(new UsernameChangeEvent($account->uuid, $previousUsername, $account->username));
$this->eventDispatcher->dispatch(
new UsernameChangeEvent($account->uuid, $previousUsername, $account->username)
);
}
}

Expand Down
2 changes: 2 additions & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
realpath(__DIR__.'/../')
);

$app->configure('swoole_http');

$app->withFacades();
$app->withEloquent();

Expand Down
2 changes: 1 addition & 1 deletion bootstrap/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

const ROUTE_UUID_MATCH = '/{uuid:[0-9a-fA-F]{32}|[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}}';
const ROUTE_USERNAME_MATCH = '/{username:[a-zA-Z0-9_]+}';
const ROUTE_SIZE_MATCH = '/{size:[\d]+}';
const ROUTE_SIZE_MATCH = '/{size:[\d]+}';

0 comments on commit e224814

Please sign in to comment.