GitList
Repositories
Help
Report an Issue
blackjack
Code
Commits
Branches
Tags
Search
Tree:
d7b0224
Branches
Tags
master
blackjack
index.php
initial commit
Dev Ghai
commited
d7b0224
at 2014-04-17 16:55:27
index.php
Blame
History
Raw
<?php require_once 'Config/Config.php'; require_once 'Objects/Shoe.php'; require_once 'Objects/Hand.php'; require_once 'Objects/Response.php'; require_once 'Blackjack.php'; define('BJ_SESSION_KEY_TABLE', 'table'); define('BJ_REQUEST_KEY_NAME', 'name'); define('BJ_REQUEST_KEY_MOVE', 'move'); define('BJ_REQUEST_KEY_HAND', 'handId'); //$requestURI = explode('/', $_SERVER['REQUEST_URI']); //$decodedUri = urldecode($_SERVER['REQUEST_URI']); //$query = parse_url($decodedUri, PHP_URL_QUERY); //var_dump($_SERVER); //var_dump($_REQUEST); //var_dump(session_id()); function Debug() { //DEBUG HELPER if(!array_key_exists('d', $_REQUEST)) return; if(array_key_exists('cs', $_REQUEST)) { //clear session if(session_id() == '') session_start(); session_unset(); session_destroy(); } } function ValidateSession() { } function GetResponse() { //TODO: This function is looking huge and dirty now. Split it. $responseObject =null; $config = new \Blackjack\Config\Config(); if(session_id()=='') session_start(); Debug(); if(!array_key_exists(BJ_SESSION_KEY_TABLE, $_SESSION)) { if(!array_key_exists(BJ_REQUEST_KEY_NAME, $_REQUEST)) { //we don't have a session or a player name, that's an error. return new \Blackjack\Objects\Error(E_BJ_NO_NAME); } //Player is trying to start a new session $table = new \Blackjack\Blackjack($config, urldecode($_REQUEST[BJ_REQUEST_KEY_NAME])); //initialize and store it in the session for the user. $_SESSION[BJ_SESSION_KEY_TABLE] = $table; return $table; } //if a session exists, it will be resurrected without the name too. if(array_key_exists(BJ_REQUEST_KEY_MOVE, $_REQUEST)) { //Return an error if hand id is not specified, but a move is. if(!array_key_exists(BJ_REQUEST_KEY_HAND, $_REQUEST) || !is_numeric($_REQUEST[BJ_REQUEST_KEY_HAND])) { return new \Blackjack\Objects\Error(E_BJ_MOVE_NO_HAND_ID); } $table = $_SESSION[BJ_SESSION_KEY_TABLE]; $handIndex = $_REQUEST[BJ_REQUEST_KEY_HAND]; $isMoveSuccessful = true; switch($_REQUEST[BJ_REQUEST_KEY_MOVE]) { //REFERENCE GAME: http://www.games.com/play/masque-publishing/blackjack/single case 'hit': //Get a card from the shoe $card = $table->GetShoe()->PickCard(); //Add the card to the hand player wants. $isMoveSuccessful = $table->player->Hit($handIndex, $card); break; case 'split': //Check if we can split $canSplit = $table->player->CanSplit($handIndex); if($canSplit instanceof \Blackjack\Objects\Error) //Return an error if we got an error from CanSplit function. return $canSplit; elseif (!$canSplit) //Otherwise check the boolean and tell the player that we cannot split. new \Blackjack\Objects\Error(E_BJ_CANNOT_SPLIT); //Get two cards for hits on two hands $card1 = $table->GetShoe()->PickCard(); $card2 = $table->GetShoe()->PickCard(); //Split the hand and update the state of the table $isMoveSuccessful = $table->player->SplitHand($handIndex, $card1, $card2); break; case 'dd': //Get a card from the shoe $card = $table->GetShoe()->PickCard(); //DoubleDown with the card on the hand that user wants. $isMoveSuccessful = $table->player->DoubleDown($handIndex, $card); case 'stand': $isMoveSuccessful = $table->player->GetHand($handIndex)->SetState(BJ_HAND_STAND); $table->SetDealerTurn(); break; default: $isMoveSuccessful = new \Blackjack\Objects\Error(E_BJ_MOVE_UNKNOWN); } if($isMoveSuccessful instanceof \Blackjack\Objects\Error) return $isMoveSuccessful; $table->UpdateTableState($handIndex); return $table; } // end of move evaluator //return the table in the session if nothing else return $_SESSION[BJ_SESSION_KEY_TABLE]; } $responseObject = GetResponse(); $response = new \Blackjack\Objects\Response($responseObject, $responseObject instanceof \Blackjack\Objects\Error, false); header('Content-type: application/json'); $jsonResponse = json_encode($response); if($_SESSION[BJ_SESSION_KEY_TABLE]->GetTableState() == BJ_TABLE_ENDED) { //clear session if(session_id() == '') session_start(); session_unset(); session_destroy(); $response->isSessionEnded = true; } echo $jsonResponse; ?>