merge master into this branch

# Conflicts:
#	core/src/netwerkprog/game/util/game/Character.java
#	core/src/netwerkprog/game/util/game/Faction.java
This commit is contained in:
Sem van der Hoeven
2020-05-27 23:04:04 +02:00
parent cc4297d86d
commit 6c1e073348
15 changed files with 151 additions and 72 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@@ -2,7 +2,7 @@ package netwerkprog.game.client;
import netwerkprog.game.util.application.Controller;
import netwerkprog.game.util.data.Data;
import netwerkprog.game.util.data.DataParser;
import netwerkprog.game.util.data.ParserCallback;
import java.io.DataInputStream;
import java.io.DataOutputStream;
@@ -10,10 +10,10 @@ import java.io.IOException;
import java.net.Socket;
import java.util.Arrays;
public class Client extends Controller {
public class Client extends Controller implements ParserCallback {
private final int port;
private final String hostname;
private final DataParser parser;
private final Parser parser;
private boolean isConnected = true;
private Socket socket;
private Thread receiveThread;
@@ -22,7 +22,7 @@ public class Client extends Controller {
public Client(String hostname) {
this.port = Data.port();
this.hostname = hostname;
this.parser = new DataParser();
this.parser = new Parser(this);
}
/**
@@ -71,16 +71,12 @@ public class Client extends Controller {
* @param in The inputStream
*/
public void receive(DataInputStream in) {
Data data = null;
while (isConnected) {
try {
data = this.parser.parse(in.readUTF());
this.parser.parse(in.readUTF());
} catch (IOException e) {
e.printStackTrace();
}
if (data != null) {
send(this.parser.parse(data));
}
}
}
@@ -100,4 +96,9 @@ public class Client extends Controller {
e.printStackTrace();
}
}
@Override
public void onDataReceived(String data) {
send(data);
}
}

View File

@@ -0,0 +1,15 @@
package netwerkprog.game.client;
import netwerkprog.game.util.data.ParserCallback;
public class Parser {
private final ParserCallback callback;
public Parser(ParserCallback callback) {
this.callback = callback;
}
public void parse(String data) {
callback.onDataReceived(data);
}
}

View File

@@ -1,4 +1,10 @@
package netwerkprog.game.client.game.characters;
public class DevTest1 {
import netwerkprog.game.util.game.GameCharacter;
import netwerkprog.game.util.game.Faction;
public class DevTest1 extends GameCharacter {
public DevTest1() {
super("DevTest1", Faction.HACKER, "core/assets/ScifiCritters4.PNG");
}
}

View File

@@ -1,4 +1,10 @@
package netwerkprog.game.client.game.characters;
public class DevTest2 {
import netwerkprog.game.util.game.GameCharacter;
import netwerkprog.game.util.game.Faction;
public class DevTest2 extends GameCharacter {
public DevTest2() {
super("DevTest2", Faction.MEGACORPORATION, null);
}
}

View File

@@ -0,0 +1,10 @@
package netwerkprog.game.client.game.characters;
import netwerkprog.game.util.game.GameCharacter;
import netwerkprog.game.util.game.Faction;
public class DevTest3 extends GameCharacter {
public DevTest3() {
super("DevTest3", Faction.AI, null);
}
}

View File

@@ -0,0 +1,15 @@
package netwerkprog.game.server;
import netwerkprog.game.util.data.ParserCallback;
public class Parser {
private final ParserCallback callback;
public Parser(ParserCallback callback) {
this.callback = callback;
}
public void parse(String request) {
callback.onDataReceived(null);
}
}

View File

@@ -1,25 +1,25 @@
package netwerkprog.game.server;
import netwerkprog.game.server.controllers.SessionController;
import netwerkprog.game.util.data.Data;
import netwerkprog.game.util.data.ParserCallback;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ServerClient implements Runnable {
public class ServerClient implements Runnable, ParserCallback {
private DataInputStream in;
private DataOutputStream out;
private Socket socket;
private String name;
private SessionController server;
private boolean isConnected = false;
private final String name;
private final SessionController server;
private final Parser parser;
private boolean isConnected;
public ServerClient(String name, Socket socket, SessionController server) {
this.name = name;
this.server = server;
this.socket = socket;
this.parser = new Parser(this);
try {
this.in = new DataInputStream(socket.getInputStream());
this.out = new DataOutputStream(socket.getOutputStream());
@@ -30,14 +30,6 @@ public class ServerClient implements Runnable {
}
}
public void readUTF() {
try {
System.out.println("[SERVERCLIENT] got message: " + in.readUTF());
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeUTF(String text) {
try {
this.out.writeUTF(text);
@@ -48,27 +40,26 @@ public class ServerClient implements Runnable {
@Override
public void run() {
while (isConnected) {
while (this.isConnected) {
try {
String received = this.in.readUTF();
Data data = server.parseData(received);
if (data.toString().equals("\\quit")) {
this.isConnected = false;
this.server.removeClient(this);
} else {
this.out.writeUTF(data.toString());
}
this.parser.parse(received);
} catch (IOException e) {
System.out.println("[SERVERCLIENT] caught exception - " + e.getMessage());
System.out.println("[SERVERCLIENT] terminating failing connection...");
isConnected = false;
this.isConnected = false;
System.out.println("[SERVERCLIENT] done!");
}
}
}
public String getName() {
return name;
return this.name;
}
@Override
public void onDataReceived(String data) {
writeUTF(data);
}
}

View File

@@ -0,0 +1,8 @@
package netwerkprog.game.server.controllers;
public class DataController {
public DataController() {
}
}

View File

@@ -1,13 +1,12 @@
package netwerkprog.game.server.controllers;
import netwerkprog.game.server.ServerClient;
import netwerkprog.game.util.data.Data;
import netwerkprog.game.util.data.DataParser;
import netwerkprog.game.util.application.Controller;
import netwerkprog.game.util.data.Data;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
@@ -19,13 +18,11 @@ import java.util.Set;
*/
public class SessionController extends Controller {
private ServerSocket serverSocket;
private final DataParser parser;
private final ArrayList<ServerClient> clients = new ArrayList<>();
private final HashMap<String, Thread> clientThreads = new HashMap<>();
private boolean listening;
public SessionController() {
this.parser = new DataParser();
this.listening = true;
}
@@ -61,7 +58,7 @@ public class SessionController extends Controller {
public void registerClient(Socket socket) {
try {
System.out.println("[SERVER] got new client on " + socket.getInetAddress().getHostAddress());
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
outputStream.writeUTF("Enter username: ");
@@ -80,15 +77,6 @@ public class SessionController extends Controller {
}
}
/**
* Parses the request to the requested Data.
* @param request The request to parse.
* @return Parsed Data.
*/
public Data parseData(String request) {
return this.parser.parse(request);
}
/**
* Sends a server message to all connected clients.
* @param text message.

View File

@@ -1,5 +0,0 @@
package netwerkprog.game.util.data;
public interface Callback {
void onDataReceived();
}

View File

@@ -1,14 +0,0 @@
package netwerkprog.game.util.data;
public class DataParser {
public DataParser() {
}
public Data parse(String request) {
return null;
}
public String parse(Data data) {
return null;
}
}

View File

@@ -0,0 +1,5 @@
package netwerkprog.game.util.data;
public interface ParserCallback {
void onDataReceived(String data);
}

View File

@@ -1,6 +1,13 @@
package netwerkprog.game.util.game;
public enum Faction {
HACKER,
MEGACORPORATION
MEGACORPORATION("MegaCorp"),
HACKER("Hacker"),
AI("AI");
String name;
Faction(String name) {
this.name = name;
}
}

View File

@@ -0,0 +1,46 @@
package netwerkprog.game.util.game;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import java.util.Arrays;
import java.util.HashSet;
public abstract class GameCharacter extends Actor {
protected String name;
protected Faction faction;
protected HashSet<Ability> abilities;
protected TextureRegion[][] sprites;
protected boolean override;
public GameCharacter(String name, Faction faction, String spriteSheet, Ability... abilities) {
this.name = name;
this.faction = faction;
this.abilities = new HashSet<>(Arrays.asList(abilities));
this.override = false;
this.sprites = TextureRegion.split(new Texture(spriteSheet),32,32);
super.setX(0);
super.setY(0);
}
public void addAbilities(Ability ability) {
this.abilities.add(ability);
}
public void removeAbility(Ability ability) {
this.abilities.remove(ability);
}
public void changeControl() {
this.override = !this.override;
}
public void draw(SpriteBatch batch) {
batch.begin();
batch.draw(this.sprites[0][0],super.getX(),super.getY());
batch.end();
}
}