Fix: error sended only to interessed player
This commit is contained in:
@@ -117,8 +117,9 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
|
||||
* @param action The desired action
|
||||
*/
|
||||
public void notifyAll(NetworkEvent action) throws RemoteException {
|
||||
for (IClientCallback cb : clients.values()) {
|
||||
cb.onAction(action);
|
||||
for (Map.Entry<String,IClientCallback> entry : clients.entrySet()) {
|
||||
if(!action.getIsError() ||(action.getIsError()&& action.getUsername().equals(entry.getKey())))
|
||||
entry.getValue().onAction(action);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,13 @@ import java.util.concurrent.BlockingQueue;
|
||||
* It is also responsible to send events and game model updates back to the client.
|
||||
*/
|
||||
public class ClientHandler implements Runnable {
|
||||
|
||||
/**
|
||||
* The username of the connected client on this handler
|
||||
*/
|
||||
private final String username;
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
/** The TCP socket */
|
||||
private Socket clientSocket;
|
||||
|
||||
@@ -41,7 +47,8 @@ public class ClientHandler implements Runnable {
|
||||
* @param clientHandlers The shared list of all active client handlers
|
||||
* @param actionQueue The queue containing incoming events
|
||||
*/
|
||||
public ClientHandler(Socket clientSocket, ObjectOutputStream out, ObjectInputStream in, List<ClientHandler> clientHandlers, BlockingQueue<NetworkEvent> actionQueue) {
|
||||
public ClientHandler(String username, Socket clientSocket, ObjectOutputStream out, ObjectInputStream in, List<ClientHandler> clientHandlers, BlockingQueue<NetworkEvent> actionQueue) {
|
||||
this.username=username;
|
||||
this.clientSocket = clientSocket;
|
||||
this.in = in;
|
||||
this.out = out;
|
||||
|
||||
@@ -115,7 +115,7 @@ public class TCPServer {
|
||||
clientSocket.getOutputStream().write((int) (1));
|
||||
|
||||
System.out.println("Accepted player: " + eventAddPlayer.getUsername());
|
||||
ClientHandler clientHandler = new ClientHandler(clientSocket, clientSend, clientReceive, clientHandlers, actionQueue);
|
||||
ClientHandler clientHandler = new ClientHandler(eventAddPlayer.getUsername(),clientSocket, clientSend, clientReceive, clientHandlers, actionQueue);
|
||||
clientHandlers.add(clientHandler);
|
||||
ConnectedPlayers++;
|
||||
|
||||
@@ -141,7 +141,10 @@ public class TCPServer {
|
||||
|
||||
/** Sends an action to all TCP clients */
|
||||
public void notifyAll(NetworkEvent event){
|
||||
clientHandlers.forEach((x) -> x.notifyEvent(event));
|
||||
clientHandlers.forEach((x) -> {
|
||||
if(!event.getIsError()||(event.getIsError()&& event.getUsername().equals(x.getUsername())))
|
||||
x.notifyEvent(event);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user