aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorcirakg <ciraboxkg@gmail.com>2022-12-30 07:27:32 +0100
committercirakg <ciraboxkg@gmail.com>2022-12-30 07:27:32 +0100
commite5476a4f36551f11db0dc33e972a906adf0e0b5b (patch)
treecdc05bf1a3098f926188be0a3ae58a0399074dfa /src/main/java
parent8d493c16f4bcfcb5c9f9754c999915ad00e650dd (diff)
Omoguceno slanje zahteva za igru. Omoguceno accept i refuse zahteva(TODO ISPRAVITI).
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/models/ComboBoxUser.java6
-rw-r--r--src/main/java/models/GameStatus.java9
-rw-r--r--src/main/java/pokemon/Game.java65
-rw-r--r--src/main/java/pokemon/Server.java88
-rw-r--r--src/main/java/ui/ChooseOpponentPanel.java26
-rw-r--r--src/main/java/ui/MainFrame.java54
-rw-r--r--src/main/java/ui/OpponentConfirmation.java93
7 files changed, 330 insertions, 11 deletions
diff --git a/src/main/java/models/ComboBoxUser.java b/src/main/java/models/ComboBoxUser.java
index 562f882..91e444d 100644
--- a/src/main/java/models/ComboBoxUser.java
+++ b/src/main/java/models/ComboBoxUser.java
@@ -2,6 +2,12 @@ package models;
public class ComboBoxUser {
User user;
+ public User getUser() {
+ return user;
+ }
+ public void setUser(User user) {
+ this.user = user;
+ }
@Override
public String toString() {
return user.getUsername();
diff --git a/src/main/java/models/GameStatus.java b/src/main/java/models/GameStatus.java
new file mode 100644
index 0000000..0230b8b
--- /dev/null
+++ b/src/main/java/models/GameStatus.java
@@ -0,0 +1,9 @@
+package models;
+
+public enum GameStatus {
+ WAITING_FOR_SECOND_PLAYER,
+ PLAYING,
+ FINISHED,
+ NOT_IN_GAME,
+ RECEIVING_GAME_REQUEST
+}
diff --git a/src/main/java/pokemon/Game.java b/src/main/java/pokemon/Game.java
index fa08a1b..57af464 100644
--- a/src/main/java/pokemon/Game.java
+++ b/src/main/java/pokemon/Game.java
@@ -1,24 +1,27 @@
package pokemon;
+import java.beans.XMLEncoder;
+import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
+import models.GameStatus;
import models.MonsterViewModel;
public class Game implements Runnable, Serializable {
- boolean player1Turn;
- long player1Id;
- long player2Id;
- MonsterViewModel monster1;
- MonsterViewModel monster2;
- int currentHp1;
- int currentHp2;
- int shield1;
- int shield2;
- SocketChannel []players;
+ public boolean player1Turn;
+ public long player1Id;
+ public long player2Id;
+ public MonsterViewModel monster1;
+ public MonsterViewModel monster2;
+ public int currentHp1;
+ public int currentHp2;
+ public int shield1;
+ public int shield2;
+ GameStatus status;
public Game() {
super();
@@ -109,5 +112,47 @@ public class Game implements Runnable, Serializable {
this.shield2 = shield2;
}
+ public boolean isPlayer1Turn() {
+ return player1Turn;
+ }
+
+ public void setPlayer1Turn(boolean player1Turn) {
+ this.player1Turn = player1Turn;
+ }
+
+ public GameStatus getStatus() {
+ return status;
+ }
+
+ public void setStatus(GameStatus status) {
+ this.status = status;
+ }
+
+ @Override
+ public String toString()
+ {
+ XMLEncoder coder = null;
+ String xmlString = null;
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ try
+ {
+ coder = new XMLEncoder(baos);
+ coder.writeObject(this);
+
+ }
+ catch(Exception e)
+ {
+ System.out.println(e);
+ }
+ finally
+ {
+ coder.close();
+ }
+
+ xmlString = new String(baos.toByteArray());
+
+ return xmlString.replace("\n", " ");
+ }
}
diff --git a/src/main/java/pokemon/Server.java b/src/main/java/pokemon/Server.java
index 2e51a69..555d040 100644
--- a/src/main/java/pokemon/Server.java
+++ b/src/main/java/pokemon/Server.java
@@ -15,13 +15,14 @@ import java.util.Iterator;
import java.util.Map.Entry;
import models.CONSTS;
+import models.GameStatus;
import models.User;
import models.UserListWrapper;
public class Server implements Runnable {
private ServerSocketChannel serverSocketChannel;
private Selector selector;
- private ArrayList<Game> games;
+ private ArrayList<Game> games=new ArrayList<>();
HashMap<SocketChannel,Long > players = new HashMap<SocketChannel,Long>();
ArrayList<SocketChannel> inGame=new ArrayList<SocketChannel>();
IService s;
@@ -138,6 +139,7 @@ public class Server implements Runnable {
else {
if(!res.admin) {
message="ACCEPTED"+":"+res.id;
+ System.out.println(message);
players.put(sc,res.id);
ByteBuffer buff = ByteBuffer.wrap(message.getBytes());
sc.write(buff);
@@ -170,6 +172,90 @@ public class Server implements Runnable {
sc.write(buff);
+ }else if(msg[0].equals("SELECTOPPONENT")) {
+ long opponentId=Long.parseLong(msg[1]);
+ SocketChannel opponentSocket=null;
+ for(Entry<SocketChannel, Long> player : players.entrySet()) {
+ if(player.getValue()==opponentId) {
+ opponentSocket=player.getKey();
+ break;
+ }
+ }
+ inGame.add(sc);
+ inGame.add(opponentSocket);
+ sendAvailablePlayers();
+ User player1=s.getUserById(players.get(sc));
+ User player2=s.getUserById(opponentId);
+ System.out.println(player1);
+ Game game=new Game();
+ game.setPlayer1Id(player1.id);
+ game.setPlayer2Id(player2.id);
+ game.player1Turn=true;
+ game.status=GameStatus.WAITING_FOR_SECOND_PLAYER;
+ game.monster1=s.getMonsterViewModel(player1.monsterId);
+ game.monster2=s.getMonsterViewModel(player2.monsterId);
+ game.currentHp1=game.monster1.hp;
+ game.currentHp2=game.monster2.hp;
+ game.shield1=0;
+ game.shield2=0;
+ games.add(game);
+ System.out.println(game);
+ ByteBuffer buff = ByteBuffer.wrap(("GAMEREQUEST:"+player1.id).getBytes());
+ opponentSocket.write(buff);
+
+
+ }
+ else if(msg[0].equals("ACCEPTOPPONENT")) {
+ long opponentId=Long.parseLong(msg[1]);
+ SocketChannel opponentSocket=null;
+ for(Entry<SocketChannel, Long> player : players.entrySet()) {
+ if(player.getValue()==opponentId) {
+ opponentSocket=player.getKey();
+ break;
+ }
+ }
+ System.out.println(opponentId);
+ Game game=null;
+ for(Game tempGame :games) {
+ if(tempGame.player1Id==opponentId)
+ game=tempGame;
+ break;
+ }
+ game.status=GameStatus.PLAYING;
+ ByteBuffer buff = ByteBuffer.wrap(game.toString().getBytes());
+ sc.write(buff);
+ buff = ByteBuffer.wrap(game.toString().getBytes());
+ opponentSocket.write(buff);
+
+
+ }
+ else if(msg[0].equals("REFUSEOPPONENT")) {
+ long opponentId=Long.parseLong(msg[1]);
+ Game game=null;
+ for(Game tempGame :games) {
+ if(tempGame.player1Id==opponentId)
+ game=tempGame;
+ break;
+ }
+ SocketChannel opponentSocket=null;
+ for(Entry<SocketChannel, Long> player : players.entrySet()) {
+ if(player.getValue()==opponentId) {
+ opponentSocket=player.getKey();
+ break;
+ }
+ }
+
+ inGame.remove(sc);
+ inGame.remove(opponentSocket);
+ games.remove(game);
+ sendAvailablePlayers();
+ ByteBuffer buff = ByteBuffer.wrap("REFUSEGAME".getBytes());
+ sc.write(buff);
+ ByteBuffer buff1 = ByteBuffer.wrap("REFUSEGAME".getBytes());
+ opponentSocket.write(buff1);
+
+
+
}
diff --git a/src/main/java/ui/ChooseOpponentPanel.java b/src/main/java/ui/ChooseOpponentPanel.java
index 196477f..c318aaf 100644
--- a/src/main/java/ui/ChooseOpponentPanel.java
+++ b/src/main/java/ui/ChooseOpponentPanel.java
@@ -35,12 +35,38 @@ public class ChooseOpponentPanel extends JPanel {
setLayout(null);
btnSendGameRequest = new JButton("Send Game Request");
+
btnSendGameRequest.setBounds(134, 153, 166, 23);
add(btnSendGameRequest);
comboBox = new JComboBox();
comboBox.setBounds(134, 101, 166, 22);
add(comboBox);
+
+ btnSendGameRequest.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ if(comboBox.getItemCount()==0)
+ return;
+ User opponent=((ComboBoxUser)comboBox.getSelectedItem()).getUser();
+ String msg="SELECTOPPONENT:"+opponent.id;
+ ByteBuffer bb=ByteBuffer.wrap(msg.getBytes());
+ MainFrame topFrame=(MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, ChooseOpponentPanel.this);
+ try {
+ SocketChannel client=topFrame.getClient();
+ client.write(bb);
+ topFrame.getContentPane().removeAll();
+ topFrame.opponentConfirmation.getBtnAccept().setVisible(false);
+ topFrame.opponentConfirmation.getBtnRefuse().setVisible(false);
+ topFrame.opponentConfirmation.setOpponentId(opponent.id);
+ topFrame.opponentConfirmation.lblNewLabel.setText("Wait for opponent");
+ topFrame.getContentPane().add(topFrame.opponentConfirmation,BorderLayout.CENTER);
+ SwingUtilities.updateComponentTreeUI(topFrame);
+ } catch (Exception e1) {
+ e1.printStackTrace();
+ }
+
+ }
+ });
}
diff --git a/src/main/java/ui/MainFrame.java b/src/main/java/ui/MainFrame.java
index 2aac225..8b3e949 100644
--- a/src/main/java/ui/MainFrame.java
+++ b/src/main/java/ui/MainFrame.java
@@ -8,8 +8,10 @@ import javax.swing.border.EmptyBorder;
import models.CONSTS;
import models.ComboBoxUser;
+import models.GameStatus;
import models.User;
import models.UserListWrapper;
+import pokemon.Game;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
@@ -42,14 +44,18 @@ public class MainFrame extends JFrame implements Runnable {
public SocketChannel client;
public LoginPanel loginPanel=null;
public ChooseOpponentPanel chooseOpponentPanel=null;
+ public OpponentConfirmation opponentConfirmation=null;
GamePanel gamePanel=null;
public long userId=-1;
public boolean run=true;
+ public GameStatus status;
+ public Game game;
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 727, 429);
loginPanel=new LoginPanel();
chooseOpponentPanel=new ChooseOpponentPanel();
+ opponentConfirmation=new OpponentConfirmation();
gamePanel=new GamePanel();
getContentPane().setLayout(new BorderLayout(0, 0));
add(loginPanel,BorderLayout.CENTER);
@@ -110,6 +116,20 @@ public class MainFrame extends JFrame implements Runnable {
}else if(response[0].trim().equals("BADLOGINADMIN")){
System.out.println("Bad Login");
this.loginPanel.warningTextArea.setText("Admin nema pristup aplikaciji");
+ }else if(response[0].trim().equals("GAMEREQUEST")){
+ System.out.println("Game Request");
+ this.getContentPane().removeAll();
+ this.opponentConfirmation.getBtnAccept().setVisible(true);
+ this.opponentConfirmation.getBtnRefuse().setVisible(true);
+ this.opponentConfirmation.lblNewLabel.setText("Game Request"+"id:"+response[1].trim());
+ this.opponentConfirmation.setOpponentId(Long.parseLong(response[1].trim()));
+ this.getContentPane().add(this.opponentConfirmation,BorderLayout.CENTER);
+ SwingUtilities.updateComponentTreeUI(this);
+ }else if(response[0].trim().equals("REFUSEGAME")){
+ System.out.println("REFUSEGAME");
+ this.getContentPane().removeAll();
+ this.getContentPane().add(this.chooseOpponentPanel,BorderLayout.CENTER);
+ SwingUtilities.updateComponentTreeUI(this);
}else {
//XML OBJECTS
XMLDecoder decoder = null;
@@ -128,6 +148,16 @@ public class MainFrame extends JFrame implements Runnable {
} catch (Exception e) {
System.out.println("Nije refresh");
}
+ try {
+ game=(Game) decoder.readObject();
+ decoder.close();
+ this.getContentPane().removeAll();
+ this.getContentPane().add(this.gamePanel,BorderLayout.CENTER);
+ SwingUtilities.updateComponentTreeUI(this);
+
+ } catch (Exception e) {
+ System.out.println("Nije GAME");
+ }
}
@@ -182,5 +212,29 @@ public class MainFrame extends JFrame implements Runnable {
public void setUserId(long userId) {
this.userId = userId;
}
+ public OpponentConfirmation getOpponentConfirmation() {
+ return opponentConfirmation;
+ }
+ public void setOpponentConfirmation(OpponentConfirmation opponentConfirmation) {
+ this.opponentConfirmation = opponentConfirmation;
+ }
+ public boolean isRun() {
+ return run;
+ }
+ public void setRun(boolean run) {
+ this.run = run;
+ }
+ public GameStatus getStatus() {
+ return status;
+ }
+ public void setStatus(GameStatus status) {
+ this.status = status;
+ }
+ public Game getGame() {
+ return game;
+ }
+ public void setGame(Game game) {
+ this.game = game;
+ }
}
diff --git a/src/main/java/ui/OpponentConfirmation.java b/src/main/java/ui/OpponentConfirmation.java
new file mode 100644
index 0000000..d0c2a95
--- /dev/null
+++ b/src/main/java/ui/OpponentConfirmation.java
@@ -0,0 +1,93 @@
+package ui;
+
+import javax.swing.JPanel;
+import javax.swing.SwingUtilities;
+
+import models.ComboBoxUser;
+import models.User;
+
+import javax.swing.JLabel;
+import javax.swing.JButton;
+import java.awt.event.ActionListener;
+import java.nio.ByteBuffer;
+import java.nio.channels.SocketChannel;
+import java.awt.BorderLayout;
+import java.awt.event.ActionEvent;
+
+public class OpponentConfirmation extends JPanel {
+ public JLabel lblNewLabel;
+ public JButton btnAccept;
+ public JButton btnRefuse;
+ public long opponentId;
+
+ public OpponentConfirmation() {
+ setLayout(null);
+
+ lblNewLabel = new JLabel("New label");
+ lblNewLabel.setBounds(95, 66, 208, 98);
+ add(lblNewLabel);
+
+ btnAccept = new JButton("Accept");
+ btnAccept.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ String msg="ACCEPTOPPONENT:"+opponentId;
+ ByteBuffer bb=ByteBuffer.wrap(msg.getBytes());
+ MainFrame topFrame=(MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, OpponentConfirmation.this);
+ try {
+ SocketChannel client=topFrame.getClient();
+ client.write(bb);
+ SwingUtilities.updateComponentTreeUI(topFrame);
+ } catch (Exception e1) {
+ e1.printStackTrace();
+ }
+
+
+ }
+ });
+ btnAccept.setBounds(92, 194, 89, 23);
+ add(btnAccept);
+
+ btnRefuse = new JButton("Refuse");
+ btnRefuse.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ String msg="REFUSEOPPONENT:"+opponentId;
+ ByteBuffer bb=ByteBuffer.wrap(msg.getBytes());
+ MainFrame topFrame=(MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, OpponentConfirmation.this);
+ try {
+ SocketChannel client=topFrame.getClient();
+ client.write(bb);
+ SwingUtilities.updateComponentTreeUI(topFrame);
+ } catch (Exception e1) {
+ e1.printStackTrace();
+ }
+ }
+ });
+ btnRefuse.setBounds(214, 194, 89, 23);
+ add(btnRefuse);
+
+ }
+ public long getOpponentId() {
+ return opponentId;
+ }
+ public void setOpponentId(long opponentId) {
+ this.opponentId = opponentId;
+ }
+ public JLabel getLblNewLabel() {
+ return lblNewLabel;
+ }
+ public void setLblNewLabel(JLabel lblNewLabel) {
+ this.lblNewLabel = lblNewLabel;
+ }
+ public JButton getBtnAccept() {
+ return btnAccept;
+ }
+ public void setBtnAccept(JButton btnAccept) {
+ this.btnAccept = btnAccept;
+ }
+ public JButton getBtnRefuse() {
+ return btnRefuse;
+ }
+ public void setBtnRefuse(JButton btnRefuse) {
+ this.btnRefuse = btnRefuse;
+ }
+}