Skip to content

Commit 0520bea

Browse files
author
Jasper Geurtz
committed
Wire AsyncClientData in BotWrapperAsync and update SynchronizationEnvironment
1 parent dcc7537 commit 0520bea

3 files changed

Lines changed: 201 additions & 196 deletions

File tree

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
1-
package bwapi;
2-
3-
/**
4-
* Manages invocation of bot event handlers
5-
*/
6-
class BotWrapper {
7-
protected final BWClientConfiguration configuration;
8-
protected final BWEventListener eventListener;
9-
10-
protected Game botGame;
11-
protected boolean gameOver;
12-
protected PerformanceMetrics performanceMetrics;
13-
14-
BotWrapper(BWClientConfiguration configuration, BWEventListener eventListener) {
15-
this.configuration = configuration;
16-
this.eventListener = eventListener;
17-
}
18-
19-
/**
20-
* Resets the BotWrapper for a new botGame.
21-
*/
22-
void startNewGame(WrappedBuffer liveData, PerformanceMetrics performanceMetrics) {
23-
this.performanceMetrics = performanceMetrics;
24-
botGame = new Game();
25-
botGame.setConfiguration(configuration);
26-
botGame.botClientData().setBuffer(liveData);
27-
gameOver = false;
28-
}
29-
30-
/**
31-
* @return The Game object used by the bot
32-
* In asynchronous mode this Game object may point at a copy of a previous frame.
33-
*/
34-
Game getGame() {
35-
return botGame;
36-
}
37-
38-
/**
39-
* Handles the arrival of a new frame from BWAPI
40-
*/
41-
void onFrame() {
42-
configuration.log("Main: onFrame synchronous start");
43-
handleEvents();
44-
configuration.log("Main: onFrame synchronous end");
45-
}
46-
47-
void endGame() { }
48-
49-
protected void handleEvents() {
50-
ClientData.GameData botGameData = botGame.botClientData().gameData();
51-
52-
// Populate gameOver before invoking event handlers (in case the bot throws)
53-
for (int i = 0; i < botGameData.getEventCount(); i++) {
54-
gameOver = gameOver || botGameData.getEvents(i).getType() == EventType.MatchEnd;
55-
}
56-
57-
performanceMetrics.getBotResponse().timeIf(
58-
! gameOver && (botGameData.getFrameCount() > 0 || ! configuration.getUnlimitedFrameZero()),
59-
() -> {
60-
for (int i = 0; i < botGameData.getEventCount(); i++) {
61-
EventHandler.operation(eventListener, botGame, botGameData.getEvents(i));
62-
}
63-
});
64-
}
65-
}
1+
package bwapi;
2+
3+
/**
4+
* Manages invocation of bot event handlers
5+
*/
6+
class BotWrapper {
7+
protected final BWClientConfiguration configuration;
8+
protected final BWEventListener eventListener;
9+
10+
protected Game botGame;
11+
protected boolean gameOver;
12+
protected PerformanceMetrics performanceMetrics;
13+
14+
BotWrapper(BWClientConfiguration configuration, BWEventListener eventListener) {
15+
this.configuration = configuration;
16+
this.eventListener = eventListener;
17+
}
18+
19+
/**
20+
* Resets the BotWrapper for a new botGame.
21+
*/
22+
void startNewGame(WrappedBuffer liveData, PerformanceMetrics performanceMetrics) {
23+
this.performanceMetrics = performanceMetrics;
24+
botGame = new Game(new SyncClientData());
25+
botGame.setConfiguration(configuration);
26+
botGame.botClientData().setBuffer(liveData);
27+
gameOver = false;
28+
}
29+
30+
/**
31+
* @return The Game object used by the bot
32+
* In asynchronous mode this Game object may point at a copy of a previous frame.
33+
*/
34+
Game getGame() {
35+
return botGame;
36+
}
37+
38+
/**
39+
* Handles the arrival of a new frame from BWAPI
40+
*/
41+
void onFrame() {
42+
configuration.log("Main: onFrame synchronous start");
43+
handleEvents();
44+
configuration.log("Main: onFrame synchronous end");
45+
}
46+
47+
void endGame() { }
48+
49+
protected void handleEvents() {
50+
ClientData.GameData botGameData = botGame.botClientData().gameData();
51+
52+
// Populate gameOver before invoking event handlers (in case the bot throws)
53+
for (int i = 0; i < botGameData.getEventCount(); i++) {
54+
gameOver = gameOver || botGameData.getEvents(i).getType() == EventType.MatchEnd;
55+
}
56+
57+
performanceMetrics.getBotResponse().timeIf(
58+
! gameOver && (botGameData.getFrameCount() > 0 || ! configuration.getUnlimitedFrameZero()),
59+
() -> {
60+
for (int i = 0; i < botGameData.getEventCount(); i++) {
61+
EventHandler.operation(eventListener, botGame, botGameData.getEvents(i));
62+
}
63+
});
64+
}
65+
}

‎src/main/java/bwapi/BotWrapperAsync.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ class BotWrapperAsync extends BotWrapper {
2929
void startNewGame(WrappedBuffer liveData, PerformanceMetrics performanceMetrics) {
3030
frameBuffer.initialize(liveData, performanceMetrics);
3131

32-
super.startNewGame(liveData, performanceMetrics);
32+
this.performanceMetrics = performanceMetrics;
33+
AsyncClientData asyncClientData = new AsyncClientData(liveData);
34+
botGame = new Game(asyncClientData);
35+
botGame.setConfiguration(configuration);
36+
botGame.botClientData().setBuffer(liveData);
37+
gameOver = false;
3338

3439
liveClientData.setBuffer(liveData);
3540
this.liveData = liveData;

0 commit comments

Comments
 (0)