mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 23:29:36 +01:00
Rydder opp i kode
Legger til manglende kommentarer Fjerner en del debug kode Forbedrer variabelnavn Fjerner ubrukt feltvariabel
This commit is contained in:
parent
e67ad0a939
commit
88102327a0
@ -4,48 +4,59 @@ import com.esotericsoftware.kryonet.Client;
|
|||||||
import com.esotericsoftware.kryonet.Connection;
|
import com.esotericsoftware.kryonet.Connection;
|
||||||
import com.esotericsoftware.kryonet.Listener;
|
import com.esotericsoftware.kryonet.Listener;
|
||||||
import inf112.fiasko.roborally.game_wrapper.RoboRallyWrapper;
|
import inf112.fiasko.roborally.game_wrapper.RoboRallyWrapper;
|
||||||
|
import inf112.fiasko.roborally.networking.containers.ErrorResponse;
|
||||||
|
import inf112.fiasko.roborally.networking.containers.GameStartInfo;
|
||||||
import inf112.fiasko.roborally.objects.RoboRallyGame;
|
import inf112.fiasko.roborally.objects.RoboRallyGame;
|
||||||
import inf112.fiasko.roborally.utility.NetworkUtil;
|
import inf112.fiasko.roborally.utility.NetworkUtil;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents a client capable of connecting to a Robo Rally server
|
||||||
|
*/
|
||||||
public class RoboRallyClient {
|
public class RoboRallyClient {
|
||||||
Client client;
|
private Client client;
|
||||||
RoboRallyWrapper wrapper;
|
|
||||||
public RoboRallyClient(String IPaddresse,RoboRallyWrapper wrapper) throws IOException {
|
/**
|
||||||
|
* Instantiates a new Robo Rally client
|
||||||
|
* @param ipAddress The ip address of the server to connect to
|
||||||
|
* @param wrapper The Robo Rally wrapper to be used
|
||||||
|
* @throws IOException If the server cannot be reached
|
||||||
|
*/
|
||||||
|
public RoboRallyClient(String ipAddress, RoboRallyWrapper wrapper) throws IOException {
|
||||||
client = new Client();
|
client = new Client();
|
||||||
this.wrapper=wrapper;
|
|
||||||
client.start();
|
client.start();
|
||||||
NetworkUtil.registerClasses(client.getKryo());
|
NetworkUtil.registerClasses(client.getKryo());
|
||||||
client.connect(5000, IPaddresse, 54555, 54777);
|
client.connect(5000, ipAddress, 54555, 54777);
|
||||||
|
|
||||||
SomeRequest request = new SomeRequest();
|
|
||||||
request.text = "Here is the request";
|
|
||||||
client.sendTCP(request);
|
|
||||||
|
|
||||||
client.addListener(new RoboRallyClientListener(wrapper));
|
client.addListener(new RoboRallyClientListener(wrapper));
|
||||||
}
|
}
|
||||||
public void sendElement(Object obj) {
|
|
||||||
client.sendTCP(obj);
|
/**
|
||||||
|
* Sends something to the server
|
||||||
|
* @param object The object to send to the server
|
||||||
|
*/
|
||||||
|
public void sendElement(Object object) {
|
||||||
|
client.sendTCP(object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This listener handles all receiving from the server
|
||||||
|
*/
|
||||||
class RoboRallyClientListener extends Listener {
|
class RoboRallyClientListener extends Listener {
|
||||||
RoboRallyWrapper wrapper;
|
private RoboRallyWrapper wrapper;
|
||||||
public RoboRallyClientListener( RoboRallyWrapper wrapper){
|
|
||||||
|
RoboRallyClientListener(RoboRallyWrapper wrapper) {
|
||||||
super();
|
super();
|
||||||
this.wrapper = wrapper;
|
this.wrapper = wrapper;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void received (Connection connection, Object object) {
|
public void received (Connection connection, Object object) {
|
||||||
if (object instanceof SomeResponse) {
|
if (object instanceof ErrorResponse) {
|
||||||
SomeResponse response = (SomeResponse)object;
|
|
||||||
System.out.println("Client received: " + response.text);
|
|
||||||
} else if (object instanceof ErrorResponse) {
|
|
||||||
ErrorResponse errorResponse = (ErrorResponse) object;
|
ErrorResponse errorResponse = (ErrorResponse) object;
|
||||||
System.out.println(errorResponse.getErrorMessage());
|
System.out.println(errorResponse.getErrorMessage());
|
||||||
}
|
} else if (object instanceof GameStartInfo) {
|
||||||
else if(object instanceof GameStartInfo){
|
|
||||||
GameStartInfo info = (GameStartInfo) object;
|
GameStartInfo info = (GameStartInfo) object;
|
||||||
wrapper.roboRallyGame = new RoboRallyGame(info.getPlayerlist(), info.getBoardname(),
|
wrapper.roboRallyGame = new RoboRallyGame(info.getPlayerlist(), info.getBoardname(),
|
||||||
wrapper.server != null);
|
wrapper.server != null);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user