46 lines
1.4 KiB
PHP
46 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace BattleForge\Http\Handlers;
|
|
|
|
use BattleForge\Domain\CombatEngine;
|
|
use BattleForge\Domain\CombatException;
|
|
use BattleForge\Domain\Position;
|
|
use BattleForge\Http\MatchActionSupport;
|
|
use BattleForge\Http\Request;
|
|
use BattleForge\Http\Response;
|
|
|
|
final class PostMatchAbility
|
|
{
|
|
public function __construct(private readonly string $secret)
|
|
{
|
|
}
|
|
|
|
/** @param array<string, string> $params */
|
|
public function handle(Request $request, array $params): Response
|
|
{
|
|
$support = new MatchActionSupport($this->secret);
|
|
$result = $support->verify($request);
|
|
if ($result->response !== null) {
|
|
return $result->response;
|
|
}
|
|
$matchId = $result->matchIdAndMatch['matchId'];
|
|
$match = $result->matchIdAndMatch['match'];
|
|
|
|
$payload = json_decode($request->rawBody, true);
|
|
$unitId = (string) ($payload['unitId'] ?? '');
|
|
$abilityId = (string) ($payload['abilityId'] ?? '');
|
|
$x = (int) ($payload['x'] ?? -1);
|
|
$y = (int) ($payload['y'] ?? -1);
|
|
|
|
try {
|
|
$next = (new CombatEngine())->useAbility($match, $unitId, $abilityId, new Position($x, $y));
|
|
} catch (CombatException $exception) {
|
|
return PostMatchMove::rejectionResponse($this->secret, $matchId, $match, $exception->getMessage());
|
|
}
|
|
|
|
return Response::json(200, PostMatchMove::successResponse($this->secret, $matchId, $next));
|
|
}
|
|
}
|