27 lines
709 B
PHP
27 lines
709 B
PHP
<?php
|
|
|
|
namespace App\Websocket;
|
|
|
|
use App\Game\GameRuleViolation;
|
|
use BlaxSoftware\LaravelWebSockets\Websocket\Controller;
|
|
use Closure;
|
|
|
|
/**
|
|
* Base for the game's WS controllers: authed only, and a refused intent answers
|
|
* `<event>:error` with `{message, code}` so the client can translate the code.
|
|
* Lives outside Controllers/ so the resolver never maps an event to it.
|
|
*/
|
|
abstract class GameController extends Controller
|
|
{
|
|
public $need_auth = true;
|
|
|
|
protected function play(Closure $intent): mixed
|
|
{
|
|
try {
|
|
return $intent();
|
|
} catch (GameRuleViolation $e) {
|
|
return $this->error(['message' => $e->getMessage(), 'code' => $e->reason]);
|
|
}
|
|
}
|
|
}
|