GameController.class.php 2.98 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

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();

Zohten's avatar
Zohten committed
21 22 23 24 25 26
        // Auth with token phase (id = 0 because not used when checking validtoken)
        $authResponse = $this->authUser(-1, 'validtoken');
        if($authResponse->getCode()!=200){
            return $authResponse;
        }

Zohten's avatar
Zohten committed
27 28 29 30
        switch ($httpMethod) {
            case 'GET':
                // If there is a uriParams, it is the /game/{id} endpoint
                if ($uriParams) {
Zohten's avatar
Zohten committed
31 32 33
                    if ($uriParams[0]=='public'){
                        return $this->getAllPublicGames($uriParams[0]);
                    }
Zohten's avatar
Zohten committed
34 35 36 37 38
                    return $this->getGame($uriParams[0]);
                }
                // Else, it is the /game endpoint
                return $this->getAllGames();
                break;
Zohten's avatar
Zohten committed
39 40 41 42
            case 'POST':
                $body = $this->request->getData();
                return $this->newGame($body);
                break;
Zohten's avatar
Zohten committed
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()
    {
Zohten's avatar
Zohten committed
55 56 57 58 59
        // Auth with token phase (id = 0 because not used when checking validtoken)
        $authResponse = $this->authUser(-1, 'admin');
        if($authResponse->getCode()!=200){
            return $authResponse;
        }
Zohten's avatar
Zohten committed
60 61 62 63 64 65
        $games = Game::getList();
        $response = Response::okResponse(json_encode($games, JSON_PRETTY_PRINT));
        return $response;
    }

    /**
Zohten's avatar
Zohten committed
66
    * (GET) Get a specific game in Game table based on id
Zohten's avatar
Zohten committed
67
    * TODO : check if user is in the game
Zohten's avatar
Zohten committed
68 69 70 71 72 73 74 75 76
    *
    * @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
77

Zohten's avatar
Zohten committed
78 79 80 81 82 83 84 85 86 87 88 89
        /**
    * (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
90
    /**
Zohten's avatar
Zohten committed
91
    * (POST) Add a new game in Game table
Zohten's avatar
Zohten committed
92
    *
Zohten's avatar
Zohten committed
93
    * @param    array    $array    array containing PRIVATE
Zohten's avatar
Zohten committed
94 95 96 97 98 99 100 101 102
    * @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
103
}