Skip to content

Commit

Permalink
Hold command request (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiyotoko committed May 27, 2024
1 parent 683aeea commit 41d7e5a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 47 deletions.
18 changes: 18 additions & 0 deletions src/main/java/org/seekers/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.seekers.game;

import io.grpc.stub.StreamObserver;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.BooleanProperty;
Expand All @@ -32,6 +33,7 @@
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.util.Duration;
import javafx.util.Pair;
import org.ini4j.Ini;
import org.seekers.grpc.Corresponding;
import org.seekers.grpc.service.CommandResponse;
Expand Down Expand Up @@ -62,6 +64,9 @@ public class Game extends Scene {
private final @Nonnull List<Goal> goals = new ArrayList<>();
private final @Nonnull List<Camp> camps = new ArrayList<>();

// List of all observers for sending the next status update
private final @Nonnull Map<String, Pair<StreamObserver<CommandResponse>, Integer>> observers = new HashMap<>();

// Graphics
private final @Nonnull BooleanProperty finished = new SimpleBooleanProperty(false);
private final @Nonnull Label time = new Label();
Expand Down Expand Up @@ -150,6 +155,13 @@ private Timeline getTimeline() {
for (Entity entity : List.copyOf(getEntities())) {
entity.update();
}
var response = getCommandResponse();
for (var pair : getObservers().values()) {
var observer = pair.getKey();
observer.onNext(response.setSeekersChanged(pair.getValue()).build());
observer.onCompleted();
}
getObservers().clear();
time.setText("[ " + (++tick) + " ]");
}));
timeline.setCycleCount(javafx.animation.Animation.INDEFINITE);
Expand All @@ -170,6 +182,7 @@ public synchronized void reset() {
players.clear();
seekers.clear();
camps.clear();
observers.clear();

// Clear scene content
getBack().getChildren().clear();
Expand Down Expand Up @@ -258,6 +271,11 @@ public List<Camp> getCamps() {
return camps;
}

@Nonnull
public Map<String, Pair<StreamObserver<CommandResponse>, Integer>> getObservers() {
return observers;
}

@CheckReturnValue
public GameMap getGameMap() {
return gameMap;
Expand Down
53 changes: 9 additions & 44 deletions src/main/java/org/seekers/game/Seeker.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
import org.ini4j.Ini;
import org.seekers.grpc.game.PhysicalOuterClass;
import org.seekers.grpc.game.SeekerOuterClass;
import org.seekers.plugin.GameMap;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -119,44 +117,6 @@ public void collision(@Nonnull Physical<?> another, double minDistance) {
super.collision(another, minDistance);
}

/**
* Finds the nearest Physical object to a given position.
*
* @param p The position to find the nearest Physical object from.
* @param physicals The collection of Physical objects to search from.
* @return The nearest Physical object.
*/
private static @Nullable Physical<?> getNearestPhysicalOf(GameMap map, @Nonnull Point2D p, @Nonnull Iterable<? extends Physical<?>> physicals) {
double distance = map.getDiameter();
Physical<?> nearest = null;

for (Physical<?> physical : physicals) {
double dif = map.getDistance(p, physical.getPosition());
if (dif < distance) {
distance = dif;
nearest = physical;
}
}
return nearest;
}

public void setAutoCommands() {
@SuppressWarnings("null")
@Nullable final Goal goal = (Goal) getNearestPhysicalOf(getGame().getGameMap(), getPosition(), getGame().getGoals());
if (goal != null) {
if (getGame().getGameMap().getDistance(getPosition(), goal.getPosition()) > 30) {
setTarget(goal.getPosition());
setMagnet(0);
} else {
final Camp checked = getPlayer().getCamp();
if (checked != null) {
setTarget(checked.getPosition());
setMagnet(1);
}
}
}
}

/**
* Calculates the magnetic force between the Seeker and a given position.
*
Expand Down Expand Up @@ -257,7 +217,6 @@ public void setTarget(@Nonnull Point2D target) {
*
* @param color The color to set.
*/
@SuppressWarnings("null")
public void setColor(final @Nonnull Color color) {
this.activated = color;
this.disabled = color.darker().darker();
Expand Down Expand Up @@ -286,16 +245,22 @@ public class SeekerAnimation extends Animation {
protected SeekerAnimation(@Nonnull Game game) {
super(game);
indicator.setFill(Color.TRANSPARENT);
indicator.setStrokeWidth(5);
indicator.setStrokeWidth(3);
indicator.setStroke(player.getColor());
getChildren().add(indicator);
setVisible(false);
}

private int frameTime = 30;

@Override
public void update() {
double expansion = (indicator.getRadius() + Math.signum(magnet)) % getAnimationRange();
indicator.setRadius(expansion + properties.radius);
frameTime--;
if (frameTime < 0) {
double expansion = (indicator.getRadius() + Math.signum(magnet)) % getAnimationRange();
indicator.setRadius(expansion + properties.radius);
frameTime = 30;
}
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/seekers/grpc/SeekersServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javafx.geometry.Point2D;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Pair;
import org.ini4j.Ini;
import org.ini4j.Profile;
import org.seekers.game.*;
Expand Down Expand Up @@ -233,7 +234,7 @@ protected class SeekersService extends SeekersGrpc.SeekersImplBase {
@Override
public void command(CommandRequest request, StreamObserver<CommandResponse> responseObserver) {
Player player = players.get(request.getToken());
if (player != null) {
if (player != null && !game.getObservers().containsKey(request.getToken())) {
int changed = 0;
for (Command command : request.getCommandsList()) {
Seeker seeker = player.getSeekers().get(command.getSeekerId());
Expand All @@ -248,8 +249,7 @@ public void command(CommandRequest request, StreamObserver<CommandResponse> resp
}
}
}
responseObserver.onNext(game.getCommandResponse().setSeekersChanged(changed).build());
responseObserver.onCompleted();
game.getObservers().put(request.getToken(), new Pair<>(responseObserver, changed));
} else {
responseObserver.onError(new StatusException(Status.PERMISSION_DENIED));
}
Expand Down

0 comments on commit 41d7e5a

Please sign in to comment.