Legger til litt mer ubrukt funksjonalitet for RoboRallyClient og RoboRallyServer

This commit is contained in:
Kristian Knarvik 2020-03-19 11:48:59 +01:00
parent dac90e413d
commit db23aa6041
2 changed files with 39 additions and 2 deletions

View File

@ -28,6 +28,9 @@ class RoboRallyClientListener extends Listener {
if (object instanceof SomeResponse) { if (object instanceof SomeResponse) {
SomeResponse response = (SomeResponse)object; SomeResponse response = (SomeResponse)object;
System.out.println("Client received: " + response.text); System.out.println("Client received: " + response.text);
} else if (object instanceof ErrorResponse) {
ErrorResponse errorResponse = (ErrorResponse) object;
System.out.println(errorResponse.getErrorMessage());
} }
} }
} }

View File

@ -3,19 +3,31 @@ package inf112.fiasko.roborally.networking;
import com.esotericsoftware.kryonet.Connection; import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener; import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server; import com.esotericsoftware.kryonet.Server;
import inf112.fiasko.roborally.element_properties.RobotID;
import inf112.fiasko.roborally.objects.IDeck;
import inf112.fiasko.roborally.objects.ProgrammingCard;
import inf112.fiasko.roborally.objects.ProgrammingCardDeck;
import inf112.fiasko.roborally.utility.DeckLoaderUtil;
import inf112.fiasko.roborally.utility.NetworkUtil; import inf112.fiasko.roborally.utility.NetworkUtil;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class RoboRallyServer { public class RoboRallyServer {
private Server server; private Server server;
private IDeck<ProgrammingCard> programmingCardDeck;
private RoboRallyServerListener listener;
public RoboRallyServer() throws IOException { public RoboRallyServer() throws IOException {
server = new Server(); server = new Server();
server.start(); server.start();
NetworkUtil.registerClasses(server.getKryo()); NetworkUtil.registerClasses(server.getKryo());
server.bind(54555, 54777); server.bind(54555, 54777);
server.addListener(new RoboRallyServerListener()); listener = new RoboRallyServerListener();
server.addListener(listener);
programmingCardDeck = DeckLoaderUtil.loadProgrammingCardsDeck();
} }
/** /**
@ -25,13 +37,27 @@ public class RoboRallyServer {
public void sendToAllClients(Object object) { public void sendToAllClients(Object object) {
server.sendToAllTCP(object); server.sendToAllTCP(object);
} }
/**
* Deals cards to all players
*/
public void dealCards() {
programmingCardDeck.shuffle();
for (Connection connection : server.getConnections()) {
IDeck<ProgrammingCard> hand = new ProgrammingCardDeck(new ArrayList<>());
hand.draw(programmingCardDeck, 9);
connection.sendTCP(hand);
}
}
} }
class RoboRallyServerListener extends Listener { class RoboRallyServerListener extends Listener {
Connection host; protected Connection host;
protected Map<Connection, RobotID> clients;
public RoboRallyServerListener() { public RoboRallyServerListener() {
super(); super();
clients = new HashMap<>();
} }
@Override @Override
@ -52,6 +78,14 @@ class RoboRallyServerListener extends Listener {
if (host == null) { if (host == null) {
host = connection; host = connection;
} }
//Prevents more than 8 players from connecting at once
if (clients.size() >= 8) {
String errorMessage = "The server already has 8 players. You cannot join.";
connection.sendTCP(new ErrorResponse(errorMessage, new IOException(errorMessage)));
connection.close();
return;
}
clients.put(connection, RobotID.getRobotIDFromID(clients.size() + 1));
System.out.println(connection.getRemoteAddressTCP() + " connected"); System.out.println(connection.getRemoteAddressTCP() + " connected");
} }