La til noen metoder for å gi spillernavn videre til de klassene som trenger dem.

This commit is contained in:
Steinar Aalstad Lillesund 2020-04-06 16:15:44 +02:00
parent 208cf0332d
commit 3e024d0b89
2 changed files with 28 additions and 2 deletions

View File

@ -8,8 +8,9 @@ import inf112.fiasko.roborally.utility.NetworkUtil;
import java.io.IOException;
public class RoboRallyClient {
Client client;
public RoboRallyClient(String IPaddresse) throws IOException {
Client client = new Client();
client = new Client();
client.start();
NetworkUtil.registerClasses(client.getKryo());
client.connect(5000, IPaddresse, 54555, 54777);
@ -20,6 +21,9 @@ public class RoboRallyClient {
client.addListener(new RoboRallyClientListener());
}
public void sendElement(Object obj) {
client.sendTCP(obj);
}
}
class RoboRallyClientListener extends Listener {
@ -33,4 +37,5 @@ class RoboRallyClientListener extends Listener {
System.out.println(errorResponse.getErrorMessage());
}
}
}
}

View File

@ -30,6 +30,13 @@ public class RoboRallyServer {
programmingCardDeck = DeckLoaderUtil.loadProgrammingCardsDeck();
}
public Map<Connection, RobotID> getRobotID() {
return listener.getRobotID();
}
public Map<Connection, String> getPlayerNames() {
return listener.getPlayerNames();
}
/**
* Sends an object to all clients
* @param object The object to send
@ -54,10 +61,20 @@ public class RoboRallyServer {
class RoboRallyServerListener extends Listener {
protected Connection host;
protected Map<Connection, RobotID> clients;
protected Map<Connection, String> playerNames;
public RoboRallyServerListener() {
super();
clients = new HashMap<>();
playerNames = new HashMap<>();
}
public Map<Connection, String> getPlayerNames() {
return playerNames;
}
public Map<Connection, RobotID> getRobotID() {
return clients;
}
@Override
@ -70,6 +87,10 @@ class RoboRallyServerListener extends Listener {
response.text = "Thanks";
connection.sendTCP(response);
}
else if (object instanceof String) {
String playerName = (String) object;
playerNames.put(connection, playerName);
}
}
@Override