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

added notification feature to commants

parent 2b8d40c4
No related branches found
No related tags found
2 merge requests!8Dev,!6added notification feature to commants
Showing
with 98 additions and 49 deletions
......@@ -26,7 +26,8 @@ public class NetworkController {
this.client = new Client();
client.start();
try {
String IP4_LAN_ADDRESS = "192.168.87.34";
//192.168.87.34
String IP4_LAN_ADDRESS = "192.168.10.154";
client.connect(10000, IP4_LAN_ADDRESS, 54555, 54777);
} catch (IOException e) {
e.printStackTrace();
......
......@@ -27,7 +27,7 @@ public class PlayerController {
}
}
});
net.sendToServer(new cPlayerJoin(0));
connectPlayer(0);
}
public void issueACommand(String commandString) {
......@@ -37,14 +37,18 @@ public class PlayerController {
}
}
public void joinLobby(int id){ net.sendToServer(new cLobbyJoin(id)); }
public void lobbyGetList(){ net.sendToServer(new cLobbyGetList()); }
public void connectPlayer(int avatarIndex){ net.sendToServer(new cPlayerJoin(avatarIndex));}
public void createLobby(String name, int MAX_PLAYERS){ net.sendToServer(new cLobbyCreate(name, MAX_PLAYERS)); }
public void deleteLobby(int id){ net.sendToServer(new cLobbyDelete(id)); }
public void joinLobby(int id){ net.sendToServer(new cLobbyJoin(id)); }
public void leaveLobby(int id) { net.sendToServer(new cLobbyLeave(id)); }
public Lobby getLobby(){ return lobby; }
public ArrayList<Lobby> getLobbyList(){ return lobbies; }
......@@ -59,7 +63,5 @@ public class PlayerController {
public PlayerController getPlayerController(){ return this; }
public void leaveLobby(int id) { net.sendToServer(new cLobbyLeave(id)); }
public NetworkController getNetWorkController() { return net; }
}
......@@ -12,13 +12,23 @@ public class cLobbyLeave extends Command{
@Override
public void execute(PlayerController playerController, Connection connection){
if(data instanceof Integer){
int result = (int) data;
if (result == -1) System.out.println("Failed to leave lobby");
if(data instanceof Lobby){
Lobby lobby = (Lobby) data;
if(lobby.getID() != -1){
if (lobby.getPlayersID().contains((Integer) connection.getID())) {
//Somebody else left
System.out.println("Somebody left the lobby");
playerController.setLobby(lobby);
}
else{
//The current player left
playerController.setLobby(new Lobby(-1));
System.out.println("Request to leave lobby successful");
}
}
else{
System.out.println("Received leaveLobby command with an error");
}
}
}
}
package com.mygdx.game.model;
import java.util.ArrayList;
import java.util.HashSet;
public class Lobby {
......@@ -41,6 +42,14 @@ public class Lobby {
return players;
}
public HashSet<Integer> getPlayersID(){
HashSet set = new HashSet();
for(Player player : players){
set.add((Integer) player.getID());
}
return set;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
......
No preview for this file type
<component name="libraryTable">
<library name="Gradle: CheckersServer.desktop.desktop-1.0">
<CLASSES>
<root url="jar://$PROJECT_DIR$/desktop/build/libs/desktop-1.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
......@@ -10,6 +10,7 @@ import com.mygdx.game.model.Player;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
public class NetworkController {
......@@ -83,32 +84,44 @@ public class NetworkController {
}
public Lobby joinLobby(int playerID, int lobbyID){
int lobby_idx = lobbies.indexOf(new Lobby(lobbyID));
int player_idx = players.indexOf(new Player(playerID));
if (lobby_idx != -1 && player_idx != -1) {
Lobby lobby = lobbies.get(lobby_idx);
Player player = players.get(player_idx);
Lobby lobby = getLobby(lobbyID);
Player player = getPlayer(playerID);
if (lobby.getID() != -1 && player.getID() != -1) {
if(lobby.playerJoin(player) != -1){
lobbies.set(lobby_idx, lobby);
lobbies.set(lobbies.indexOf(lobby), lobby);
return lobby;
}
}
return new Lobby(-1);
}
public int leaveLobby(int playerID, int lobbyID){
int lobby_idx = lobbies.indexOf(new Lobby(lobbyID));
int player_idx = players.indexOf(new Player(playerID));
if (lobby_idx != -1 && player_idx != -1) {
Lobby lobby = lobbies.get(lobby_idx);
Player player = players.get(player_idx);
public Lobby leaveLobby(int playerID, int lobbyID){
Lobby lobby = getLobby(lobbyID);
Player player = getPlayer(playerID);
if (lobby.getID() != -1 && player.getID() != -1) {
if(lobby.playerKick(player) != -1){
if(lobby.getPlayersCount() == 0) deleteLobby(lobby_idx);
else lobbies.set(lobby_idx, lobby);
return 0;
if(lobby.getPlayersCount() == 0) deleteLobby(lobbyID);
else lobbies.set(lobbies.indexOf(lobby), lobby);
return lobby;
}
}
return new Lobby(-1);
}
public Lobby getLobby(int lobbyID){
int lobby_idx = lobbies.indexOf(new Lobby(lobbyID));
if (lobby_idx != -1){
return lobbies.get(lobby_idx);
}
return new Lobby(-1);
}
return -1;
public Player getPlayer(int playerID){
int player_idx = players.indexOf(new Player(playerID));
if (player_idx != -1){
return players.get(player_idx);
}
return new Player(-1);
}
public ArrayList<Lobby> getLobbies(){ return lobbies; }
......@@ -129,6 +142,13 @@ public class NetworkController {
public NetworkController getNetworkController() { return this; }
public Connection[] getConnections(){ return server.getConnections(); }
public ArrayList<Connection> getConnections(Lobby lobby){
HashSet<Integer> players_id = lobby.getPlayersID();
ArrayList<Connection> connections = new ArrayList<Connection>();
for (Connection c : server.getConnections()) {
if (players_id.contains((Integer) c.getID())) connections.add(c);
}
return connections;
}
}
......@@ -14,23 +14,14 @@ public class cLobbyDelete extends Command{
public void execute(NetworkController net, Connection connection){
if(data instanceof Integer) {
int lobby_id = (int) data;
int lobby_idx = net.getLobbies().indexOf(new Lobby(lobby_id));
if (lobby_idx != -1) {
Lobby lobby = net.getLobby(lobby_id);
//Notify all players in the lobby
Lobby lobby = net.getLobbies().get(lobby_idx);
HashSet<Integer> players_id = lobby.getPlayersID();
data = (Integer) net.deleteLobby(lobby_id);
for (Connection c : net.getConnections()) {
if (players_id.contains((Integer) c.getID())) c.sendTCP(this);
}
}
else {
//Notify the sender only
data = (Integer) net.deleteLobby(lobby_id);
connection.sendTCP(this);
}
System.out.printf("Request from Player w. ID: %d to delete Lobby w. ID: %d. Returning code: %d \n",
connection.getID(), lobby_id, ((Integer) data));
for (Connection c : net.getConnections(lobby)) {
c.sendTCP(this);
}
}
}
}
......@@ -17,7 +17,9 @@ public class cLobbyJoin extends Command{
data = net.joinLobby(connection.getID(), lobby_id);
System.out.printf("Request from Player w. ID: %d to join Lobby w. ID: %d. Returning Lobby w. ID: %d \n",
connection.getID(), lobby_id, ((Lobby)data).getID());
connection.sendTCP(this);
for (Connection c : net.getConnections(net.getLobby(lobby_id))) {
c.sendTCP(this);
}
}
}
}
......@@ -4,6 +4,7 @@ import com.esotericsoftware.kryonet.Connection;
import com.mygdx.game.controller.NetworkController;
import com.mygdx.game.model.Lobby;
import java.util.ArrayList;
import java.util.HashSet;
public class cLobbyLeave extends Command{
......@@ -14,10 +15,14 @@ public class cLobbyLeave extends Command{
public void execute(NetworkController net, Connection connection){
if(data instanceof Integer) {
int lobby_id = (int) data;
data = (Integer) net.leaveLobby(connection.getID(),lobby_id);
System.out.printf("Request from Player w. ID: %d to leave Lobby w. ID: %d. Returning code: %d \n",
connection.getID(), lobby_id, ((Integer)data));
connection.sendTCP(this);
Lobby lobby = net.getLobby(lobby_id);
ArrayList<Connection> connections = net.getConnections(lobby);
data = net.leaveLobby(connection.getID(),lobby_id);
System.out.printf("Request from Player w. ID: %d to leave Lobby w. ID: %d. Returning Lobby w. ID: %d \n",
connection.getID(), lobby_id, ((Lobby)data).getID());
for (Connection c : connections) {
c.sendTCP(this);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment