aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pokemon
diff options
context:
space:
mode:
authorcirakg <ciraboxkg@gmail.com>2023-01-02 15:32:29 +0100
committercirakg <ciraboxkg@gmail.com>2023-01-02 15:32:29 +0100
commit8d0fedf836cfd418f9c05d5c21b3c6cc1875e5c9 (patch)
tree57338e5914403e214b2eb630b7e9b102d1a9ddb4 /src/main/java/pokemon
parent743bf39206ba8d51d1c60928ea49b5daf8970444 (diff)
Omoguceno igranje igre. Prikaz rezultata po zavrsetku igre. Postavljanje korisnika da budu dostupni za nove zahteve.TODO(Popraviti progress bar,upisivanje rezultata igre u bazu, povratak korisnika da biranje protivnika,provera unosa web i client, ispit istorije meceva web)
Diffstat (limited to 'src/main/java/pokemon')
-rw-r--r--src/main/java/pokemon/Game.java124
-rw-r--r--src/main/java/pokemon/Server.java45
2 files changed, 167 insertions, 2 deletions
diff --git a/src/main/java/pokemon/Game.java b/src/main/java/pokemon/Game.java
index 57af464..a4a121e 100644
--- a/src/main/java/pokemon/Game.java
+++ b/src/main/java/pokemon/Game.java
@@ -7,6 +7,8 @@ import java.io.Serializable;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
+import models.Ability;
+import models.AbilityType;
import models.GameStatus;
import models.MonsterViewModel;
@@ -26,6 +28,128 @@ public class Game implements Runnable, Serializable {
public Game() {
super();
}
+ public void player1Move(long abilityId) {
+ Ability a=null;
+ player1Turn=false;
+ double rand=Math.random();
+ for(Ability temp :monster1.abilities) {
+ if(temp.id==abilityId) {
+ a=temp;
+ break;
+ }
+ }
+ int abilityResult=(int)(a.power*rand);
+ if(a.getType()==AbilityType.ATTACK) {
+ if(shield2>0) {
+ if(shield2<abilityResult) {
+ shield2=shield2-abilityResult;
+ }
+ else {
+ currentHp2=currentHp2+shield2-abilityResult;
+ shield2=0;
+ }
+
+ }else {
+ currentHp2=currentHp2-abilityResult;
+ }
+
+ }else if(a.getType()==AbilityType.SPECIAL) {
+ abilityResult=(int)(abilityResult*(1+Math.random()/2));
+ if(shield2>0) {
+ if(shield2<abilityResult) {
+ shield2=shield2-abilityResult;
+ }
+ else {
+ currentHp2=currentHp2+shield2-abilityResult;
+ shield2=0;
+ }
+
+ }else {
+ currentHp2=currentHp2-abilityResult;
+ }
+
+ }else if(a.getType()==AbilityType.HEAL) {
+ if(currentHp1+abilityResult>=monster1.hp) {
+ currentHp1=monster1.hp;
+ }
+ else {
+ currentHp1=currentHp1+abilityResult;
+ }
+
+ }else if(a.getType()==AbilityType.SHIELD) {
+ shield1=shield1+abilityResult;
+
+ }
+
+ }
+ public void player2Move(long abilityId) {
+ Ability a=null;
+ player1Turn=true;
+ double rand=Math.random();
+ for(Ability temp :monster2.abilities) {
+ if(temp.id==abilityId) {
+ a=temp;
+ break;
+ }
+ }
+ int abilityResult=(int)(a.power*rand);
+ if(a.getType()==AbilityType.ATTACK) {
+ if(shield1>0) {
+ if(shield1<abilityResult) {
+ shield1=shield1-abilityResult;
+ }
+ else {
+ currentHp1=currentHp1+shield1-abilityResult;
+ shield1=0;
+ }
+
+ }else {
+ currentHp1=currentHp1-abilityResult;
+ }
+
+ }else if(a.getType()==AbilityType.SPECIAL) {
+ abilityResult=(int)(abilityResult*(1+Math.random()/2));
+ if(shield1>0) {
+ if(shield1<abilityResult) {
+ shield1=shield1-abilityResult;
+ }
+ else {
+ currentHp1=currentHp1+shield1-abilityResult;
+ shield1=0;
+ }
+
+ }else {
+ currentHp1=currentHp1-abilityResult;
+ }
+
+ }else if(a.getType()==AbilityType.HEAL) {
+ if(currentHp2+abilityResult>=monster2.hp) {
+ currentHp2=monster2.hp;
+ }
+ else {
+ currentHp2=currentHp2+abilityResult;
+ }
+
+ }else if(a.getType()==AbilityType.SHIELD) {
+ shield2=shield2+abilityResult;
+
+ }
+
+ }
+ public int checkWin() {
+ int res=0;
+ if(currentHp1<=0) {
+ res=1;
+ status=GameStatus.PLAYER2WIN;
+ }
+ else if (currentHp2<=0){
+ res=1;
+ status=GameStatus.PLAYER1WIN;
+ }
+
+ return res;
+
+ }
@Override
public void run() {
diff --git a/src/main/java/pokemon/Server.java b/src/main/java/pokemon/Server.java
index 9d56a27..0c8e31e 100644
--- a/src/main/java/pokemon/Server.java
+++ b/src/main/java/pokemon/Server.java
@@ -217,9 +217,10 @@ public class Server implements Runnable {
System.out.println(opponentId);
Game game=null;
for(Game tempGame :games) {
- if(tempGame.player1Id==opponentId)
+ if(tempGame.player1Id==opponentId) {
game=tempGame;
- break;
+ break;
+ }
}
game.status=GameStatus.PLAYING;
ByteBuffer buff = ByteBuffer.wrap(game.toString().getBytes());
@@ -285,8 +286,48 @@ public class Server implements Runnable {
opponentSocket.write(buff);
ByteBuffer buff1 = ByteBuffer.wrap(myMsg.getBytes());
sc.write(buff1);
+ }else if(msg[0].equals("GAMEPLAY")) {
+ long myId=players.get(sc);
+ long moveId=Long.parseLong(msg[1]);
+
+ Game game=null;
+ for(Game tempGame :games) {
+ if(tempGame.player1Id==myId || tempGame.player2Id==myId) {
+ game=tempGame;
+ break;
+ }
+ }
+ long opponentId=-1;
+
+ if(game.player1Id==myId) {
+ opponentId=game.player2Id;
+ game.player1Move(moveId);
+ }else {
+ opponentId=game.player1Id;
+ game.player2Move(moveId);
+ }
+ SocketChannel opponentSocket=null;
+ for(Entry<SocketChannel, Long> player : players.entrySet()) {
+ if(player.getValue()==opponentId) {
+ opponentSocket=player.getKey();
+ break;
+ }
+ }
+ int gameRes=game.checkWin();
+ ByteBuffer buff = ByteBuffer.wrap(game.toString().getBytes());
+ opponentSocket.write(buff);
+ ByteBuffer buff1 = ByteBuffer.wrap(game.toString().getBytes());
+ sc.write(buff1);
+
+ if(gameRes==1) {
+ inGame.remove(sc);
+ inGame.remove(opponentSocket);
+ games.remove(game);
+ System.out.println("GAME FINISHED"+games.size());
+ //add game to database
+ }
}