Skip to content

Commit

Permalink
feat: improve random room names
Browse files Browse the repository at this point in the history
Also: show current room name in client

Native image build of api was missing the resources needed for the random names and I
couldn't get docker/quarkus/gradle to comply. So data-faker dependency was removed and
the resources simply moved to private properties in our own classes.
  • Loading branch information
sne11ius committed May 20, 2024
1 parent 664ba47 commit 7c652bb
Show file tree
Hide file tree
Showing 10 changed files with 900 additions and 18 deletions.
1 change: 0 additions & 1 deletion api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ dependencies {
implementation("io.quarkus:quarkus-websockets")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("io.quarkus:quarkus-arc")
implementation(libs.datafaker)
testImplementation("io.quarkus:quarkus-junit5")
testImplementation("io.quarkus:quarkus-junit5-mockito")
testImplementation(libs.hamcrest.json)
Expand Down
2 changes: 0 additions & 2 deletions api/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ quarkus = "3.10.0"
hamcrest-json = "0.3"
mockito = "5.3.1"
mockito-inline = "5.2.0"
data-faker = "2.2.2"

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
Expand All @@ -19,5 +18,4 @@ detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
hamcrest-json = { module = "uk.co.datumedge:hamcrest-json", version.ref = "hamcrest-json" }
mockito-kotlin = { module = "org.mockito.kotlin:mockito-kotlin", version.ref = "mockito" }
mockito-inline = { module = "org.mockito:mockito-inline", version.ref = "mockito-inline" }
datafaker = { module = "net.datafaker:datafaker", version.ref = "data-faker" }
jackson-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin" }
2 changes: 0 additions & 2 deletions api/src/main/docker/Dockerfile.distroless
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ WORKDIR /code
COPY api/src /code/src
RUN \
./gradlew build -Dquarkus.native.enabled=true -Dquarkus.package.jar.enabled=false \
# Prevent "Error: Detected an instance of Random/SplittableRandom class in the image heap"
-Dquarkus.native.additional-build-args="--initialize-at-run-time=net.datafaker.service.RandomService" && \
# Quarkus settings like "quarkus.native.compression.additional-args" seemed to have no effect - maybe we applied them
# wrong. But its just as easy to run upx by ourselves
upx --ultra-brute /code/build/*-runner
Expand Down
5 changes: 1 addition & 4 deletions api/src/main/docker/Dockerfile.distroless-test
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ COPY --chown=quarkus:quarkus .git /code/
USER quarkus
WORKDIR /code
COPY api/src /code/src
RUN \
./gradlew build -Dquarkus.native.enabled=true -Dquarkus.package.jar.enabled=false \
# Prevent "Error: Detected an instance of Random/SplittableRandom class in the image heap"
-Dquarkus.native.additional-build-args="--initialize-at-run-time=net.datafaker.service.RandomService"
RUN ./gradlew build -Dquarkus.native.enabled=true -Dquarkus.package.jar.enabled=false

## Stage 2 : create the docker final image
FROM quay.io/quarkus/quarkus-distroless-image:2.0
Expand Down
131 changes: 128 additions & 3 deletions api/src/main/kotlin/pp/api/RoomsResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,138 @@ import jakarta.ws.rs.core.Response
import jakarta.ws.rs.core.Response.temporaryRedirect
import jakarta.ws.rs.core.UriInfo
import java.net.URI
import java.util.UUID.randomUUID
import java.net.URLEncoder.encode
import java.nio.charset.StandardCharsets.UTF_8

/**
* List and join rooms
*/
@Path("/rooms")
class RoomsResource {
/*
* Portions of this file are derived from work licensed under the Apache License, Version 2.0
* See the original file at http://www.apache.org/licenses/LICENSE-2.0
* Modifications have been made to the original code to convert from yaml format to kotlin.
* see https://github.com/datafaker-net/datafaker/blob/main/src/main/resources/en/sword_art_online.yml
* see https://github.com/datafaker-net/datafaker/blob/main/src/main/resources/en/stargate.yml
* see https://github.com/datafaker-net/datafaker/blob/main/src/main/resources/en/star_trek.yml
*/
// from https://github.com/datafaker-net/datafaker/blob/main/src/main/resources/en/sword_art_online.yml
private val saoItems = listOf(
"Blackwyrm Coat",
"Anneal Blade",
"Dark Repulser",
"Ebon Dagger",
"Elucidator",
"Guilty Thorn",
"Karakurenai",
"Lambent Light",
"Liberator",
"Mate Chopper",
"Stout Brand",
"Sword Breaker",
"Throwing Pick",
"Tyrant Dragon",
"Wind Fleuret",
"Argyro's Sheet",
"Coat of Midnight",
"Cor",
"Crystal Bottle of Kales'Oh",
"Crystallite Ingot",
"Crystals",
"Diving Stone of Returning Soul",
"Dusk Lizard Hide",
"Eternal Storage Trinket",
"Hand Mirror",
"Mighty Strap of Leather",
"Mirage Sphere",
"Pneuma Flower",
"Potions",
"Ragout Rabbit's Meat",
"Ring of Agility",
"Ring of Angel's Whisper",
"Scavenge Toad Meat",
"Tremble Shortcake",
"Vendor's Carpet",
"Yui's Heart",
"Black Iron Great Sword",
"Blue Long Sword",
"Crest of Yggdrasil",
"Demonic Sword Gram",
"Holy Sword Excalibur",
"Lightning Hammer Mjolnir",
"Long Sword",
"Sap of the World Tree",
"Yrd",
"Accuracy International L115A3",
"Credit",
"Defense Field",
"FN Five-Seven",
"GE M134 Minigun",
"Kagemitsu G4",
"Metamaterial Optical Camouflage Mantle",
"PGM Ultima Ratio Hecate II",
"Plasma Grenade",
"Procyon SL",
"Satellite Scan Terminal",
"Starship Metal Estoc",
"Type 54 \"Black Star\"",
"Black Lily Sword",
"Blue Rose Sword",
"Conflagrant Flame Bow",
"Dragon Bone Axe",
"Fragrant Olive Sword",
"Frost Scale Whip",
"Gigas Cedar",
"Goblin Sword",
"Heaven Piercing Sword",
"Night Sky Sword",
"Silvery Eternity",
"Time Piercing Sword",
"Twin Edged Wings"
)
private final val locations = listOf(
// from https://github.com/datafaker-net/datafaker/blob/main/src/main/resources/en/stargate.yml
"Abydos",
"Altair",
"Asuras",
"Athos",
"Celestis",
"Chulak",
"Dakara",
"Earth",
"Langara",
"Lantea",
"Orilla",
"P3X-888",
"Sateda",
"Tollana",
"Vorash",

// from https://github.com/datafaker-net/datafaker/blob/main/src/main/resources/en/star_trek.yml
"Qo'noS",
"Romulus",
"Bajor",
"Vulcan",
"Neutral Zone",
"Alpha Quadrant",
"Beta Quadrant",
"Delta Quadrant",
"Gamma Quadrant",
"Tau Ceti Prime",
"Wolf 359",
"Thalos VII",
"Cardassia",
"Trillius Prime",
"Badlands",
"Betazed",
"Risa",
"Deep Space Nine",
"Ferenginar",
"The Briar Patch",
"Khitomer"
)

/**
* Redirect to a websocket url for a random room
*
Expand All @@ -23,9 +148,9 @@ class RoomsResource {
@Path("new")
@GET
fun createRandomRoom(uri: UriInfo): Response {
val roomId = randomUUID().toString()
val roomId = saoItems.random() + " " + locations.random()
Log.info("Redirecting to new room $roomId")
val redirectLocation = uri.requestUri.resolve(roomId).toString()
val redirectLocation = uri.requestUri.resolve(encode(roomId, UTF_8)).toString()
val wsRedirectLocation = redirectLocation.replace("http", "ws")
val newUri = URI(wsRedirectLocation)
Log.info("Redirecting to $newUri")
Expand Down
Loading

0 comments on commit 7c652bb

Please sign in to comment.