Skip to content
Snippets Groups Projects
Commit c5e29e01 authored by Stefano Grisendi's avatar Stefano Grisendi
Browse files

some more bugfix

parent 973639f8
Branches
No related tags found
1 merge request!13Dev
...@@ -13,12 +13,12 @@ import com.mygdx.game.views.tokens.PlayerAvatar; ...@@ -13,12 +13,12 @@ import com.mygdx.game.views.tokens.PlayerAvatar;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
public class NetworkController { public class NetworkController {
private Client client; private Client client;
private Kryo kryo; private Kryo kryo;
private Queue<Command> commandQueue;
private boolean devMode = false; private boolean devMode = false;
...@@ -34,18 +34,12 @@ public class NetworkController { ...@@ -34,18 +34,12 @@ public class NetworkController {
} }
kryo = client.getKryo(); kryo = client.getKryo();
registerClasses(); registerClasses();
commandQueue = new Queue<>();
} }
public void sendToServer(Command command){ public void sendToServer(Command command){
//commandQueue.addFirst(command);
client.sendTCP(command); client.sendTCP(command);
} }
public void clearCommands(){
commandQueue.clear();
}
public void registerClasses(){ public void registerClasses(){
kryo.register(ArrayList.class, 100); kryo.register(ArrayList.class, 100);
kryo.register(Lobby.class, 5); kryo.register(Lobby.class, 5);
......
...@@ -7,10 +7,12 @@ import com.mygdx.game.model.Lobby; ...@@ -7,10 +7,12 @@ import com.mygdx.game.model.Lobby;
import com.mygdx.game.model.Player; import com.mygdx.game.model.Player;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
public class PlayerController { public class PlayerController {
private NetworkController net; private NetworkController net;
private LinkedList<Command> receivedCommands;
private Player player; //This is the current player private Player player; //This is the current player
private Lobby lobby; //Current lobby private Lobby lobby; //Current lobby
private ArrayList<Lobby> lobbies; //List of all lobbies private ArrayList<Lobby> lobbies; //List of all lobbies
...@@ -20,6 +22,7 @@ public class PlayerController { ...@@ -20,6 +22,7 @@ public class PlayerController {
player = new Player(-1); player = new Player(-1);
lobby = new Lobby(-1); lobby = new Lobby(-1);
lobbies = new ArrayList<Lobby>(); lobbies = new ArrayList<Lobby>();
receivedCommands = new LinkedList<Command>();
net.getClient().addListener(new Listener() { net.getClient().addListener(new Listener() {
public void received (Connection connection, Object object) { public void received (Connection connection, Object object) {
...@@ -27,26 +30,32 @@ public class PlayerController { ...@@ -27,26 +30,32 @@ public class PlayerController {
Command command = (Command) object; Command command = (Command) object;
System.out.println("Received: "+ command.toString()); System.out.println("Received: "+ command.toString());
command.execute(getPlayerController(), connection); command.execute(getPlayerController(), connection);
receivedCommands.removeFirst();
receivedCommands.addFirst(command);
} }
} }
}); });
connectPlayer(0); connectPlayer(0);
} }
private void sendCommand(Command command){
receivedCommands.addFirst(null);
net.sendToServer(command);
}
public void lobbyGetList(){ net.sendToServer(new cLobbyGetList()); } public void lobbyGetList(){ sendCommand(new cLobbyGetList()); }
public void connectPlayer(int avatarIndex){ net.sendToServer(new cPlayerJoin(avatarIndex));} public void connectPlayer(int avatarIndex){ sendCommand(new cPlayerJoin(avatarIndex));}
public void createLobby(String name, int MAX_PLAYERS){ net.sendToServer(new cLobbyCreate(name, MAX_PLAYERS)); } public void createLobby(String name, int MAX_PLAYERS){ sendCommand(new cLobbyCreate(name, MAX_PLAYERS)); }
public void updateIndexAvatar(int indexAvatar){ net.sendToServer(new cSetIndexAvatar(indexAvatar)); } public void updateIndexAvatar(int indexAvatar){ sendCommand(new cSetIndexAvatar(indexAvatar)); }
public void deleteLobby(int id){ net.sendToServer(new cLobbyDelete(id)); } public void deleteLobby(int id){ sendCommand(new cLobbyDelete(id)); }
public void joinLobby(int id){ net.sendToServer(new cLobbyJoin(id)); } public void joinLobby(int id){ sendCommand(new cLobbyJoin(id)); }
public void leaveLobby(int id) { net.sendToServer(new cLobbyLeave(id)); } public void leaveLobby(int id) { sendCommand(new cLobbyLeave(id)); }
public Lobby getLobby(){ return lobby; } public Lobby getLobby(){ return lobby; }
...@@ -64,6 +73,8 @@ public class PlayerController { ...@@ -64,6 +73,8 @@ public class PlayerController {
public NetworkController getNetWorkController() { return net; } public NetworkController getNetWorkController() { return net; }
public Command getLastCommand(){ return receivedCommands.getFirst(); }
......
...@@ -275,7 +275,7 @@ public class LobbyView extends View{ ...@@ -275,7 +275,7 @@ public class LobbyView extends View{
Gdx.input.setOnscreenKeyboardVisible(false); Gdx.input.setOnscreenKeyboardVisible(false);
stage.unfocusAll(); stage.unfocusAll();
while(playerController.getLobby() == null){System.out.println("NOT FINDING A LOBBY");} if(playerController.getLobby() == null){System.out.println("NOT FINDING A LOBBY");}
if(playerController.getLobby().getPlayers().size() == 1){ if(playerController.getLobby().getPlayers().size() == 1){
System.out.println("Deleting lobby " + playerController.getLobby().getName()); System.out.println("Deleting lobby " + playerController.getLobby().getName());
...@@ -312,23 +312,18 @@ public class LobbyView extends View{ ...@@ -312,23 +312,18 @@ public class LobbyView extends View{
} }
playerController.joinLobby(getLobbyByStringHashMap.get(lobbyListStrings.getSelected())); playerController.joinLobby(getLobbyByStringHashMap.get(lobbyListStrings.getSelected()));
System.out.println("joining lobby with lobbyID: " + lobbyListStrings.getSelectedIndex()); System.out.println("joining lobby with lobbyID: " + lobbyListStrings.getSelectedIndex());
while (playerController.getLobby() == null) { while (playerController.getLastCommand() == null) {
System.out.println("Waiting for lobby"); System.out.println("Waiting for lobby");
} }
Lobby lobby = playerController.getLobby(); Lobby lobby = playerController.getLobby();
System.out.println("Lobby loaded " + lobby); System.out.println("Lobby loaded " + lobby);
}
};
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Gdx.input.setOnscreenKeyboardVisible(false); Gdx.input.setOnscreenKeyboardVisible(false);
showLobbyWindow(); showLobbyWindow();
lobbyRefresh(); lobbyRefresh();
} }
};
thread.start();
}
else{ else{
outputLabel.setText("LobbyListHash did not contain the selected key."); outputLabel.setText("LobbyListHash did not contain the selected key.");
} }
...@@ -383,7 +378,7 @@ public class LobbyView extends View{ ...@@ -383,7 +378,7 @@ public class LobbyView extends View{
public void run(){ public void run(){
playerController.createLobby(lobbyCreateTextField.getText(), lobbyCreateSizeSelectBox.getSelected()); playerController.createLobby(lobbyCreateTextField.getText(), lobbyCreateSizeSelectBox.getSelected());
System.out.println("Creating lobby '" + lobbyCreateTextField.getText() + "' with maxSize " + lobbyCreateSizeSelectBox.getSelected()); System.out.println("Creating lobby '" + lobbyCreateTextField.getText() + "' with maxSize " + lobbyCreateSizeSelectBox.getSelected());
while (playerController.getLobby() == null) { while (playerController.getLastCommand() == null) {
System.out.println("Waiting for lobby object from server.."); System.out.println("Waiting for lobby object from server..");
} }
while(playerController.getLobby().getID() == -1){ while(playerController.getLobby().getID() == -1){
...@@ -394,7 +389,7 @@ public class LobbyView extends View{ ...@@ -394,7 +389,7 @@ public class LobbyView extends View{
System.out.println("Lobby loaded successfully. '" + playerController.getLobby().getName() + "', maxPlayers: " + lobby.getMaxPlayers() + ", NumOfPlayersInLobby: " + lobby.getPlayers().size()); System.out.println("Lobby loaded successfully. '" + playerController.getLobby().getName() + "', maxPlayers: " + lobby.getMaxPlayers() + ", NumOfPlayersInLobby: " + lobby.getPlayers().size());
playerController.joinLobby(playerController.getLobby().getID()); playerController.joinLobby(playerController.getLobby().getID());
while(playerController.getLobby().getPlayers().size() == 0){System.out.println("NOT JOINED LOBBY YET");} while(playerController.getLastCommand() == null){System.out.println("NOT JOINED LOBBY YET");}
Gdx.input.setOnscreenKeyboardVisible(false); Gdx.input.setOnscreenKeyboardVisible(false);
showLobbyWindow(); showLobbyWindow();
lobbyRefresh(); lobbyRefresh();
...@@ -441,7 +436,7 @@ public class LobbyView extends View{ ...@@ -441,7 +436,7 @@ public class LobbyView extends View{
public void run(){ public void run(){
playerController.setLobbies(null); playerController.setLobbies(null);
playerController.lobbyGetList(); playerController.lobbyGetList();
while (playerController.getLobbyList() == null) { while (playerController.getLastCommand() == null) {
System.out.println("Waiting for lobby" + currentLobbyList); System.out.println("Waiting for lobby" + currentLobbyList);
} }
ArrayList<Lobby> lobbyList = playerController.getLobbyList(); ArrayList<Lobby> lobbyList = playerController.getLobbyList();
...@@ -529,9 +524,10 @@ public class LobbyView extends View{ ...@@ -529,9 +524,10 @@ public class LobbyView extends View{
private ArrayList<PlayerAvatar> lobbyAvatars = new ArrayList<>(); private ArrayList<PlayerAvatar> lobbyAvatars = new ArrayList<>();
private void lobbyRefresh(){ private void lobbyRefresh(){
Thread thread = new Thread(){ if(playerController.getLobby() == null) {
public void run(){ System.out.println("Error during lobby refresh: not in a lobby");
while(playerController.getLobby() == null) {} }
else{
lobbyAvatars.clear(); lobbyAvatars.clear();
lobby = playerController.getLobby(); lobby = playerController.getLobby();
playersInLobby = lobby.getPlayers(); playersInLobby = lobby.getPlayers();
...@@ -547,8 +543,6 @@ public class LobbyView extends View{ ...@@ -547,8 +543,6 @@ public class LobbyView extends View{
lobbyAvatars.add(pa); lobbyAvatars.add(pa);
} }
} }
};
thread.start();
} }
private void showLobbyWindow(){ private void showLobbyWindow(){
......
...@@ -312,7 +312,7 @@ public class MenuView extends View{ ...@@ -312,7 +312,7 @@ public class MenuView extends View{
} }
} }
if(playerController.getPlayer() != null){ if(playerController.getPlayer().getID() != -1){
stage.addActor(connectionBarGreenCircle); stage.addActor(connectionBarGreenCircle);
connectionBarRedCircle.remove(); connectionBarRedCircle.remove();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment