GameController.class.php 2.49 KB
Newer Older
Zohten's avatar
Zohten committed
1
<?php
Zohten's avatar
Zohten committed
2
// TODO : ID partie, Nom de la partie, nombres de joueurs déjà connectés
Zohten's avatar
Zohten committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

class GameController extends Controller
{
    public function __construct($name, $request)
    {
        parent::__construct($name, $request);
    }

    /**
    * Process incoming request for the /game endpoint
    *
    * @return    Response
    */
    public function processRequest()
    {
        $httpMethod=$this->request->getHttpMethod();
        $uriParams=$this->request->getUriParams();

        switch ($httpMethod) {
            case 'GET':
                // If there is a uriParams, it is the /game/{id} endpoint
                if ($uriParams) {
Zohten's avatar
Zohten committed
25 26 27
                    if ($uriParams[0]=='public'){
                        return $this->getAllPublicGames($uriParams[0]);
                    }
Zohten's avatar
Zohten committed
28 29 30 31 32
                    return $this->getGame($uriParams[0]);
                }
                // Else, it is the /game endpoint
                return $this->getAllGames();
                break;
Zohten's avatar
Zohten committed
33 34 35 36
            case 'POST':
                $body = $this->request->getData();
                return $this->newGame($body);
                break;
Zohten's avatar
Zohten committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        }
        $message = json_encode(["message" => "unsupported parameters or method in game"]);
        return Response::errorResponse($message);
    }

    /**
    * (GET) Get all games in Game table
    *
    * @return    Response
    */
    protected function getAllGames()
    {
        $games = Game::getList();
        $response = Response::okResponse(json_encode($games, JSON_PRETTY_PRINT));
        return $response;
    }

    /**
Zohten's avatar
Zohten committed
55
    * (GET) Get a specific game in Game table based on id
Zohten's avatar
Zohten committed
56 57 58 59 60 61 62 63 64
    *
    * @return    Response
    */
    protected function getGame($id)
    {
        $games = Game::getRow($id);
        $response = Response::okResponse(json_encode($games, JSON_PRETTY_PRINT));
        return $response;
    }
Zohten's avatar
Zohten committed
65

Zohten's avatar
Zohten committed
66 67 68 69 70 71 72 73 74 75 76 77
        /**
    * (GET) Get all games in Game table
    *
    * @return    Response
    */
    protected function getAllPublicGames()
    {
        $games = Game::getListPublic();
        $response = Response::okResponse(json_encode($games, JSON_PRETTY_PRINT));
        return $response;
    }

Zohten's avatar
Zohten committed
78
    /**
Zohten's avatar
Zohten committed
79
    * (POST) Add a new game in Game table
Zohten's avatar
Zohten committed
80
    *
Zohten's avatar
Zohten committed
81
    * @param    array    $array    array containing PRIVATE
Zohten's avatar
Zohten committed
82 83 84 85 86 87 88 89 90
    * @return    Response
    */
    protected function newGame($array)
    {
        Game::createGame($array);
        $message = json_encode(["message" => 'Game succesfully created!']);
        $response = Response::createdResponse($message);
        return $response;
    }
Zohten's avatar
Zohten committed
91
}