fix up game loop to _actually_ be interruptable
This commit is contained in:
parent
57608ad74a
commit
4ab3980d24
|
@ -117,64 +117,84 @@ public class Mirror {
|
||||||
* is running. If your code holds a copy of this object anywhere else, do not try to use it
|
* is running. If your code holds a copy of this object anywhere else, do not try to use it
|
||||||
* again after this method returns.
|
* again after this method returns.
|
||||||
*
|
*
|
||||||
* @param autoReconnect
|
* @param returnOnMatchEnd
|
||||||
* If true, will run an infinite loop allowing you to keep your bot running as many
|
* If true, will disconnect from Broodwar and return after the first match ends
|
||||||
* subsequent matches as desired. Will automatically reconnect to a Broodwar instance
|
|
||||||
* if the connection is interrupted.
|
|
||||||
* If false, will disconnect from Broodwar and return after the first match ends
|
|
||||||
* (regardless of how it ended). Will not attempt to reconnect to Broodwar if the
|
* (regardless of how it ended). Will not attempt to reconnect to Broodwar if the
|
||||||
* connection is interrupted once the first match has been started. You can call
|
* connection is interrupted once the first match has been started. You can call
|
||||||
* {@link #startGame} again to run another match as needed.
|
* {@link #startGame} again to run another match as needed.
|
||||||
|
* If false, will run an infinite loop allowing you to keep your bot running as many
|
||||||
|
* subsequent matches as desired. Will automatically reconnect to a Broodwar instance
|
||||||
|
* if the connection is interrupted.
|
||||||
*/
|
*/
|
||||||
public void startGame(boolean autoReconnect) {
|
public void startGame(boolean returnOnMatchEnd) {
|
||||||
try
|
|
||||||
{
|
|
||||||
System.out.println("Connecting to Broodwar...");
|
System.out.println("Connecting to Broodwar...");
|
||||||
reconnect();
|
if (reconnect())
|
||||||
System.out.println("Connection successful, starting match...");
|
System.out.println("Connection successful, starting match...");
|
||||||
|
else {
|
||||||
|
System.out.println("Connection attempt aborted.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
game = getInternalGame();
|
game = getInternalGame();
|
||||||
|
|
||||||
do {
|
boolean inGame = game.isInGame();
|
||||||
System.out.println("Waiting...");
|
boolean previouslyInGame = inGame;
|
||||||
while (!game.isInGame()) {
|
if (inGame)
|
||||||
update();
|
System.out.println("Match already running.");
|
||||||
if (!isConnected()) {
|
|
||||||
System.out.println("Reconnecting...");
|
while (true) {
|
||||||
reconnect();
|
if (Thread.interrupted()) {
|
||||||
}
|
System.out.println("Interrupted.");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!inGame) {
|
||||||
|
if (previouslyInGame) {
|
||||||
|
System.out.println("Match ended.");
|
||||||
|
if (returnOnMatchEnd)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
update();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if (!previouslyInGame)
|
||||||
System.out.println("Game ready!!!");
|
System.out.println("Game ready!!!");
|
||||||
|
|
||||||
while (game.isInGame()) {
|
|
||||||
processGameEvents();
|
processGameEvents();
|
||||||
|
|
||||||
update();
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
if (!isConnected()) {
|
if (!isConnected()) {
|
||||||
System.out.println("Reconnecting...");
|
System.out.println("Reconnecting...");
|
||||||
reconnect();
|
reconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
previouslyInGame = inGame;
|
||||||
|
inGame = game.isInGame();
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Match ended.");
|
System.out.println("Finished.");
|
||||||
} while(autoReconnect);
|
System.out.println("Disconnecting from Broodwar...");
|
||||||
|
|
||||||
System.out.println("Finished. Disconnecting from Broodwar...");
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
System.out.println("Interrupted. Disconnecting from Broodwar...");
|
|
||||||
}
|
|
||||||
if (isConnected())
|
if (isConnected())
|
||||||
disconnect();
|
disconnect();
|
||||||
|
|
||||||
game = null;
|
game = null;
|
||||||
|
|
||||||
|
System.out.println("Returning...");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reconnect() throws InterruptedException {
|
private boolean reconnect() {
|
||||||
while (!connect()) {
|
while (!connect()) {
|
||||||
|
try {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current connection state to a running Broodwar instance.
|
* Returns the current connection state to a running Broodwar instance.
|
||||||
|
|
Reference in a new issue