diff options
author | cirakg <ciraboxkg@gmail.com> | 2023-01-02 14:33:47 +0100 |
---|---|---|
committer | cirakg <ciraboxkg@gmail.com> | 2023-01-02 14:33:47 +0100 |
commit | 743bf39206ba8d51d1c60928ea49b5daf8970444 (patch) | |
tree | 5a455e75ba0c774bdddbc6ec71851a3e98c98ee9 /src/main/java | |
parent | d0df0ed9a37ecd9288997ff0e1a68e7f37b52a47 (diff) |
Implementiran chat izmedju korisnika.
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/pokemon/Server.java | 35 | ||||
-rw-r--r-- | src/main/java/ui/GamePanel.java | 72 | ||||
-rw-r--r-- | src/main/java/ui/MainFrame.java | 6 |
3 files changed, 111 insertions, 2 deletions
diff --git a/src/main/java/pokemon/Server.java b/src/main/java/pokemon/Server.java index 6a555f2..9d56a27 100644 --- a/src/main/java/pokemon/Server.java +++ b/src/main/java/pokemon/Server.java @@ -234,9 +234,10 @@ public class Server implements Runnable { long opponentId=Long.parseLong(msg[1]); Game game=null; for(Game tempGame :games) { - if(tempGame.player1Id==opponentId) + if(tempGame.player1Id==opponentId) { game=tempGame; - break; + break; + } } SocketChannel opponentSocket=null; for(Entry<SocketChannel, Long> player : players.entrySet()) { @@ -258,6 +259,36 @@ public class Server implements Runnable { } + else if(msg[0].equals("CHATMESSAGE")) { + long myId=players.get(sc); + long opponentId=Long.parseLong(msg[1]); + User myUser=s.getUserById(myId); + User opponentUser=s.getUserById(opponentId); + + String message=msg[2]; + Game game=null; + for(Game tempGame :games) { + if(tempGame.player1Id==opponentId || tempGame.player2Id==opponentId) { + game=tempGame; + break; + } + } + SocketChannel opponentSocket=null; + for(Entry<SocketChannel, Long> player : players.entrySet()) { + if(player.getValue()==opponentId) { + opponentSocket=player.getKey(); + break; + } + } + String myMsg="MESSAGE:"+myUser.username+":"+message; + ByteBuffer buff = ByteBuffer.wrap(myMsg.getBytes()); + opponentSocket.write(buff); + ByteBuffer buff1 = ByteBuffer.wrap(myMsg.getBytes()); + sc.write(buff1); + + + + } } diff --git a/src/main/java/ui/GamePanel.java b/src/main/java/ui/GamePanel.java index b29597c..711ed48 100644 --- a/src/main/java/ui/GamePanel.java +++ b/src/main/java/ui/GamePanel.java @@ -5,6 +5,8 @@ import javax.swing.JPanel; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.ByteBuffer; import java.util.Base64; import javax.imageio.ImageIO; @@ -24,6 +26,8 @@ import models.MonsterViewModel; import models.User; import javax.swing.JTextArea; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; public class GamePanel extends JPanel { public JTextField textFieldChat; @@ -42,6 +46,8 @@ public class GamePanel extends JPanel { public JButton btnSendText; public JScrollPane scrollPane; public JTextArea txtAreaChat; + public long opponentId=-1; + public String chat=""; /** * Create the panel. @@ -50,18 +56,44 @@ public class GamePanel extends JPanel { setLayout(null); btnAttack = new JButton("Attack"); + btnAttack.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + + } + }); btnAttack.setBounds(0, 239, 130, 30); add(btnAttack); btnSpecial = new JButton("Special"); + btnSpecial.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + + } + }); btnSpecial.setBounds(130, 239, 130, 30); add(btnSpecial); btnHeal = new JButton("Heal"); + btnHeal.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + + + } + }); btnHeal.setBounds(0, 272, 130, 30); add(btnHeal); btnShield = new JButton("Shield"); + btnShield.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + + + } + }); btnShield.setBounds(130, 272, 130, 30); add(btnShield); @@ -103,6 +135,13 @@ public class GamePanel extends JPanel { textFieldChat.setColumns(10); btnSendText = new JButton("Send"); + btnSendText.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + sendChat(); + + + } + }); btnSendText.setBounds(585, 266, 89, 23); add(btnSendText); @@ -119,6 +158,21 @@ public class GamePanel extends JPanel { scrollPane.setViewportView(txtAreaChat); } + public void sendChat() { + String message=textFieldChat.getText(); + MainFrame topFrame=(MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, GamePanel.this); + String sendMessage="CHATMESSAGE:"+opponentId+":"+message; + ByteBuffer bb=ByteBuffer.wrap(sendMessage.getBytes()); + try { + topFrame.client.write(bb); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + public void refreshChat() { + txtAreaChat.setText(chat); + } public void loadElements() { MainFrame topFrame=(MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, GamePanel.this); long you=-1; @@ -134,6 +188,7 @@ public class GamePanel extends JPanel { if(topFrame.userId==topFrame.game.player1Id) { you=topFrame.game.player1Id; enemy=topFrame.game.player2Id; + opponentId=enemy; yourMonster=topFrame.game.monster1; enemyMonster=topFrame.game.monster2; yourHp=topFrame.game.currentHp1; @@ -142,14 +197,23 @@ public class GamePanel extends JPanel { enemyShield=topFrame.game.shield2; if(topFrame.game.player1Turn) { yourTurn=true; + btnAttack.setEnabled(true); + btnSpecial.setEnabled(true); + btnHeal.setEnabled(true); + btnShield.setEnabled(true); }else { yourTurn=false; + btnAttack.setEnabled(false); + btnSpecial.setEnabled(false); + btnHeal.setEnabled(false); + btnShield.setEnabled(false); } }else if(topFrame.userId==topFrame.game.player2Id) { you=topFrame.game.player2Id; enemy=topFrame.game.player1Id; + opponentId=enemy; yourMonster=topFrame.game.monster2; enemyMonster=topFrame.game.monster1; yourHp=topFrame.game.currentHp2; @@ -158,9 +222,17 @@ public class GamePanel extends JPanel { enemyShield=topFrame.game.shield1; if(topFrame.game.player1Turn) { yourTurn=false; + btnAttack.setEnabled(false); + btnSpecial.setEnabled(false); + btnHeal.setEnabled(false); + btnShield.setEnabled(false); }else { yourTurn=true; + btnAttack.setEnabled(true); + btnSpecial.setEnabled(true); + btnHeal.setEnabled(true); + btnShield.setEnabled(true); } } diff --git a/src/main/java/ui/MainFrame.java b/src/main/java/ui/MainFrame.java index 85b2175..ef5f304 100644 --- a/src/main/java/ui/MainFrame.java +++ b/src/main/java/ui/MainFrame.java @@ -53,6 +53,7 @@ public class MainFrame extends JFrame implements Runnable { public MainFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 727, 429); + setTitle("IMIPOKEMON 90/2019"); loginPanel=new LoginPanel(); chooseOpponentPanel=new ChooseOpponentPanel(); opponentConfirmation=new OpponentConfirmation(); @@ -130,6 +131,10 @@ public class MainFrame extends JFrame implements Runnable { this.getContentPane().removeAll(); this.getContentPane().add(this.chooseOpponentPanel,BorderLayout.CENTER); SwingUtilities.updateComponentTreeUI(this); + }else if(response[0].trim().equals("MESSAGE")){ + String msg=response[1]+":"+response[2]+"\n"; + this.gamePanel.chat+=msg; + this.gamePanel.refreshChat(); }else { //XML OBJECTS XMLDecoder decoder = null; @@ -155,6 +160,7 @@ public class MainFrame extends JFrame implements Runnable { this.game=game; this.gamePanel.loadElements(); this.gamePanel.txtAreaChat.setText(null);//clear chat between games + this.gamePanel.chat=""; } |