Skip to content

Commit

Permalink
Multiple fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ia3andy committed Dec 9, 2023
1 parent e7d1b00 commit b9c8d1c
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 49 deletions.
24 changes: 24 additions & 0 deletions dashboard/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>delete-mvnpm-deps</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- Delete libraries starting with org.mvnpm -->
<delete includeemptydirs="true">
<fileset dir="${project.build.directory}/quarkus-app/lib/main">
<include name="org.mvnpm*"/>
</fileset>
</delete>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
Expand Down
30 changes: 27 additions & 3 deletions dashboard/src/main/java/rest/Dashboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,35 @@ public void reset() {

@Produces(MediaType.SERVER_SENT_EVENTS)
@RestStreamElementType(MediaType.TEXT_PLAIN)
public Multi<OutboundSseEvent> events(@Context Sse sse) {
public Multi<OutboundSseEvent> boardEvents(@Context Sse sse) {
return gameService.events()
.filter(e -> e.type().sseEventName().equals("BoardUpdate") || e.type().sseEventName().equals("GameUpdate"))
.group().intoLists()
.every(Duration.ofMillis(200))
.map(g -> sse.newEvent(resolveSseEventName(g), ""));
.every(Duration.ofMillis(500))
.map(g -> sse.newEvent("BoardUpdate", compactHtml(Templates.board().render())));
}

@Produces(MediaType.SERVER_SENT_EVENTS)
@RestStreamElementType(MediaType.TEXT_PLAIN)
public Multi<OutboundSseEvent> controlsEvents(@Context Sse sse) {
return gameService.events()
.filter(e -> e.type().sseEventName().equals("ControlsUpdate") || e.type().sseEventName().equals("GameUpdate"))
.map(g -> sse.newEvent("ControlsUpdate", compactHtml(Templates.controls().render())));
}

private static String compactHtml(String html) {
return html.replaceAll("(\\s+|\\v)", " ");
}

private String resolveEventData(String eventName) {
switch (eventName) {
case "BoardUpdate":
return Templates.board().render();
case "ControlsUpdate":
return Templates.controls().render();
default:
return Templates.index$game().render();
}
}

private static String resolveSseEventName(List<GameEvent> g) {
Expand Down
28 changes: 18 additions & 10 deletions dashboard/src/main/java/service/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void stop() {
rank.set(null);
watching.set(null);
watchStatus.set(OFF);
runners.replaceAll((r, v) -> v.runner().initialState());
runners.replaceAll((r, v) -> v.active() ? v.runner().initialState() : v.runner().setInactive());
emitEvent(STOP);
}

Expand All @@ -129,7 +129,7 @@ public void reset() {
watching.set(null);
watchStatus.set(OFF);
rank.set(null);
runners.replaceAll((r, v) -> v.runner().inactive());
runners.replaceAll((r, v) -> v.runner().setInactive());
emitEvent(RESET);
}

Expand Down Expand Up @@ -177,6 +177,10 @@ public Collection<RunnerState> runners() {
return runners.values();
}

public Collection<RunnerState> activeRunners() {
return runners.values().stream().filter(RunnerState::active).toList();
}

public WatchStatus watchStatus() {
return watchStatus.get();
}
Expand All @@ -190,7 +194,7 @@ public Runner newRunner(String prevId) {
if (prevId != null && runners.containsKey(prevId)) {
final RunnerState state = runners.get(prevId);
runner = state.runner();
if (!state.isActive()) {
if (!state.active()) {
runners.put(runner.id(), runner.initialState());
}
} else {
Expand Down Expand Up @@ -229,7 +233,7 @@ public void run(String runnerId, int distance, long time) {
return null;
}
// inactive user becoming active
if (!state.isActive()) {
if (!state.active()) {
emitEvent(NEW_RUNNER);
return state.runner().initialState();
}
Expand Down Expand Up @@ -296,7 +300,7 @@ public int getRank(String id) {
}

static Comparator<RunnerState> rankComparator() {
return comparing(RunnerState::isActive).reversed()
return comparing(RunnerState::active).reversed()
.thenComparing(comparing(RunnerState::saved).reversed())
.thenComparing(comparing(RunnerState::alive).reversed())
.thenComparing(comparing(RunnerState::distance).reversed())
Expand Down Expand Up @@ -339,14 +343,14 @@ public RunnerState initialState() {
return new RunnerState(this, 0, 0, RunnerState.Status.alive);
}

public RunnerState inactive() {
public RunnerState setInactive() {
return new RunnerState(this, 0, 0, RunnerState.Status.inactive);
}

}

public int indexPercentage(int index) {
return (int) Math.floor((index + 0.5) * 100 / runners().size());
public int indexPercentage(int index, int max) {
return (int) Math.floor((index + 0.5) * 100 / max);
}

public record RunnerState(Runner runner, int distance, long duration, Status status) {
Expand All @@ -366,11 +370,15 @@ public boolean saved() {
return status == Status.saved;
}

public boolean inactive() {
return status == Status.inactive;
}

public boolean gameOver() {
return saved() || dead();
return saved() || dead() || inactive();
}

public boolean isActive() {
public boolean active() {
return status != Status.inactive;
}

Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mp.messaging.incoming.runners-in.topic=runners

%dev.quarkus.http.cors=true
%dev.quarkus.http.cors.origins=*

quarkus.http.enable-compression=true
quarkus.csrf-reactive.require-form-url-encoded=false

game.target-distance=100
Expand Down
6 changes: 3 additions & 3 deletions dashboard/src/main/resources/templates/Dashboard/board.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{#for item in cdi:game.runners}
{#let activeRunners = cdi:game.activeRunners}
{#for item in activeRunners}
{#if item.active}
<div class="runner {item.status}"
style="left:calc({cdi:game.indexPercentage(item_index)}% - 50px); bottom: calc({item.distancePercentage(cdi:game.targetDistance)}%); ">
<div id="{item.runner.id}" class="runner {item.status}" style="left:calc({cdi:game.indexPercentage(item_index, activeRunners.size())}% - 15px); bottom: calc({item.distancePercentage(cdi:game.targetDistance)}%); ">
{item.runner.name}
</div>
{/if}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="controls {cdi:game.watchStatus.toString.toLowerCase} d-flex justify-content-center align-items-center flex-grow-1 flex-column">
<div id="controls" class="controls {cdi:game.watchStatus.toString.toLowerCase} d-flex justify-content-center align-items-center flex-grow-1 flex-column">
{#when cdi:game.watchStatus}
{#is WATCHING}
<h2 class="one-two-three">One-Two-Three</h2>
Expand All @@ -11,13 +11,13 @@ <h2 class="venue">{config:venue}</h2>
{#is WARNING}
<button class="btn btn-link blink"
hx-post="{uri:Dashboard.stopGame}">
<img class="rocking-duke" src="{duke:get(cdi:game.rockingDuke)}" alt="Rocking Duke">
<img id="rocking-duke" class="rocking-duke" src="{duke:get(cdi:game.rockingDuke)}" alt="Rocking Duke">
</button>
{#is ROCKING}
<button class="btn btn-link dance"
_="on load audio.game.play()"
hx-post="{uri:Dashboard.stopGame}">
<img class="rocking-duke" src="{duke:get(cdi:game.rockingDuke)}" alt="Rocking Duke">
<img id="rocking-duke" class="rocking-duke" src="{duke:get(cdi:game.rockingDuke)}" alt="Rocking Duke">
</button>
{#is GAME_OVER}
<button class="btn btn-link"
Expand Down
23 changes: 4 additions & 19 deletions dashboard/src/main/resources/templates/Dashboard/index.html
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
{#include main.html}

<div class="container" hx-sse="connect:{uri:Dashboard.events}" hx-ext="morph">
<div class="container" hx-ext="morph">
<div
id="game-container"
hx-headers='{"{inject:csrf.headerName}":"{inject:csrf.token}"}'
hx-trigger="sse:GameUpdate"
hx-get="{uri:Dashboard.index}"
hx-swap="morph:innerHTML"
>
>
{#fragment id=game}
<div class="board-container d-flex justify-content-center align-items-center"
hx-trigger="sse:BoardUpdate"
hx-get="{uri:Dashboard.board}"
hx-swap="morph:innerHTML"
>
{#include Dashboard/board /}
</div>
<div class="controls-container d-flex justify-content-center align-items-center flex-column"
hx-trigger="sse:ControlsUpdate"
hx-get="{uri:Dashboard.controls}"
hx-swap="morph:innerHTML"
>
{#include Dashboard/controls /}
</div>
<div class="board-container d-flex justify-content-center align-items-center" hx-ext="sse" sse-connect="{uri:Dashboard.boardEvents}" sse-swap="BoardUpdate" hx-swap="morph:innerHTML">{#include Dashboard/board /}</div>
<div class="controls-container d-flex justify-content-center align-items-center flex-column" hx-ext="sse" sse-connect="{uri:Dashboard.controlsEvents}" sse-swap="ControlsUpdate" hx-swap="morph:innerHTML">{#include Dashboard/controls /}</div>
{/fragment}
</div>
</div>
5 changes: 4 additions & 1 deletion dashboard/src/main/resources/web/app/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import "htmx.org";
import htmx from "htmx.org";
import "idiomorph";
import _hyperscript from "hyperscript.org";
import "bootstrap";

window.htmx = htmx;

require("htmx.org/dist/ext/sse");
_hyperscript.browserInit();
9 changes: 5 additions & 4 deletions dashboard/src/main/resources/web/app/board.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
bottom: 0;
left: 0;
border-radius: 50px;
height: 70px;
width: 70px;
height: 30px;
width: 30px;
font-size: 30px;
border: 3px solid blue;
font-size: 12px;
border: 2px solid blue;
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
transition: bottom 200ms linear;
transition: all 1000ms linear;
background-color: white;

&.dead {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<skipITs>true</skipITs>
<surefire-plugin.version>3.1.2</surefire-plugin.version>
<quarkus-renarde.version>3.0.4</quarkus-renarde.version>
<quarkus-web-bundler.version>1.2.0.alpha1</quarkus-web-bundler.version>
<quarkus-web-bundler.version>1.2.0.alpha2</quarkus-web-bundler.version>
<quarkus-playwright.version>0.0.1</quarkus-playwright.version>
</properties>
<modules>
Expand Down
24 changes: 24 additions & 0 deletions runners/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,30 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>delete-mvnpm-deps</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- Delete libraries starting with org.mvnpm -->
<delete includeemptydirs="true">
<fileset dir="${project.build.directory}/quarkus-app/lib/main">
<include name="org.mvnpm*"/>
</fileset>
</delete>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
Expand Down
File renamed without changes.
9 changes: 5 additions & 4 deletions scripts/GameLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class GameLoader implements Callable<Integer> {



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

@Option(names = {"-c", "--clicks"}, description = "How many click each player will trigger", defaultValue = "200")
Expand Down Expand Up @@ -102,10 +102,11 @@ void load() throws InterruptedException {
});
}
latchLogin.await();
Thread.sleep(5000);
Thread.sleep(1000);
System.out.println(users.size() + "users created");
System.out.println(names.size() + " different names");
AtomicReference<String> statusRef = new AtomicReference<>("OFF");
Random random = new Random();
while (!Objects.equals(statusRef.get(), "GAME_OVER")) {
client.request(HttpMethod.GET, portDashboard, urlDashboard.getHost(),
"/api/game/status")
Expand All @@ -123,20 +124,20 @@ void load() throws InterruptedException {
});

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")))
.sendJsonObject(new JsonObject().put("distance", random.nextInt(power)).put("runner", user.getString("id")))
.onComplete((r) -> {
if (r.failed()) {
r.cause().printStackTrace();
}
});
}
}
Thread.sleep(5);

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

0 comments on commit b9c8d1c

Please sign in to comment.