diff options
| -rw-r--r-- | config/messages.php | 3 | ||||
| -rw-r--r-- | server.php | 19 | ||||
| -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 | ||||
| -rw-r--r-- | web/home.php | 50 | ||||
| -rw-r--r-- | web/welcome.php | 3 | 
8 files changed, 243 insertions, 34 deletions
| diff --git a/config/messages.php b/config/messages.php index 53f21db..34b2a84 100644 --- a/config/messages.php +++ b/config/messages.php @@ -1,5 +1,6 @@  <?php  return [ -    'overview' => 'This is a message, {{inject}} is injected. Also {{second}}' +    'overview' => 'This is a message, {{inject}} is injected. Also {{second}}', +    'welcome' => 'Welcome {{name}}. Your game code is {{gameCode}}'  ]; diff --git a/server.php b/server.php new file mode 100644 index 0000000..2e54925 --- /dev/null +++ b/server.php @@ -0,0 +1,19 @@ +<?php + +use Ratchet\Server\IoServer; +use Ratchet\Http\HttpServer; +use Ratchet\WebSocket\WsServer; +use App\Ratchet\Chat; + +require 'vendor/autoload.php'; + +$server = IoServer::factory( +    new HttpServer( +        new WsServer( +            new Chat() +        ) +    ), +    8080 +); + +$server->run(); 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(); +    } +} diff --git a/web/home.php b/web/home.php new file mode 100644 index 0000000..fdcd976 --- /dev/null +++ b/web/home.php @@ -0,0 +1,50 @@ +<?php + +// define site root +define('SITE_ROOT', realpath('../')); + +// Bring in our composer autoloader +require SITE_ROOT . '/vendor/autoload.php'; + +use App\Core\Main; + +if ($_POST) { +    $name = $_POST['name']; +    $game = $_POST['game']; +    $action = $_POST['action']; +    new Main($name, $game, $action); +    exit; +} +?> + +<html> + +    <head> + +    </head> + +    <body> + +        <div class="welcome"> + +            <h1>Welcome</h1> +            <p>This is a trial of a game</p> + +        </div> + +        <div class="identify"> + +            <form class="identify__form" action="" method="post"> +                <label for="identify-name">Enter a Nickname</label> +                <input type="text" name="name" value="" placeholder="Example: CoolDude69"> +                <label for="identify-name">Enter a Game Code</label> +                <input type="text" name="game" value="" placeholder="#abc123"> +                <input type="hidden" name="action" value="identify"> +                <button type="submit" name="identify-action">Go</button> +            </form> + +        </div> + +    </body> + +</html> diff --git a/web/welcome.php b/web/welcome.php new file mode 100644 index 0000000..568ecf2 --- /dev/null +++ b/web/welcome.php @@ -0,0 +1,3 @@ +<?php + +echo $this->game->getHandler()->renderWelcome(); | 
