Updated ConnectedClient
parent
0228d6947a
commit
9c878306b4
|
@ -140,9 +140,9 @@ public class GameEngine {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateMap(Map map) {
|
public void updateMap(Map map) {
|
||||||
for (Building b : map.contains){
|
for (int i = 0; i < map.contains.size(); i++) {
|
||||||
if ((b instanceof ResourceBuilding)) {
|
if ((map.contains.get(i) instanceof ResourceBuilding)) {
|
||||||
((ResourceBuilding) b).update(map.getTownHall());
|
((ResourceBuilding) map.contains.get(i)).update(map.getTownHall());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import java.rmi.ServerException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.PriorityQueue;
|
import java.util.PriorityQueue;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
public class Server implements Runnable {
|
public class Server implements Runnable {
|
||||||
|
@ -84,11 +85,13 @@ public class Server implements Runnable {
|
||||||
private final int port;
|
private final int port;
|
||||||
private final Queue<Message.Received> pendingRequests = new PriorityQueue<>();
|
private final Queue<Message.Received> pendingRequests = new PriorityQueue<>();
|
||||||
private final ReentrantLock requestLock = new ReentrantLock();
|
private final ReentrantLock requestLock = new ReentrantLock();
|
||||||
|
private final AtomicBoolean allowUpdate;
|
||||||
private final HashMap<Long, Message.Sent> sentMessages = new HashMap<>();
|
private final HashMap<Long, Message.Sent> sentMessages = new HashMap<>();
|
||||||
private final DatagramSocket serverSocket;
|
private final DatagramSocket serverSocket;
|
||||||
private final long clientID;
|
private final long clientID;
|
||||||
private volatile boolean running = true;
|
private volatile boolean running = true;
|
||||||
private final Thread processingThread;
|
private final Thread processingThread;
|
||||||
|
private final Thread gameEngineThread;
|
||||||
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){
|
||||||
|
@ -97,8 +100,17 @@ public class Server implements Runnable {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.clientID = clientID;
|
this.clientID = clientID;
|
||||||
this.clientMap = engine.generateInitialMap();
|
this.clientMap = engine.generateInitialMap();
|
||||||
|
this.allowUpdate = new AtomicBoolean(true);
|
||||||
processingThread = new Thread(this);
|
processingThread = new Thread(this);
|
||||||
processingThread.start();
|
processingThread.start();
|
||||||
|
gameEngineThread = new Thread(() -> {
|
||||||
|
while (true) {
|
||||||
|
if (this.allowUpdate.get()) {
|
||||||
|
engine.updateMap(clientMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
gameEngineThread.start();
|
||||||
|
|
||||||
sendMessage(new Message.Sent(PacketTable.ACK, clientID, messageID));
|
sendMessage(new Message.Sent(PacketTable.ACK, clientID, messageID));
|
||||||
}
|
}
|
||||||
|
@ -135,7 +147,9 @@ public class Server implements Runnable {
|
||||||
requestLock.lock();
|
requestLock.lock();
|
||||||
if (!pendingRequests.isEmpty()) {
|
if (!pendingRequests.isEmpty()) {
|
||||||
Message.Received request = pendingRequests.remove();
|
Message.Received request = pendingRequests.remove();
|
||||||
|
allowUpdate.set(false);
|
||||||
processRequest(request);
|
processRequest(request);
|
||||||
|
allowUpdate.set(true);
|
||||||
}
|
}
|
||||||
requestLock.unlock();
|
requestLock.unlock();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue