Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request Response Syncrnization across threads and Replay/Observer updates #70

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,12 @@ private S2Controller launch() {
log.info("Launching Starcraft II with configuration: {}.", cfg);
try {
Path gameRoot = Paths.get(cfg.getString(GAME_EXE_ROOT));
String exeFile = gameRoot
String exeFile = cfg.getString(GAME_EXE_PATH);/*gameRoot
.resolve(Paths.get(
ExecutableParser.VERSIONS_DIR,
cfg.getString(GAME_EXE_BUILD),
cfg.getString(GAME_EXE_FILE)))
.toString();
.toString();*/

List<String> args = new ArrayList<>();
args.add(exeFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ private boolean startReplay() {
}
if (shouldRelaunch(replayObserver)) break;
boolean launched = !replayObserver.replayControl()
.loadReplay(replay, interfaceSettings, 1, processSettings.getRealtime())
.loadReplay(replay, interfaceSettings, 0, processSettings.getRealtime())
.isEmpty()
.blockingGet();
replays.remove(replays.size() - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ObserverActionInterface observerAction() {
* @param replayInfo Replay information used to decide if the replay should be filtered.
* @return If 'true', the replay will be rejected and not analyzed.
*/
boolean ignoreReplay(ReplayInfo replayInfo, int playerId) {
public boolean ignoreReplay(ReplayInfo replayInfo, int playerId) {
return replayInfo.getGameDurationSeconds() < MINIMUM_REPLAY_DURATION;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
* #L%
*/

import com.github.ocraft.s2client.protocol.spatial.PointI;
import com.github.ocraft.s2client.protocol.spatial.Point2d;
import com.github.ocraft.s2client.protocol.unit.Tag;
import com.github.ocraft.s2client.protocol.unit.Unit;

/**
* The ObserverActionInterface corresponds to the actions available in the observer UI.
Expand All @@ -40,7 +42,7 @@ public interface ObserverActionInterface {
* @param distance Distance between camera and terrain. Larger value zooms out camera. Defaults to standard camera
* distance if set to 0.
*/
ObserverActionInterface cameraMove(PointI point, float distance);
ObserverActionInterface cameraMove(Point2d point, float distance);

/**
* Makes the observer camera follow the observed player's perspective.
Expand All @@ -49,6 +51,27 @@ public interface ObserverActionInterface {
*/
ObserverActionInterface cameraFollowPlayer(int playerId);

/**
* Makes the observer view the camera from the perspective of a player
*
* @param playerId Player to use perspective of.
*/
ObserverActionInterface cameraSetPerspective(int playerId);

/**
* Moves the observer to follow the specific set of units.
*
* @param units The units to follow.
*/
ObserverActionInterface cameraFollowUnits(Unit...units);

/**
* Moves the observer camera to follow a specific set of units.
*
* @param units The units to follow.
*/
ObserverActionInterface cameraFollowUnits(Tag...units);

/**
* This function sends out all batched commands. You DO NOT need to call this function.
* it is automatically called when stepping the simulation forward.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
import com.github.ocraft.s2client.protocol.request.Requests;
import com.github.ocraft.s2client.protocol.response.ResponseObserverAction;
import com.github.ocraft.s2client.protocol.spatial.Point2d;
import com.github.ocraft.s2client.protocol.spatial.PointI;
import com.github.ocraft.s2client.protocol.action.observer.ActionObserverPlayerPerspective;
import com.github.ocraft.s2client.protocol.action.observer.ActionObserverCameraFollowUnits;
import com.github.ocraft.s2client.protocol.unit.Tag;
import com.github.ocraft.s2client.protocol.unit.Unit;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -54,7 +57,7 @@ private ControlInterface control() {
}

@Override
public ObserverActionInterface cameraMove(PointI point, float distance) {
public ObserverActionInterface cameraMove(Point2d point, float distance) {
actions.add(ObserverAction.observerAction().of(
ActionObserverCameraMove
.cameraMove()
Expand All @@ -70,6 +73,24 @@ public ObserverActionInterface cameraFollowPlayer(int playerId) {
return this;
}

@Override
public ObserverActionInterface cameraSetPerspective(int playerId) {
actions.add(ObserverAction.observerAction().of(ActionObserverPlayerPerspective.playerPerspective().ofPlayer(playerId)));
return this;
}

@Override
public ObserverActionInterface cameraFollowUnits(Unit...units) {
actions.add(ObserverAction.observerAction().of(ActionObserverCameraFollowUnits.cameraFollowUnits().of(units)));
return this;
}

@Override
public ObserverActionInterface cameraFollowUnits(Tag...units) {
actions.add(ObserverAction.observerAction().of(ActionObserverCameraFollowUnits.cameraFollowUnits().withTags(units)));
return this;
}

@Override
public boolean sendActions() {
if (actions.isEmpty()) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ private boolean connect(
@Override
public <T extends Request> Maybe<Response> sendRequest(T requestData) {
require("request", requestData);
if (!requestData.responseType().equals(ResponseType.PING) &&
responseQueue.peek(requestData.responseType())) {
// TODO p.picheta to test
onError.accept(ClientError.RESPONSE_NOT_CONSUMED, Collections.emptyList());
return Maybe.empty();
Maybe<Response> responseMaybe;
if (!requestData.responseType().equals(ResponseType.PING) && responseQueue.peek(requestData.responseType())) {
// We don't need to sync when using a ping response type
synchronized(requestData.responseType()) {
responseMaybe = s2Client.waitForResponse(requestData.responseType());
}
} else {
responseMaybe = s2Client.waitForResponse(requestData.responseType());
}
Maybe<Response> responseMaybe = s2Client.waitForResponse(requestData.responseType());
s2Client.request(requestData);
countUses.compute(requestData.responseType(), (responseType, count) -> count != null ? ++count : 1);
responseQueue.offer(requestData.responseType(), responseMaybe);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ public List<AvailableAbilities> getAbilitiesForUnits(List<Unit> units, boolean i
.orElseGet(ArrayList::new);

// TODO p.picheta to test
control().errorIf(availableAbilities.isEmpty(), ClientError.NO_ABILITIES_FOR_TAG, Collections.emptyList());

if (control().isUseGeneralizedAbilityId()) {
availableAbilities = availableAbilities.stream()
.map(ability -> ability.generalizeAbility(control().observationInternal()::getGeneralizedAbility))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import SC2APIProtocol.Sc2Api;
import com.github.ocraft.s2client.bot.GameServerResponses;
import com.github.ocraft.s2client.bot.gateway.ObserverActionInterface;
import com.github.ocraft.s2client.protocol.spatial.PointI;
import com.github.ocraft.s2client.protocol.spatial.Point2d;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

Expand All @@ -43,7 +43,7 @@ void handlesObserverActions() {
gameSetup.server().onRequest(Sc2Api.Request::hasObsAction, GameServerResponses::obsAction);

ObserverActionInterface observerAction = gameSetup.control().observerAction();
observerAction.cameraMove(PointI.of(1, 2), 1.0f).cameraFollowPlayer(1);
observerAction.cameraMove(Point2d.of(1, 2), 1.0f).cameraFollowPlayer(1);

assertThat(observerAction.sendActions()).as("sending observer action status").isTrue();
assertThat(observerAction.sendActions()).as("sending empty observer action status").isFalse();
Expand Down