aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pokemon
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/pokemon')
-rw-r--r--src/main/java/pokemon/Game.java65
-rw-r--r--src/main/java/pokemon/Server.java88
2 files changed, 142 insertions, 11 deletions
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);
+
+
+
}