/** * server.java */ package boondock.holdem.Server; import boondock.holdem.Player.Player; import boondock.holdem.Cards.*; import boondock.holdem.Network.*; import java.util.*; import java.net.*; import java.io.*; import java.util.logging.*; import javax.swing.JFrame; import java.awt.*; import javax.swing.*; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; /** * Server which allows connections, handles the logic of the gameplay, and coordinates transmissions of messages and commands. * * @author Jonathan O'Keefe * @author Scott Semonian * @author Matt Brinza * @author Hamid R. Tahsildoost * * @author The Boondock Saints * @author No Limit Texas Holdem */ public class server implements MessageListener{ public static final String DEFAULT_MYSQL_PATH = "jdbc:mysql://localhost/handvalues?user=boondock&password=saints"; public int numOfPlayers = 0; private Player[] allPlayers = new Player[numOfPlayers]; private boolean gameStart = false; public int currentPlayer = 0, lastActionPlayer = 0; Deck cardDeck; int soundID = 0; boolean flop = false; boolean turn = false; boolean river = false; int smallBlindAmount = 75; int bigBlindAmount = 75; int potSize = 0; int onew = -1; private Card flop1 = new Card(0), flop2 = new Card(0), flop3 = new Card(0), turnCard = new Card(0), riverCard = new Card(0); private sidePot pot1 = new sidePot(); private sidePot pot2 = new sidePot(); private sidePot pot3 = new sidePot(); private sidePot pot4 = new sidePot(); private sidePot pot5 = new sidePot(); private sidePot pot6 = new sidePot(); private sidePot pot7 = new sidePot(); private boolean foundNextPlayer; private boolean doingBlinds; public static Logger log = Logger.getLogger(server.class.getName()); //public static StreamHandler h = new StreamHandler(System.err,new SimpleFormatter()); public static StreamHandler h; static { try { h = new StreamHandler(System.err,new SimpleFormatter()); } catch (Exception ignore) {} } /** * Start the server and allow connections */ public void startServer() { try { ServerSocket serverSocket = new ServerSocket(constants.SERVER_PORT, 100); //log.info("Server listening on port " + constants.SERVER_PORT + ":"); while (!gameStart) { log.finer(String.valueOf(gameStart)); Socket clientSocket = serverSocket.accept(); new ReceivingThread(this, clientSocket).start(); //log.info("Connection received from: " + clientSocket.getInetAddress()); //log.finer(clientSocket.userName); //log.finer(clientSocket.getInetAddress().getHostName()); } } catch (IOException ioException) { //ioException.printStackTrace(); } } /** * This is the brains of the game. Decision making and handling user input all take place here * This function handles the recieving of messages and processes them. Then acts accordingly and * sends out 'updated information' to users as necesarry. * This function is synchronized. */ public void messageReceived(String from, String text){ //processing code goes here String completeMessage = from + constants.MESSAGE_SEPARATOR + text; if(!text.matches("COMMAND:.*")) { try { wait(100); } catch (Exception e) { //do nothing } new MulticastSendingThread(completeMessage.getBytes()).start(); } int tempPlayerIndex = -1; String command; foundNextPlayer = false; int numPlayersCounter = 0; Player onePlayer = new Player(); if(gameStart) { //game logic here //if players turn, then act accordingly and send to all players //else do nothing if(text.matches("COMMAND:.*")) { log.finest("Got command"); tempPlayerIndex = getPlayerIndex(from); if((tempPlayerIndex >= 0) && (allPlayers[tempPlayerIndex].getIsMyTurn())) { tempPlayerIndex=-1; //do my command //System.out.println(text); if(allPlayers[currentPlayer].isInCurrentHand()) { command = text.substring(8); ////log.info("Doing command " + command + " for: " + allPlayers[currentPlayer].getName()); if(command.equals("fold")) { // soundID += 4; completeMessage = from + constants.MESSAGE_SEPARATOR + " FOLD!"; new MulticastSendingThread(completeMessage.getBytes()).start(); log.finest("I FOLD"); allPlayers[currentPlayer].setInCurrentHand(false); //see if only one other player exists numPlayersCounter = 0; for(int i = 0; i < numOfPlayers; i++) { if(allPlayers[i].isInCurrentHand()) { numPlayersCounter++; onew = i; //onePlayer = allPlayers[i]; } } allPlayers[currentPlayer].setCallForPot1(0); allPlayers[currentPlayer].setCallForPot2(0); allPlayers[currentPlayer].setCallForPot3(0); allPlayers[currentPlayer].setCallForPot4(0); allPlayers[currentPlayer].setCallForPot5(0); allPlayers[currentPlayer].setCallForPot6(0); allPlayers[currentPlayer].setCallForPot7(0); //int next = nextPlayer(currentPlayer); ///////allPlayers[currentPlayer].setCurrentHandCheckStatus(true); if(allPlayers[currentPlayer].getAmountBetInPot7()>0){ pot7.remove(currentPlayer); pot6.remove(currentPlayer); pot5.remove(currentPlayer); pot4.remove(currentPlayer); pot3.remove(currentPlayer); pot2.remove(currentPlayer); pot1.remove(currentPlayer); } else{ if(allPlayers[currentPlayer].getAmountBetInPot6()>0){ pot6.remove(currentPlayer); pot5.remove(currentPlayer); pot4.remove(currentPlayer); pot3.remove(currentPlayer); pot2.remove(currentPlayer); pot1.remove(currentPlayer); } else{ if(allPlayers[currentPlayer].getAmountBetInPot5()>0){ pot5.remove(currentPlayer); pot4.remove(currentPlayer); pot3.remove(currentPlayer); pot2.remove(currentPlayer); pot1.remove(currentPlayer); } else{ if(allPlayers[currentPlayer].getAmountBetInPot4()>0){ pot4.remove(currentPlayer); pot3.remove(currentPlayer); pot2.remove(currentPlayer); pot1.remove(currentPlayer); } else{ if(allPlayers[currentPlayer].getAmountBetInPot3()>0){ pot3.remove(currentPlayer); pot2.remove(currentPlayer); pot1.remove(currentPlayer); } else{ if(allPlayers[currentPlayer].getAmountBetInPot2()>0){ pot2.remove(currentPlayer); pot1.remove(currentPlayer); } else{ if(allPlayers[currentPlayer].getAmountBetInHand()>0){ pot1.remove(currentPlayer); } } } } } } } if(numPlayersCounter == 1) {//if yes, then he/she wins flop = true; river = true; turn = true; newCardDealtInHand(); //THE LAST PLAYER LEFT WINS BY FOLD, SEE ////log.info ABOVE return; } if(lastActionPlayer == nextPlayer(currentPlayer)) { ////log.info("THIS TURN IS OVER, SET VARS TO SHOW NEW CARD"); newCardDealtInHand(); return; //KEEPS IT AS THIS PLAYERS TURN FOR NOW, THIS IS WHERE YOU DEAL NEW CARD(S) } }//END OF FOLD if(command.matches("raise.*")) { // soundID += 2; int raise = (int)Integer.parseInt(command.substring(5)); completeMessage = from + " " + constants.MESSAGE_SEPARATOR + " RAISE by " + raise; new MulticastSendingThread(completeMessage.getBytes()).start(); int smallGuy = getSmallestChipHolder(); if(raise<=allPlayers[smallGuy].getChipCount()){ if(allPlayers[currentPlayer].getAmountBetInHand()<=0){ pot1.setInSidePot(currentPlayer); } allPlayers[currentPlayer].setRaiseAmount(raise); allPlayers[currentPlayer].setAmountBetInRound(raise+allPlayers[currentPlayer].getAmountBetInRound()); allPlayers[currentPlayer].setChipCount(allPlayers[currentPlayer].getChipCount() - raise - allPlayers[currentPlayer].getCallAmount()); allPlayers[currentPlayer].setAmountBetInHand(allPlayers[currentPlayer].getAmountBetInHand() + raise + allPlayers[currentPlayer].getCallAmount()); potSize = potSize + raise + allPlayers[currentPlayer].getCallAmount(); pot1.addToSidePot(raise + allPlayers[currentPlayer].getCallAmount()); allPlayers[currentPlayer].setAmountBetInPot1(raise +allPlayers[currentPlayer].getCallForPot1() + allPlayers[currentPlayer].getAmountBetInPot1()); for(int c = 0; c < numOfPlayers; c++) { if(currentPlayer != c) { if(allPlayers[c].isInCurrentHand()) { allPlayers[c].setCallForPot1(raise + allPlayers[c].getCallAmount()); allPlayers[c].setCallAmount(raise + allPlayers[c].getCallAmount()); System.out.println(allPlayers[c].getCallAmount()); allPlayers[c].setRaiseAmount(0); } } } allPlayers[currentPlayer].setCallAmount(0); lastActionPlayer = currentPlayer; } else{ int[] order = orderOfChipHolders(); smallGuy = 0; int secondSmallest = 0; int thirdSmallest = 0; int fourthSmallest = 0; int fifthSmallest = 0; int sixthSmallest = 0; int seventhSmallest = 0; int betStack = allPlayers[currentPlayer].getCallAmount() + raise; int smallStack = order[0];//allPlayers[smallGuy].getChipCount() + allPlayers[smallGuy].getAmountBetInHand(); int secondStack = order[1];//allPlayers[secondSmallest].getChipCount() + allPlayers[secondSmallest].getAmountBetInHand(); int thirdStack = order[2];//allPlayers[thirdSmallest].getChipCount() + allPlayers[thirdSmallest].getAmountBetInHand(); int fourthStack = order[3];//allPlayers[fourthSmallest].getChipCount() + allPlayers[fourthSmallest].getAmountBetInHand(); int fifthStack = order[4];//allPlayers[fifthSmallest].getChipCount() + allPlayers[fifthSmallest].getAmountBetInHand(); int sixthStack = order[5];//allPlayers[sixthSmallest].getChipCount() + allPlayers[sixthSmallest].getAmountBetInHand(); int seventhStack = order[6];//allPlayers[seventhSmallest].getChipCount()+ allPlayers[seventhSmallest].getAmountBetInHand(); for(int i = 0; i=0){ allPlayers[p].setCallForPot1(smallStack - allPlayers[p].getAmountBetInPot1()); } else{ allPlayers[p].setCallForPot1(smallStack- allPlayers[p].getAmountBetInHand()); } } for(int p = 0; p=diff1){ if(allPlayers[p].getChipCount()>allPlayers[smallGuy].getChipCount()){ allPlayers[p].setCallForPot2(diff1- allPlayers[p].getAmountBetInPot2()); } } else{ if(allPlayers[p].getChipCount()>allPlayers[smallGuy].getChipCount()){ allPlayers[p].setCallForPot2(betStack-smallStack-allPlayers[p].getAmountBetInPot2()); } } } for(int p = 0; p=diff2){ if(allPlayers[p].getChipCount()>allPlayers[secondSmallest].getChipCount()){ allPlayers[p].setCallForPot3(diff2- allPlayers[p].getAmountBetInPot3()); } } else{ if(allPlayers[p].getChipCount()>allPlayers[secondSmallest].getChipCount()){ allPlayers[p].setCallForPot3(betStack-secondStack- allPlayers[p].getAmountBetInPot3()); } } } for(int p = 0; p=diff3){ if(allPlayers[p].getChipCount()>allPlayers[thirdSmallest].getChipCount()){ allPlayers[p].setCallForPot4(diff3- allPlayers[p].getAmountBetInPot4()); } } else{ if(allPlayers[p].getChipCount()>allPlayers[thirdSmallest].getChipCount()){ allPlayers[p].setCallForPot4(betStack-thirdStack-allPlayers[p].getAmountBetInPot4()); } } } for(int p = 0; p=diff4){ if(allPlayers[p].getChipCount()>allPlayers[fourthSmallest].getChipCount()){ allPlayers[p].setCallForPot5(diff4- allPlayers[p].getAmountBetInPot5()); } } else{ if(allPlayers[p].getChipCount()>allPlayers[fourthSmallest].getChipCount()){ allPlayers[p].setCallForPot5(betStack- fourthStack- allPlayers[p].getAmountBetInPot5()); } } } for(int p = 0; p=diff5){ if(allPlayers[p].getChipCount()>allPlayers[fifthSmallest].getChipCount()){ allPlayers[p].setCallForPot6(diff5- allPlayers[p].getAmountBetInPot6()); } } else{ if(allPlayers[p].getChipCount()>allPlayers[fifthSmallest].getChipCount()){ allPlayers[p].setCallForPot6(betStack- fifthStack- allPlayers[p].getAmountBetInPot6()); } } } for(int p = 0; p=diff6){ if(allPlayers[p].getChipCount()>allPlayers[sixthSmallest].getChipCount()){ allPlayers[p].setCallForPot7(diff6- allPlayers[p].getAmountBetInPot7()); } } else{ if(allPlayers[p].getChipCount()>allPlayers[sixthSmallest].getChipCount()){ allPlayers[p].setCallForPot7(betStack-sixthStack- allPlayers[p].getAmountBetInPot7()); } } } for(int x = 0; x< numOfPlayers; x+=1){ if (x!=currentPlayer){ allPlayers[x].setCallAmount(allPlayers[x].getCallForPot1() + allPlayers[x].getCallForPot2() + allPlayers[x].getCallForPot3() + allPlayers[x].getCallForPot4() + allPlayers[x].getCallForPot5() + allPlayers[x].getCallForPot6() + allPlayers[x].getCallForPot7()); } else{ allPlayers[x].setCallAmount(0); } } allPlayers[currentPlayer].setChipCount(allPlayers[currentPlayer].getChipCount()-(allPlayers[currentPlayer].getCallAmount() + raise)); potSize = allPlayers[currentPlayer].getCallAmount() + raise; if(nextPlayer(currentPlayer) == lastActionPlayer) { //log.info("new turn should happen here"); newCardDealtInHand(); return; //KEEPS IT AS THIS PLAYERS TURN FOR NOW, THIS IS WHERE YOU DEAL NEW CARD(S) } if(betStack >= smallStack){ allPlayers[currentPlayer].setAmountBetInPot1(smallStack); pot1.addToSidePot(smallStack); pot1.setInSidePot(currentPlayer); allPlayers[currentPlayer].setAmountBetInPot1(smallStack); //log.info("pot1 " + String.valueOf(pot1.getSidePot())); if(betStack>=secondStack){ allPlayers[currentPlayer].setAmountBetInPot2(diff1); pot2.addToSidePot(diff1); pot2.setInSidePot(currentPlayer); allPlayers[currentPlayer].setAmountBetInPot2(diff1); //log.info("pot2 " + String.valueOf(pot2.getSidePot())); if(betStack>=thirdStack){ allPlayers[currentPlayer].setAmountBetInPot3(diff2); pot3.addToSidePot(diff2); pot3.setInSidePot(currentPlayer); allPlayers[currentPlayer].setAmountBetInPot3(diff2); //log.info("pot 3" + String.valueOf(pot3.getSidePot())); if(betStack>=fourthStack){ allPlayers[currentPlayer].setAmountBetInPot4(diff3); pot4.addToSidePot(diff3); pot4.setInSidePot(currentPlayer); allPlayers[currentPlayer].setAmountBetInPot4(diff3); //log.info("pot 4" + String.valueOf(pot4.getSidePot())); if(betStack>=fifthStack){ allPlayers[currentPlayer].setAmountBetInPot5(diff4); pot5.addToSidePot(diff4); pot5.setInSidePot(currentPlayer); allPlayers[currentPlayer].setAmountBetInPot5(diff4); //log.info("pot5 " + String.valueOf(pot5.getSidePot())); if(betStack>=sixthStack){ allPlayers[currentPlayer].setAmountBetInPot6(diff5); pot6.addToSidePot(diff5); pot6.setInSidePot(currentPlayer); allPlayers[currentPlayer].setAmountBetInPot6(diff5); //log.info("pot6 " + String.valueOf(pot5.getSidePot())); if(betStack>=seventhStack){ allPlayers[currentPlayer].setAmountBetInPot7(diff6); pot7.addToSidePot(diff6); pot7.setInSidePot(currentPlayer); allPlayers[currentPlayer].setAmountBetInPot7(diff6); } else{ allPlayers[currentPlayer].setAmountBetInPot7(betStack-sixthStack); pot7.addToSidePot(betStack-sixthStack); pot7.setInSidePot(currentPlayer); allPlayers[currentPlayer].setAmountBetInPot7(betStack-sixthStack); } } else{ allPlayers[currentPlayer].setAmountBetInPot6(betStack-fifthStack); pot6.addToSidePot(betStack-fifthStack); pot6.setInSidePot(currentPlayer); allPlayers[currentPlayer].setAmountBetInPot6(betStack-fifthStack); } } else{ allPlayers[currentPlayer].setAmountBetInPot5(betStack-fourthStack); pot5.addToSidePot(betStack-fourthStack); pot5.setInSidePot(currentPlayer); allPlayers[currentPlayer].setAmountBetInPot5(betStack-fourthStack); } } else{ allPlayers[currentPlayer].setAmountBetInPot4(betStack-thirdStack); pot4.addToSidePot(betStack-thirdStack); pot4.setInSidePot(currentPlayer); allPlayers[currentPlayer].setAmountBetInPot4(betStack-thirdStack); } } else{ allPlayers[currentPlayer].setAmountBetInPot3(betStack-secondStack); pot3.addToSidePot(betStack-secondStack); pot3.setInSidePot(currentPlayer); allPlayers[currentPlayer].setAmountBetInPot3(betStack-secondStack); } } else{ allPlayers[currentPlayer].setAmountBetInPot2(betStack-smallStack); pot2.addToSidePot(betStack-smallStack); pot2.setInSidePot(currentPlayer); allPlayers[currentPlayer].setAmountBetInPot2(betStack-smallStack); } } else{ allPlayers[currentPlayer].setAmountBetInPot1(betStack); pot1.addToSidePot(betStack); pot1.setInSidePot(currentPlayer); allPlayers[currentPlayer].setAmountBetInPot1(betStack); } }//closes else }//END OF RAISE if(command.matches("check.*")) { // soundID += 1; completeMessage = from + constants.MESSAGE_SEPARATOR + " CHECK."; new MulticastSendingThread(completeMessage.getBytes()).start(); if(allPlayers[currentPlayer].getCallAmount() != 0){ //log.info("Denied Ability to Check"); return; } if(lastActionPlayer == nextPlayer(currentPlayer)) { newCardDealtInHand(); return; //KEEPS IT AS THIS PLAYERS TURN FOR NOW, THIS IS WHERE YOU DEAL NEW CARD(S) } }//END OF CHECK if(command.matches("call.*")) { // soundID += 2; completeMessage = from + constants.MESSAGE_SEPARATOR + " CALL."; new MulticastSendingThread(completeMessage.getBytes()).start(); allPlayers[currentPlayer].setChipCount(allPlayers[currentPlayer].getChipCount() - allPlayers[currentPlayer].getCallAmount()); potSize = potSize + allPlayers[currentPlayer].getCallAmount(); pot1.addToSidePot(allPlayers[currentPlayer].getCallForPot1()); allPlayers[currentPlayer].setAmountBetInHand(allPlayers[currentPlayer].getAmountBetInHand() + allPlayers[currentPlayer].getCallAmount()); allPlayers[currentPlayer].setCallAmount(0); pot2.addToSidePot(allPlayers[currentPlayer].getCallForPot2()); pot3.addToSidePot(allPlayers[currentPlayer].getCallForPot3()); pot4.addToSidePot(allPlayers[currentPlayer].getCallForPot4()); pot5.addToSidePot(allPlayers[currentPlayer].getCallForPot5()); pot6.addToSidePot(allPlayers[currentPlayer].getCallForPot6()); pot7.addToSidePot(allPlayers[currentPlayer].getCallForPot7()); allPlayers[currentPlayer].setAmountBetInPot1(allPlayers[currentPlayer].getCallForPot1() + allPlayers[currentPlayer].getAmountBetInPot1()); allPlayers[currentPlayer].setAmountBetInPot2(allPlayers[currentPlayer].getCallForPot2() + allPlayers[currentPlayer].getAmountBetInPot2()); allPlayers[currentPlayer].setAmountBetInPot3(allPlayers[currentPlayer].getCallForPot3() + allPlayers[currentPlayer].getAmountBetInPot3()); allPlayers[currentPlayer].setAmountBetInPot4(allPlayers[currentPlayer].getCallForPot4() + allPlayers[currentPlayer].getAmountBetInPot4()); allPlayers[currentPlayer].setAmountBetInPot5(allPlayers[currentPlayer].getCallForPot5() + allPlayers[currentPlayer].getAmountBetInPot5()); allPlayers[currentPlayer].setAmountBetInPot6(allPlayers[currentPlayer].getCallForPot6() + allPlayers[currentPlayer].getAmountBetInPot6()); allPlayers[currentPlayer].setAmountBetInPot7(allPlayers[currentPlayer].getCallForPot7() + allPlayers[currentPlayer].getAmountBetInPot7()); if(allPlayers[currentPlayer].getCallForPot1()>0){ pot1.setInSidePot(currentPlayer); } if(allPlayers[currentPlayer].getCallForPot2()>0){ pot2.setInSidePot(currentPlayer); } if(allPlayers[currentPlayer].getCallForPot3()>0){ pot3.setInSidePot(currentPlayer); } if(allPlayers[currentPlayer].getCallForPot4()>0){ pot4.setInSidePot(currentPlayer); } if(allPlayers[currentPlayer].getCallForPot5()>0){ pot5.setInSidePot(currentPlayer); } if(allPlayers[currentPlayer].getCallForPot6()>0){ pot6.setInSidePot(currentPlayer); } if(allPlayers[currentPlayer].getCallForPot7()>0){ pot7.setInSidePot(currentPlayer); } allPlayers[currentPlayer].setCallForPot1(0); allPlayers[currentPlayer].setCallForPot2(0); allPlayers[currentPlayer].setCallForPot3(0); allPlayers[currentPlayer].setCallForPot4(0); allPlayers[currentPlayer].setCallForPot5(0); allPlayers[currentPlayer].setCallForPot6(0); allPlayers[currentPlayer].setCallForPot7(0); if(lastActionPlayer == nextPlayer(currentPlayer)) { ////log.info("THIS TURN IS OVER, SHOW NEW CARD"); newCardDealtInHand(); return; //KEEPS IT AS THIS PLAYERS TURN FOR NOW, THIS IS WHERE YOU DEAL NEW CARD(S) } }//END OF CALL if(command.matches("allin.*")) { //shortcut for a 'raise' by 'amount of chips the player has left' // soundID += 3; completeMessage = from + constants.MESSAGE_SEPARATOR + " All In! Bring It!"; new MulticastSendingThread(completeMessage.getBytes()).start(); int smallGuy = getSmallestChipHolder(); if(smallGuy == currentPlayer || ((allPlayers[currentPlayer].getChipCount() + allPlayers[currentPlayer].getAmountBetInHand()) == (allPlayers[smallGuy].getChipCount() + allPlayers[smallGuy].getAmountBetInHand())) ) { int raise = (int)Integer.parseInt(command.substring(5)); allPlayers[currentPlayer].setRaiseAmount(raise); allPlayers[currentPlayer].setAmountBetInRound(raise+allPlayers[currentPlayer].getAmountBetInRound()); allPlayers[currentPlayer].setChipCount(allPlayers[currentPlayer].getChipCount() - raise); allPlayers[currentPlayer].setAmountBetInHand(allPlayers[currentPlayer].getAmountBetInHand() + raise); potSize = potSize + raise; pot1.addToSidePot(raise); pot1.setInSidePot(currentPlayer); for(int c = 0; c < numOfPlayers; c++) { if(currentPlayer != c) { if(allPlayers[c].isInCurrentHand()) { allPlayers[c].setCallAmount(raise - (allPlayers[currentPlayer].getCallAmount())+allPlayers[c].getCallAmount()); allPlayers[c].setCallForPot1(raise - (allPlayers[currentPlayer].getCallAmount())+allPlayers[c].getCallForPot1()); allPlayers[c].setRaiseAmount(0); } } } if(raise>allPlayers[currentPlayer].getCallAmount()){ lastActionPlayer = currentPlayer; ////log.info("Last action: " + lastActionPlayer); } if(nextPlayer(currentPlayer) == lastActionPlayer) { // ////log.info("new turn should happen here"); newCardDealtInHand(); return; //KEEPS IT AS THIS PLAYERS TURN FOR NOW, THIS IS WHERE YOU DEAL NEW CARD(S) } allPlayers[currentPlayer].setCallAmount(0); } else{ int[] order = orderOfChipHolders(); smallGuy = 0; int secondSmallest = 0; int thirdSmallest = 0; int fourthSmallest = 0; int fifthSmallest = 0; int sixthSmallest = 0; int seventhSmallest = 0; int betStack = allPlayers[currentPlayer].getChipCount() + allPlayers[currentPlayer].getAmountBetInHand(); int smallStack = order[0];//allPlayers[smallGuy].getChipCount() + allPlayers[smallGuy].getAmountBetInHand(); int secondStack = order[1];//allPlayers[secondSmallest].getChipCount() + allPlayers[secondSmallest].getAmountBetInHand(); int thirdStack = order[2];//allPlayers[thirdSmallest].getChipCount() + allPlayers[thirdSmallest].getAmountBetInHand(); int fourthStack = order[3];//allPlayers[fourthSmallest].getChipCount() + allPlayers[fourthSmallest].getAmountBetInHand(); int fifthStack = order[4];//allPlayers[fifthSmallest].getChipCount() + allPlayers[fifthSmallest].getAmountBetInHand(); int sixthStack = order[5];//allPlayers[sixthSmallest].getChipCount() + allPlayers[sixthSmallest].getAmountBetInHand(); int seventhStack = order[6];//allPlayers[seventhSmallest].getChipCount()+ allPlayers[seventhSmallest].getAmountBetInHand(); for(int i = 0; iallPlayers[currentPlayer].getCallAmount()){ lastActionPlayer = currentPlayer; int diff1 = secondStack - smallStack; int diff2 = thirdStack - secondStack; int diff3 = fourthStack - thirdStack; int diff4 = fifthStack - fourthStack; int diff5 = sixthStack - fifthStack; int diff6 = seventhStack - sixthStack; for(int p = 0; p=0){ allPlayers[p].setCallForPot1(smallStack - allPlayers[p].getAmountBetInPot1()); } else{ allPlayers[p].setCallForPot1(smallStack- allPlayers[p].getAmountBetInHand()); } } for(int p = 0; p=diff1){ if(allPlayers[p].getChipCount()>allPlayers[smallGuy].getChipCount()){ allPlayers[p].setCallForPot2(diff1- allPlayers[p].getAmountBetInPot2()); } } else{ if(allPlayers[p].getChipCount()>allPlayers[smallGuy].getChipCount()){ allPlayers[p].setCallForPot2(betStack-smallStack-allPlayers[p].getAmountBetInPot2()); } } } for(int p = 0; p=diff2){ if(allPlayers[p].getChipCount()>allPlayers[secondSmallest].getChipCount()){ allPlayers[p].setCallForPot3(diff2- allPlayers[p].getAmountBetInPot3()); } } else{ if(allPlayers[p].getChipCount()>allPlayers[secondSmallest].getChipCount()){ allPlayers[p].setCallForPot3(betStack-secondStack- allPlayers[p].getAmountBetInPot3()); } } } for(int p = 0; p=diff3){ if(allPlayers[p].getChipCount()>allPlayers[thirdSmallest].getChipCount()){ allPlayers[p].setCallForPot4(diff3- allPlayers[p].getAmountBetInPot4()); } } else{ if(allPlayers[p].getChipCount()>allPlayers[thirdSmallest].getChipCount()){ allPlayers[p].setCallForPot4(betStack-thirdStack-allPlayers[p].getAmountBetInPot4()); } } } for(int p = 0; p=diff4){ if(allPlayers[p].getChipCount()>allPlayers[fourthSmallest].getChipCount()){ allPlayers[p].setCallForPot5(diff4- allPlayers[p].getAmountBetInPot5()); } } else{ if(allPlayers[p].getChipCount()>allPlayers[fourthSmallest].getChipCount()){ allPlayers[p].setCallForPot5(betStack- fourthStack- allPlayers[p].getAmountBetInPot5()); } } } for(int p = 0; p=diff5){ if(allPlayers[p].getChipCount()>allPlayers[fifthSmallest].getChipCount()){ allPlayers[p].setCallForPot6(diff5- allPlayers[p].getAmountBetInPot6()); } } else{ if(allPlayers[p].getChipCount()>allPlayers[fifthSmallest].getChipCount()){ allPlayers[p].setCallForPot6(betStack- fifthStack- allPlayers[p].getAmountBetInPot6()); } } } for(int p = 0; p=diff6){ if(allPlayers[p].getChipCount()>allPlayers[sixthSmallest].getChipCount()){ allPlayers[p].setCallForPot7(diff6- allPlayers[p].getAmountBetInPot7()); } } else{ if(allPlayers[p].getChipCount()>allPlayers[sixthSmallest].getChipCount()){ allPlayers[p].setCallForPot7(betStack-sixthStack- allPlayers[p].getAmountBetInPot7()); } } } for(int x = 0; x< numOfPlayers; x+=1){ if (x!=currentPlayer){ allPlayers[x].setCallAmount(allPlayers[x].getCallForPot1() + allPlayers[x].getCallForPot2() + allPlayers[x].getCallForPot3() + allPlayers[x].getCallForPot4() + allPlayers[x].getCallForPot5() + allPlayers[x].getCallForPot6() + allPlayers[x].getCallForPot7()); } else{ allPlayers[x].setCallAmount(0); } } allPlayers[currentPlayer].setChipCount(0); potSize = potSize + allPlayers[currentPlayer].getChipCount(); allPlayers[currentPlayer].setChipCount(0); if(betStack >= smallStack){ if(smallStack-allPlayers[currentPlayer].getAmountBetInPot1()>0){ pot1.addToSidePot(smallStack-allPlayers[currentPlayer].getAmountBetInPot1()); allPlayers[currentPlayer].setAmountBetInPot1(smallStack-allPlayers[currentPlayer].getAmountBetInPot1()); pot1.setInSidePot(currentPlayer); } if(betStack>=secondStack){ if(diff1-allPlayers[currentPlayer].getAmountBetInPot2()>0){ pot2.addToSidePot(diff1-allPlayers[currentPlayer].getAmountBetInPot2()); allPlayers[currentPlayer].setAmountBetInPot2(diff1-allPlayers[currentPlayer].getAmountBetInPot2()); pot2.setInSidePot(currentPlayer); } if(betStack>=thirdStack){ if(diff2-allPlayers[currentPlayer].getAmountBetInPot3()>0){ pot3.addToSidePot(diff2-allPlayers[currentPlayer].getAmountBetInPot3()); allPlayers[currentPlayer].setAmountBetInPot3(diff2-allPlayers[currentPlayer].getAmountBetInPot3()); pot3.setInSidePot(currentPlayer); } if(betStack>=fourthStack){ if(diff3-allPlayers[currentPlayer].getAmountBetInPot4()>0){ pot4.addToSidePot(diff3-allPlayers[currentPlayer].getAmountBetInPot4()); allPlayers[currentPlayer].setAmountBetInPot4(diff3-allPlayers[currentPlayer].getAmountBetInPot4()); pot4.setInSidePot(currentPlayer); } if(betStack>=fifthStack){ if(diff4-allPlayers[currentPlayer].getAmountBetInPot5()>0){ pot5.addToSidePot(diff4-allPlayers[currentPlayer].getAmountBetInPot5()); allPlayers[currentPlayer].setAmountBetInPot5(diff4-allPlayers[currentPlayer].getAmountBetInPot5()); pot5.setInSidePot(currentPlayer); } if(betStack>=sixthStack){ if(diff5-allPlayers[currentPlayer].getAmountBetInPot6()>0){ pot6.addToSidePot(diff5-allPlayers[currentPlayer].getAmountBetInPot6()); allPlayers[currentPlayer].setAmountBetInPot6(diff5-allPlayers[currentPlayer].getAmountBetInPot6()); pot6.setInSidePot(currentPlayer); } if(betStack>=seventhStack){ if(diff6-allPlayers[currentPlayer].getAmountBetInPot7()>0){ pot7.addToSidePot(diff6-allPlayers[currentPlayer].getAmountBetInPot7()); allPlayers[currentPlayer].setAmountBetInPot7(diff6-allPlayers[currentPlayer].getAmountBetInPot7()); pot7.setInSidePot(currentPlayer); } } } } } } } } allPlayers[currentPlayer].setCallAmount(0); } else{ if(allPlayers[currentPlayer].getChipCount() == allPlayers[currentPlayer].getCallAmount()){ potSize = potSize + allPlayers[currentPlayer].getChipCount(); if(allPlayers[currentPlayer].getCallForPot1()>0){ pot1.setInSidePot(currentPlayer); } if(allPlayers[currentPlayer].getCallForPot2()>0){ pot2.setInSidePot(currentPlayer); } if(allPlayers[currentPlayer].getCallForPot3()>0){ pot3.setInSidePot(currentPlayer); } if(allPlayers[currentPlayer].getCallForPot4()>0){ pot4.setInSidePot(currentPlayer); } if(allPlayers[currentPlayer].getCallForPot5()>0){ pot5.setInSidePot(currentPlayer); } if(allPlayers[currentPlayer].getCallForPot6()>0){ pot6.setInSidePot(currentPlayer); } if(allPlayers[currentPlayer].getCallForPot7()>0){ pot7.setInSidePot(currentPlayer); } pot1.addToSidePot(allPlayers[currentPlayer].getCallForPot1()); pot2.addToSidePot(allPlayers[currentPlayer].getCallForPot2()); pot3.addToSidePot(allPlayers[currentPlayer].getCallForPot3()); pot4.addToSidePot(allPlayers[currentPlayer].getCallForPot4()); pot5.addToSidePot(allPlayers[currentPlayer].getCallForPot5()); pot6.addToSidePot(allPlayers[currentPlayer].getCallForPot6()); pot7.addToSidePot(allPlayers[currentPlayer].getCallForPot7()); allPlayers[currentPlayer].setChipCount(0); allPlayers[currentPlayer].setCallForPot1(0); allPlayers[currentPlayer].setCallForPot2(0); allPlayers[currentPlayer].setCallForPot3(0); allPlayers[currentPlayer].setCallForPot4(0); allPlayers[currentPlayer].setCallForPot5(0); allPlayers[currentPlayer].setCallForPot6(0); allPlayers[currentPlayer].setCallForPot7(0); allPlayers[currentPlayer].setCallAmount(0); if(nextPlayer(currentPlayer) == lastActionPlayer) { // ////log.info("new turn should happen here"); newCardDealtInHand(); return; //KEEPS IT AS THIS PLAYERS TURN FOR NOW, THIS IS WHERE YOU DEAL NEW CARD(S) } } } } }//END OF ALL IN } else { ////log.info("Player already folded: " + allPlayers[currentPlayer].getName()); } //go to next persons turn goNextPlayersTurn(); while(!allPlayers[currentPlayer].isInCurrentHand()) { goNextPlayersTurn(); }//end while }//if player exists and it is their turn, then do above. else just ignore command }//end of if statement that does command when commands are recieved. if ((text.matches("COMMAND:EXIT.*"))){ int p = getPlayerIndex(text.substring(12)); if((p > 0) && (p < numOfPlayers)){ if(allPlayers[p].getIsMyTurn()){ currentPlayer = nextPlayer(currentPlayer); } if(allPlayers[p].getIsDealer() && p == lastActionPlayer){ lastActionPlayer = nextPlayer(p); } System.out.println(text.substring(12)); allPlayers[p].setInGame(false); allPlayers[p].setInCurrentHand(false); allPlayers[p].setChipCount(0); System.out.println("Someone exited game."); } } }//do below of !gamestart else { //if game has NOT started if ((text.matches("COMMAND:EXIT.*"))){ int p = getPlayerIndex(text.substring(12)); if((p > 0) && (p < numOfPlayers)){ if(allPlayers[p].getIsMyTurn()){ currentPlayer = nextPlayer(currentPlayer); } if(allPlayers[p].getIsDealer() && p == lastActionPlayer){ lastActionPlayer = nextPlayer(p); } System.out.println(text.substring(12)); allPlayers[p].setInGame(false); allPlayers[p].setInCurrentHand(false); allPlayers[p].setChipCount(0); System.out.println("Someone exited game."); } } if ((text.equals("COMMAND:STARTGAME") && (numOfPlayers > 1)) ) { soundID += 6; cardDeck = new Deck(); gameStart = true; ////log.info("Game started"); currentPlayer = 0; potSize = 0; allPlayers[currentPlayer].setIsMyTurn(false); String tempMessage = ""; for(int i = 0; i < numOfPlayers; i++) { allPlayers[i].setFirstCard(cardDeck.nextCard());//deal first card to all } for(int i = 0; i < numOfPlayers; i++) { allPlayers[i].setSecondCard(cardDeck.nextCard());//deal second card to all } //give money for(int i = 0; i < numOfPlayers; i++) { allPlayers[i].setInGame(true); allPlayers[i].setInCurrentHand(true); allPlayers[i].setChipCount(10000); tempMessage = i + ":" + currentPlayer + ":" + soundID + ":" + allPlayers[i].getName() + ":" + allPlayers[i].getChipCount() + ":" + toTF(allPlayers[i].isInCurrentHand()) + ":" + toTF(allPlayers[i].getIsDealer()) + ":" + allPlayers[i].getFirstCard().toString() + ":" + allPlayers[i].getSecondCard().toString() + ":" + flop1 + ":" + flop2 + ":" + flop3 + ":" + turnCard + ":" + riverCard + ":" + toTF(flop) + ":" + toTF(turn) + ":" + toTF(river) + ":" + potSize + ":" + allPlayers[i].getAmountBetInHand() + ":" + allPlayers[i].getCallAmount() + ":" + toTF(doingBlinds); //BIND PROBLEM HERE, and in UPDATEALL FUNCTION, NEED A 'WAITER' tempMessage = "COMMAND_SERVER" + constants.MESSAGE_SEPARATOR + tempMessage; try { wait(500); } catch (Exception e) { ////log.info("Bind Exception occured and was caught. Information may not have been send out properly.");//do nothing } new MulticastSendingThread(tempMessage.getBytes()).start(); } //set dealer, big blind, small blind doingBlinds = true; allPlayers[currentPlayer].setIsDealer(true); currentPlayer = nextPlayer(currentPlayer); allPlayers[currentPlayer].setIsSmallBlind(true); allPlayers[currentPlayer].setIsMyTurn(true); doSmallBlind(currentPlayer); allPlayers[currentPlayer].setIsMyTurn(false); allPlayers[currentPlayer].setIsBigBlind(true); allPlayers[currentPlayer].setIsMyTurn(true); doBigBlind(currentPlayer); allPlayers[currentPlayer].setIsMyTurn(false); allPlayers[currentPlayer].setIsMyTurn(true); lastActionPlayer = currentPlayer; doingBlinds = false; }//start the game if the user said to. if (from.equals("CONNECT_SERVER") && (notInGame(text)) && (numOfPlayers < 8)) { //log.info("Player added: " + text); Player[] tempPlayers = new Player[numOfPlayers]; for(int i = 0; i < numOfPlayers; i++) { tempPlayers[i] = allPlayers[i]; }//copied players into larger array numOfPlayers++; allPlayers = new Player[numOfPlayers]; for(int i = 0; i < numOfPlayers-1;i++) { allPlayers[i] = tempPlayers[i]; } allPlayers[numOfPlayers-1] = new Player(); allPlayers[numOfPlayers-1].setName(text); } }//end of 'if game has not started' if(gameStart) { updatePlayerInfo(); } } /** * Main function basically starts up the server and waits for connections. * This function is synchronized. */ public synchronized static void main (String args[]) { h.setLevel(Level.INFO); log.addHandler(h); initPropertiesSetup(); SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); new server().startServer(); } private static void initPropertiesSetup() { try { Properties systemProperties = System.getProperties(); try { //get old file info // append project properties to System.properties FileInputStream propertyFile = new FileInputStream( "holdem.properties" ); systemProperties.load( propertyFile ); propertyFile.close(); } catch (Exception e) { //write new file // get the property value, if null then set the default String key = "BOONDOCK.holdem.MySQL.PATH"; String mySQLPath = systemProperties.getProperty( key ); if(mySQLPath == null) mySQLPath = DEFAULT_MYSQL_PATH; //from constants.java // Set the property value systemProperties.setProperty( key, mySQLPath ); // store properties FileOutputStream newPropertyFile = new FileOutputStream( "holdem.properties" ); systemProperties.store( newPropertyFile, "*** Boondock.Holdem ***" ); newPropertyFile.close(); } } catch(Exception e) { System.out.println("Unable to setup properties file."); } } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); JFrame serverGui = new ServerGui(); serverGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); serverGui.pack(); serverGui.setVisible(true); } /** * Function that determines if the name given is a player who's turn it is. * This function is synchronized. * * @param name The name of the user being searched for. * * @return True if the user is in the current game, False if the user is not in the current game. */ private synchronized boolean notInGame(String name) { for(int i=0; i < numOfPlayers;i++) { if(allPlayers[i].equals(name)) return false; } return true; } /** * Returns the integer value of the index, aka order in turn, where the player with the given name is located. * Otherwise returns a -1 * This function is synchronized. * * @param playerName The name of the player who's index is being requested. */ private synchronized int getPlayerIndex(String playerName) { for(int i = 0; i < numOfPlayers; i++) { if(playerName.equals(allPlayers[i].getName()) ) return i; } return -1; } /** * Updates all the player's information by sending messages to each user. * Allows users to see information about other players, which is filterd by the GUI such that * a player only sees his/her own cards, but can still see money and other attributes of opponents. * This function is synchronized. */ private synchronized void updatePlayerInfo() { String tempMessage; for(int i = 0; i < numOfPlayers; i++) { tempMessage = i + ":" + currentPlayer + ":" + soundID + ":" + allPlayers[i].getName() + ":" + allPlayers[i].getChipCount() + ":" + toTF(allPlayers[i].isInCurrentHand()) + ":" + toTF(allPlayers[i].getIsDealer()) + ":" + allPlayers[i].getFirstCard().toString() + ":" + allPlayers[i].getSecondCard().toString() + ":" + flop1 + ":" + flop2 + ":" + flop3 + ":" + turnCard + ":" + riverCard + ":" + toTF(flop) + ":" + toTF(turn) + ":" + toTF(river) + ":" + potSize + ":" + allPlayers[i].getAmountBetInHand() + ":" + allPlayers[i].getCallAmount() + ":" + toTF(doingBlinds); tempMessage = "COMMAND_SERVER" + constants.MESSAGE_SEPARATOR + tempMessage; //////log.info(tempMessage); try { wait(500); } catch (Exception e) { //do nothing } new MulticastSendingThread(tempMessage.getBytes()).start(); } } private synchronized int nextPlayer(int playerStart) { do { if(playerStart == numOfPlayers-1) playerStart = 0; else playerStart++; }while(!allPlayers[playerStart].isInCurrentHand()); return playerStart; } private synchronized void newCardDealtInHand() { if(flop == false){ soundID += 7; flop = true; cardDeck.nextCard(); flop1 = cardDeck.nextCard(); flop2 = cardDeck.nextCard(); flop3 = cardDeck.nextCard(); for(int i = 0; i < numOfPlayers; i++) { allPlayers[i].setThirdCard(flop1);//deal first card allPlayers[i].setFourthCard(flop2);//deal first card allPlayers[i].setFifthCard(flop3);//deal first card } String tempMessage="DEALING_FLOP" + constants.MESSAGE_SEPARATOR + "[" + flop1 + ", " + flop2 + ", " + flop3 + "]"; new MulticastSendingThread(tempMessage.getBytes()).start(); for(int i = 0; i 0){ con = true; } if(con){ for (int p = i+1; p< numOfPlayers;p++){ if (allPlayers[p].isInCurrentHand()){ if(allPlayers[p].getChipCount() > 0){ con2 = true; break; } } } } } } if(!con2 && onlyOne>1){ newCardDealtInHand(); try{ wait(100); } catch(Exception e){ } } } else if(turn == false){ soundID += 5; turn = true; cardDeck.nextCard(); turnCard = cardDeck.nextCard(); for(int i = 0; i < numOfPlayers; i++) { allPlayers[i].setSixthCard(turnCard);//deal first card } String tempMessage="DEALING_TURN" + constants.MESSAGE_SEPARATOR + "[" + turnCard + "]"; new MulticastSendingThread(tempMessage.getBytes()).start(); for(int i = 0; i 0){ con = true; } if(con){ for (int p = i+1; p< numOfPlayers;p++){ if (allPlayers[p].isInCurrentHand()){ if(allPlayers[p].getChipCount() > 0){ con2 = true; break; } } } } } } if(!con2 && onlyOne>1){ newCardDealtInHand(); try{ wait(100); } catch(Exception e){ } } } else if (river == false){ soundID += 5; river = true; cardDeck.nextCard(); riverCard = cardDeck.nextCard(); //System.out.println(riverCard); for(int i = 0; i < numOfPlayers; i++) { allPlayers[i].setSeventhCard(riverCard);//deal first card } String tempMessage="DEALING_RIVER" + constants.MESSAGE_SEPARATOR + "[" + riverCard + "]"; new MulticastSendingThread(tempMessage.getBytes()).start(); for(int i = 0; i 0){ con = true; } if(con){ for (int p = i+1; p< numOfPlayers;p++){ if (allPlayers[p].isInCurrentHand()){ if(allPlayers[p].getChipCount() > 0){ con2 = true; break; } } } } } } if(!con2 && onlyOne>1){ newCardDealtInHand(); try{ wait(100); } catch(Exception e){ } } } else{ Card[] community = new Card[5]; String scoreType[] = {"Nothing","Pair","Two Pair","Three of a Kind","Straight","Flush","Full House","Four of a Kind","Straight Flush"}; community[0] = flop1; community[1] = flop2; community[2] = flop3; community[3] = turnCard; community[4] = riverCard; for(int i = 0; i < numOfPlayers; i++) { flop = false; turn = false; river = false; allPlayers[i].setCommunityCards(community); for(int c = 0; c < 5; c++) { allPlayers[i].getHandScore(); } } int[] winners1 = getWinnerIndexes(pot1.getInSidePot()); int[] winners2 = getWinnerIndexes(pot2.getInSidePot()); int[] winners3 = getWinnerIndexes(pot3.getInSidePot()); int[] winners4 = getWinnerIndexes(pot4.getInSidePot()); int[] winners5 = getWinnerIndexes(pot5.getInSidePot()); int[] winners6 = getWinnerIndexes(pot6.getInSidePot()); int[] winners7 = getWinnerIndexes(pot7.getInSidePot()); for(int c = 0; c < numOfPlayers; c++) { if(winners1[c] == -1) { for(int q = 0; q < c; q++) { allPlayers[winners1[q]].setChipCount(allPlayers[winners1[q]].getChipCount() + pot1.getSidePot()/(c)); System.out.println("winners pot1 " + winners1[q]); System.out.println(allPlayers[winners1[q]].getChipCount()); } break; } } for(int c = 0; c < numOfPlayers; c++) { if(winners2[c] == -1) { for(int q = 0; q < c; q++) { allPlayers[winners2[q]].setChipCount(allPlayers[winners2[q]].getChipCount() + pot2.getSidePot()/(c)); } break; } } for(int c = 0; c < numOfPlayers; c++) { if(winners3[c] == -1) { for(int q = 0; q < c; q++) { allPlayers[winners3[q]].setChipCount(allPlayers[winners3[q]].getChipCount() + pot3.getSidePot()/(c)); } break; } } for(int c = 0; c < numOfPlayers; c++) { if(winners4[c] == -1) { for(int q = 0; q < c; q++) { allPlayers[winners4[q]].setChipCount(allPlayers[winners4[q]].getChipCount() + pot4.getSidePot()/(c)); } break; } } for(int c = 0; c < numOfPlayers; c++) { if(winners5[c] == -1) { for(int q = 0; q < c; q++) { allPlayers[winners5[q]].setChipCount(allPlayers[winners5[q]].getChipCount() + pot5.getSidePot()/(c)); } break; } } for(int c = 0; c < numOfPlayers; c++) { if(winners6[c] == -1) { for(int q = 0; q < c; q++) { allPlayers[winners6[q]].setChipCount(allPlayers[winners6[q]].getChipCount() + pot6.getSidePot()/(c)); } break; } } for(int c = 0; c < numOfPlayers; c++) { if(winners7[c] == -1) { for(int q = 0; q < c; q++) { allPlayers[winners7[q]].setChipCount(allPlayers[winners7[q]].getChipCount() + pot7.getSidePot()/(c)); } break; } } String tempMessage = ""; for(int i = 0; i 0){ allPlayers[l].setInCurrentHand(true); } } for(int p = 0; p