Skip to content

Commit ff87324

Browse files
authored
Remove some unnecessary sleep invocations & clean up some code. (#10419)
* Remove some unnecessary sleep invocations. * Also apply to two other places and clean up some null checks along the way. * Omit debug code. * More clean up to address codeclimate.
1 parent f6d0400 commit ff87324

File tree

5 files changed

+60
-68
lines changed

5 files changed

+60
-68
lines changed

game-app/game-core/src/main/java/games/strategy/triplea/attachments/AbstractTriggerAttachment.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import games.strategy.engine.data.changefactory.ChangeFactory;
1212
import games.strategy.engine.data.gameparser.GameParseException;
1313
import games.strategy.engine.delegate.IDelegateBridge;
14+
import games.strategy.engine.posted.game.pbem.PbemMessagePoster;
1415
import games.strategy.engine.random.IRandomStats.DiceType;
1516
import games.strategy.triplea.formatter.MyFormatter;
1617
import java.util.ArrayList;
@@ -182,12 +183,12 @@ protected boolean testChance(final IDelegateBridge bridge) {
182183
return false;
183184
}
184185
// there is an issue with maps using thousands of chance triggers: they are causing the cypted
185-
// random source (ie:
186-
// live and pbem games) to lock up or error out
187-
// so we need to slow them down a bit, until we come up with a better solution (like aggregating
188-
// all the chances
189-
// together, then getting a ton of random numbers at once instead of one at a time)
190-
Interruptibles.sleep(100);
186+
// random source (ie: live and pbem games) to lock up or error out so we need to slow them down
187+
// a bit, until we come up with a better solution (like aggregating all the chances together,
188+
// then getting a ton of random numbers at once instead of one at a time)
189+
if (PbemMessagePoster.gameDataHasPlayByEmailOrForumMessengers(getData())) {
190+
Interruptibles.sleep(100);
191+
}
191192
final int rollResult =
192193
bridge.getRandom(
193194
diceSides,

game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import games.strategy.engine.data.UnitType;
2121
import games.strategy.engine.data.gameparser.GameParseException;
2222
import games.strategy.engine.delegate.IDelegateBridge;
23+
import games.strategy.engine.posted.game.pbem.PbemMessagePoster;
2324
import games.strategy.engine.random.IRandomStats.DiceType;
2425
import games.strategy.triplea.Constants;
2526
import games.strategy.triplea.Properties;
@@ -857,12 +858,12 @@ public boolean isSatisfied(
857858
changeChanceDecrementOrIncrementOnSuccessOrFailure(delegateBridge, objectiveMet, false);
858859
} else {
859860
// there is an issue with maps using thousands of chance triggers: they are causing the
860-
// crypted random source
861-
// (i.e.: live and pbem games) to lock up or error out
862-
// so we need to slow them down a bit, until we come up with a better solution (like
863-
// aggregating all the chances
864-
// together, then getting a ton of random numbers at once instead of one at a time)
865-
Interruptibles.sleep(100);
861+
// cypted random source (ie: live and pbem games) to lock up or error out so we need to slow
862+
// them down a bit, until we come up with a better solution (like aggregating all the
863+
// chances together, then getting a ton of random numbers at once instead of one at a time)
864+
if (PbemMessagePoster.gameDataHasPlayByEmailOrForumMessengers(data)) {
865+
Interruptibles.sleep(100);
866+
}
866867
final int rollResult =
867868
delegateBridge.getRandom(
868869
diceSides,

game-app/game-core/src/main/java/games/strategy/triplea/delegate/AbstractEndTurnDelegate.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -529,13 +529,13 @@ private int getBlockadeProductionLoss(
529529
}
530530
if (numberOfDice > 0) {
531531
// there is an issue with maps that have lots of rolls without any pause between them:
532-
// they are causing the
533-
// crypted random source (ie: live and pbem games) to lock up or error out
534-
// so we need to slow them down a bit, until we come up with a better solution (like
535-
// aggregating all the
536-
// chances together, then getting a ton of random numbers at once instead of one at a
537-
// time)
538-
Interruptibles.sleep(100);
532+
// they are causing the crypted random source (ie: live and pbem games) to lock up or
533+
// error out so we need to slow them down a bit, until we come up with a better solution
534+
// (like aggregating all the chances together, then getting a ton of random numbers at
535+
// once instead of one at a time)
536+
if (PbemMessagePoster.gameDataHasPlayByEmailOrForumMessengers(data)) {
537+
Interruptibles.sleep(100);
538+
}
539539
final String transcript = "Rolling for Convoy Blockade Damage in " + b.getName();
540540
final int[] dice =
541541
bridge.getRandom(

game-app/game-core/src/main/java/games/strategy/triplea/delegate/EndTurnDelegate.java

+36-49
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import games.strategy.engine.data.changefactory.ChangeFactory;
1414
import games.strategy.engine.delegate.AutoSave;
1515
import games.strategy.engine.delegate.IDelegateBridge;
16+
import games.strategy.engine.posted.game.pbem.PbemMessagePoster;
1617
import games.strategy.engine.random.IRandomStats.DiceType;
1718
import games.strategy.triplea.Constants;
1819
import games.strategy.triplea.Properties;
@@ -80,7 +81,7 @@ private String createUnits(final IDelegateBridge bridge) {
8081
final CompositeChange change = new CompositeChange();
8182
for (final Territory t : data.getMap().getTerritories()) {
8283
final Collection<Unit> myCreators = CollectionUtils.getMatches(t.getUnits(), myCreatorsMatch);
83-
if (myCreators != null && !myCreators.isEmpty()) {
84+
if (!myCreators.isEmpty()) {
8485
final Collection<Unit> toAdd = new ArrayList<>();
8586
final Collection<Unit> toAddSea = new ArrayList<>();
8687
final Collection<Unit> toAddLand = new ArrayList<>();
@@ -89,62 +90,34 @@ private String createUnits(final IDelegateBridge bridge) {
8990
final IntegerMap<UnitType> createsUnitsMap = ua.getCreatesUnitsList();
9091
final Collection<UnitType> willBeCreated = createsUnitsMap.keySet();
9192
for (final UnitType ut : willBeCreated) {
92-
if (UnitAttachment.get(ut).getIsSea() && Matches.territoryIsLand().test(t)) {
93+
final UnitAttachment uaToCreate = UnitAttachment.get(ut);
94+
if (uaToCreate.getIsSea() && !t.isWater()) {
9395
toAddSea.addAll(ut.create(createsUnitsMap.getInt(ut), player));
94-
} else if (!UnitAttachment.get(ut).getIsSea()
95-
&& !UnitAttachment.get(ut).getIsAir()
96-
&& Matches.territoryIsWater().test(t)) {
96+
} else if (!uaToCreate.getIsSea() && !uaToCreate.getIsAir() && t.isWater()) {
9797
toAddLand.addAll(ut.create(createsUnitsMap.getInt(ut), player));
9898
} else {
9999
toAdd.addAll(ut.create(createsUnitsMap.getInt(ut), player));
100100
}
101101
}
102102
}
103103
if (!toAdd.isEmpty()) {
104-
final String transcriptText =
105-
player.getName()
106-
+ " creates "
107-
+ MyFormatter.unitsToTextNoOwner(toAdd)
108-
+ " in "
109-
+ t.getName();
110-
bridge.getHistoryWriter().startEvent(transcriptText, toAdd);
111-
endTurnReport.append(transcriptText).append("<br />");
112-
final Change place = ChangeFactory.addUnits(t, toAdd);
113-
change.add(place);
104+
createUnits(t, toAdd, change, endTurnReport);
114105
}
115106
if (!toAddSea.isEmpty()) {
116107
final Predicate<Territory> myTerrs = Matches.territoryIsWater();
117108
final Collection<Territory> waterNeighbors = data.getMap().getNeighbors(t, myTerrs);
118-
if (waterNeighbors != null && !waterNeighbors.isEmpty()) {
119-
final Territory tw = getRandomTerritory(waterNeighbors, bridge);
120-
final String transcriptText =
121-
player.getName()
122-
+ " creates "
123-
+ MyFormatter.unitsToTextNoOwner(toAddSea)
124-
+ " in "
125-
+ tw.getName();
126-
bridge.getHistoryWriter().startEvent(transcriptText, toAddSea);
127-
endTurnReport.append(transcriptText).append("<br />");
128-
final Change place = ChangeFactory.addUnits(tw, toAddSea);
129-
change.add(place);
109+
if (!waterNeighbors.isEmpty()) {
110+
final Territory location = getRandomTerritory(data, waterNeighbors, bridge);
111+
createUnits(location, toAddSea, change, endTurnReport);
130112
}
131113
}
132114
if (!toAddLand.isEmpty()) {
133115
final Predicate<Territory> myTerrs =
134116
Matches.isTerritoryOwnedBy(player).and(Matches.territoryIsLand());
135117
final Collection<Territory> landNeighbors = data.getMap().getNeighbors(t, myTerrs);
136-
if (landNeighbors != null && !landNeighbors.isEmpty()) {
137-
final Territory tl = getRandomTerritory(landNeighbors, bridge);
138-
final String transcriptText =
139-
player.getName()
140-
+ " creates "
141-
+ MyFormatter.unitsToTextNoOwner(toAddLand)
142-
+ " in "
143-
+ tl.getName();
144-
bridge.getHistoryWriter().startEvent(transcriptText, toAddLand);
145-
endTurnReport.append(transcriptText).append("<br />");
146-
final Change place = ChangeFactory.addUnits(tl, toAddLand);
147-
change.add(place);
118+
if (!landNeighbors.isEmpty()) {
119+
final Territory location = getRandomTerritory(data, landNeighbors, bridge);
120+
createUnits(location, toAddLand, change, endTurnReport);
148121
}
149122
}
150123
}
@@ -155,21 +128,35 @@ private String createUnits(final IDelegateBridge bridge) {
155128
return endTurnReport.toString();
156129
}
157130

131+
private void createUnits(
132+
final Territory location,
133+
Collection<Unit> units,
134+
CompositeChange change,
135+
StringBuilder endTurnReport) {
136+
final String transcriptText =
137+
player.getName()
138+
+ " creates "
139+
+ MyFormatter.unitsToTextNoOwner(units)
140+
+ " in "
141+
+ location.getName();
142+
bridge.getHistoryWriter().startEvent(transcriptText, units);
143+
endTurnReport.append(transcriptText).append("<br />");
144+
final Change place = ChangeFactory.addUnits(location, units);
145+
change.add(place);
146+
}
147+
158148
private static Territory getRandomTerritory(
159-
final Collection<Territory> territories, final IDelegateBridge bridge) {
160-
if (territories == null || territories.isEmpty()) {
161-
return null;
162-
}
149+
final GameState data, final Collection<Territory> territories, final IDelegateBridge bridge) {
163150
if (territories.size() == 1) {
164151
return CollectionUtils.getAny(territories);
165152
}
166153
// there is an issue with maps that have lots of rolls without any pause between them: they are
167-
// causing the crypted
168-
// random source (ie: live and pbem games) to lock up or error out
169-
// so we need to slow them down a bit, until we come up with a better solution (like aggregating
170-
// all the chances
171-
// together, then getting a ton of random numbers at once instead of one at a time)
172-
Interruptibles.sleep(100);
154+
// causing the crypted random source (ie: live and pbem games) to lock up or error out so we
155+
// need to slow them down a bit, until we come up with a better solution (like aggregating all
156+
// the chances together, then getting a ton of random numbers at once instead of one at a time)
157+
if (PbemMessagePoster.gameDataHasPlayByEmailOrForumMessengers(data)) {
158+
Interruptibles.sleep(100);
159+
}
173160
final List<Territory> list = new ArrayList<>(territories);
174161
final int random =
175162
// ZERO BASED

lib/java-extras/src/main/java/org/triplea/java/Interruptibles.java

+3
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ public static boolean join(final Thread thread) {
105105
* @throws IllegalArgumentException If {@code millis} is negative.
106106
*/
107107
public static boolean sleep(final long millis) {
108+
if (millis == 0) {
109+
return true;
110+
}
108111
return await(() -> Thread.sleep(millis));
109112
}
110113

0 commit comments

Comments
 (0)