From c106cca6b11d6f8cf717ddc7c0daac16954c27c0 Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 20 Apr 2023 15:30:39 -0400 Subject: [PATCH] print village stats --- .../src/ca/cosc3p91/a4/game/GameEngine.java | 14 +++++-- .../a4/userinterface/GameDisplay.java | 11 +++-- .../ca/cosc3p91/a4/util/network/Client.java | 2 +- .../ca/cosc3p91/a4/util/network/Server.java | 41 +++++++++++++++---- 4 files changed, 53 insertions(+), 15 deletions(-) diff --git a/Assignment 4/src/ca/cosc3p91/a4/game/GameEngine.java b/Assignment 4/src/ca/cosc3p91/a4/game/GameEngine.java index ae9ea89..45f958a 100644 --- a/Assignment 4/src/ca/cosc3p91/a4/game/GameEngine.java +++ b/Assignment 4/src/ca/cosc3p91/a4/game/GameEngine.java @@ -19,13 +19,13 @@ public class GameEngine { public static final double IRON_FACTOR = 1; public static final double WOOD_FACTOR = 0.1; - private float pillageFactor = 0.5f; + private final float pillageFactor = 0.5f; private int currentTime; private final Random random = new Random(System.nanoTime()); - public GameDisplay view; + public GameDisplay view = new GameDisplay(); public GameEngine() { } @@ -147,11 +147,11 @@ public class GameEngine { } } - public synchronized boolean build (Map map, String buildingArg) { + public synchronized boolean build (Map map, String buildingArg) throws BuildingErrorException { BuildingFactory bfactory = new BuildingFactory(); Building type = bfactory.getBuilding(buildingArg); if (type == null) - return false; + throw new BuildingErrorException("Invalid building type!"); return map.build(new Tile(), type); } @@ -281,4 +281,10 @@ public class GameEngine { } } + public static class BuildingErrorException extends Exception { + public BuildingErrorException(String message){ + super(message); + } + } + } diff --git a/Assignment 4/src/ca/cosc3p91/a4/userinterface/GameDisplay.java b/Assignment 4/src/ca/cosc3p91/a4/userinterface/GameDisplay.java index 7120eee..116d5f3 100644 --- a/Assignment 4/src/ca/cosc3p91/a4/userinterface/GameDisplay.java +++ b/Assignment 4/src/ca/cosc3p91/a4/userinterface/GameDisplay.java @@ -9,10 +9,11 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.ArrayList; import java.util.Scanner; public class GameDisplay { - private BufferedReader reader; + private final BufferedReader reader; private String input; public GameDisplay() { @@ -30,7 +31,7 @@ public class GameDisplay { System.out.println("\t->" + input + '\n'); } - public void printVillageState(Map map, String displayName) { + public ArrayList getVillageStateTable(Map map, String displayName){ Print resourcesPrinter = new Print(displayName, 2); resourcesPrinter.addColumn(new Print.Column("Resource Type")); @@ -73,7 +74,11 @@ public class GameDisplay { for (Inhabitant i : map.inhabitants) inhabs.addRow(new Print.Row(i.getClass().getSimpleName(), Integer.toString(i.getLevel() + 1))); - Print.print(inhabs.createTable(true, true, true)); + return inhabs.createTable(true, true, true); + } + + public void printVillageState(Map map, String displayName) { + Print.print(getVillageStateTable(map, displayName)); } public void printGameMenu() { diff --git a/Assignment 4/src/ca/cosc3p91/a4/util/network/Client.java b/Assignment 4/src/ca/cosc3p91/a4/util/network/Client.java index 5b63458..7292945 100644 --- a/Assignment 4/src/ca/cosc3p91/a4/util/network/Client.java +++ b/Assignment 4/src/ca/cosc3p91/a4/util/network/Client.java @@ -101,7 +101,7 @@ public class Client implements Runnable { long clientID = stream.readLong(); long messageID = stream.readLong(); - System.out.println("Receiving message with ID " + messageID + " to client: " + clientID + " of type " + packetID); + System.out.println("Receiving message with ID " + messageID + " from server of type " + packetID + " our ClientID " + clientID + " / " + ourClientID); switch (packetID) { case PacketTable.ACK: diff --git a/Assignment 4/src/ca/cosc3p91/a4/util/network/Server.java b/Assignment 4/src/ca/cosc3p91/a4/util/network/Server.java index ed18178..9ee675f 100644 --- a/Assignment 4/src/ca/cosc3p91/a4/util/network/Server.java +++ b/Assignment 4/src/ca/cosc3p91/a4/util/network/Server.java @@ -10,6 +10,8 @@ import java.nio.charset.StandardCharsets; import java.rmi.ServerException; import java.util.*; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.ReentrantLock; @@ -23,7 +25,7 @@ public class Server implements Runnable { private long clientAssignmentID = 0; private final DatagramSocket socket; private final Thread ioThread; - private long lastSentMessageID = 0; +// private static volatile long lastSentMessageID = 0; private GameEngine mainEngine; @@ -89,7 +91,7 @@ public class Server implements Runnable { private static class ConnectedClient implements Runnable { private final InetAddress address; private final int port; - private final Queue pendingRequests = new PriorityQueue<>(); + private final Queue pendingRequests = new LinkedBlockingQueue<>(); private final ReentrantLock requestLock = new ReentrantLock(); private final AtomicBoolean allowUpdate; private final java.util.Map sentMessages = Collections.synchronizedMap(new HashMap<>()); @@ -99,6 +101,7 @@ public class Server implements Runnable { private final Thread processingThread; private final Thread gameEngineThread; private final GameEngine usingEngine; + private long lastSentMessageID = 0; private final Map clientMap; public ConnectedClient(DatagramSocket serverSocket, GameEngine engine, long clientID, long messageID, InetAddress address, int port){ @@ -145,10 +148,14 @@ public class Server implements Runnable { System.out.println(request.getReader().readUTF()); break; case PacketTable.BUILD: - if (usingEngine.build(clientMap, request.getReader().readUTF())){ - System.out.println("Client " + clientID + " has built something!"); - } else { - System.out.println("Client " + clientID + " failed to build!"); + try { + String type = request.getReader().readUTF().trim(); + if (usingEngine.build(clientMap, type)) + sendAndLogLn("Client " + clientID + " has successfully built " + type + "!"); + else + sendAndLogLn("Client " + clientID + " has insufficient funds to build " + type + "!"); + } catch (GameEngine.BuildingErrorException e){ + sendAndLogLn(e.getMessage()); } break; case PacketTable.TRAIN: @@ -157,8 +164,12 @@ public class Server implements Runnable { case PacketTable.UPGRADE: usingEngine.upgradeBuilding(clientMap, Integer.parseInt(request.getReader().readUTF())); break; + case PacketTable.PRINT_MAP_DATA: + usingEngine.view.getVillageStateTable(clientMap, "Home Village").forEach(this::sendMessageLn); + break; } - sendMessage(new Message.Sent(PacketTable.ACK, clientID, request.getMessageID())); + if (request.getPacketID() != PacketTable.ACK) + sendMessage(new Message.Sent(PacketTable.ACK, clientID, request.getMessageID())); } catch (Exception e) { throw new RuntimeException(e); } @@ -190,6 +201,22 @@ public class Server implements Runnable { } } + private void sendMessageLn(String str) { + Message.Sent mess = new Message.Sent(PacketTable.MESSAGE, clientID, ++lastSentMessageID); + try { + mess.getWriter().writeUTF(str + "\n"); + sendMessage(mess); + } catch (IOException e){ + e.printStackTrace(); + } + } + + private void sendAndLogLn(String str){ + sendMessageLn(str); + System.out.println(str); + } + + public void sendMessage(Message.Sent message){ if (message.getPacketID() != PacketTable.ACK) this.sentMessages.put(message.getMessageID(), message);