mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 23:29:36 +01:00
Legger til litt mer ubrukt funksjonalitet for RoboRallyClient og RoboRallyServer
This commit is contained in:
parent
dac90e413d
commit
db23aa6041
@ -28,6 +28,9 @@ class RoboRallyClientListener extends Listener {
|
||||
if (object instanceof SomeResponse) {
|
||||
SomeResponse response = (SomeResponse)object;
|
||||
System.out.println("Client received: " + response.text);
|
||||
} else if (object instanceof ErrorResponse) {
|
||||
ErrorResponse errorResponse = (ErrorResponse) object;
|
||||
System.out.println(errorResponse.getErrorMessage());
|
||||
}
|
||||
}
|
||||
}
|
@ -3,19 +3,31 @@ package inf112.fiasko.roborally.networking;
|
||||
import com.esotericsoftware.kryonet.Connection;
|
||||
import com.esotericsoftware.kryonet.Listener;
|
||||
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 java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class RoboRallyServer {
|
||||
private Server server;
|
||||
private IDeck<ProgrammingCard> programmingCardDeck;
|
||||
private RoboRallyServerListener listener;
|
||||
|
||||
public RoboRallyServer() throws IOException {
|
||||
server = new Server();
|
||||
server.start();
|
||||
NetworkUtil.registerClasses(server.getKryo());
|
||||
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) {
|
||||
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 {
|
||||
Connection host;
|
||||
protected Connection host;
|
||||
protected Map<Connection, RobotID> clients;
|
||||
|
||||
public RoboRallyServerListener() {
|
||||
super();
|
||||
clients = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -52,6 +78,14 @@ class RoboRallyServerListener extends Listener {
|
||||
if (host == null) {
|
||||
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");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user