Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Room store #26

Merged
merged 10 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions client/src/main/java/online/screen/GameRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.*;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.prefs.Preferences;

Expand Down Expand Up @@ -60,7 +61,7 @@ public abstract class GameRoom extends JFrame
public Bid lastBid = null;

private ConcurrentHashMap<Integer, Player> hmPlayerByAdjustedPlayerNumber = new ConcurrentHashMap<>();
public ConcurrentHashMap<Integer, String[]> hmHandByAdjustedPlayerNumber = new ConcurrentHashMap<>();
public ConcurrentHashMap<Integer, List<String>> hmHandByAdjustedPlayerNumber = new ConcurrentHashMap<>();
public ConcurrentHashMap<Integer, Bid> hmBidByPlayerNumber = new ConcurrentHashMap<>();
private int personToStartLocal = -1;
private int personToStart = -1;
Expand Down Expand Up @@ -494,7 +495,7 @@ public void addOrUpdatePlayer(int playerNumber, String username)
newPlayer.setEnabled(true);
hmPlayerByAdjustedPlayerNumber.put(adjustedNo, newPlayer);

handPanel.initialisePlayer(adjustedNo, username, colour);
handPanel.initialisePlayer(adjustedNo, username, colour, 5);
handPanel.activateEmptySeats();
}
else
Expand Down Expand Up @@ -679,7 +680,7 @@ private void sendGameInitialiseRequest(int delay)
}
}

public void setHand(int playerNumber, String[] hand)
public void setHand(int playerNumber, List<String> hand)
{
int playerNumberAdjusted = adjustForMe(playerNumber);
hmHandByAdjustedPlayerNumber.put(playerNumberAdjusted, hand);
Expand Down Expand Up @@ -892,7 +893,7 @@ private void processEndOfGame(int winningPlayerAdjusted)
return;
}

handPanel.resetPlayers();
handPanel.resetPlayers(5);
clearScreenAfterGameEnd();

int currentSize = hmPlayerByAdjustedPlayerNumber.size();
Expand Down Expand Up @@ -970,15 +971,15 @@ private void adjustPlayersBasedOnHands()
{
Map.Entry<Integer, Player> entry = it.next();
int playerNumberAdjusted = entry.getKey();
String[] hand = hmHandByAdjustedPlayerNumber.get(playerNumberAdjusted);
List<String> hand = hmHandByAdjustedPlayerNumber.get(playerNumberAdjusted);

Player player = entry.getValue();
boolean enabled = hand != null;
player.setEnabled(enabled);

if (enabled)
{
player.setNumberOfCards(hand.length);
player.setNumberOfCards(hand.size());
player.resetHand();
}
else if (playerNumberAdjusted == playerNumberLocal)
Expand All @@ -994,10 +995,10 @@ private int getTotalFromHands()

for (int i=0; i<MAX_NUMBER_OF_PLAYERS; i++)
{
String[] hand = hmHandByAdjustedPlayerNumber.get(i);
List<String> hand = hmHandByAdjustedPlayerNumber.get(i);
if (hand != null)
{
total += hand.length;
total += hand.size();
}
}

Expand Down Expand Up @@ -1112,28 +1113,28 @@ private void saveHands(int roundNumber)
int opponentTwoNumberOfCards = 0;
int opponentThreeNumberOfCards = 0;

String[] playerHand = hmHandByAdjustedPlayerNumber.get(ADJUSTED_PLAYER_NUMBER_ME);
List<String> playerHand = hmHandByAdjustedPlayerNumber.get(ADJUSTED_PLAYER_NUMBER_ME);
if (playerHand != null)
{
playerNumberOfCards = playerHand.length;
playerNumberOfCards = playerHand.size();
}

String[] opponentOneHand = hmHandByAdjustedPlayerNumber.get(1);
List<String> opponentOneHand = hmHandByAdjustedPlayerNumber.get(1);
if (opponentOneHand != null)
{
opponentOneNumberOfCards = opponentOneHand.length;
opponentOneNumberOfCards = opponentOneHand.size();
}

String[] opponentTwoHand = hmHandByAdjustedPlayerNumber.get(2);
List<String> opponentTwoHand = hmHandByAdjustedPlayerNumber.get(2);
if (opponentTwoHand != null)
{
opponentTwoNumberOfCards = opponentTwoHand.length;
opponentTwoNumberOfCards = opponentTwoHand.size();
}

String[] opponentThreeHand = hmHandByAdjustedPlayerNumber.get(3);
List<String> opponentThreeHand = hmHandByAdjustedPlayerNumber.get(3);
if (opponentThreeHand != null)
{
opponentThreeNumberOfCards = opponentThreeHand.length;
opponentThreeNumberOfCards = opponentThreeHand.size();
}

//save the player hands
Expand All @@ -1144,19 +1145,19 @@ private void saveHands(int roundNumber)

for (int i=0; i<playerNumberOfCards; i++)
{
replay.put(roundNumber + REPLAY_STRING_PLAYER_HAND + i, playerHand[i]);
replay.put(roundNumber + REPLAY_STRING_PLAYER_HAND + i, playerHand.get(i));
}
for (int i=0; i<opponentOneNumberOfCards; i++)
{
replay.put(roundNumber + REPLAY_STRING_OPPONENT_ONE_HAND + i, opponentOneHand[i]);
replay.put(roundNumber + REPLAY_STRING_OPPONENT_ONE_HAND + i, opponentOneHand.get(i));
}
for (int i=0; i<opponentTwoNumberOfCards; i++)
{
replay.put(roundNumber + REPLAY_STRING_OPPONENT_TWO_HAND + i, opponentTwoHand[i]);
replay.put(roundNumber + REPLAY_STRING_OPPONENT_TWO_HAND + i, opponentTwoHand.get(i));
}
for (int i=0; i<opponentThreeNumberOfCards; i++)
{
replay.put(roundNumber + REPLAY_STRING_OPPONENT_THREE_HAND + i, opponentThreeHand[i]);
replay.put(roundNumber + REPLAY_STRING_OPPONENT_THREE_HAND + i, opponentThreeHand.get(i));
}
}

