Skip to content

Commit dbf28eb

Browse files
committed
fix(sdk) : add max game duration
1 parent 01ae5a4 commit dbf28eb

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

engine/core/src/main/java/com/codingame/gameengine/core/GameManager.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ abstract public class GameManager<T extends AbstractPlayer> {
3434
private static final int GAME_SUMMARY_TOTAL_HARD_QUOTA = 512 * 1024;
3535
private static final int GAME_SUMMARY_PER_TURN_HARD_QUOTA = 800;
3636
private static final int GAME_TURN_SOFT_QUOTA = 200;
37+
private static final int GAME_DURATION_HARD_QUOTA = 30_000;
38+
private static final int GAME_DURATION_SOFT_QUOTA = 25_000;
39+
private static final int MAX_TURN_TIME = GAME_DURATION_SOFT_QUOTA;
40+
private static final int MIN_TURN_TIME = 50;
3741

3842
protected List<T> players;
3943
private int maxTurns = 200;
@@ -67,6 +71,7 @@ abstract public class GameManager<T extends AbstractPlayer> {
6771
private boolean outputsRead = false;
6872
private int totalViewDataBytesSent = 0;
6973
private int totalGameSummaryBytes = 0;
74+
private int totalTurnTime = 0;
7075

7176
private boolean viewWarning, summaryWarning;
7277

@@ -167,6 +172,9 @@ void execute(T player) {
167172
dumpView();
168173
dumpInfos();
169174
dumpNextPlayerInput(player.getInputs().toArray(new String[0]));
175+
if (player.getExpectedOutputLines() > 0) {
176+
this.addTurnTime();
177+
}
170178
dumpNextPlayerInfos(player.getIndex(), player.getExpectedOutputLines(), player.hasNeverBeenExecuted() ? firstTurnMaxTime : turnMaxTime);
171179

172180
// READ PLAYER OUTPUTS
@@ -394,8 +402,10 @@ public int getMaxTurns() {
394402
* if turnMaxTime &le; 0
395403
*/
396404
public void setTurnMaxTime(int turnMaxTime) throws IllegalArgumentException {
397-
if (turnMaxTime <= 0) {
398-
throw new IllegalArgumentException("Invalid turn max time");
405+
if (turnMaxTime <= MIN_TURN_TIME) {
406+
throw new IllegalArgumentException("Invalid turn max time : stay above 50ms");
407+
} else if (turnMaxTime > MAX_TURN_TIME) {
408+
throw new IllegalArgumentException("Invalid turn max time : stay under 25s");
399409
}
400410
this.turnMaxTime = turnMaxTime;
401411
}
@@ -488,6 +498,15 @@ public void addToGameSummary(String summary) {
488498
}
489499
}
490500

501+
private void addTurnTime() {
502+
this.totalTurnTime += this.turnMaxTime;
503+
if (this.totalTurnTime > GAME_DURATION_HARD_QUOTA) {
504+
throw new RuntimeException("Total game duration too long");
505+
} else if (this.totalTurnTime > GAME_DURATION_SOFT_QUOTA) {
506+
log.warn("Warning: please keep the total duration of your total game turns under 25s");
507+
}
508+
}
509+
491510
/**
492511
* Register a module to the gameManager. After this, the gameManager will call the module callbacks automatically.
493512
*

0 commit comments

Comments
 (0)