From 1dc09eb45e4702a44a41d7770cffafaf1bbe4209 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 4 May 2020 00:25:18 +0200 Subject: [PATCH] Laster listen av brett fra en fil og forbedrer visning av brettlisten --- .../roborally/ui/screens/LobbyScreen.java | 11 +++-- .../roborally/utility/BoardLoaderUtil.java | 42 +++++++++++++++++++ src/main/resources/boards.txt | 13 ++++++ 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/boards.txt diff --git a/src/main/java/inf112/fiasko/roborally/ui/screens/LobbyScreen.java b/src/main/java/inf112/fiasko/roborally/ui/screens/LobbyScreen.java index 3880513..f5cb4e3 100644 --- a/src/main/java/inf112/fiasko/roborally/ui/screens/LobbyScreen.java +++ b/src/main/java/inf112/fiasko/roborally/ui/screens/LobbyScreen.java @@ -15,8 +15,10 @@ import inf112.fiasko.roborally.networking.containers.GameStartInfoResponse; import inf112.fiasko.roborally.objects.Player; import inf112.fiasko.roborally.ui.RoboRallyWrapper; import inf112.fiasko.roborally.ui.SimpleButton; +import inf112.fiasko.roborally.utility.BoardLoaderUtil; import inf112.fiasko.roborally.utility.IOUtil; +import java.io.IOException; import java.util.List; import java.util.Map; @@ -45,8 +47,11 @@ public class LobbyScreen extends InteractiveScreen { Skin skin = new Skin(Gdx.files.internal("uiskin.json")); selectBox = new SelectBox<>(skin); - selectBox.setItems("Dizzy_Dash", "Checkmate", "Risky_Exchange", "Twister", "Bloodbath_Chess", "Vault_Assault", - "Island_Hop", "Chop_Shop_Challenge", "Around_The_World", "Death_Trap", "Whirlwind_Tour"); + try { + selectBox.setItems(BoardLoaderUtil.getBoardListHumanReadable()); + } catch (IOException e) { + e.printStackTrace(); + } selectBox.setSize(200, 50); selectBox.setPosition((applicationWidth - selectBox.getWidth()) / 2f, applicationHeight / 2f - 120); @@ -77,7 +82,7 @@ public class LobbyScreen extends InteractiveScreen { roboRallyWrapper.server.getRobotID()); for (Connection connection : playerNames.keySet()) { roboRallyWrapper.server.sendToClient(connection, new GameStartInfoResponse( - selectBox.getSelected() + ".txt", playerList, playerNames.get(connection))); + BoardLoaderUtil.getRealBoardName(selectBox.getSelected()), playerList, playerNames.get(connection))); } roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(roboRallyWrapper)); } diff --git a/src/main/java/inf112/fiasko/roborally/utility/BoardLoaderUtil.java b/src/main/java/inf112/fiasko/roborally/utility/BoardLoaderUtil.java index 4a46155..0c51290 100644 --- a/src/main/java/inf112/fiasko/roborally/utility/BoardLoaderUtil.java +++ b/src/main/java/inf112/fiasko/roborally/utility/BoardLoaderUtil.java @@ -27,6 +27,48 @@ public final class BoardLoaderUtil { private BoardLoaderUtil() { } + /** + * Gets the actual file name of a board given its human-readable name + * + * @param boardName The human-readable name of a board + * @return The file name of the board + */ + public static String getRealBoardName(String boardName) { + return boardName.replace(" ", "_") + ".txt"; + } + + /** + * Gets a list of all available boards with human-readable names + * + * @return A list of all available boards + * @throws IOException If the board list cannot be read + */ + public static String[] getBoardListHumanReadable() throws IOException { + String[] boards = getBoardList(); + for (int i = 0; i < boards.length; i++) { + boards[i] = boards[i].replace("_", " ").replace(".txt", ""); + } + return boards; + } + + /** + * Gets a list of all available boards + * + * @return A list of all available boards + * @throws IOException If the board list cannot be read + */ + private static String[] getBoardList() throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader( + ResourceUtil.getResourceAsInputStream("boards.txt"))); + int numberOfBoards = Integer.parseInt(reader.readLine()); + String[] boardList = new String[numberOfBoards]; + for (int i = 0; i < numberOfBoards; i++) { + String board = reader.readLine(); + boardList[i] = board; + } + return boardList; + } + /** * Loads and rotates a board described in a file * diff --git a/src/main/resources/boards.txt b/src/main/resources/boards.txt new file mode 100644 index 0000000..c8d1a6b --- /dev/null +++ b/src/main/resources/boards.txt @@ -0,0 +1,13 @@ +12 +Around_The_World.txt +Bloodbath_Chess.txt +Checkmate.txt +Chop_Shop_Challenge.txt +Death_Trap.txt +Dizzy_Dash.txt +Island_Hop.txt +Lost_Bearings.txt +Risky_Exchange.txt +Twister.txt +Vault_Assault.txt +Whirlwind_Tour.txt \ No newline at end of file