Skip to content

Commit

Permalink
Loader wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ia3andy committed Dec 8, 2023
1 parent 7ac9b9c commit e7d1b00
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .quarkus/cli/plugins/quarkus-cli-catalog.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version" : "v1",
"lastUpdate" : "08/11/2023 13:10:48",
"lastUpdate" : "08/12/2023 11:35:25",
"plugins" : { }
}
1 change: 1 addition & 0 deletions dashboard/src/main/java/rest/Dashboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.quarkus.logging.Log;
import io.quarkus.qute.CheckedTemplate;
import io.quarkus.qute.TemplateInstance;
import io.smallrye.common.annotation.NonBlocking;
import io.smallrye.mutiny.Multi;
import jakarta.inject.Inject;
import jakarta.ws.rs.POST;
Expand Down
13 changes: 11 additions & 2 deletions dashboard/src/main/java/rest/Game.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rest;

import io.quarkus.logging.Log;
import io.smallrye.common.annotation.NonBlocking;
import io.smallrye.mutiny.Multi;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
Expand Down Expand Up @@ -83,7 +84,15 @@ public void stop() {
gameService.stop();
}

@Path("/state/{runnerId}")
@Path("/status")
@GET
@Produces(MediaType.APPLICATION_JSON)
@NonBlocking
public GameService.WatchStatus status() {
return gameService.watchStatus();
}

@Path("/{runnerId}/state")
@GET
@Produces(MediaType.APPLICATION_JSON)
public GameState state(@RestPath String runnerId) {
Expand All @@ -102,7 +111,7 @@ public GameState state(@RestPath String runnerId) {
return new GameState(GameStatus.valueOf(runner.status().toString()), data);
}

@Path("/events/{runnerId}")
@Path("/{runnerId}/events")
@Produces(MediaType.SERVER_SENT_EVENTS)
@RestStreamElementType(MediaType.APPLICATION_JSON)
public Multi<GameEvent> events(@RestPath String runnerId) {
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions runners/src/main/resources/web/app/api/GameApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function getState(runner) {
headers: { Accept: 'application/json' },
};
return fetch(
`${API_CONFIG.game}/state/${runner}`,
`${API_CONFIG.game}/${runner}/state`,
{ ...fetchOptions },
)
.then(convertResponse)
Expand Down Expand Up @@ -85,7 +85,7 @@ export function events(user, setState, reset) {
let i = 0;
function connect() {
console.log('Connecting to game event stream');
stream = new EventSource(`${API_CONFIG.game}/events/${user.id}`);
stream = new EventSource(`${API_CONFIG.game}/${user.id}/events`);
stream.onopen = () => {
i = 0;
console.log('Connected to game event stream');
Expand Down
145 changes: 145 additions & 0 deletions scripts/GameLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.6.3
//DEPS io.vertx:vertx-web-client:4.3.4
//JAVA 17+

import io.vertx.ext.web.client.predicate.ResponsePredicate;
import io.vertx.ext.web.codec.BodyCodec;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.net.URL;
import java.util.concurrent.Callable;


import static java.lang.System.*;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

@Command(name = "GameLoader", mixinStandardHelpOptions = true, version = "GameLoader 0.1",
description = "GameLoader made with jbang")
class GameLoader implements Callable<Integer> {

private static final Random R = new Random();

@Parameters(index = "0", description = "The url for the dashboard service", defaultValue = "http://localhost:8079")
private URL urlDashboard;

@Parameters(index = "1", description = "The url for the runners service", defaultValue = "http://localhost:8080")
private URL urlRunners;



@Option(names = {"-p", "--players"}, description = "The amount of players to assign", defaultValue = "1000")
private int players;

@Option(names = {"-c", "--clicks"}, description = "How many click each player will trigger", defaultValue = "200")
private int clicks;

@Option(names = {"--power"}, description = "Click power", defaultValue = "5")
private int power;


public static void main(String... args) {
int exitCode = new CommandLine(new GameLoader()).execute(args);
System.exit(exitCode);
}

@Override
public Integer call() throws Exception { // your business logic goes here...
load();
return 0;
}

void load() throws InterruptedException {
boolean ssl = urlRunners.getProtocol().equals("https");
Vertx vertx = Vertx.vertx();
WebClient client = WebClient.create(vertx,
new WebClientOptions().setTrustAll(true).setVerifyHost(false).setMaxPoolSize(500));
final List<JsonObject> users = Collections.synchronizedList(new ArrayList<>());
final Set<String> names = new HashSet<>();
final CountDownLatch latchLogin = new CountDownLatch(players);
final int portDashboard = urlDashboard.getPort() == -1 ? ssl ? 443 : 80 : urlDashboard.getPort();
final int portRunners = urlRunners.getPort() == -1 ? ssl ? 443 : 80 : urlRunners.getPort();
for (int i = 0; i < players; i++) {
final int index = i;
client.request(HttpMethod.POST, portDashboard, urlDashboard.getHost(),
"/api/game/assign/")
.ssl(ssl)
.expect(ResponsePredicate.SC_SUCCESS)
.send()
.onComplete(r -> {
if (r.failed()) {
r.cause().printStackTrace();
return;
}
try {
final JsonObject user = r.result().bodyAsJsonObject();
users.add(user);
names.add(user.getString("name"));
System.out.println("login " + user + " " + index);
latchLogin.countDown();
} catch (Exception e) {
System.out.println(r.result().bodyAsString());
}

});
}
latchLogin.await();
Thread.sleep(5000);
System.out.println(users.size() + "users created");
System.out.println(names.size() + " different names");
AtomicReference<String> statusRef = new AtomicReference<>("OFF");
while (!Objects.equals(statusRef.get(), "GAME_OVER")) {
client.request(HttpMethod.GET, portDashboard, urlDashboard.getHost(),
"/api/game/status")
.ssl(ssl)
.expect(ResponsePredicate.SC_SUCCESS)
.send()
.onComplete(r -> {
if (r.failed()) {
r.cause().printStackTrace();
return;
}
final String status = r.result().bodyAsJson(String.class);
System.out.println(status);
statusRef.set(status);
});

for (JsonObject user : users) {
Thread.sleep(2);
if(statusRef.get().equals("ROCKING")) {
client.request(HttpMethod.POST, portRunners, urlRunners.getHost(),
"/api/run")
.expect(ResponsePredicate.SC_SUCCESS)
.ssl(ssl)
.sendJsonObject(new JsonObject().put("distance", power).put("runner", user.getString("id")))
.onComplete((r) -> {
if (r.failed()) {
r.cause().printStackTrace();
}
});
}
}

}
System.out.println("game started");
}
}

0 comments on commit e7d1b00

Please sign in to comment.