Expand Down Expand Up @@ -1461,6 +1462,7 @@ public void illegalCalled()
MessageUtil.sendMessage(illegal, 0);
}

public String getRoomName() { return roomName; }
public int getJokerValue()
{
return settings.getJokerValue();
Expand Down
40 changes: 3 additions & 37 deletions client/src/main/java/online/util/ResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ public static void handleDecryptedResponse(String messageStr, String responseStr
Element root = response.getDocumentElement();
String responseName = root.getNodeName();
EntropyLobby lobby = ScreenCache.get(EntropyLobby.class);

if (responseName.equals(RESPONSE_TAG_KICK_OFF))
{
handleKickOff(root);
return;
}

if (responseName.equals(RESPONSE_TAG_ACKNOWLEDGEMENT))
{
Expand Down Expand Up @@ -105,27 +99,6 @@ else if (responseName.equals(RESPONSE_TAG_SOCKET_TIME_OUT))
}
}

private static void handleKickOff(Element root)
{
ConnectingDialog dialog = ScreenCache.getConnectingDialog();
if (dialog.isVisible())
{
dialog.dismissDialog();
DialogUtil.showErrorLater("Unable to connect to the Entropy server.");
return;
}

EntropyLobby lobby = ScreenCache.get(EntropyLobby.class);
if (lobby.isVisible())
{
String removalReason = root.getAttribute("RemovalReason");
String message = removalReason + "\nEntropyOnline will now exit.";
DialogUtil.showErrorLater(message);

lobby.exit(true);
}
}

private static void handleChatResponse(Element root, EntropyLobby lobby)
{
String name = root.getAttribute("RoomName");
Expand Down Expand Up @@ -386,12 +359,12 @@ private static void setHandsFromXml(GameRoom gameRoom, Element root)
Element child = (Element)children.item(i);
int playerNumber = XmlUtil.getAttributeInt(child, "PlayerNumber");

String[] hand = getHandFromElement(child);
List<String> hand = getHandFromElement(child);
gameRoom.setHand(playerNumber, hand);
}
}

public static String[] getHandFromElement(Element handElement)
public static List<String> getHandFromElement(Element handElement)
{
List<String> cards = new ArrayList<>();

Expand All @@ -404,14 +377,7 @@ public static String[] getHandFromElement(Element handElement)
}
}

int size = cards.size();
String[] hand = new String[size];
for (int j=0; j<size; j++)
{
hand[j] = cards.get(j);
}

return hand;
return cards;
}

private static void handleLeaderboardResponse(Element root)
Expand Down
9 changes: 5 additions & 4 deletions client/src/main/java/screen/EntropyScreen.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package screen;

import java.awt.BorderLayout;
import java.util.List;

