Mick #6
BIN
core/assets/ScifiCritters4.PNG
Normal file
BIN
core/assets/ScifiCritters4.PNG
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
@@ -2,7 +2,7 @@ package netwerkprog.game.client;
|
|||||||
|
|
||||||
import netwerkprog.game.util.application.Controller;
|
import netwerkprog.game.util.application.Controller;
|
||||||
import netwerkprog.game.util.data.Data;
|
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.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
@@ -10,10 +10,10 @@ import java.io.IOException;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class Client extends Controller {
|
public class Client extends Controller implements ParserCallback {
|
||||||
private final int port;
|
private final int port;
|
||||||
private final String hostname;
|
private final String hostname;
|
||||||
private final DataParser parser;
|
private final Parser parser;
|
||||||
private boolean isConnected = true;
|
private boolean isConnected = true;
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private Thread receiveThread;
|
private Thread receiveThread;
|
||||||
@@ -22,7 +22,7 @@ public class Client extends Controller {
|
|||||||
public Client(String hostname) {
|
public Client(String hostname) {
|
||||||
this.port = Data.port();
|
this.port = Data.port();
|
||||||
this.hostname = hostname;
|
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
|
* @param in The inputStream
|
||||||
*/
|
*/
|
||||||
public void receive(DataInputStream in) {
|
public void receive(DataInputStream in) {
|
||||||
Data data = null;
|
|
||||||
while (isConnected) {
|
while (isConnected) {
|
||||||
try {
|
try {
|
||||||
data = this.parser.parse(in.readUTF());
|
this.parser.parse(in.readUTF());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
if (data != null) {
|
|
||||||
send(this.parser.parse(data));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,4 +96,9 @@ public class Client extends Controller {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDataReceived(String data) {
|
||||||
|
send(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
15
core/src/netwerkprog/game/client/Parser.java
Normal file
15
core/src/netwerkprog/game/client/Parser.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,10 @@
|
|||||||
package netwerkprog.game.client.game.characters;
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
package netwerkprog.game.client.game.characters;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
core/src/netwerkprog/game/server/Parser.java
Normal file
15
core/src/netwerkprog/game/server/Parser.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,25 +1,25 @@
|
|||||||
package netwerkprog.game.server;
|
package netwerkprog.game.server;
|
||||||
|
|
||||||
import netwerkprog.game.server.controllers.SessionController;
|
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.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
public class ServerClient implements Runnable {
|
public class ServerClient implements Runnable, ParserCallback {
|
||||||
private DataInputStream in;
|
private DataInputStream in;
|
||||||
private DataOutputStream out;
|
private DataOutputStream out;
|
||||||
private Socket socket;
|
private final String name;
|
||||||
private String name;
|
private final SessionController server;
|
||||||
private SessionController server;
|
private final Parser parser;
|
||||||
private boolean isConnected = false;
|
private boolean isConnected;
|
||||||
|
|
||||||
public ServerClient(String name, Socket socket, SessionController server) {
|
public ServerClient(String name, Socket socket, SessionController server) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.server = server;
|
this.server = server;
|
||||||
this.socket = socket;
|
this.parser = new Parser(this);
|
||||||
try {
|
try {
|
||||||
this.in = new DataInputStream(socket.getInputStream());
|
this.in = new DataInputStream(socket.getInputStream());
|
||||||
this.out = new DataOutputStream(socket.getOutputStream());
|
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) {
|
public void writeUTF(String text) {
|
||||||
try {
|
try {
|
||||||
this.out.writeUTF(text);
|
this.out.writeUTF(text);
|
||||||
@@ -48,27 +40,26 @@ public class ServerClient implements Runnable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (isConnected) {
|
while (this.isConnected) {
|
||||||
try {
|
try {
|
||||||
String received = this.in.readUTF();
|
String received = this.in.readUTF();
|
||||||
Data data = server.parseData(received);
|
this.parser.parse(received);
|
||||||
if (data.toString().equals("\\quit")) {
|
|
||||||
this.isConnected = false;
|
|
||||||
this.server.removeClient(this);
|
|
||||||
} else {
|
|
||||||
this.out.writeUTF(data.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("[SERVERCLIENT] caught exception - " + e.getMessage());
|
System.out.println("[SERVERCLIENT] caught exception - " + e.getMessage());
|
||||||
System.out.println("[SERVERCLIENT] terminating failing connection...");
|
System.out.println("[SERVERCLIENT] terminating failing connection...");
|
||||||
isConnected = false;
|
this.isConnected = false;
|
||||||
System.out.println("[SERVERCLIENT] done!");
|
System.out.println("[SERVERCLIENT] done!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDataReceived(String data) {
|
||||||
|
writeUTF(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package netwerkprog.game.server.controllers;
|
||||||
|
|
||||||
|
public class DataController {
|
||||||
|
public DataController() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,13 +1,12 @@
|
|||||||
package netwerkprog.game.server.controllers;
|
package netwerkprog.game.server.controllers;
|
||||||
|
|
||||||
import netwerkprog.game.server.ServerClient;
|
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.application.Controller;
|
||||||
|
import netwerkprog.game.util.data.Data;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -19,13 +18,11 @@ import java.util.Set;
|
|||||||
*/
|
*/
|
||||||
public class SessionController extends Controller {
|
public class SessionController extends Controller {
|
||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
private final DataParser parser;
|
|
||||||
private final ArrayList<ServerClient> clients = new ArrayList<>();
|
private final ArrayList<ServerClient> clients = new ArrayList<>();
|
||||||
private final HashMap<String, Thread> clientThreads = new HashMap<>();
|
private final HashMap<String, Thread> clientThreads = new HashMap<>();
|
||||||
private boolean listening;
|
private boolean listening;
|
||||||
|
|
||||||
public SessionController() {
|
public SessionController() {
|
||||||
this.parser = new DataParser();
|
|
||||||
this.listening = true;
|
this.listening = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,7 +58,7 @@ public class SessionController extends Controller {
|
|||||||
public void registerClient(Socket socket) {
|
public void registerClient(Socket socket) {
|
||||||
try {
|
try {
|
||||||
System.out.println("[SERVER] got new client on " + socket.getInetAddress().getHostAddress());
|
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());
|
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
|
||||||
|
|
||||||
outputStream.writeUTF("Enter username: ");
|
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.
|
* Sends a server message to all connected clients.
|
||||||
* @param text message.
|
* @param text message.
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
package netwerkprog.game.util.data;
|
|
||||||
|
|
||||||
public interface Callback {
|
|
||||||
void onDataReceived();
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
5
core/src/netwerkprog/game/util/data/ParserCallback.java
Normal file
5
core/src/netwerkprog/game/util/data/ParserCallback.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package netwerkprog.game.util.data;
|
||||||
|
|
||||||
|
public interface ParserCallback {
|
||||||
|
void onDataReceived(String data);
|
||||||
|
}
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
package netwerkprog.game.util.game;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashSet;
|
|
||||||
|
|
||||||
public abstract class Character {
|
|
||||||
protected String name;
|
|
||||||
protected Faction faction;
|
|
||||||
protected HashSet<Ability> abilities;
|
|
||||||
protected boolean override;
|
|
||||||
|
|
||||||
public Character(String name, Faction faction, Ability... abilities) {
|
|
||||||
this.name = name;
|
|
||||||
this.faction = faction;
|
|
||||||
this.abilities = new HashSet<>(Arrays.asList(abilities));
|
|
||||||
this.override = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,13 @@
|
|||||||
package netwerkprog.game.util.game;
|
package netwerkprog.game.util.game;
|
||||||
|
|
||||||
public enum Faction {
|
public enum Faction {
|
||||||
|
MEGACORPORATION("MegaCorp"),
|
||||||
|
HACKER("Hacker"),
|
||||||
|
AI("AI");
|
||||||
|
|
||||||
|
String name;
|
||||||
|
|
||||||
|
Faction(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
46
core/src/netwerkprog/game/util/game/GameCharacter.java
Normal file
46
core/src/netwerkprog/game/util/game/GameCharacter.java
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user