aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pokemon
diff options
context:
space:
mode:
authorcirakg <ciraboxkg@gmail.com>2023-01-04 17:49:53 +0100
committercirakg <ciraboxkg@gmail.com>2023-01-04 17:49:53 +0100
commitffcda75a9b417cdc8b6c1c3f7046edb62b9c30a5 (patch)
tree43fc832ac199b9b79792d88b0082c3e2e5936a47 /src/main/java/pokemon
parentb0cf9f4b0c4eece41cb5e3542ab8c54f6dd3d32e (diff)
Omocueno ukolko igrac izadje iz aplikacije u toku igre da se igra prekine i drugi korisnik se obavesti o tome.
Diffstat (limited to 'src/main/java/pokemon')
-rw-r--r--src/main/java/pokemon/Server.java42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/main/java/pokemon/Server.java b/src/main/java/pokemon/Server.java
index 77b7fad..09ced57 100644
--- a/src/main/java/pokemon/Server.java
+++ b/src/main/java/pokemon/Server.java
@@ -89,6 +89,46 @@ public class Server implements Runnable {
}
}
+ private void leftGame(SocketChannel sc)throws Exception {
+ long id=players.get(sc);
+ Game game=null;
+ for(Game tempGame :games) {
+ if(tempGame.player1Id==id || tempGame.player2Id==id) {
+ game=tempGame;
+ break;
+ }
+ }
+ if(game==null)
+ return;
+ long notifyId=-1;
+ if(game.player1Id==id)
+ notifyId=game.player2Id;
+ else if(game.player2Id==id)
+ notifyId=game.player1Id;
+
+ if(notifyId==-1)
+ return;
+
+ SocketChannel notifySocket=null;
+ for(Entry<SocketChannel, Long> player : players.entrySet()) {
+ if(player.getValue()==notifyId) {
+ notifySocket=player.getKey();
+ break;
+ }
+ }
+ if(notifySocket==null)
+ return;
+
+ inGame.remove(sc);
+ inGame.remove(notifySocket);
+ games.remove(game);
+ ByteBuffer buff = ByteBuffer.wrap("OPPONENTLEFT".getBytes());
+ notifySocket.write(buff);
+
+
+
+
+ }
private void readMessage(SelectionKey key)throws Exception {
SocketChannel sc = (SocketChannel) key.channel();
StringBuilder sb = new StringBuilder();
@@ -105,6 +145,7 @@ public class Server implements Runnable {
} catch (Exception e) {
Long closedId=players.get(sc);
+ leftGame(sc);
players.remove(sc);
System.out.println("Client dissconnected: "+closedId );
System.out.println("Remaining clients: "+players.size() );
@@ -115,6 +156,7 @@ public class Server implements Runnable {
if(read==-1)
{
Long closedId=players.get(sc);
+ leftGame(sc);
players.remove(sc);
System.out.println("Client dissconnected: "+closedId );
System.out.println("Remaining clients: "+players.size() );