function processCommand($clientInfo, $data) { if ($this->State != "lobby") { switch ($data["command"]) { case "bid": if ($this->State->NextAction === "Team1Player1Bid" && $clientInfo["teamNumber"] === 1 && $clientInfo["playerNumber"] === 1 || $this->State->NextAction === "Team1Player2Bid" && $clientInfo["teamNumber"] === 1 && $clientInfo["playerNumber"] === 2 || $this->State->NextAction === "Team2Player1Bid" && $clientInfo["teamNumber"] === 2 && $clientInfo["playerNumber"] === 1 || $this->State->NextAction === "Team2Player2Bid" && $clientInfo["teamNumber"] === 2 && $clientInfo["playerNumber"] === 2) { if ((string) $data["arguments"] === "pass") { if ($clientInfo["teamNumber"] === 1) { if ($clientInfo["playerNumber"] === 1) { $this->Team1->Player1->HasPassed = true; } else { $this->Team1->Player2->HasPassed = true; } } else { if ($clientInfo["playerNumber"] === 1) { $this->Team2->Player1->HasPassed = true; } else { $this->Team2->Player2->HasPassed = true; } } if (getNumberOfPassedPlayers($clientInfo["game"]) == 3) { $round = end($this->Rounds); array_push($round->Tricks, new Trick()); if ($this->Team1->Player1->HasPassed === false) { $round->TeamBidWinner = $this->Team1; $round->PlayerBidWinner = $this->Team1->Player1; $this->State->NextAction = "Team1Player1Kitty"; $waitingOn = 0; } if ($this->Team1->Player2->HasPassed === false) { $round->TeamBidWinner = $this->Team1; $round->PlayerBidWinner = $this->Team1->Player2; $this->State->NextAction = "Team1Player2Kitty"; $waitingOn = 2; } if ($this->Team2->Player1->HasPassed === false) { $round->TeamBidWinner = $this->Team2; $round->PlayerBidWinner = $this->Team2->Player1; $this->State->NextAction = "Team2Player1Kitty"; $waitingOn = 1; } if ($this->Team2->Player2->HasPassed === false) { $round->TeamBidWinner = $this->Team2; $round->PlayerBidWinner = $this->Team2->Player2; $this->State->NextAction = "Team2Player2Kitty"; $waitingOn = 3; } $allClients = getAllClientIdsInGame($clientInfo["game"]); foreach ($allClients as $id) { if ($id != $round->PlayerBidWinner->ClientId) { $response = array("action" => "command", "message" => "waitforkitty", "data" => array("bid" => $round->Bid, "bidwinner" => $round->PlayerBidWinner->Name)); sendJson($id, $response); $response = array("action" => "command", "message" => "losepermission"); sendJson($id, $response); } else { $kittyCards = array(); foreach ($round->Kitty as $card) { array_push($kittyCards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } $response = array("action" => "command", "message" => "kitty", "data" => $kittyCards); sendJson($id, $response); $response = array("action" => "command", "message" => "gainpermission"); sendJson($id, $response); } $response = array("action" => "command", "message" => "waitingon", "data" => $waitingOn); sendJson($id, $response); } } else { $round = end($this->Rounds); $allClients = getAllClientIdsInGame($clientInfo["game"]); $nextClientId = getNextBidder($clientInfo); foreach ($allClients as $thisClient) { $response = array("action" => "command", "message" => "newsfeed", "data" => $clientInfo["player"]->Name . " has passed"); sendJson($thisClient, $response); if ($thisClient === $nextClientId) { $response = array("action" => "command", "message" => "gainpermission"); sendJson($nextClientId, $response); $response = array("action" => "command", "message" => "yourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($nextClientId, $response); } else { $response = array("action" => "command", "message" => "losepermission"); sendJson($thisClient, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($thisClient, $response); } } } } elseif ($data["arguments"] % 5 === 0) { $round = end($this->Rounds); if ((int) $data["arguments"] > $round->Bid && (int) $data["arguments"] < 180) { $round->Bid = (int) $data["arguments"]; $round->CurrentHighestBidder = $clientInfo["player"]; $allClients = getAllClientIdsInGame($clientInfo["game"]); $nextClientId = getNextBidder($clientInfo); foreach ($allClients as $id) { $response = array("action" => "command", "message" => "newsfeed", "data" => $clientInfo["player"]->Name . " just bid " . (string) $round->Bid); sendJson($id, $response); if ($nextClientId === $id) { $response = array("action" => "command", "message" => "gainpermission"); sendJson($id, $response); $response = array("action" => "command", "message" => "yourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($id, $response); } else { $response = array("action" => "command", "message" => "losepermission"); sendJson($id, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($id, $response); } } } elseif ((int) $data["arguments"] === 180) { $round = end($this->Rounds); array_push($round->Tricks, new Trick()); $round->Bid = 180; if ($this->Team1->Player1->ClientId === $clientInfo["clientId"]) { $round->TeamBidWinner = $this->Team1; $round->PlayerBidWinner = $this->Team1->Player1; $this->State->NextAction = "Team1Player1Kitty"; $waitingOn = 0; } if ($this->Team1->Player2->ClientId === $clientInfo["clientId"]) { $round->TeamBidWinner = $this->Team1; $round->PlayerBidWinner = $this->Team1->Player2; $this->State->NextAction = "Team1Player2Kitty"; $waitingOn = 2; } if ($this->Team2->Player1->ClientId === $clientInfo["clientId"]) { $round->TeamBidWinner = $this->Team2; $round->PlayerBidWinner = $this->Team2->Player1; $this->State->NextAction = "Team2Player1Kitty"; $waitingOn = 1; } if ($this->Team2->Player2->ClientId === $clientInfo["clientId"]) { $round->TeamBidWinner = $this->Team2; $round->PlayerBidWinner = $this->Team2->Player2; $this->State->NextAction = "Team2Player2Kitty"; $waitingOn = 3; } $allClients = getAllClientIdsInGame($clientInfo["game"]); foreach ($allClients as $id) { if ($id != $round->PlayerBidWinner->ClientId) { $response = array("action" => "command", "message" => "waitforkitty", "data" => array("bid" => $round->Bid, "bidwinner" => $round->PlayerBidWinner->Name)); sendJson($id, $response); $response = array("action" => "command", "message" => "losepermission"); sendJson($id, $response); } else { $kittyCards = array(); foreach ($round->Kitty as $card) { array_push($kittyCards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } $response = array("action" => "command", "message" => "kitty", "data" => $kittyCards); sendJson($id, $response); $response = array("action" => "command", "message" => "gainpermission"); sendJson($id, $response); } $response = array("action" => "command", "message" => "waitingon", "data" => $waitingOn); sendJson($id, $response); } } else { $response = array("action" => "alert", "message" => "Sorry, your bid must be higher than the current bid of " . (string) $round->Bid . " and no greater than 180."); sendJson($clientInfo["clientId"], $response); } } else { $response = array("action" => "alert", "message" => "Sorry, your bid must be a multiple of 5."); sendJson($clientInfo["clientId"], $response); } } break; case "kitty": if ($this->State->NextAction === "Team1Player1Kitty" && $clientInfo["teamNumber"] === 1 && $clientInfo["playerNumber"] === 1 || $this->State->NextAction === "Team1Player2Kitty" && $clientInfo["teamNumber"] === 1 && $clientInfo["playerNumber"] === 2 || $this->State->NextAction === "Team2Player1Kitty" && $clientInfo["teamNumber"] === 2 && $clientInfo["playerNumber"] === 1 || $this->State->NextAction === "Team2Player2Kitty" && $clientInfo["teamNumber"] === 2 && $clientInfo["playerNumber"] === 2) { $allCardsAreValid = true; $chosenHandCards = array(); $chosenKittyCards = array(); foreach ($data["arguments"] as $card) { $round = end($this->Rounds); $kitty = $round->Kitty; $handCard = playerHasCard($clientInfo, $card); $kittyCard = kittyHasCard($clientInfo, $card); if (!$handCard && !$kittyCard) { $allCardsAreValid = false; break; } if ($handCard) { array_push($chosenHandCards, $handCard); } elseif ($kittyCard) { array_push($chosenKittyCards, $kittyCard); } } $round = end($this->Rounds); if ($allCardsAreValid) { //set the trump for the round if ((string) $data["trumpcolor"] === "green") { $round->Trump = Suit::Green; } elseif ((string) $data["trumpcolor"] === "red") { $round->Trump = Suit::Red; } elseif ((string) $data["trumpcolor"] === "black") { $round->Trump = Suit::Black; } elseif ((string) $data["trumpcolor"] === "yellow") { $round->Trump = Suit::Yellow; } else { $response = array("action" => "alert", "message" => "Invalid color choice"); sendJson($clientInfo["clientId"], $response); break; } moveCardsToKitty($clientInfo, $chosenHandCards, $chosenKittyCards); $allClients = getAllClientIdsInGame($clientInfo["game"]); $round = end($this->Rounds); $trumpColor = getSuitAsString($round->Trump, true); if (checkIfPointsInKitty($clientInfo)) { $messageString = "There are points in the kitty."; } else { $messageString = "There are no points in the kitty."; } $firstlayplayer = getAbsolutePlayerNumber($clientInfo); foreach ($allClients as $id) { $response = array("action" => "command", "message" => "newsfeed", "data" => (string) $clientInfo["player"]->Name . " is finished with the kitty, and trump is " . $trumpColor . ". " . $messageString); sendJson($id, $response); $response = array("action" => "command", "message" => "beginlay", "playernumber" => (string) $firstlayplayer, "data" => cardsToJsonArray($clientInfo["player"]->Hand)); sendJson($id, $response); } $teamNumber = $clientInfo["teamNumber"]; $playerNumber = $clientInfo["playerNumber"]; if ($teamNumber === 1 && $playerNumber === 1) { $this->State->NextAction = "Team1Player1Lay"; } if ($teamNumber === 1 && $playerNumber === 2) { $this->State->NextAction = "Team1Player2Lay"; } if ($teamNumber === 2 && $playerNumber === 1) { $this->State->NextAction = "Team2Player1Lay"; } if ($teamNumber === 2 && $playerNumber === 2) { $this->State->NextAction = "Team2Player2Lay"; } } else { $response = array("action" => "alert", "message" => "Invalid card selection."); sendJson($clientInfo["clientId"], $response); } } break; case "lay": if ($this->State->NextAction === "Team1Player1Lay" && $clientInfo["teamNumber"] === 1 && $clientInfo["playerNumber"] === 1 || $this->State->NextAction === "Team1Player2Lay" && $clientInfo["teamNumber"] === 1 && $clientInfo["playerNumber"] === 2 || $this->State->NextAction === "Team2Player1Lay" && $clientInfo["teamNumber"] === 2 && $clientInfo["playerNumber"] === 1 || $this->State->NextAction === "Team2Player2Lay" && $clientInfo["teamNumber"] === 2 && $clientInfo["playerNumber"] === 2) { $card = playerHasCard($clientInfo, $data["arguments"]); if ($card !== null) { $round = end($this->Rounds); $trick = end($round->Tricks); if (isLegalMove($clientInfo, $trick, $card)) { if ($card->Suit === Suit::Rook && $this->Rules->NoRookOnFirstTrick && count($round->Tricks) === 1) { $response = array("action" => "alert", "message" => "You cannot play the Rook on the first trick"); sendJson($clientInfo["clientId"], $response); return; } array_push($trick->CardSet, $card); array_push($trick->PlayerOrder, $clientInfo); removeCardFromHand($clientInfo["clientId"], $clientInfo["player"], $card); $allClients = getAllClientIdsInGame($clientInfo["game"]); $thisPlayerNumber = getAbsolutePlayerNumber($clientInfo); foreach ($allClients as $id) { $response = array("action" => "command", "message" => "cardlaid", "data" => array("player" => $thisPlayerNumber, "suit" => $card->getSuitAsString(), "number" => $card->Number, "numberofcardsintrick" => count($trick->CardSet))); sendJson($id, $response); } if (count($trick->CardSet) === 4) { $leadSuit = $trick->CardSet[0]->Suit; if ($leadSuit === Suit::Rook) { $leadSuit === $round->Trump; } $highestValue = 0; $highestCard; $trumpIsInRound = false; foreach ($trick->CardSet as $card) { $cardSuit = $card->Suit; if ($cardSuit === Suit::Rook) { $cardSuit = $round->Trump; } if ($cardSuit === $round->Trump && !$trumpIsInRound) { $highestValue = 0; $trumpIsInRound = true; } if ($cardSuit === $leadSuit && !$trumpIsInRound) { $cardNumber = $card->Number; if ($cardNumber === 1) { $cardNumber = 15; } if ($cardNumber > $highestValue) { $highestValue = $cardNumber; $highestCard = $card; } } elseif ($cardSuit === $round->Trump && $trumpIsInRound) { $cardNumber = $card->Number; if ($cardNumber === 1) { $cardNumber = 15; } if ($cardNumber > $highestValue) { $highestValue = $cardNumber; $highestCard = $card; } } } $key = array_search($highestCard, $trick->CardSet); $winnerInfo = $trick->PlayerOrder[$key]; $trick->WinningTeam = $winnerInfo["teamNumber"]; $waitingOn; if ($winnerInfo["teamNumber"] === 1 && $winnerInfo["playerNumber"] === 1) { $this->State->NextAction = "Team1Player1Lay"; $waitingOn = 0; } if ($winnerInfo["teamNumber"] === 1 && $winnerInfo["playerNumber"] === 2) { $this->State->NextAction = "Team1Player2Lay"; $waitingOn = 2; } if ($winnerInfo["teamNumber"] === 2 && $winnerInfo["playerNumber"] === 1) { $this->State->NextAction = "Team2Player1Lay"; $waitingOn = 1; } if ($winnerInfo["teamNumber"] === 2 && $winnerInfo["playerNumber"] === 2) { $this->State->NextAction = "Team2Player2Lay"; $waitingOn = 3; } $endOfGameInfo = null; $isLastRound = false; if (count($round->Tricks) === 10) { $isLastRound = true; $endOfGameInfo = computeEndOfGameInfo($clientInfo, $winnerInfo); $this->State->NextAction = "ConfirmNextGame"; } else { array_push($round->Tricks, new Trick()); } $allClients = getAllClientIdsInGame($clientInfo["game"]); if (!$isLastRound) { foreach ($allClients as $id) { if ($id === $winnerInfo["clientId"]) { $response = array("action" => "command", "message" => "gainpermission"); sendJson($id, $response); $response = array("action" => "command", "message" => "setallowedsuits", "numberofcardsintrick" => "0", "data" => array("black", "yellow", "red", "green", "rook")); sendJson($id, $response); } else { $response = array("action" => "command", "message" => "losepermission"); sendJson($id, $response); } $response = array("action" => "command", "message" => "newsfeed", "data" => "The trick was won by " . $winnerInfo["player"]->Name . " with the " . $highestCard->toString()); sendJson($id, $response); $response = array("action" => "command", "message" => "trickdone", "data" => $waitingOn); sendJson($id, $response); } } else { foreach ($allClients as $id) { $response = array("action" => "command", "message" => "gainpermission"); sendJson($id, $response); $response = array("action" => "command", "message" => "newsfeed", "data" => "The trick was won by " . $winnerInfo["player"]->Name . " with the " . $highestCard->toString()); sendJson($id, $response); $response = array("action" => "command", "message" => "trickdone", "data" => $waitingOn); sendJson($id, $response); $response = array("action" => "command", "message" => "endgame", "data" => $endOfGameInfo); sendJson($id, $response); } if (!is_null($endOfGameInfo) && $endOfGameInfo["gameIsDone"] === "yes") { $this->DeleteMe = true; } } } else { setNextGameState($clientInfo); $allClients = getAllClientIdsInGame($clientInfo["game"]); $allowedSuits = getAllowedSuitsForNextPlayer($clientInfo); foreach ($allClients as $id) { if ($id === getNextClientId($clientInfo)) { $response = array("action" => "command", "message" => "gainpermission"); sendJson($id, $response); $response = array("action" => "command", "message" => "setallowedsuits", "data" => $allowedSuits, "numberofcardsintrick" => count($trick->CardSet)); sendJson($id, $response); } else { $response = array("action" => "command", "message" => "losepermission"); sendJson($id, $response); } } } } else { $response = array("action" => "alert", "message" => "Sorry, you must follow suit."); sendJson($clientInfo["clientId"], $response); } } else { $response = array("action" => "alert", "message" => "Sorry, you do not have that card in your hand."); sendJson($clientInfo["clientId"], $response); } } break; case "nextgame": if ($this->State->NextAction === "ConfirmNextGame") { $thisGame = $clientInfo["game"]; $clientInfo["player"]->NextGameConfirmed = true; if ($thisGame->Team1->Player1->NextGameConfirmed && $thisGame->Team1->Player2->NextGameConfirmed && $thisGame->Team2->Player1->NextGameConfirmed && $thisGame->Team2->Player2->NextGameConfirmed) { $thisGame->Team1->Player1->HasPassed = false; $thisGame->Team1->Player2->HasPassed = false; $thisGame->Team2->Player1->HasPassed = false; $thisGame->Team2->Player2->HasPassed = false; $thisGame->Team1->Player1->NextGameConfirmed = false; $thisGame->Team1->Player2->NextGameConfirmed = false; $thisGame->Team2->Player1->NextGameConfirmed = false; $thisGame->Team2->Player2->NextGameConfirmed = false; $bidStarter = $thisGame->BidStarter + 1; if ($bidStarter === 5) { $bidStarter = 1; } if ($bidStarter === 1) { $thisGame->State->NextAction = "Team1Player1Bid"; } else { if ($bidStarter === 2) { $thisGame->State->NextAction = "Team2Player1Bid"; } else { if ($bidStarter === 3) { $thisGame->State->NextAction = "Team1Player2Bid"; } else { if ($bidStarter === 4) { $thisGame->State->NextAction = "Team2Player2Bid"; } } } } $thisGame->BidStarter = $bidStarter; array_push($thisGame->Rounds, new Round()); $gamePlayers = array($thisGame->Team1->Player1->ClientId, $thisGame->Team2->Player1->ClientId, $thisGame->Team1->Player2->ClientId, $thisGame->Team2->Player2->ClientId); for ($i = 0; $i < 4; $i++) { $response = array("action" => "command", "message" => "resetfornextgame"); sendJson($gamePlayers[$i], $response); $response = array("action" => "command", "message" => "losepermission"); sendJson($gamePlayers[$i], $response); $response = array("action" => "command", "message" => "waitingon", "data" => $bidStarter - 1); sendJson($gamePlayers[$i], $response); } deal($thisGame); $p1Cards = array(); $p2Cards = array(); $p3Cards = array(); $p4Cards = array(); $kittyCards = ""; $round = end($thisGame->Rounds); if ($bidStarter === 1) { $round->CurrentHighestBidder = $thisGame->Team2->Player2; } if ($bidStarter === 2) { $round->CurrentHighestBidder = $thisGame->Team1->Player1; } if ($bidStarter === 3) { $round->CurrentHighestBidder = $thisGame->Team2->Player1; } if ($bidStarter === 4) { $round->CurrentHighestBidder = $thisGame->Team1->Player2; } foreach ($thisGame->Team1->Player1->Hand as $card) { array_push($p1Cards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } foreach ($thisGame->Team1->Player2->Hand as $card) { array_push($p2Cards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } foreach ($thisGame->Team2->Player1->Hand as $card) { array_push($p3Cards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } foreach ($thisGame->Team2->Player2->Hand as $card) { array_push($p4Cards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } foreach ($round->Kitty as $card) { $kittyCards = $kittyCards . $card->toString() . ", "; } $response = array("action" => "command", "message" => "initializecards", "data" => $p1Cards); sendJson($thisGame->Team1->Player1->ClientId, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($thisGame->Team1->Player1->ClientId, $response); $response = array("action" => "command", "message" => "initializecards", "data" => $p2Cards); sendJson($thisGame->Team1->Player2->ClientId, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($thisGame->Team1->Player2->ClientId, $response); $response = array("action" => "command", "message" => "initializecards", "data" => $p3Cards); sendJson($thisGame->Team2->Player1->ClientId, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($thisGame->Team2->Player1->ClientId, $response); $response = array("action" => "command", "message" => "initializecards", "data" => $p4Cards); sendJson($thisGame->Team2->Player2->ClientId, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($thisGame->Team2->Player2->ClientId, $response); $response = array("action" => "command", "message" => "gainpermission"); sendJson($gamePlayers[$bidStarter - 1], $response); $response = array("action" => "command", "message" => "yourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($gamePlayers[$bidStarter - 1], $response); } } } } }
function setNextGameState($clientInfo) { $clientTeam = $clientInfo["teamNumber"]; $clientPlayer = $clientInfo["playerNumber"]; $game = $clientInfo["game"]; if ($clientTeam === 1) { if ($clientPlayer === 1) { $game->State->NextAction = "Team2Player1Lay"; $waitingOn = 1; } else { $game->State->NextAction = "Team2Player2Lay"; $waitingOn = 3; } } else { if ($clientPlayer === 1) { $game->State->NextAction = "Team1Player2Lay"; $waitingOn = 2; } else { $game->State->NextAction = "Team1Player1Lay"; $waitingOn = 0; } } $allClientIds = getAllClientIdsInGame($game); foreach ($allClientIds as $id) { $response = array("action" => "command", "message" => "waitingon", "data" => $waitingOn); sendJson($id, $response); } }