From 4f427f964d75921dbb8f4da10ca7da3e54f713cf Mon Sep 17 00:00:00 2001 From: Potato455 <90859863+Potato455@users.noreply.github.com> Date: Fri, 4 Mar 2022 17:46:42 -0500 Subject: [PATCH 1/2] fixed --- client/clientsrc/ClientMain.java | 20 ++---- network/networksrc/MoveBootACK.java | 31 ++++++++++ network/networksrc/MoveBootAction.java | 84 ++++++++++++++++++++++++++ server/serversrc/Boot.java | 3 + server/serversrc/Player.java | 11 ++++ server/serversrc/ServerGame.java | 65 ++++++++++++++++++++ server/serversrc/TownGraph.java | 82 +++++++++++++++++++++++++ 7 files changed, 282 insertions(+), 14 deletions(-) create mode 100644 network/networksrc/MoveBootACK.java create mode 100644 network/networksrc/MoveBootAction.java create mode 100644 server/serversrc/TownGraph.java diff --git a/client/clientsrc/ClientMain.java b/client/clientsrc/ClientMain.java index 65926f5..b1dc917 100644 --- a/client/clientsrc/ClientMain.java +++ b/client/clientsrc/ClientMain.java @@ -36,6 +36,9 @@ public class ClientMain { public static User currentUser; public static LobbyServiceGameSession currentSession; + public static Player currentPlayer; + public static Game currentGame; + static GUI gui; static MinuetoEventQueue entryScreenQueue, loginScreenQueue, moveBootQueue, lobbyScreenQueue, createGameQueue, elfenlandLobbyQueue; @@ -792,15 +795,6 @@ public void handleMousePress(int x, int y, int button) { && (currentUser.getName().equals(currentSession.getCreator()))) { // click on Launch button -> launch the session REGISTRATOR.launchSession(currentSession, currentUser); - // go to board screen - LobbyServiceGame gameService = currentSession.getGameService(); - Game game = gameService.getGame(); - Mode currentMode = game.getMode(); - if (currentMode.equals(Mode.ELFENLAND)) { - gui.currentBackground = GUI.Screen.ELFENLAND; - } else if (currentMode.equals(Mode.ELFENGOLD)) { - gui.currentBackground = GUI.Screen.ELFENGOLD; - } } } else if (x >= 822 & x <= 998 && y <= 655 && y >= 585) { @@ -1274,16 +1268,14 @@ public static void displayUsers() throws MinuetoFileException { } MinuetoImage background = null; - LobbyServiceGame gameService = currentSession.getGameService(); - Game game = gameService.getGame(); - Mode currentMode = game.getMode(); - if (currentMode.equals(Mode.ELFENLAND)) { + + if (modeSel.equals(Mode.ELFENLAND)) { if (currentUser.getName().equals(currentSession.getCreator())) { background = lobbyElfenlandCreatorBackground; } else { background = lobbyElfenlandBackground; } - } else if (currentMode.equals(Mode.ELFENGOLD)) { + } else if (modeSel.equals(Mode.ELFENGOLD)) { if (currentUser.getName().equals(currentSession.getCreator())) { background = lobbyElfengoldCreatorBackground; } else { diff --git a/network/networksrc/MoveBootACK.java b/network/networksrc/MoveBootACK.java new file mode 100644 index 0000000..1ef92d1 --- /dev/null +++ b/network/networksrc/MoveBootACK.java @@ -0,0 +1,31 @@ +package networksrc; + +import clientsrc.*; + + +// updates state of boot on GUI for clients +public class MoveBootACK implements Action{ + + private String newTown; + private String bootColor; + + public MoveBootACK(String newTown, String bootColor){ + this.newTown = newTown; + this.bootColor = bootColor; + } + + @Override + public boolean isValid() { + return true; + } + + @Override + public void execute() { + System.out.println("MoveBootACK received"); + Player p = ClientMain.currentPlayer; + Game g = ClientMain.currentGame; + // TODO: call a method that updates GUI by changing the boot with bootColor to newTown + ClientMain.moveBoot(String bootColor, String toTown); + } + +} diff --git a/network/networksrc/MoveBootAction.java b/network/networksrc/MoveBootAction.java new file mode 100644 index 0000000..421700d --- /dev/null +++ b/network/networksrc/MoveBootAction.java @@ -0,0 +1,84 @@ +package networksrc; + +import java.util.ArrayList; +import serversrc.*; + + +public class MoveBootAction implements Action { + + private String senderName; + private String srcTown; + private String dstTown; + + public MoveBootAction(String senderName, String srcTown, String dstTown) { + this.senderName = senderName; + this.srcTown = srcTown; + this.dstTown = dstTown; + } + + @Override + public boolean isValid() { + // TODO: check if parameters are not null + + Player playerWhoSent = Player.getPlayerByName(senderName); + ServerGame playersCurrentGame = playerWhoSent.getCurrentGame(); + + // sanity check : the player is actually in that game + ArrayList allPlayers = playersCurrentGame.getAllPlayers(); + if (!allPlayers.contains(playerWhoSent)) { + return false; + } + + // add other validation here + // check for moveBoot phase + if (playersCurrentGame.getCurrentPhase() != 5){ + // do nothing ? + System.out.println("ERROR: Not moveBoot phase!"); + return false; + } + // check if it's not player's turn + if (!playerWhoSent.getIsTurn()){ + // do nothing ? + System.out.println("ERROR: Not " + playerWhoSent.getName() + "'s turn!"); + return false; + } + Town sTown = playersCurrentGame.getTownByName(srcTown); + Town dTown = playersCurrentGame.getTownByName(dstTown); + Route route = playersCurrentGame.getTownGraph().getRoute(sTown, dTown); + // check if route is not adjacent to player + if (!(route.getSource() == playerWhoSent.getTown() || route.getDest() == playerWhoSent.getTown())){ + // do nothing ? + System.out.println("ERROR: Invalid route (not adjacent to player's location)!"); + return false; + } + // check if player has the cards required to travel that route + // if (!playerWhoSent.hasCards(route.getRequiredCards())){ + // System.out.println("ERROR: Player doesn't have all cards required!"); + // return false; + // } + + return true; + } + + @Override + public void execute() { + // server has received the message + System.out.println("Executing the MoveBootAction on the server"); + + Player playerWhoSent = Player.getPlayerByName(senderName); + ServerGame playersCurrentGame = playerWhoSent.getCurrentGame(); + Town sTown = playersCurrentGame.getTownByName(srcTown); + Town dTown = playersCurrentGame.getTownByName(dstTown); + Route route = playersCurrentGame.getTownGraph().getRoute(sTown, dTown); + + System.out.println(playerWhoSent + " is in game " + playersCurrentGame.getGameID()); + + // here you can do stuff with playerWhoSent and playersCurrentGame + playersCurrentGame.playerMovedBoot(playerWhoSent, route); + + // send an ACK to all clients in the game + ACKManager ackManager = ACKManager.getInstance(); + MoveBootACK actionToSend = new MoveBootACK(dstTown, playerWhoSent.getBootColor().name()); + ackManager.sentToAllPlayersInGame(actionToSend, playersCurrentGame); + } +} diff --git a/server/serversrc/Boot.java b/server/serversrc/Boot.java index ccac08a..bc29ef9 100644 --- a/server/serversrc/Boot.java +++ b/server/serversrc/Boot.java @@ -6,5 +6,8 @@ public class Boot { Color color; + public Color getColor(){ + return this.color; + } } diff --git a/server/serversrc/Player.java b/server/serversrc/Player.java index ce0856d..58ced24 100644 --- a/server/serversrc/Player.java +++ b/server/serversrc/Player.java @@ -73,6 +73,17 @@ public Action getBootAction() { public String getName() { return aServerUser.getName(); } + public Town getTown(){ + return this.inTown; + } + + public boolean getIsTurn(){ + return this.isTurn; + } + + public Color getBootColor(){ + return this.aBoot.getColor(); + } // draw counter from face down pile /* diff --git a/server/serversrc/ServerGame.java b/server/serversrc/ServerGame.java index d0fdd77..65ca578 100644 --- a/server/serversrc/ServerGame.java +++ b/server/serversrc/ServerGame.java @@ -36,6 +36,7 @@ public class ServerGame { public ArrayList faceUpTokenPile; public TokenStack faceDownTokenPile; private String gameID; + public TownGraph aTownGraph; /** @@ -168,6 +169,10 @@ public ServerGame(int numberOfPlayers, int gameRoundsLimit, boolean destinationT routes.add(kihromahDagamura); routes.add(grangorMahdavikia); + // initialize town graph + this.aTownGraph = new TownGraph(); + this.aTownGraph.addEdges(routes); + // add all counters ingame to faceDownTokenPile // first make list with all tokens: // depending on mode, tokens are different @@ -189,6 +194,15 @@ else if (this.mode == Mode.ELFENGOLD){ this.faceDownTokenPile = new TokenStack(allTokens); } + public Town getTownByName(String townName){ + for(Town t:towns){ + if (t.getTownName().equalsIgnoreCase(townName)){ + return t; + } + } + return null; + } + /** * Adds a player to the players arraylist. If the max number of players has already been reached, throw an error * @param player player to add to the game @@ -232,6 +246,57 @@ public String getGameID() { return gameID; } + public void nextPhase(){ + if (currentPhase == 6){ + + } + else{ + this.currentPhase++; + } + } + + /* + * Notes for moving boot: + * - It's currently the move boot phase of the game (phase 5) + * - It's currently player's turn to move boot (Player.getIsTurn()) + * - Player (from client) sends coordinate where they clicked + * - server receives coordinate (or receive route clicked ?) + * - server makes sure it's a valid coordinates + * - server checks wether or not that town is adjacent to player's town (is there a route) (send message to client if not valid) + * + * ^^^^^^^^^^^^^^^^^^^^^^^^^ (this part might not be necessary for m7) + * - server checks wether the player has cards required to move to that town + * - if it doesn't then return message to client + * - if it does then do the move and take away player's cards + * - send meesage to client to move boot (update state on all player's screens) + * + * ^^^^^^^^^^^^^^^^^^^^^^^^^ + * - Player can move as many time as he wishes, (until no more moves available click on end turn) + * - goes to next player's turn + * - if everyone passed turn (keep track of this somehow) go to next phase of the game (not necessary for m7?) + */ + + // @pre: current phase == 5 and it's player's turn and town is adjacent + public void playerMovedBoot(Player p, Route r){ + + // check if the route is valid (i.e. it's adjacent to player's town) + // if it's valid, move boot + if (r.getSource() == p.getTown() || r.getDest() == p.getTown()){ + // remove the cards from the player + // update player's town location + // update the town's player list + } + } + + public int getCurrentPhase(){ + return this.currentPhase; + } + + public TownGraph getTownGraph(){ + return this.aTownGraph; + } + + /* Operation: Game::loadGame(savedGame: Game) Scope: Player; diff --git a/server/serversrc/TownGraph.java b/server/serversrc/TownGraph.java new file mode 100644 index 0000000..12a5a26 --- /dev/null +++ b/server/serversrc/TownGraph.java @@ -0,0 +1,82 @@ +package serversrc; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +// import serversrc.Route; +// import serversrc.Town; + +public class TownGraph { + // use HashMap to store edges in graph + private Map > map; + + // HashMap to store relations between Towns + private Map > townMap; + + public TownGraph(){ + this.map = new HashMap<>(); + this.townMap = new HashMap<>(); + } + + // adds vertex to graph + public void addVertex(Town t){ + map.put(t, new LinkedList<>()); + } + + // adds edge (route) between source and destination + public void addEdge(Route pRoute){ + Town source = pRoute.getSource(); + Town dest = pRoute.getDest(); + // add towns if not included yet in map + if (!map.containsKey(source)){ + addVertex(source); + } + if (!map.containsKey(dest)){ + addVertex(dest); + } + // add towns if not included in townMap + if (!townMap.containsKey(source)){ + townMap.put(source, new LinkedList<>()); + } + if (!townMap.containsKey(dest)){ + townMap.put(dest, new LinkedList<>()); + } + // add Route to list of routes for the towns given + map.get(source).add(pRoute); + map.get(dest).add(pRoute); + // add Towns to town relation map + townMap.get(source).add(dest); + townMap.get(dest).add(source); + } + + // to add list of edges + public void addEdges(List li){ + for (Route r: li){ + addEdge(r); + } + } + + // function to check wether edge exist or not between 2 towns + public boolean hasEdge(Town s, Town d){ + return townMap.get(s).contains(d); + } + + // function to get the route between 2 towns + // @pre town s and d needs to have an existing edge + public Route getRoute(Town s, Town d){ + assert hasEdge(s, d); + List routes = map.get(s); + // loop through routes to find the right one + for (Route r: routes){ + Town source = r.getSource(); + Town dest = r.getDest(); + // return if d matches one of the towns in route + if (d.equal(source)||d.equal(dest)){ + return r; + } + } + return null; + } +} From 6fe194c37726e4dad4ba1dd53532c7cbe78c8a5e Mon Sep 17 00:00:00 2001 From: dex <83675950+special-place@users.noreply.github.com> Date: Wed, 9 Mar 2022 12:55:40 -0500 Subject: [PATCH 2/2] added an untested phaseOne who knows if it will work ;) --- client/clientsrc/Card.java | 8 +++++++- client/clientsrc/ClientMain.java | 10 ++++++++++ client/clientsrc/Game.java | 11 ++++++++++- client/clientsrc/Player.java | 6 ++++++ network/networksrc/LaunchGameACK.java | 15 +++++++++++++++ server/serversrc/Card.java | 7 +++++++ server/serversrc/Player.java | 13 +++++++++++++ server/serversrc/ServerGame.java | 16 ++++++++++++++++ 8 files changed, 84 insertions(+), 2 deletions(-) diff --git a/client/clientsrc/Card.java b/client/clientsrc/Card.java index 5c1ff29..58ea7db 100644 --- a/client/clientsrc/Card.java +++ b/client/clientsrc/Card.java @@ -24,6 +24,7 @@ public abstract class Card extends Image{ public Card(int minX, int maxX, int minY, int maxY, MinuetoImage image, String name) { super(minX, maxX, minY, maxY, image); // name of card + // TODO: make sure is consistent w CardType enum in serversrc aName = name; } @@ -45,7 +46,12 @@ public boolean equals(Object o){ return c.getName().equalsIgnoreCase(this.aName); } - private String getName(){ + public String getName(){ return this.aName; } + + /** DID NOT NEED THIS. USED Game.getFaceDownCard(cardString) directly from game in Player + public Card getCardByName(String cardString){ + return Game.getFaceDownCard(cardString); + } **/ } diff --git a/client/clientsrc/ClientMain.java b/client/clientsrc/ClientMain.java index b1dc917..523005e 100644 --- a/client/clientsrc/ClientMain.java +++ b/client/clientsrc/ClientMain.java @@ -1401,4 +1401,14 @@ public static void displayAvailableGames() { e.printStackTrace(); } } + + public static void receivePhaseOne(String playerID, ArrayList cardArray){ + for (Player p: players){ + if (p.getName().equalsIgnoreCase(playerID)){ + p.addCardStringArray(cardArray); + } + } + + + } } diff --git a/client/clientsrc/Game.java b/client/clientsrc/Game.java index f5b9cf5..3029683 100644 --- a/client/clientsrc/Game.java +++ b/client/clientsrc/Game.java @@ -29,7 +29,7 @@ public class Game { private boolean witchEnabled; private Mode mode; private TownGoldOption townGoldOption; - private ArrayList faceDownCardPile; + private static ArrayList faceDownCardPile; private ArrayList faceUpCardPile; private ArrayList goldCardPile; //private Auction auction; not doing this now @@ -218,4 +218,13 @@ public static boolean notClickingOnATown(int x, int y) { return false; } + public static Card getFaceDownCard(String cardString){ + for (Card aCard : faceDownCardPile){ + if (aCard.getName().equalsIgnoreCase(cardString)){ + return aCard; + } + } + return null; //hopefully this never happens LOL + } + } diff --git a/client/clientsrc/Player.java b/client/clientsrc/Player.java index 2db2804..356c76e 100644 --- a/client/clientsrc/Player.java +++ b/client/clientsrc/Player.java @@ -88,6 +88,12 @@ public void draw() { public Action getBootAction() { return aBootAction; } + + public void addCardStringArray(ArrayList cardArray){ + for (String cardString : cardArray) { + this.cardsInHand.add(Game.getFaceDownCard(cardString)); + } + } /* Operation: Player::startGame(gameSession: Session) Scope: Player; Session; diff --git a/network/networksrc/LaunchGameACK.java b/network/networksrc/LaunchGameACK.java index 106e738..7367866 100644 --- a/network/networksrc/LaunchGameACK.java +++ b/network/networksrc/LaunchGameACK.java @@ -18,6 +18,21 @@ public void execute() { ClientMain.currentSession.launch(); // modify game objects based on the game state received +<<<<<<< Updated upstream +======= + // TODO: iterate thru playerNames, index for loop --> use to iterate thru playerCards, give Players their cards :) + for (int i = 0; i < playerNames.size(); i++){ + + String playerID = playerNames.get(i); + ArrayList aPlayersCards = playerCards.get(i); + + ClientMain.receivePhaseOne(playerID, aPlayersCards); + + + } + + +>>>>>>> Stashed changes // display the new board System.out.println("LaunchGameACK received"); diff --git a/server/serversrc/Card.java b/server/serversrc/Card.java index 96100d7..e52c14a 100644 --- a/server/serversrc/Card.java +++ b/server/serversrc/Card.java @@ -26,7 +26,14 @@ public boolean equals(Object o){ return c.getName().equalsIgnoreCase(this.aName); } +<<<<<<< Updated upstream private String getName(){ return this.aName; +======= + // public or private idk it says to make it public + public String getName(){ + return this.aCardType.name(); +>>>>>>> Stashed changes } + } diff --git a/server/serversrc/Player.java b/server/serversrc/Player.java index 58ced24..c1a492e 100644 --- a/server/serversrc/Player.java +++ b/server/serversrc/Player.java @@ -86,6 +86,19 @@ public Color getBootColor(){ } // draw counter from face down pile +<<<<<<< Updated upstream +======= + public void addCard(AbstractCard c){ + cardsInHand.add(c); + } + + public List getCards(){ + return cardsInHand; + } + + + +>>>>>>> Stashed changes /* Operation: Player::drawFaceUpToken(token: Token, tStack: TokenStack) Scope: Player; Game; Token; diff --git a/server/serversrc/ServerGame.java b/server/serversrc/ServerGame.java index 65ca578..5036c83 100644 --- a/server/serversrc/ServerGame.java +++ b/server/serversrc/ServerGame.java @@ -255,6 +255,22 @@ public void nextPhase(){ } } +<<<<<<< Updated upstream +======= + public void phaseOne(){ + + // shuffle + aCardStack.shuffle(); + + for (Player p: players){ + for (int i; i < 8 ; i++) { + p.addCard(aCardStack.pop()); + } + } + + } + +>>>>>>> Stashed changes /* * Notes for moving boot: * - It's currently the move boot phase of the game (phase 5)