* * @package ApostaAiApi\Routes */ $this->group("/game", function () { $this->post('/{id}/result', function ($req, $res, $id) { $postData = $req->getParsedBody(); $game = GameQuery::create()->findPk($id); if (!$game) { return $res->withJson(["Message" => "Game not found"], 404); } if ($game->getResult()) { return $res->withJson(["Message" => "Game already has a result"], 409); } $game->setResult($postData['Result']); $game->setFinish(time()); $winner = ParticipantQuery::create()->filterByName($postData['Winner'])->findOneByGameId($game->getId()); if (!$winner) { return $res->withJson(['Message' => 'Participant ' . $postData['winner'] . ' not found'], 404); } $winner->setIsWinner(true); $bets = BetQuery::create()->joinWithParticipant()->useParticipantQuery()->filterByGameId($game->getId())->endUse()->find(); foreach ($bets as $bet) { $bet->submitResult($winner->getId(), $game->getResult()); } $game->save(); $winner->save(); $bets->save(); return $res->withJson(['Message' => 'Game ' . $game->getId() . ' updated'], 200); }); $this->get('/{id}', function ($req, $res, $id) { $game = GameQuery::create()->findPk($id);
unset($result['Bets'][$i]['Participant']['Bets']); unset($result['Bets'][$i]['Participant']['GameId']); unset($result['Bets'][$i]['Participant']['Game']['Participants']); $result['Bets'][$i]['Participant']['Game']['Participants'] = $bet->getParticipant()->getGame()->getParticipants()->toArray(); unset($result['Bets'][$i]['Participant']['Game']['Participants'][0]['Game']); unset($result['Bets'][$i]['Participant']['Game']['Participants'][0]['Bets']); 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);
/** * Performs an INSERT on the database, given a Participant or Criteria object. * * @param mixed $criteria Criteria or Participant object containing data that is used to create the INSERT statement. * @param ConnectionInterface $con the ConnectionInterface connection to use * @return mixed The new primary key. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doInsert($criteria, ConnectionInterface $con = null) { if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(ParticipantTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from Participant object } if ($criteria->containsKey(ParticipantTableMap::COL_ID) && $criteria->keyContainsValue(ParticipantTableMap::COL_ID)) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . ParticipantTableMap::COL_ID . ')'); } // Set the correct dbName $query = ParticipantQuery::create()->mergeWith($criteria); // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) return $con->transaction(function () use($con, $query) { return $query->doInsert($con); }); }
/** * Builds a Criteria object containing the primary key for this object. * * Unlike buildCriteria() this method includes the primary key values regardless * of whether or not they have been modified. * * @throws LogicException if no primary key is defined * * @return Criteria The Criteria object containing value(s) for primary key(s). */ public function buildPkeyCriteria() { $criteria = ChildParticipantQuery::create(); $criteria->add(ParticipantTableMap::COL_ID, $this->id); return $criteria; }
/** * Returns the number of related Participant objects. * * @param Criteria $criteria * @param boolean $distinct * @param ConnectionInterface $con * @return int Count of related Participant objects. * @throws PropelException */ public function countParticipants(Criteria $criteria = null, $distinct = false, ConnectionInterface $con = null) { $partial = $this->collParticipantsPartial && !$this->isNew(); if (null === $this->collParticipants || null !== $criteria || $partial) { if ($this->isNew() && null === $this->collParticipants) { return 0; } if ($partial && !$criteria) { return count($this->getParticipants()); } $query = ChildParticipantQuery::create(null, $criteria); if ($distinct) { $query->distinct(); } return $query->filterByGame($this)->count($con); } return count($this->collParticipants); }
/** * Get the associated ChildParticipant object * * @param ConnectionInterface $con Optional Connection object. * @return ChildParticipant The associated ChildParticipant object. * @throws PropelException */ public function getParticipant(ConnectionInterface $con = null) { if ($this->aParticipant === null && $this->chosen_participant_id !== null) { $this->aParticipant = ChildParticipantQuery::create()->findPk($this->chosen_participant_id, $con); /* The following can be used additionally to guarantee the related object contains a reference to this object. This level of coupling may, however, be undesirable since it could result in an only partially populated collection in the referenced object. $this->aParticipant->addBets($this); */ } return $this->aParticipant; }