|
|
@ -34,6 +34,7 @@ public class ServerMain extends SimpleApplication{ |
|
|
|
int clientIDMax = 0; |
|
|
|
int clientIDMax = 0; |
|
|
|
public static HashMap<String,Instance> instances = new HashMap<>(); |
|
|
|
public static HashMap<String,Instance> instances = new HashMap<>(); |
|
|
|
public static HashMap<Integer,Instance> players = new HashMap<>(); //The last instance location this player was seen in.
|
|
|
|
public static HashMap<Integer,Instance> players = new HashMap<>(); //The last instance location this player was seen in.
|
|
|
|
|
|
|
|
public static int INSTANCE_UNIQUE_ID = 0; |
|
|
|
|
|
|
|
|
|
|
|
ServerMain() { |
|
|
|
ServerMain() { |
|
|
|
try { |
|
|
|
try { |
|
|
@ -51,7 +52,9 @@ public class ServerMain extends SimpleApplication{ |
|
|
|
if (players.containsKey(conn.getId())) { |
|
|
|
if (players.containsKey(conn.getId())) { |
|
|
|
Instance i = players.get(conn.getId()); |
|
|
|
Instance i = players.get(conn.getId()); |
|
|
|
i.removePlayer(conn); |
|
|
|
i.removePlayer(conn); |
|
|
|
|
|
|
|
server.broadcast(Filters.in(i.clients),new PlayerLeaveMessage(i.levelName,conn.getId())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
players.remove(conn.getId()); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
@ -61,6 +64,7 @@ public class ServerMain extends SimpleApplication{ |
|
|
|
Serializer.registerClass(JoinMessage.class); |
|
|
|
Serializer.registerClass(JoinMessage.class); |
|
|
|
Serializer.registerClass(SyncLevelMessage.class); |
|
|
|
Serializer.registerClass(SyncLevelMessage.class); |
|
|
|
Serializer.registerClass(Entity.class); |
|
|
|
Serializer.registerClass(Entity.class); |
|
|
|
|
|
|
|
Serializer.registerClass(PlayerLeaveMessage.class); |
|
|
|
//Serializer.registerClass(PlayerJoinMessage.class);
|
|
|
|
//Serializer.registerClass(PlayerJoinMessage.class);
|
|
|
|
Serializer.registerClass(PlayerActionMessage.class); |
|
|
|
Serializer.registerClass(PlayerActionMessage.class); |
|
|
|
|
|
|
|
|
|
|
@ -71,6 +75,7 @@ public class ServerMain extends SimpleApplication{ |
|
|
|
server.addMessageListener(new ServerListener(), SyncLevelMessage.class); |
|
|
|
server.addMessageListener(new ServerListener(), SyncLevelMessage.class); |
|
|
|
//server.addMessageListener(new ServerListener(), PlayerJoinMessage.class);
|
|
|
|
//server.addMessageListener(new ServerListener(), PlayerJoinMessage.class);
|
|
|
|
server.addMessageListener(new ServerListener(), PlayerActionMessage.class); |
|
|
|
server.addMessageListener(new ServerListener(), PlayerActionMessage.class); |
|
|
|
|
|
|
|
server.addMessageListener(new ServerListener(), PlayerLeaveMessage.class); |
|
|
|
|
|
|
|
|
|
|
|
server.start(); |
|
|
|
server.start(); |
|
|
|
} catch (IOException ex) { |
|
|
|
} catch (IOException ex) { |
|
|
@ -123,8 +128,11 @@ public class ServerMain extends SimpleApplication{ |
|
|
|
} else |
|
|
|
} else |
|
|
|
if (message instanceof PlayerPositionMessage) { |
|
|
|
if (message instanceof PlayerPositionMessage) { |
|
|
|
System.out.println("Position update for client "+source.getId()+". Broadcasting to others."); |
|
|
|
System.out.println("Position update for client "+source.getId()+". Broadcasting to others."); |
|
|
|
server.broadcast(Filters.notEqualTo(source), message); |
|
|
|
if (players.containsKey(source.getId())) { |
|
|
|
server.broadcast(Filters.in(source), new ServerMessage("Sent an update to other clients!")); |
|
|
|
Instance i = players.get(source.getId()); |
|
|
|
|
|
|
|
SendToAllButMe(i, source, message); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//server.broadcast(Filters.in(source), new ServerMessage("Sent an update to other clients!"));
|
|
|
|
} else |
|
|
|
} else |
|
|
|
if (message instanceof JoinMessage) { |
|
|
|
if (message instanceof JoinMessage) { |
|
|
|
JoinMessage msg = (JoinMessage)message; |
|
|
|
JoinMessage msg = (JoinMessage)message; |
|
|
@ -139,6 +147,7 @@ public class ServerMain extends SimpleApplication{ |
|
|
|
instance = new Instance(msg.levelName); |
|
|
|
instance = new Instance(msg.levelName); |
|
|
|
System.out.println("Instance "+msg.levelName+" does not exist. Creating... "+instance); |
|
|
|
System.out.println("Instance "+msg.levelName+" does not exist. Creating... "+instance); |
|
|
|
CreateTestObj(instance); |
|
|
|
CreateTestObj(instance); |
|
|
|
|
|
|
|
instance.INSTANCE_ID=INSTANCE_UNIQUE_ID++; |
|
|
|
System.out.println(instance); |
|
|
|
System.out.println(instance); |
|
|
|
instances.put(msg.levelName, instance); |
|
|
|
instances.put(msg.levelName, instance); |
|
|
|
} |
|
|
|
} |
|
|
@ -146,15 +155,15 @@ public class ServerMain extends SimpleApplication{ |
|
|
|
instance.addPlayer(source); |
|
|
|
instance.addPlayer(source); |
|
|
|
SyncLevelMessage sync = new SyncLevelMessage(instance.getEntities(),instance.getPlayers(),instance.getPlayerPositions()); |
|
|
|
SyncLevelMessage sync = new SyncLevelMessage(instance.getEntities(),instance.getPlayers(),instance.getPlayerPositions()); |
|
|
|
server.broadcast(Filters.in(source),sync); |
|
|
|
server.broadcast(Filters.in(source),sync); |
|
|
|
server.broadcast(Filters.notEqualTo(source),message); |
|
|
|
SendToAllButMe(instance, source, message); |
|
|
|
} else |
|
|
|
} else |
|
|
|
if (message instanceof PlayerActionMessage) { |
|
|
|
if (message instanceof PlayerActionMessage) { |
|
|
|
System.out.println("Received player action message: "+message); |
|
|
|
System.out.println("Received player action message: "+message); |
|
|
|
server.broadcast(Filters.notEqualTo(source), message); |
|
|
|
|
|
|
|
PlayerActionMessage msg = (PlayerActionMessage)message; |
|
|
|
PlayerActionMessage msg = (PlayerActionMessage)message; |
|
|
|
if (players.containsKey(source.getId())) { |
|
|
|
if (players.containsKey(source.getId())) { |
|
|
|
Instance i = players.get(source.getId()); |
|
|
|
Instance i = players.get(source.getId()); |
|
|
|
i.updatePosition(source.getId(),msg.getPosition()); |
|
|
|
i.updatePosition(source.getId(),msg.getPosition()); |
|
|
|
|
|
|
|
SendToAllButMe(i, source, message); |
|
|
|
} |
|
|
|
} |
|
|
|
} /*else |
|
|
|
} /*else |
|
|
|
if (message instanceof PlayerJoinMessage) { |
|
|
|
if (message instanceof PlayerJoinMessage) { |
|
|
@ -162,6 +171,14 @@ public class ServerMain extends SimpleApplication{ |
|
|
|
server.broadcast(Filters.notEqualTo(source), message); |
|
|
|
server.broadcast(Filters.notEqualTo(source), message); |
|
|
|
}*/ |
|
|
|
}*/ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void SendToAllButMe(Instance i, HostedConnection source, Message message) { |
|
|
|
|
|
|
|
for (HostedConnection conn : i.clients) { |
|
|
|
|
|
|
|
if (conn!=source) { |
|
|
|
|
|
|
|
server.broadcast(Filters.in(conn),message); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void CreateTestObj(Instance instance) { |
|
|
|
public void CreateTestObj(Instance instance) { |
|
|
@ -248,6 +265,28 @@ public class ServerMain extends SimpleApplication{ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@Serializable |
|
|
|
@Serializable |
|
|
|
|
|
|
|
public static class PlayerLeaveMessage extends AbstractMessage { |
|
|
|
|
|
|
|
String levelName; |
|
|
|
|
|
|
|
int id; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public PlayerLeaveMessage() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public PlayerLeaveMessage(String levelName, int id) { |
|
|
|
|
|
|
|
this.levelName = levelName; |
|
|
|
|
|
|
|
this.id=id; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public String toString() { |
|
|
|
|
|
|
|
return "Client ID "+id+" left. In Instance "+levelName.toString(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int getId() { |
|
|
|
|
|
|
|
return id; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Serializable |
|
|
|
public static class SyncLevelMessage extends AbstractMessage { |
|
|
|
public static class SyncLevelMessage extends AbstractMessage { |
|
|
|
Entity[] entities; |
|
|
|
Entity[] entities; |
|
|
|
Integer[] clients; |
|
|
|
Integer[] clients; |
|
|
|