import game.GameMode;
import object.EntropyAchievementsTracker;
Expand Down Expand Up @@ -119,10 +120,10 @@ public String processCommand(String command)
else if (command.equals("perfectbid"))
{
cheatUsed = true;
String[] playerHand = player.getHand();
String[] opponentOneHand = opponentOne.getHand();
String[] opponentTwoHand = opponentTwo.getHand();
String[] opponentThreeHand = opponentThree.getHand();
List<String> playerHand = player.getHand();
List<String> opponentOneHand = opponentOne.getHand();
List<String> opponentTwoHand = opponentTwo.getHand();
List<String> opponentThreeHand = opponentThree.getHand();
int perfectBidSuitCode = EntropyUtil.getPerfectBidSuitCode(playerHand, opponentOneHand, opponentTwoHand, opponentThreeHand, jokerValue, includeStars);
int perfectBidAmount = EntropyUtil.getPerfectBidAmount(playerHand, opponentOneHand, opponentTwoHand, opponentThreeHand, jokerValue);
String suit = CardsUtil.getSuitDesc(perfectBidAmount, perfectBidSuitCode);
Expand Down
71 changes: 24 additions & 47 deletions client/src/main/java/screen/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,10 @@ public void cancelNewRound()

protected String getMaxBidsStr()
{
String[] playerHand = player.getHand();
String[] opponentOneHand = opponentOne.getHand();
String[] opponentTwoHand = opponentTwo.getHand();
String[] opponentThreeHand = opponentThree.getHand();
List<String> playerHand = player.getHand();
List<String> opponentOneHand = opponentOne.getHand();
List<String> opponentTwoHand = opponentTwo.getHand();
List<String> opponentThreeHand = opponentThree.getHand();

int maxClubs = CardsUtil.countSuit(CardsUtil.SUIT_CLUBS, playerHand, opponentOneHand, opponentTwoHand, opponentThreeHand, jokerValue);
int maxDiamonds = CardsUtil.countSuit(CardsUtil.SUIT_DIAMONDS, playerHand, opponentOneHand, opponentTwoHand, opponentThreeHand, jokerValue);
Expand Down Expand Up @@ -807,7 +807,7 @@ protected void randomlyReplaceCardsWithJokers()
int jokersToAdd = random.nextInt(5) + 1; //1-5
logger.info("rainingJokers", "Adding " + jokersToAdd + " jokers");

String[] allCards = getConcatenatedHands();
List<String> allCards = getConcatenatedHands();

while (CardsUtil.containsNonJoker(allCards) && jokersToAdd > 0)
{
Expand All @@ -824,25 +824,25 @@ private void replaceRandomCardWithJoker()
{
Random rand = new Random();
String jokerToAdd = "Jo" + rand.nextInt(4); //0, 1, 2, 3
String[] hand = pickHandWithNonJokerAtRandom();
List<String> hand = pickHandWithNonJokerAtRandom();

int length = hand.length;
int length = hand.size();
int position = (rand.nextInt(length));
String cardToReplace = hand[position];
String cardToReplace = hand.get(position);
if (cardToReplace.contains("Jo"))
{
replaceRandomCardWithJoker();
return;
}

hand[position] = jokerToAdd;
hand.set(position, jokerToAdd);
}

private String[] pickHandWithNonJokerAtRandom()
private List<String> pickHandWithNonJokerAtRandom()
{
String[][] allHands = {player.getHand(), opponentOne.getHand(), opponentTwo.getHand(), opponentThree.getHand()};
List<String>[] allHands = new List[]{player.getHand(), opponentOne.getHand(), opponentTwo.getHand(), opponentThree.getHand()};
Random rand = new Random();
String[] hand = allHands[rand.nextInt(4)];
List<String> hand = allHands[rand.nextInt(4)];

if (!CardsUtil.containsNonJoker(hand))
{
Expand All @@ -854,42 +854,19 @@ private String[] pickHandWithNonJokerAtRandom()
}
}

public String[] getConcatenatedHands()
public List<String> getConcatenatedHands()
{
String[] playerHand = player.getHand();
String[] opponentOneHand = opponentOne.getHand();
String[] opponentTwoHand = opponentTwo.getHand();
String[] opponentThreeHand = opponentThree.getHand();

int length = playerHand.length + opponentOneHand.length + opponentTwoHand.length + opponentThreeHand.length;
String[] allCards = new String[length];

int fromIndex = 0;
for (int i=0; i<playerHand.length; i++)
{
allCards[fromIndex] = playerHand[i];
fromIndex++;
}

for (int i=0; i<opponentOneHand.length; i++)
{
allCards[fromIndex] = opponentOneHand[i];
fromIndex++;
}

for (int i=0; i<opponentTwoHand.length; i++)
{
allCards[fromIndex] = opponentTwoHand[i];
fromIndex++;
}

for (int i=0; i<opponentThreeHand.length; i++)
{
allCards[fromIndex] = opponentThreeHand[i];
fromIndex++;
}

return allCards;
List<String> playerHand = player.getHand();
List<String> opponentOneHand = opponentOne.getHand();
List<String> opponentTwoHand = opponentTwo.getHand();
List<String> opponentThreeHand = opponentThree.getHand();

var result = new ArrayList<String>();
result.addAll(playerHand);
result.addAll(opponentOneHand);
result.addAll(opponentTwoHand);
result.addAll(opponentThreeHand);
return result;
}

private void processPlayerBid()
Expand Down
Loading