Data sending tests YAY!
This commit is contained in:
@@ -99,6 +99,6 @@ public class Client extends Controller implements ParserCallback {
|
||||
|
||||
@Override
|
||||
public void onDataReceived(String data) {
|
||||
send(data);
|
||||
System.out.println(data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,43 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.client.game.characters.DevTest1;
|
||||
import netwerkprog.game.client.game.characters.DevTest2;
|
||||
import netwerkprog.game.client.game.characters.DevTest3;
|
||||
import netwerkprog.game.server.controllers.DataController;
|
||||
import netwerkprog.game.util.data.ParserCallback;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Parser {
|
||||
private final ParserCallback callback;
|
||||
private Scanner scanner;
|
||||
|
||||
private final DataController dataController;
|
||||
|
||||
public Parser(ParserCallback callback) {
|
||||
this.callback = callback;
|
||||
this.dataController = new DataController();
|
||||
|
||||
this.dataController.addAllCharacters(new DevTest1(), new DevTest2(), new DevTest3());
|
||||
}
|
||||
|
||||
public void parse(String request) {
|
||||
callback.onDataReceived(null);
|
||||
String data = "";
|
||||
this.scanner = new Scanner(request);
|
||||
scanner.useDelimiter("~");
|
||||
String type = scanner.next();
|
||||
String name = scanner.next();
|
||||
|
||||
if (type.equals("character")) {
|
||||
try {
|
||||
data = dataController.getCharacter(name).toString();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
data = ex.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
callback.onDataReceived(data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,47 @@
|
||||
package netwerkprog.game.server.controllers;
|
||||
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class DataController {
|
||||
private final HashSet<GameCharacter> gameCharacters;
|
||||
|
||||
public DataController() {
|
||||
gameCharacters = new HashSet<>();
|
||||
}
|
||||
|
||||
public void addCharacter(GameCharacter gameCharacter) {
|
||||
this.gameCharacters.add(gameCharacter);
|
||||
}
|
||||
|
||||
public void addAllCharacters(GameCharacter... gameCharacters) {
|
||||
this.gameCharacters.addAll(Arrays.asList(gameCharacters));
|
||||
}
|
||||
|
||||
public void removeCharacter(String name) {
|
||||
this.gameCharacters.removeIf(character -> character.getName().equals(name));
|
||||
}
|
||||
|
||||
public void removeCharacter(GameCharacter character) {
|
||||
this.gameCharacters.remove(character);
|
||||
}
|
||||
|
||||
public void clearCharacters() {
|
||||
this.gameCharacters.clear();
|
||||
}
|
||||
|
||||
public HashSet<GameCharacter> getGameCharacters() {
|
||||
return gameCharacters;
|
||||
}
|
||||
|
||||
public GameCharacter getCharacter(String name) throws IllegalArgumentException {
|
||||
for (GameCharacter character : gameCharacters) {
|
||||
if (character.getName().equals(name)) {
|
||||
return character;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("The character does not exist.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,10 @@ public abstract class GameCharacter extends Actor {
|
||||
super.setY(0);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void addAbilities(Ability ability) {
|
||||
this.abilities.add(ability);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user