Skip to content

Commit 72d6833

Browse files
authored
Merge pull request #10 from LittleBeasts/LIT-114
Lit 114 Rip Threads out of CE
2 parents 79c03b8 + 0e3fd2a commit 72d6833

7 files changed

Lines changed: 101 additions & 80 deletions

File tree

.github/workflows/maven.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
uses: christian-draeger/increment-semantic-version@1.0.2
2626
with:
2727
current-version: '1.0.1-alpha1'
28-
version-fragment: 'bug'
28+
version-fragment: 'major'
2929
- name: Upload Artifacts
3030
run: mkdir builds && cp target/*.jar builds/calculationEngine-${{ steps.bump_version.outputs.next-version }}.jar
3131
- uses: actions/upload-artifact@v2

src/main/java/calculationEngine/CeExecuterService.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package calculationEngine;
22

3-
import java.util.concurrent.ExecutorService;
4-
import java.util.concurrent.Executors;
5-
import java.util.concurrent.TimeUnit;
3+
import config.BattleConstants;
4+
5+
import java.util.concurrent.*;
66

77
public class CeExecuterService {
88

9-
private static final ExecutorService executorService = Executors.newFixedThreadPool(4);
9+
private static final ThreadPoolExecutor executorService = (ThreadPoolExecutor) Executors.newFixedThreadPool(4);
10+
private static final boolean debug = BattleConstants.battleDebug;
1011

1112
public static void addThreadToExecutor(Runnable runnable){
1213
executorService.execute(runnable);
14+
if(debug) System.out.println("[CE Executor Service] Active Threads: " + executorService.getActiveCount());
1315
}
1416

1517
public static void shutdownExecutor(){
1618
executorService.shutdown();
17-
System.out.println("Shut Down Executor");
19+
if(debug) System.out.println("[CE Executor Service] Active Threads: " + executorService.getActiveCount());
20+
if(debug) System.out.println("[CE Executor Service]: Shut Down Executor");
1821
try {
1922
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
2023
} catch (InterruptedException e) {

src/main/java/calculationEngine/battle/CeBattle.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ public class CeBattle implements Runnable {
1515
private CeEntity selectedFightEntityPlayer2;
1616
private final CePlayer cePlayer1;
1717
private final CePlayer cePlayer2;
18+
private CeAi cePlayer2Ai;
1819
private boolean turnPlayer1;
1920
private boolean turnPlayer2;
2021
private boolean fightOngoing = true;
2122
private boolean threadSleep;
23+
private static final boolean debug = BattleConstants.battleDebug;
2224
// private boolean onServer = false; // Maybe we need this for Server specific Logic
2325

2426

2527
public CeBattle(CePlayer cePlayer1, CePlayer cePlayer2) {
26-
// System.out.println("Running main constructor");
28+
if (debug) System.out.println("[Battle Main Thread]: Running main constructor");
2729
this.selectedFightEntityPlayer1 = cePlayer1.getTeam().get(cePlayer1.getActiveMonsterIndex());
2830
this.selectedFightEntityPlayer1.setPlayerNumber(1);
2931
this.selectedFightEntityPlayer2 = cePlayer2.getTeam().get(cePlayer2.getActiveMonsterIndex());
@@ -37,9 +39,8 @@ public CeBattle(CePlayer cePlayer1, CePlayer cePlayer2) {
3739

3840
public CeBattle(CePlayer cePlayer1, CeAi cePlayer2) {
3941
this(cePlayer1, (CePlayer) cePlayer2);
40-
// System.out.println("Constructor2");
42+
this.cePlayer2Ai = cePlayer2;
4143
cePlayer2.setBattle(this);
42-
CeExecuterService.addThreadToExecutor(cePlayer2);
4344
}
4445

4546
public boolean isFightOngoing() {
@@ -51,31 +52,33 @@ public void run() {
5152
final int maxTickAmount = BattleConstants.tickAmount;
5253
int tickAmountPlayer1 = maxTickAmount;
5354
int tickAmountPlayer2 = maxTickAmount;
54-
// System.out.println("Battle Thread Started!");
55+
if (debug) System.out.println("[Battle Main Thread]: Battle Thread Started!");
5556
while (isFightOngoing()) {
56-
// System.out.println("[THREAD] " + this.fightOngoing);
57+
if (debug) System.out.println("[Battle Main Thread]: is fightOngoing: " + this.fightOngoing);
5758
tickAmountPlayer1 -= selectedFightEntityPlayer1.getCeStats().getSpeed();
5859
tickAmountPlayer2 -= selectedFightEntityPlayer2.getCeStats().getSpeed();
5960
if (tickAmountPlayer1 <= 0) {
60-
// System.out.println("HP of EP1: " + selectedFightEntityPlayer1.getCeStats().getCurrentHitPoints());
61-
// System.out.println("[THREAD]: PLAYER 1 TURN");
61+
if (debug) System.out.println("[Battle Main Thread]: HP of EP1: " + selectedFightEntityPlayer1.getCeStats().getCurrentHitPoints());
62+
if (debug) System.out.println("[Battle Main Thread]: PLAYER 1 TURN");
63+
if (debug) System.out.println("[Battle Main Thread]: HP of EP2: " + selectedFightEntityPlayer2.getCeStats().getCurrentHitPoints());
6264
turnPlayer1 = true;
63-
// System.out.println("HP of EP2: " + selectedFightEntityPlayer2.getCeStats().getCurrentHitPoints());
6465
tickAmountPlayer1 = maxTickAmount + tickAmountPlayer1;
6566
threadSleep();
6667
}
6768
if (isFightOngoing() && tickAmountPlayer2 <= 0) {
68-
// System.out.println("HP of EP2: " + selectedFightEntityPlayer2.getCeStats().getCurrentHitPoints());
69-
// System.out.println("[THREAD]: PLAYER 2 TURN");
69+
if (debug) System.out.println("[Battle Main Thread]: HP of EP2: " + selectedFightEntityPlayer2.getCeStats().getCurrentHitPoints());
70+
if (debug) System.out.println("[Battle Main Thread]: PLAYER 2 TURN");
71+
if (debug) System.out.println("[Battle Main Thread]: cePlayer2 is AI: " + cePlayer2.isAI());
72+
if (debug) System.out.println("[Battle Main Thread]: HP of EP1: " + selectedFightEntityPlayer1.getCeStats().getCurrentHitPoints());
7073
turnPlayer2 = true;
71-
// System.out.println("HP of EP1: " + selectedFightEntityPlayer1.getCeStats().getCurrentHitPoints());
7274
tickAmountPlayer2 = maxTickAmount + tickAmountPlayer2;
73-
threadSleep();
75+
if(this.cePlayer2.isAI()) this.cePlayer2Ai.useAttack();
76+
else threadSleep();
7477
}
7578
}
76-
// System.out.println("Battle Thread Ended");
7779
turnPlayer1 = false;
7880
turnPlayer2 = false;
81+
if (debug) System.out.println("[Battle Main Thread]: Battle Thread Ended");
7982
}
8083

8184
public void setSelectedFightEntityPlayer1(CeEntity entity) {
@@ -86,10 +89,15 @@ public void setSelectedFightEntityPlayer2(CeEntity entity) {
8689
this.selectedFightEntityPlayer2 = entity;
8790
}
8891

92+
public void endBatte(){
93+
setBattleEnd();
94+
setActionDone();
95+
}
96+
8997

9098
private void setBattleEnd() {
9199
this.fightOngoing = false;
92-
System.out.println(fightOngoing);
100+
if (debug) System.out.println("[Battle Main Thread]: is fightOngoing: " + this.fightOngoing);
93101
}
94102

95103
public void flee() { //ToDo: in Progress
@@ -102,7 +110,7 @@ public void flee() { //ToDo: in Progress
102110
}
103111

104112
public boolean catchBeast(CeItem item) throws Exception {
105-
// System.out.println("Ce_Catch");
113+
if (debug) System.out.println("[Battle Main Thread]: Ce_Catch");
106114
boolean caught = false;
107115
if (turnPlayer1) {
108116
turnPlayer1 = false;
@@ -134,10 +142,10 @@ public void useAttack(CeAttack ceAttack) {
134142
private void applyAttack(CeEntity attacker, CeEntity defender, CeAttack ceAttack) {
135143
final int damage = CeDamage.calculateDamage(attacker, defender, ceAttack);
136144
if (damage != -1) {
137-
// System.out.println("Damage: " + damage);
145+
if (debug) System.out.println("[Battle Main Thread]: Damage: " + damage);
138146
defender.dealDamage(damage);
139147
if (defender.getCeStats().getType() == CeBeastTypes.PlayerStandard) {
140-
// System.out.println("Dealing Damage to player");
148+
if (debug) System.out.println("[Battle Main Thread]: Dealing Damage to player");
141149
if (defender.getPlayerNumber() == 1) cePlayer1.dealDamage(damage);
142150
else cePlayer2.dealDamage(damage);
143151
}
@@ -160,32 +168,33 @@ private void applyAttack(CeEntity attacker, CeEntity defender, CeAttack ceAttack
160168
this.selectedFightEntityPlayer2.setPlayerNumber(2);
161169
defender = selectedFightEntityPlayer2;
162170
if (selectedFightEntityPlayer2.getCeStats().getCurrentHitPoints() == 0) {
163-
// System.out.println("IM DOIN SOMETHING WITH MY USELESS LIFE");
171+
if (debug) System.out.println("[Battle Main Thread]: Player2 fight entity HitPoints 0");
164172
setBattleEnd();
165173
}
166174
}
167175

168176
}
169177
}
170-
} else{ System.out.println("Missed!");}
178+
} else{ if (debug) System.out.println("[Battle Main Thread]: Attack missed!");}
171179
setActionDone();
172180
}
173181

174182
private void threadSleep() {
175183
threadSleep = true;
176-
// System.out.println("Thread now sleeping!");
184+
if (debug) System.out.println("[Battle Main Thread]: Thread now sleeping!");
177185
while (threadSleep) {
178186
try {
179187
Thread.sleep(1);
180188
} catch (InterruptedException e) {
181189
e.printStackTrace();
182190
}
183191
}
184-
// System.out.println("Thread continue");
192+
if (debug) System.out.println("[Battle Main Thread]: Thread continue");
185193
}
186194

187195
private void setActionDone() {
188196
this.threadSleep = false;
197+
if (debug) System.out.println("[Battle Main Thread]: setAction Done");
189198
}
190199

191200
public CePlayer getTurn() {

src/main/java/calculationEngine/battle/testBattle.java

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,65 +11,81 @@
1111
public class testBattle {
1212

1313
public static void main(String[] args) throws Exception {
14-
//simulateAiBattle();
14+
// simulateAiBattle();
1515
simulateCatch();
16+
CeExecuterService.shutdownExecutor();
1617
}
1718

1819
private static void simulateCatch() throws Exception {
1920

2021
List<CeEntity> team = new ArrayList<>();
2122
List<CeAttack> attacks = new ArrayList<>();
2223
attacks.add(new CeAttack(CeAttacks.Punch));
23-
CePlayer cePlayer1 = new CePlayer(new CeStats(CeBeastTypes.PlayerStandard, CeNature.ANGRY, 1,100,100,100,200,200,200,1), attacks,team, false);
24+
CePlayer cePlayer1 = new CePlayer(new CeStats(CeBeastTypes.PlayerStandard, CeNature.ANGRY, 1, 100, 100, 50, 200, 200, 200, 1), attacks, team, false);
2425

2526
team.add(new CeEntity(CeRegions.ArkhamCity, cePlayer1));
2627
cePlayer1.setTeam(team);
2728
cePlayer1.setActiveMonsterIndex(0);
28-
cePlayer1.getInventory().addItemToInventory(CeLoot.lootItem("cage"));
29+
for (int i = 0; i < 1; i++) {
30+
cePlayer1.getInventory().addItemToInventory(CeLoot.lootItem("cage"));
31+
}
2932
CeAi cePlayer2 = new CeAi(cePlayer1, CeRegions.ArkhamCity);
3033
CeBattle battle = new CeBattle(cePlayer1, cePlayer2);
31-
System.out.println("Battle started");
34+
System.out.println("[Test Battle]: Battle started");
3235

33-
while (battle.isFightOngoing()){
34-
if(battle.getTurn() != null){
36+
while (battle.isFightOngoing()) {
37+
if (battle.getTurn() != null) {
3538
if (battle.getTurn().getNumber() == cePlayer1.getNumber()) {
36-
System.out.println("Turn of: Player 1");
37-
boolean caught = battle.catchBeast(CeLoot.lootItem("cage"));
38-
cePlayer1.getInventory().addItemToInventory(CeLoot.lootItem("cage"));
39-
if (caught) System.out.println("Beast caught! CONGRATS");
40-
else System.out.println("Beast doesn't like you");
39+
System.out.println("[Test Battle]: Turn of: Player 1");
40+
try {
41+
boolean caught = battle.catchBeast(CeLoot.lootItem("cage"));
42+
if (caught) System.out.println("[Test Battle]: Beast caught! CONGRATS");
43+
else System.out.println("[Test Battle]: Beast doesn't like you");
44+
} catch (ItemNotInInventoryException itemNotInInventoryException) {
45+
System.out.println("[Test Battle]: ItemNotInInventoryException occurred Ending battle");
46+
battle.endBatte();
47+
} catch (WrongItemException wrongItemException){
48+
System.out.println("[Test Battle]: WrongItemException occurred Ending battle");
49+
battle.endBatte();
50+
}
51+
} } else {
52+
System.out.println("[Test Battle]: End of fight");
4153
}
54+
Thread.sleep(10);
4255
}
43-
else {
44-
System.out.println("End of fight");}
45-
Thread.sleep(10);
56+
System.out.println("[Test Battle]: End of fight");
4657
}
47-
}
4858

4959
private static void simulateAiBattle() throws InterruptedException {
5060

5161
List<CeEntity> team = new ArrayList<>();
5262
List<CeAttack> attacks = new ArrayList<>();
5363
attacks.add(new CeAttack(CeAttacks.Punch));
54-
CePlayer cePlayer1 = new CePlayer(new CeStats(CeBeastTypes.PlayerStandard, CeNature.ANGRY, 1,100,100,50,200,200,200,1), attacks,team, false);
64+
CePlayer cePlayer1 = new CePlayer(new CeStats(CeBeastTypes.PlayerStandard, CeNature.ANGRY, 1, 100, 100, 50, 200, 200, 200, 1), attacks, team, false);
5565
team.add(new CeEntity(CeRegions.ArkhamCity, cePlayer1));
5666
cePlayer1.setTeam(team);
5767
cePlayer1.setActiveMonsterIndex(0);
5868
CeAi cePlayer2 = new CeAi(cePlayer1, CeRegions.ArkhamCity);
5969
CeBattle battle = new CeBattle(cePlayer1, cePlayer2);
60-
System.out.println("Battle started");
70+
System.out.println("[Test Battle]: Battle started");
6171

62-
while (battle.isFightOngoing()){
63-
if(battle.getTurn() != null){
72+
int counter = 0;
73+
while (battle.isFightOngoing()) {
74+
if (counter >= 100) System.out.println("[Test Battle]: Test Battle still running");
75+
counter++;
76+
if (battle.getTurn() != null) {
77+
if (counter >= 100) System.out.println("[Test Battle]: get turn not null");
78+
if (counter >= 100)
79+
System.out.println("[Test Battle]: battle.getTurn.getNumber: " + battle.getTurn().getNumber());
6480
if (battle.getTurn().getNumber() == cePlayer1.getNumber()) {
65-
System.out.println("Turn of: Player 1");
81+
System.out.println("[Test Battle]: Turn of: Player 1");
6682
battle.useAttack(new CeAttack(CeAttacks.Punch));
6783
}
84+
} else {
85+
System.out.println("[Test Battle]: End of fight");
6886
}
69-
// else {System.out.println("End of fight");}
7087
Thread.sleep(10);
7188
}
72-
System.out.println("End of fight");
73-
CeExecuterService.shutdownExecutor();
89+
System.out.println("[Test Battle]: End of fight");
7490
}
7591
}

src/main/java/calculationEngine/entities/CeAi.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
import calculationEngine.battle.CeBattle;
44
import calculationEngine.environment.CeRegions;
5+
import config.BattleConstants;
56

67
import java.util.ArrayList;
78
import java.util.List;
89
import java.util.Random;
910

10-
public class CeAi extends CePlayer implements Runnable {
11+
public class CeAi extends CePlayer {
1112
private CeEntity currentMonster;
1213
private CeBattle battle;
14+
private static final boolean debug = BattleConstants.battleDebug;
1315

1416
// Constructor for new Random Beast AI
1517
public CeAi(CePlayer player, CeRegions region) {
@@ -29,27 +31,11 @@ public CeAi(CePlayer player, CeBeastTypes type, List<CeEntity> team) {
2931
super();
3032
this.setTeam(team);
3133
this.setCeStats(new CeStats(player.getCeStats().getLevel(), type));
34+
this.currentMonster = this.getTeam().get(0);
3235
}
3336

34-
@Override
35-
public void run() {
36-
this.currentMonster = this.getTeam().get(0);
37-
while (battle.isFightOngoing()) {
38-
// System.out.println(battle.isFightOngoing());
39-
// System.out.println("running");
40-
if (battle.getTurn() != null) {
41-
if (battle.getTurn().getNumber() == this.getNumber()) {
42-
// System.out.println("Turn of AI");
43-
battle.useAttack(pickAttack());
44-
}
45-
} else break;
46-
try {
47-
Thread.sleep(20);
48-
} catch (InterruptedException e) {
49-
e.printStackTrace();
50-
}
51-
}
52-
// System.out.println("End of AI Thread");
37+
public void useAttack(){
38+
battle.useAttack(pickAttack());
5339
}
5440

5541
private void finishAIConstruction(CeEntity ceBeast){
@@ -58,17 +44,18 @@ private void finishAIConstruction(CeEntity ceBeast){
5844
ceBeast
5945
);
6046
this.setTeam(team);
61-
// System.out.println("AI CREATION: " + ceBeast.toString());
47+
if (debug) System.out.println("[AI Construction]: AI CREATION: " + ceBeast.toString());
6248
this.setCeStats(new CeStats(ceBeast.getCeStats()));
6349
this.getCeStats().setMaxHitPoints(0);
6450
this.getCeStats().setCurrentHitPoints(0);
51+
this.currentMonster = this.getTeam().get(0);
6552
}
6653

6754
private CeAttack pickAttack() {
6855
List<CeAttack> ceAttacks = this.currentMonster.getAttacks();
6956
Random random = new Random();
7057
int ind = random.nextInt(ceAttacks.size());
71-
// System.out.println(ind);
58+
if (debug) System.out.println("[AI pickAttack]: " + ind);
7259
return ceAttacks.get(ind);
7360
}
7461

src/main/java/calculationEngine/entities/CeInventory.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,27 @@ else if (slot.getItem() == null && !foundSlot) {
5151

5252
}
5353

54-
public void useItem(CeItem item) throws Exception {
54+
public void useItem(CeItem item) throws ItemNotInInventoryException {
5555
boolean matchingItem = false;
5656

5757
// maybe add exception for type cage
5858

5959
for (CeSlots slot : slots) {
60-
if (slot.getItem().compareTo(item)) {
61-
matchingItem = true;
62-
int remainingAmount = slot.decreaseAmount();
63-
if (remainingAmount <= 0) {
64-
slot.reset();
60+
CeItem slotItem = slot.getItem();
61+
if (slotItem != null){
62+
if (slotItem.compareTo(item)) {
63+
matchingItem = true;
64+
int remainingAmount = slot.decreaseAmount();
65+
if (remainingAmount <= 0) {
66+
slot.reset();
67+
}
68+
break;
6569
}
66-
break;
6770
}
6871
}
69-
if (!matchingItem) throw new ItemNotInInventoryException(item);
72+
if (!matchingItem){
73+
throw new ItemNotInInventoryException(item);
74+
}
7075
}
7176

7277
public void setEquippedArmorShoulder(CeItem equippedArmorShoulder) throws WrongItemException {

0 commit comments

Comments
 (0)