示例#1
0
            unset($result['Bets'][$i]['Participant']['Game']['Participants'][1]['Game']);
            unset($result['Bets'][$i]['Participant']['Game']['Participants'][1]['Bets']);
        }
        return $res->write(json_encode($result));
    })->add(new LoggedInMiddleware());
    $this->post("", function ($req, $res) {
        $body = $req->getParsedBody();
        $chosenParticipantId = $body['ChosenParticipantId'];
        $chosenResult = $body['ChosenResult'];
        $participant = ParticipantQuery::create()->findPk($chosenParticipantId);
        if (!$participant) {
            return $res->withJson(["Message" => "Participant couldn't be found"], 404);
        }
        $user = UserQuery::create()->findPk($_SESSION['id']);
        if ($user->getScore() < $participant->getGame()->getBetCost()) {
            return $res->withJson(["Message" => "User doesn't have enough points"], 400);
        }
        $bet = new Bet();
        $bet->setUserId($_SESSION['id']);
        $bet->setChosenParticipantId($chosenParticipantId);
        $bet->setChosenResult($chosenResult);
        try {
            if ($bet->save()) {
                $user->removeScore($participant->getGame()->getBetCost());
                return $res->withJson(["Message" => "Bet was placed"], 200);
            }
        } catch (Exception $e) {
        }
        return $res->withJson(["Message" => "Bet could not be placed"], 501);
    })->add(new LoggedInMiddleware());
});