RoundController.class.php 3.63 KB
Newer Older
Zohten's avatar
Zohten committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php

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

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

        switch ($httpMethod) {
            case 'GET':
Zohten's avatar
Zohten committed
22
                // If there is a uriParams, it is the /round/{id} or /round/{id}/user endpoint
Zohten's avatar
Zohten committed
23
                if ($uriParams) {
Zohten's avatar
Zohten committed
24 25 26 27 28 29 30
                    $lenUriParams = count($uriParams);
                    // Check if it is the /round/{id}/user endpoint
                    if (($lenUriParams == 2) && ($uriParams[1]=='user')) {
                        return $this->getUsersInRound($uriParams[0]);
                    }
                    // Nope, it is the /round/{id} endpoint
                    return $this->getRound($uriParams[0]);
Zohten's avatar
Zohten committed
31
                }
Zohten's avatar
Zohten committed
32
                // If there is no uriParams, it is the /round endpoint
Zohten's avatar
Zohten committed
33 34
                return $this->getAllRounds();
                break;
Zohten's avatar
Zohten committed
35 36 37 38
            case 'POST':
                $body = $this->request->getData();
                return $this->newRound($body);
                break;
Zohten's avatar
Zohten committed
39 40 41 42 43 44 45 46 47 48
            case 'PUT':
                // If there is a uriParams, it is the /round/{id}/action endpoint
                if ($uriParams) {
                    $lenUriParams = count($uriParams);
                    if (($lenUriParams == 2) && ($uriParams[1]=='action')) {
                        $body = $this->request->getData();
                        return $this->updateActions(array_merge($body, ['id'=>$uriParams[0]]));
                    }
                }
                break;
Zohten's avatar
Zohten committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        }
        $message = json_encode(["message" => "unsupported parameters or method in round"]);
        return Response::errorResponse($message);
    }

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

    /**
    * (GET) Get a specific round in Round table based 
    *
    * @return    Response
    */
    protected function getRound($id)
    {
        $rounds = Round::getRow($id);
        $response = Response::okResponse(json_encode($rounds, JSON_PRETTY_PRINT));
        return $response;
    }

    /**
    * (GET) Get Users of the round $id
    *
    * @return    Response
    */
    protected function getUsersInRound($id)
    {
        $rounds = Round::getUsersInRound($id);
        $response = Response::okResponse(json_encode($rounds, JSON_PRETTY_PRINT));
        return $response;
    }
Zohten's avatar
Zohten committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

    /**
    * (POST) Add a new round in Round table
    *
    * @param    array    $array    array containing ID_GAME, PREVAILING_WIND ,SEED
    * @return    Response
    */
    protected function newRound($array)
    {
        Round::createRound($array);
        $message = json_encode(["message" => 'Round succesfully created!']);
        $response = Response::createdResponse($message);
        return $response;
    }

Zohten's avatar
Zohten committed
104 105
    /**
    * (PUT) Add action of the round in Round table
Zohten's avatar
Zohten committed
106
    * TODO : ADD MAIN LOGIC FOR THE GAME
Zohten's avatar
Zohten committed
107 108 109 110 111 112 113 114 115 116 117
    *
    * @param    string    $action    string containing all actions
    * @return    Response
    */
    protected function updateActions($action)
    {
        Round::updateActions($action);
        $message = json_encode(["message" => 'Actions updated!']);
        $response = Response::createdResponse($message);
        return $response;
    }
Zohten's avatar
Zohten committed
118
}