username sending done
This commit is contained in:
@@ -26,6 +26,8 @@ import netwerkprog.game.client.game.map.MapRenderer;
|
|||||||
import netwerkprog.game.util.data.Data;
|
import netwerkprog.game.util.data.Data;
|
||||||
import netwerkprog.game.util.data.DataCallback;
|
import netwerkprog.game.util.data.DataCallback;
|
||||||
import netwerkprog.game.util.data.DataSource;
|
import netwerkprog.game.util.data.DataSource;
|
||||||
|
import netwerkprog.game.util.data.NameData;
|
||||||
|
import netwerkprog.game.util.data.TeamData;
|
||||||
import netwerkprog.game.util.game.Faction;
|
import netwerkprog.game.util.game.Faction;
|
||||||
import netwerkprog.game.util.game.GameCharacter;
|
import netwerkprog.game.util.game.GameCharacter;
|
||||||
import netwerkprog.game.util.graphics.FrameRate;
|
import netwerkprog.game.util.graphics.FrameRate;
|
||||||
@@ -53,6 +55,7 @@ public class MainGame extends Game implements DataCallback {
|
|||||||
private boolean gameOver = false;
|
private boolean gameOver = false;
|
||||||
private int turn = 0;
|
private int turn = 0;
|
||||||
private boolean playersTurn = true;
|
private boolean playersTurn = true;
|
||||||
|
private String username;
|
||||||
|
|
||||||
private Map map;
|
private Map map;
|
||||||
public MapRenderer mapRenderer;
|
public MapRenderer mapRenderer;
|
||||||
@@ -353,6 +356,22 @@ public class MainGame extends Game implements DataCallback {
|
|||||||
@Override
|
@Override
|
||||||
public void onDataReceived(Data data, DataSource source) {
|
public void onDataReceived(Data data, DataSource source) {
|
||||||
System.out.println("[MAINGAME CALLBACK] Got data: " + data.toString());
|
System.out.println("[MAINGAME CALLBACK] Got data: " + data.toString());
|
||||||
|
if (data instanceof NameData) {
|
||||||
|
System.out.println("[MAINGAME CALLBACK] got name data: " + data);
|
||||||
|
this.username = ((NameData) data).getName();
|
||||||
|
System.out.println("[MAINGAME CALLBACK] username is: " + username);
|
||||||
|
} else if (data instanceof TeamData) {
|
||||||
|
if (this.chosenFaction != null) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import com.badlogic.gdx.utils.TimeUtils;
|
|||||||
import netwerkprog.game.client.MainGame;
|
import netwerkprog.game.client.MainGame;
|
||||||
import netwerkprog.game.client.game.GAMESTATE;
|
import netwerkprog.game.client.game.GAMESTATE;
|
||||||
import netwerkprog.game.util.data.Data;
|
import netwerkprog.game.util.data.Data;
|
||||||
|
import netwerkprog.game.util.data.TeamData;
|
||||||
import netwerkprog.game.util.game.Faction;
|
import netwerkprog.game.util.game.Faction;
|
||||||
import netwerkprog.game.util.game.GameCharacter;
|
import netwerkprog.game.util.game.GameCharacter;
|
||||||
|
|
||||||
@@ -119,15 +120,17 @@ public class GameInputProcessor implements InputProcessor {
|
|||||||
if (keycode == Input.Keys.NUM_1) {
|
if (keycode == Input.Keys.NUM_1) {
|
||||||
System.out.println("MEGA CORP");
|
System.out.println("MEGA CORP");
|
||||||
mainGame.setChosenFaction(Faction.MEGACORPORATION);
|
mainGame.setChosenFaction(Faction.MEGACORPORATION);
|
||||||
mainGame.initCharacters();
|
mainGame.send(new TeamData(Faction.MEGACORPORATION, mainGame.getUsername()));
|
||||||
mainGame.setGamestate(GAMESTATE.PLAYING);
|
// mainGame.initCharacters();
|
||||||
|
// mainGame.setGamestate(GAMESTATE.PLAYING);
|
||||||
}
|
}
|
||||||
if (keycode == Input.Keys.NUM_2) {
|
if (keycode == Input.Keys.NUM_2) {
|
||||||
System.out.println("HACKER");
|
System.out.println("HACKER");
|
||||||
mainGame.setChosenFaction(Faction.HACKER);
|
mainGame.setChosenFaction(Faction.HACKER);
|
||||||
mainGame.initCharacters();
|
mainGame.send(new TeamData(Faction.MEGACORPORATION, mainGame.getUsername()));
|
||||||
camera.translate(-400, 0);
|
// mainGame.initCharacters();
|
||||||
mainGame.setGamestate(GAMESTATE.PLAYING);
|
// camera.translate(-400, 0);
|
||||||
|
// mainGame.setGamestate(GAMESTATE.PLAYING);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import netwerkprog.game.server.ServerClient;
|
|||||||
import netwerkprog.game.util.application.Controller;
|
import netwerkprog.game.util.application.Controller;
|
||||||
import netwerkprog.game.util.data.ConnectionData;
|
import netwerkprog.game.util.data.ConnectionData;
|
||||||
import netwerkprog.game.util.data.Data;
|
import netwerkprog.game.util.data.Data;
|
||||||
|
import netwerkprog.game.util.data.NameData;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
@@ -61,7 +62,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("[SERVERCLIENT] got new client on " + socket.getInetAddress().getHostAddress());
|
||||||
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
|
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||||
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
|
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
|
||||||
|
|
||||||
@@ -91,12 +92,19 @@ public class SessionController extends Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("[SERVER] got username " + username);
|
if (this.clients.isEmpty()) {
|
||||||
|
username = "player1";
|
||||||
|
} else {
|
||||||
|
username = "player" + (this.clients.size() + 1);
|
||||||
|
}
|
||||||
|
System.out.println("[SERVER] setting username: " + username);
|
||||||
ServerClient serverClient = new ServerClient(username, inputStream, outputStream, this, server.getDataController());
|
ServerClient serverClient = new ServerClient(username, inputStream, outputStream, this, server.getDataController());
|
||||||
|
|
||||||
Thread t = new Thread(serverClient);
|
Thread t = new Thread(serverClient);
|
||||||
t.start();
|
t.start();
|
||||||
|
|
||||||
|
serverClient.writeData(new NameData(username));
|
||||||
|
|
||||||
this.clientThreads.put(username,t);
|
this.clientThreads.put(username,t);
|
||||||
this.clients.add(serverClient);
|
this.clients.add(serverClient);
|
||||||
} catch (IOException | ClassNotFoundException ex) {
|
} catch (IOException | ClassNotFoundException ex) {
|
||||||
|
|||||||
15
core/src/netwerkprog/game/util/data/NameData.java
Normal file
15
core/src/netwerkprog/game/util/data/NameData.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package netwerkprog.game.util.data;
|
||||||
|
|
||||||
|
public class NameData extends Data {
|
||||||
|
private String name;
|
||||||
|
public NameData(String name) {
|
||||||
|
super("name");
|
||||||
|
super.setPayload(this);
|
||||||
|
this.name = name;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,14 +4,20 @@ import netwerkprog.game.util.game.Faction;
|
|||||||
|
|
||||||
public class TeamData extends Data {
|
public class TeamData extends Data {
|
||||||
private final Faction faction;
|
private final Faction faction;
|
||||||
|
private final String username;
|
||||||
|
|
||||||
public TeamData(Faction faction) {
|
public TeamData(Faction faction, String username) {
|
||||||
super("Team");
|
super("Team");
|
||||||
super.setPayload(this);
|
super.setPayload(this);
|
||||||
this.faction = faction;
|
this.faction = faction;
|
||||||
|
this.username = username;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Faction getFaction() {
|
public Faction getFaction() {
|
||||||
return faction;
|
return faction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user