mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 23:29:36 +01:00
Merge branch 'networking'
This commit is contained in:
commit
2567d4c969
@ -4,12 +4,44 @@ package inf112.fiasko.roborally.element_properties;
|
||||
* This class represents an id for marking specific robots
|
||||
*/
|
||||
public enum RobotID {
|
||||
ROBOT_1,
|
||||
ROBOT_2,
|
||||
ROBOT_3,
|
||||
ROBOT_4,
|
||||
ROBOT_5,
|
||||
ROBOT_6,
|
||||
ROBOT_7,
|
||||
ROBOT_8
|
||||
ROBOT_1 (1),
|
||||
ROBOT_2 (2),
|
||||
ROBOT_3 (3),
|
||||
ROBOT_4 (4),
|
||||
ROBOT_5 (5),
|
||||
ROBOT_6 (6),
|
||||
ROBOT_7 (7),
|
||||
ROBOT_8 (8);
|
||||
|
||||
private final int robotID;
|
||||
|
||||
/**
|
||||
* Constructor to let a robotID be represented by a numerical identifier
|
||||
* @param robotID <p>The numerical identifier assigned to the robot ID</p>
|
||||
*/
|
||||
RobotID(int robotID) {
|
||||
this.robotID = robotID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the numerical id used for identification of a robot id
|
||||
* @return <p>The numerical id of the robot id</p>
|
||||
*/
|
||||
public int getRobotIDID() {
|
||||
return this.robotID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a robot ID value from its numerical representation
|
||||
* @param robotID <p>The numerical representation of a robot id</p>
|
||||
* @return <p>The enum value representing the robot ID, or null if the id is invalid</p>
|
||||
*/
|
||||
public static RobotID getRobotIDFromID(int robotID) {
|
||||
for (RobotID type : RobotID.values()) {
|
||||
if (type.robotID == robotID) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -4,8 +4,13 @@ import com.badlogic.gdx.Game;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import inf112.fiasko.roborally.networking.RoboRallyClient;
|
||||
import inf112.fiasko.roborally.networking.RoboRallyServer;
|
||||
import inf112.fiasko.roborally.networking.SomeResponse;
|
||||
import inf112.fiasko.roborally.objects.IDrawableGame;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class RoboRallyWrapper extends Game {
|
||||
public SpriteBatch batch;
|
||||
public BitmapFont font;
|
||||
@ -18,6 +23,15 @@ public class RoboRallyWrapper extends Game {
|
||||
font = new BitmapFont(Gdx.files.internal("assets/Montserrat-Regular.fnt"));
|
||||
this.screenManager = new ScreenManager();
|
||||
this.setScreen(screenManager.getMainMenuScreen(this));
|
||||
try {
|
||||
RoboRallyServer server = new RoboRallyServer();
|
||||
RoboRallyClient client = new RoboRallyClient();
|
||||
SomeResponse response = new SomeResponse();
|
||||
response.text = "ÆÆÆÆÆÆÆÆÆ";
|
||||
server.sendToAllClients(response);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
|
@ -0,0 +1,43 @@
|
||||
package inf112.fiasko.roborally.networking;
|
||||
|
||||
/**
|
||||
* This class represents a response saying that something went wrong with the request
|
||||
*/
|
||||
public class ErrorResponse {
|
||||
private String errorMessage;
|
||||
private Exception thrownException;
|
||||
|
||||
/**
|
||||
* Constructs a new error response
|
||||
* @param errorMessage The error message describing the error
|
||||
*/
|
||||
public ErrorResponse(String errorMessage) {
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new error response
|
||||
* @param errorMessage The error message describing the error
|
||||
* @param thrownException The exception to throw
|
||||
*/
|
||||
public ErrorResponse(String errorMessage, Exception thrownException) {
|
||||
this.errorMessage = errorMessage;
|
||||
this.thrownException = thrownException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the error message attached to the error response
|
||||
* @return An error message
|
||||
*/
|
||||
public String getErrorMessage() {
|
||||
return this.errorMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the exception attached to the error response
|
||||
* @return An exception or null
|
||||
*/
|
||||
public Exception getThrownException() {
|
||||
return this.thrownException;
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package inf112.fiasko.roborally.networking;
|
||||
|
||||
import com.esotericsoftware.kryonet.Client;
|
||||
import com.esotericsoftware.kryonet.Connection;
|
||||
import com.esotericsoftware.kryonet.Listener;
|
||||
import inf112.fiasko.roborally.utility.NetworkUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class RoboRallyClient {
|
||||
public RoboRallyClient() throws IOException {
|
||||
Client client = new Client();
|
||||
client.start();
|
||||
NetworkUtil.registerClasses(client.getKryo());
|
||||
client.connect(5000, "127.0.0.1", 54555, 54777);
|
||||
|
||||
SomeRequest request = new SomeRequest();
|
||||
request.text = "Here is the request";
|
||||
client.sendTCP(request);
|
||||
|
||||
client.addListener(new RoboRallyClientListener());
|
||||
}
|
||||
}
|
||||
|
||||
class RoboRallyClientListener extends Listener {
|
||||
@Override
|
||||
public void received (Connection connection, Object object) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
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);
|
||||
listener = new RoboRallyServerListener();
|
||||
server.addListener(listener);
|
||||
programmingCardDeck = DeckLoaderUtil.loadProgrammingCardsDeck();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an object to all clients
|
||||
* @param object The object to send
|
||||
*/
|
||||
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 {
|
||||
protected Connection host;
|
||||
protected Map<Connection, RobotID> clients;
|
||||
|
||||
public RoboRallyServerListener() {
|
||||
super();
|
||||
clients = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void received (Connection connection, Object object) {
|
||||
if (object instanceof SomeRequest) {
|
||||
SomeRequest request = (SomeRequest)object;
|
||||
System.out.println(request.text);
|
||||
|
||||
SomeResponse response = new SomeResponse();
|
||||
response.text = "Thanks";
|
||||
connection.sendTCP(response);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connected(Connection connection) {
|
||||
//The first client to connect is assumed to be the host
|
||||
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");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnected(Connection connection) {
|
||||
System.out.println(connection.getRemoteAddressTCP() + " disconnected");
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package inf112.fiasko.roborally.networking;
|
||||
|
||||
/**
|
||||
* Represents a request to the server
|
||||
*/
|
||||
public class SomeRequest {
|
||||
public String text;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package inf112.fiasko.roborally.networking;
|
||||
|
||||
/**
|
||||
* Represents a response from a client
|
||||
*/
|
||||
public class SomeResponse {
|
||||
public String text;
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package inf112.fiasko.roborally.utility;
|
||||
|
||||
import com.esotericsoftware.kryo.Kryo;
|
||||
import inf112.fiasko.roborally.networking.ErrorResponse;
|
||||
import inf112.fiasko.roborally.networking.SomeRequest;
|
||||
import inf112.fiasko.roborally.networking.SomeResponse;
|
||||
import inf112.fiasko.roborally.objects.IDeck;
|
||||
import inf112.fiasko.roborally.objects.ProgrammingCard;
|
||||
|
||||
public final class NetworkUtil {
|
||||
|
||||
/**
|
||||
* Registers all classes which can be sent between a server and a client
|
||||
* @param kryo The kryo object to register the classes to
|
||||
*/
|
||||
public static void registerClasses(Kryo kryo) {
|
||||
kryo.register(SomeRequest.class);
|
||||
kryo.register(SomeResponse.class);
|
||||
kryo.register(ErrorResponse.class);
|
||||
kryo.register(IDeck.class);
|
||||
kryo.register(ProgrammingCard.class);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user