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; import java.io.IOException;
public class RoboRallyClient { public class RoboRallyClient {
Client client;
public RoboRallyClient(String IPaddresse) throws IOException { public RoboRallyClient(String IPaddresse) throws IOException {
Client client = new Client(); client = new Client();
client.start(); client.start();
NetworkUtil.registerClasses(client.getKryo()); NetworkUtil.registerClasses(client.getKryo());
client.connect(5000, IPaddresse, 54555, 54777); client.connect(5000, IPaddresse, 54555, 54777);
@ -20,6 +21,9 @@ public class RoboRallyClient {
client.addListener(new RoboRallyClientListener()); client.addListener(new RoboRallyClientListener());
} }
public void sendElement(Object obj) {
client.sendTCP(obj);
}
} }
class RoboRallyClientListener extends Listener { class RoboRallyClientListener extends Listener {
@ -34,3 +38,4 @@ class RoboRallyClientListener extends Listener {
} }
} }
} }

View File

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