aboutsummaryrefslogtreecommitdiff
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
parentb0cf9f4b0c4eece41cb5e3542ab8c54f6dd3d32e (diff)
Omocueno ukolko igrac izadje iz aplikacije u toku igre da se igra prekine i drugi korisnik se obavesti o tome.
-rw-r--r--pokemon.sql33
-rw-r--r--src/main/java/pokemon/Server.java42
-rw-r--r--src/main/java/ui/MainFrame.java5
3 files changed, 73 insertions, 7 deletions
diff --git a/pokemon.sql b/pokemon.sql
index 63abc1c..1cc6b14 100644
--- a/pokemon.sql
+++ b/pokemon.sql
@@ -3,7 +3,7 @@
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
--- Generation Time: Jan 02, 2023 at 04:34 PM
+-- Generation Time: Jan 04, 2023 at 05:48 PM
-- Server version: 10.4.27-MariaDB
-- PHP Version: 8.1.12
@@ -80,7 +80,23 @@ INSERT INTO `history` (`id`, `player`, `pokemon`, `time`, `result`) VALUES
(3, 5, 2, '2023-01-02', 0),
(4, 2, 1, '2023-01-02', 1),
(5, 2, 1, '2023-01-02', 1),
-(6, 4, 3, '2023-01-02', 0);
+(6, 4, 3, '2023-01-02', 0),
+(7, 5, 2, '2023-01-02', 0),
+(8, 2, 1, '2023-01-02', 1),
+(9, 7, 3, '2023-01-02', 0),
+(10, 5, 2, '2023-01-02', 1),
+(11, 2, 1, '2023-01-02', 1),
+(12, 7, 3, '2023-01-02', 0),
+(13, 5, 2, '2023-01-04', 1),
+(14, 2, 1, '2023-01-04', 0),
+(15, 2, 1, '2023-01-04', 0),
+(16, 5, 2, '2023-01-04', 1),
+(17, 7, 3, '2023-01-04', 1),
+(18, 4, 3, '2023-01-04', 0),
+(19, 12, 1, '2023-01-04', 1),
+(20, 4, 3, '2023-01-04', 0),
+(21, 12, 1, '2023-01-04', 0),
+(22, 4, 3, '2023-01-04', 1);
-- --------------------------------------------------------
@@ -128,7 +144,10 @@ INSERT INTO `user` (`id`, `username`, `password`, `admin`, `pokemonId`) VALUES
(2, 'cirakg1', '$2a$10$TJHJfYbgZO97r0CJi2eKpeEhOW8wKEO5BGXQBpR33js3R5rM98Y3W', 0, 1),
(4, 'cirakg2', '$2a$10$LQsWtrn7OqrNU8wSSfg/6uo47lzQqw27T3ubUDRqFQVZhI6F0uBli', 0, 3),
(5, 'cirakg5', '$2a$10$cO0oAPi5O4hoVQWTHuw0fOXXBYLqwJ2V5qrHy32ZjpmR9fTpNBGHy', 0, 2),
-(6, 'cirakg10', '$2a$10$IiK5l7wFvi/KPliiCpNMCOAuehXvJFofN6ARotiSN.5FuRpjke32G', 0, NULL);
+(6, 'cirakg10', '$2a$10$IiK5l7wFvi/KPliiCpNMCOAuehXvJFofN6ARotiSN.5FuRpjke32G', 0, NULL),
+(7, 'cirakg3', '$2a$10$AA9ttGxJSoikQk2CBIcCnufCiy0SlLfBB5FsNmW3PWcgVKDW9wbk6', 0, 3),
+(9, 'cirakg6', '$2a$10$V.d8RxW.scb/q3B2bGTt/e1guq1RMCZTF840eKe559KkiuC9fxmFC', 0, 3),
+(12, 'cirakg4', '$2a$10$GPbmdub8HTtTCXOnVPB8au8lAcI1jxVO1eTCDg.io32Zxsd0R/jkG', 0, 1);
--
-- Indexes for dumped tables
@@ -166,25 +185,25 @@ ALTER TABLE `user`
-- AUTO_INCREMENT for table `ability`
--
ALTER TABLE `ability`
- MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;
+ MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=17;
--
-- AUTO_INCREMENT for table `history`
--
ALTER TABLE `history`
- MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
+ MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=23;
--
-- AUTO_INCREMENT for table `monster`
--
ALTER TABLE `monster`
- MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
+ MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT for table `user`
--
ALTER TABLE `user`
- MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
+ MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
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() );
diff --git a/src/main/java/ui/MainFrame.java b/src/main/java/ui/MainFrame.java
index ef5f304..dc4b7f5 100644
--- a/src/main/java/ui/MainFrame.java
+++ b/src/main/java/ui/MainFrame.java
@@ -135,6 +135,11 @@ public class MainFrame extends JFrame implements Runnable {
String msg=response[1]+":"+response[2]+"\n";
this.gamePanel.chat+=msg;
this.gamePanel.refreshChat();
+ }else if(response[0].trim().equals("OPPONENTLEFT")){
+ System.out.println("OPPONENTLEFT");
+ this.getContentPane().removeAll();
+ this.getContentPane().add(this.chooseOpponentPanel,BorderLayout.CENTER);
+ SwingUtilities.updateComponentTreeUI(this);
}else {
//XML OBJECTS
XMLDecoder decoder = null;