<?php
namespace ChessServer;
..
class Socket implements MessageComponentInterface
{
...
public function onMessage(ConnectionInterface $from, $msg)
{
...
if (is_a($cmd, AcceptFriendRequestCommand::class)) {
if ($gameMode = $this->findGameMode($this->parser->argv[1])) {
if ($this->syncGameModeWith($gameMode, $from)) {
$jwt = $gameMode->getJwt();
$decoded = JWT::decode($jwt, $_ENV['JWT_SECRET'], array('HS256'));
return $this->sendToMany($gameMode->getResourceIds(), [
$cmd->name => [
'jwt' => $jwt,
'hash' => md5($jwt),
],
]);
}
}
return $this->sendToOne($from->resourceId, [
$cmd->name => [
'mode' => PlayFriendMode::NAME,
'message' => 'This friend request could not be accepted.',
],
]);
} elseif (is_a($cmd, DrawCommand::class)) {
if (is_a($gameMode, PlayFriendMode::class)) {
return $this->sendToMany(
$gameMode->getResourceIds(),
$gameMode->res($this->parser->argv, $cmd)
);
}
} elseif (is_a($cmd, PlayFenCommand::class)) {
...
} elseif (is_a($cmd, QuitCommand::class)) {
...
} elseif (is_a($cmd, ResignCommand::class)) {
...
} elseif (is_a($cmd, StartCommand::class)) {
...
} elseif (is_a($cmd, TakebackCommand::class)) {
...
} elseif ($gameMode) {
return $this->sendToOne(
$from->resourceId,
$this->gameModes[$from->resourceId]->res($this->parser->argv, $cmd)
);
}
}
...
}
Described below is the sequence of commands that would allow Bob to propose a takeback to Alice after playing 1.e4 e5
.
$ websocat ws://localhost:8080
/start playfriend w 10
{"\/start":{"mode":"playfriend","jwt":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJwY2hlc3MubmV0IiwiaWF0IjoxNjMyOTI4OTY4LCJjb2xvciI6InciLCJtaW4iOiIxMCIsImV4cCI6MTYzMjkyOTU2OH0.KAlahRGFjyg2NF7o7xlh4nV4HrcFng9JhM65IeiPPOQ","hash":"b9bef730966d51284303d9054d33fabb"}}
{"\/accept":{"jwt":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJwY2hlc3MubmV0IiwiaWF0IjoxNjMyOTI4OTY4LCJjb2xvciI6InciLCJtaW4iOiIxMCIsImV4cCI6MTYzMjkyOTU2OH0.KAlahRGFjyg2NF7o7xlh4nV4HrcFng9JhM65IeiPPOQ","hash":"b9bef730966d51284303d9054d33fabb"}}
/playfen "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b"
{"\/playfen":{"turn":"w","legal":true,"check":false,"mate":false,"movetext":"1.e4","fen":"rnbqkbnr\/pppppppp\/8\/8\/4P3\/8\/PPPP1PPP\/RNBQKBNR b KQkq e3"}}
{"\/playfen":{"turn":"b","legal":true,"check":false,"mate":false,"movetext":"1.e4 e5","fen":"rnbqkbnr\/pppp1ppp\/8\/4p3\/4P3\/8\/PPPP1PPP\/RNBQKBNR w KQkq e6"}}
{"\/takeback":"propose"}
/takeback accept
{"\/takeback":"accept"}
$ websocat ws://localhost:8080
/accept b9bef730966d51284303d9054d33fabb
{"\/accept":{"jwt":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJwY2hlc3MubmV0IiwiaWF0IjoxNjMyOTI4OTY4LCJjb2xvciI6InciLCJtaW4iOiIxMCIsImV4cCI6MTYzMjkyOTU2OH0.KAlahRGFjyg2NF7o7xlh4nV4HrcFng9JhM65IeiPPOQ","hash":"b9bef730966d51284303d9054d33fabb"}}
{"\/playfen":{"turn":"w","legal":true,"check":false,"mate":false,"movetext":"1.e4","fen":"rnbqkbnr\/pppppppp\/8\/8\/4P3\/8\/PPPP1PPP\/RNBQKBNR b KQkq e3"}}
/playfen "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w"
{"\/playfen":{"turn":"b","legal":true,"check":false,"mate":false,"movetext":"1.e4 e5","fen":"rnbqkbnr\/pppp1ppp\/8\/4p3\/4P3\/8\/PPPP1PPP\/RNBQKBNR w KQkq e6"}}
/takeback propose
{"\/takeback":"propose"}
{"\/takeback":"accept"}
/undomove
{"\/undomove":true}