diff options
author | Fbenas <philbeansburton@gmail.com> | 2017-03-12 23:57:42 +0000 |
---|---|---|
committer | Fbenas <philbeansburton@gmail.com> | 2017-03-12 23:57:42 +0000 |
commit | 1eee94d2604fb43b5a4a21c281c72ad356906a88 (patch) | |
tree | 23c095b5db60c48dae443356392f5a5eb8a9c9f2 /src | |
parent | edfd095021ee5d89f53da4fd78d7dea7346d7617 (diff) |
Added identify form and Ratchet websockets test stuff
Diffstat (limited to 'src')
-rw-r--r-- | src/Core/Main.php | 52 | ||||
-rw-r--r-- | src/Game.php | 29 | ||||
-rw-r--r-- | src/Message/Welcome.php | 62 | ||||
-rw-r--r-- | src/Ratchet/Chat.php | 59 |
4 files changed, 169 insertions, 33 deletions
diff --git a/src/Core/Main.php b/src/Core/Main.php index 7aa643e..e893d40 100644 --- a/src/Core/Main.php +++ b/src/Core/Main.php @@ -9,36 +9,52 @@ use App\Game; */ class Main { + protected $game; + /** * Construct * * @author Phil Burton <phil@pgburton.com> */ - public function __construct() + public function __construct($name, $gameCode, $action) { - $this->init(); + $this->init($name, $gameCode, $action); + } - $this->run(); + public function init($name, $gameCode, $action) + { + // Shit, but it should work for now + switch ($action) { + case 'identify': + $this->identify($name, $gameCode); + break; + default: + echo "Action not found"; + die(); + } } - /** - * Initalise - * - * @author Phil Burton <phil@pgburton.com> - * @return bool - */ - protected function init() + protected function identify($name, $gameCode) { - return true; + $this->findOrCreateGame($name, $gameCode); + + // $this->addUserToGame($name, $gameCode); } - /** - * Run the Game - * - * @author Phil Burton <phil@pgburton.com> - */ - protected function run() + public function findOrCreateGame($name, $gameCode) + { + // For now always create a new game + $this->game = $this->createGame($name, $gameCode); + $this->showWelcome($gameCode); + } + + public function showWelcome() + { + include "welcome.php"; + } + + public function createGame($name, $gameCode) { - $game = new Game(); + return new Game($name, $gameCode); } } diff --git a/src/Game.php b/src/Game.php index c50bd34..170a8eb 100644 --- a/src/Game.php +++ b/src/Game.php @@ -11,14 +11,16 @@ use App\Message\Handler; */ class Game { + protected $code; + /** * Constuct a new Game * * @author Phil Burton <phil@pgburton.com> */ - public function __construct() + public function __construct($name, $code) { - $this->init(); + $this->init($name, $code); } /** @@ -26,23 +28,20 @@ class Game * * @author Phil Burton <phil@pgburton.com> */ - protected function init() + protected function init($name, $code) { // Do any pre set-up - $this->handler = new Handler(['inject']); - $this->sendOpeningMessage(); - } + $this->code = $code; - /** - * Send opening message - * @author Phil Burton <phil@pgburton.com> - */ - public function sendOpeningMessage() - { - $message = $this->handler->renderOverview(); + $handler = new Handler(['gameCode', 'name']); + $handler->gameCode = $code; + $handler->name = $name; - $important = $this->handler->returnImportant('inject'); + $this->handler = $handler; + } - var_dump($message, $important); + public function getHandler() + { + return $this->handler; } } diff --git a/src/Message/Welcome.php b/src/Message/Welcome.php new file mode 100644 index 0000000..8a121f4 --- /dev/null +++ b/src/Message/Welcome.php @@ -0,0 +1,62 @@ +<?php + +namespace App\Message; + +use App\Message\Base; + +/** + * Welcome message + * + * @author Phil Burton <phil@pgburton.com> + */ +class Welcome extends Base +{ + /** + * The list of vars we need to inject + * + * @var array + */ + protected $injects = ['name', 'gameCode']; + + /** + * The array of important vars + * + * @var string + */ + protected $important = ['name', 'gameCode']; + + /** + * Inject + * @var string + */ + public $name; + + /** + * Second inject + * + * @var string + */ + public $gameCode; + + /** + * Construct to set any inject vars from functions if required + * + * @author Phil Burton <phil@pgburton.com> + */ + public function __construct(Handler $handler) + { + $this->inject = $this->getInject(); + parent::__construct($handler); + } + + /** + * Get our inject value + * + * @author Phil Burton <phil@pgburton.com> + * @return string + */ + public function getInject() + { + return (string) time(); + } +} diff --git a/src/Ratchet/Chat.php b/src/Ratchet/Chat.php new file mode 100644 index 0000000..3410cf4 --- /dev/null +++ b/src/Ratchet/Chat.php @@ -0,0 +1,59 @@ +<?php + +namespace App\Ratchet; + +use Ratchet\MessageComponentInterface; +use Ratchet\ConnectionInterface; +use SplObjectStorage; + +class Chat implements MessageComponentInterface +{ + protected $clients; + + public function __construct() + { + $this->clients = new SplObjectStorage; + } + + public function onOpen(ConnectionInterface $conn) + { + // Store the new connection to send messages to later + $this->clients->attach($conn); + + echo "New connection! ({$conn->resourceId})\n"; + } + + public function onMessage(ConnectionInterface $from, $msg) + { + $numRecv = count($this->clients) - 1; + echo sprintf( + 'Connection %d sending message "%s" to %d other connection%s' . "\n", + $from->resourceId, + $msg, + $numRecv, + $numRecv == 1 ? '' : 's' + ); + + foreach ($this->clients as $client) { + if ($from !== $client) { + // The sender is not the receiver, send to each client connected + $client->send($msg); + } + } + } + + public function onClose(ConnectionInterface $conn) + { + // The connection is closed, remove it, as we can no longer send it messages + $this->clients->detach($conn); + + echo "Connection {$conn->resourceId} has disconnected\n"; + } + + public function onError(ConnectionInterface $conn, \Exception $e) + { + echo "An error has occurred: {$e->getMessage()}\n"; + + $conn->close(); + } +} |