|
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 | +} |
0 commit comments