print village stats
parent
24708a2fda
commit
c106cca6b1
|
@ -19,13 +19,13 @@ public class GameEngine {
|
||||||
public static final double IRON_FACTOR = 1;
|
public static final double IRON_FACTOR = 1;
|
||||||
public static final double WOOD_FACTOR = 0.1;
|
public static final double WOOD_FACTOR = 0.1;
|
||||||
|
|
||||||
private float pillageFactor = 0.5f;
|
private final float pillageFactor = 0.5f;
|
||||||
|
|
||||||
private int currentTime;
|
private int currentTime;
|
||||||
|
|
||||||
private final Random random = new Random(System.nanoTime());
|
private final Random random = new Random(System.nanoTime());
|
||||||
|
|
||||||
public GameDisplay view;
|
public GameDisplay view = new GameDisplay();
|
||||||
|
|
||||||
public GameEngine() {
|
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();
|
BuildingFactory bfactory = new BuildingFactory();
|
||||||
Building type = bfactory.getBuilding(buildingArg);
|
Building type = bfactory.getBuilding(buildingArg);
|
||||||
if (type == null)
|
if (type == null)
|
||||||
return false;
|
throw new BuildingErrorException("Invalid building type!");
|
||||||
return map.build(new Tile(), 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,11 @@ import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class GameDisplay {
|
public class GameDisplay {
|
||||||
private BufferedReader reader;
|
private final BufferedReader reader;
|
||||||
private String input;
|
private String input;
|
||||||
|
|
||||||
public GameDisplay() {
|
public GameDisplay() {
|
||||||
|
@ -30,7 +31,7 @@ public class GameDisplay {
|
||||||
System.out.println("\t->" + input + '\n');
|
System.out.println("\t->" + input + '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printVillageState(Map map, String displayName) {
|
public ArrayList<String> getVillageStateTable(Map map, String displayName){
|
||||||
Print resourcesPrinter = new Print(displayName, 2);
|
Print resourcesPrinter = new Print(displayName, 2);
|
||||||
|
|
||||||
resourcesPrinter.addColumn(new Print.Column("Resource Type"));
|
resourcesPrinter.addColumn(new Print.Column("Resource Type"));
|
||||||
|
@ -73,7 +74,11 @@ public class GameDisplay {
|
||||||
for (Inhabitant i : map.inhabitants)
|
for (Inhabitant i : map.inhabitants)
|
||||||
inhabs.addRow(new Print.Row(i.getClass().getSimpleName(), Integer.toString(i.getLevel() + 1)));
|
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() {
|
public void printGameMenu() {
|
||||||
|
|
|
@ -101,7 +101,7 @@ public class Client implements Runnable {
|
||||||
long clientID = stream.readLong();
|
long clientID = stream.readLong();
|
||||||
long messageID = 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) {
|
switch (packetID) {
|
||||||
case PacketTable.ACK:
|
case PacketTable.ACK:
|
||||||
|
|
|
@ -10,6 +10,8 @@ import java.nio.charset.StandardCharsets;
|
||||||
import java.rmi.ServerException;
|
import java.rmi.ServerException;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
@ -23,7 +25,7 @@ public class Server implements Runnable {
|
||||||
private long clientAssignmentID = 0;
|
private long clientAssignmentID = 0;
|
||||||
private final DatagramSocket socket;
|
private final DatagramSocket socket;
|
||||||
private final Thread ioThread;
|
private final Thread ioThread;
|
||||||
private long lastSentMessageID = 0;
|
// private static volatile long lastSentMessageID = 0;
|
||||||
|
|
||||||
private GameEngine mainEngine;
|
private GameEngine mainEngine;
|
||||||
|
|
||||||
|
@ -89,7 +91,7 @@ public class Server implements Runnable {
|
||||||
private static class ConnectedClient implements Runnable {
|
private static class ConnectedClient implements Runnable {
|
||||||
private final InetAddress address;
|
private final InetAddress address;
|
||||||
private final int port;
|
private final int port;
|
||||||
private final Queue<Message.Received> pendingRequests = new PriorityQueue<>();
|
private final Queue<Message.Received> pendingRequests = new LinkedBlockingQueue<>();
|
||||||
private final ReentrantLock requestLock = new ReentrantLock();
|
private final ReentrantLock requestLock = new ReentrantLock();
|
||||||
private final AtomicBoolean allowUpdate;
|
private final AtomicBoolean allowUpdate;
|
||||||
private final java.util.Map<Long, Message.Sent> sentMessages = Collections.synchronizedMap(new HashMap<>());
|
private final java.util.Map<Long, Message.Sent> sentMessages = Collections.synchronizedMap(new HashMap<>());
|
||||||
|
@ -99,6 +101,7 @@ public class Server implements Runnable {
|
||||||
private final Thread processingThread;
|
private final Thread processingThread;
|
||||||
private final Thread gameEngineThread;
|
private final Thread gameEngineThread;
|
||||||
private final GameEngine usingEngine;
|
private final GameEngine usingEngine;
|
||||||
|
private long lastSentMessageID = 0;
|
||||||
private final Map clientMap;
|
private final Map clientMap;
|
||||||
|
|
||||||
public ConnectedClient(DatagramSocket serverSocket, GameEngine engine, long clientID, long messageID, InetAddress address, int port){
|
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());
|
System.out.println(request.getReader().readUTF());
|
||||||
break;
|
break;
|
||||||
case PacketTable.BUILD:
|
case PacketTable.BUILD:
|
||||||
if (usingEngine.build(clientMap, request.getReader().readUTF())){
|
try {
|
||||||
System.out.println("Client " + clientID + " has built something!");
|
String type = request.getReader().readUTF().trim();
|
||||||
} else {
|
if (usingEngine.build(clientMap, type))
|
||||||
System.out.println("Client " + clientID + " failed to build!");
|
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;
|
break;
|
||||||
case PacketTable.TRAIN:
|
case PacketTable.TRAIN:
|
||||||
|
@ -157,7 +164,11 @@ public class Server implements Runnable {
|
||||||
case PacketTable.UPGRADE:
|
case PacketTable.UPGRADE:
|
||||||
usingEngine.upgradeBuilding(clientMap, Integer.parseInt(request.getReader().readUTF()));
|
usingEngine.upgradeBuilding(clientMap, Integer.parseInt(request.getReader().readUTF()));
|
||||||
break;
|
break;
|
||||||
|
case PacketTable.PRINT_MAP_DATA:
|
||||||
|
usingEngine.view.getVillageStateTable(clientMap, "Home Village").forEach(this::sendMessageLn);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
if (request.getPacketID() != PacketTable.ACK)
|
||||||
sendMessage(new Message.Sent(PacketTable.ACK, clientID, request.getMessageID()));
|
sendMessage(new Message.Sent(PacketTable.ACK, clientID, request.getMessageID()));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(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){
|
public void sendMessage(Message.Sent message){
|
||||||
if (message.getPacketID() != PacketTable.ACK)
|
if (message.getPacketID() != PacketTable.ACK)
|
||||||
this.sentMessages.put(message.getMessageID(), message);
|
this.sentMessages.put(message.getMessageID(), message);
|
||||||
|
|
Loading…
Reference in New Issue