diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index e39aea0..b457f0d 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -10,6 +10,12 @@ name: "Java CI with Gradle" permissions: contents: "read" jobs: + validation: + name: "Validation" + runs-on: "ubuntu-latest" + steps: + - uses: "actions/checkout@v4" + - uses: "gradle/actions/wrapper-validation@v4" build: runs-on: "ubuntu-latest" steps: @@ -19,8 +25,6 @@ jobs: with: java-version: "11" distribution: "temurin" - - name: "Validate Gradle wrapper" - uses: "gradle/wrapper-validation-action@v1" - name: "Build with Gradle" uses: "gradle/gradle-build-action@v2" with: diff --git a/Notebook.ipynb b/Notebook.ipynb index 4be318a..ea792ec 100644 --- a/Notebook.ipynb +++ b/Notebook.ipynb @@ -58,7 +58,9 @@ "metadata": {}, "outputs": [], "source": [ - "%use kandy, dataframe\n", + "%useLatestDescriptors\n", + "%use dataframe(v=1.0.0-Beta2)\n", + "%use kandy\n", "import com.github.nwillc.ksvg.RenderMode\n", "import java.io.FileWriter\n", "\n", @@ -85,8 +87,6 @@ "metadata": {}, "outputs": [], "source": [ - "%use kandy, dataframe\n", - "\n", "objectiveLagLogger.buildDataFrame().plot {\n", " points {\n", " x(\"Timstamp (seconds)\")\n", @@ -109,21 +109,70 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2025-08-14T13:59:42.613501Z", - "start_time": "2025-08-14T13:59:42.485496Z" - } - }, + "metadata": {}, "outputs": [], "source": [ + "import org.emulinker.proto.Event.EventTypeCase.*\n", "import java.io.FileInputStream\n", "import org.emulinker.proto.GameLog\n", "\n", - "FileInputStream(\"gamelog_3pall1f.bin\").use { fileInputStream ->\n", - " val log = GameLog.parseFrom(fileInputStream.readBytes())\n", - " println(\"Number of events: ${log.eventsList.size}\")\n", - " log.eventsList.take(20).map { it.eventTypeCase }\n", + "val log: GameLog = FileInputStream(\"gamelog_2p_1f.bin\").use { GameLog.parseFrom(it.readBytes()) }\n", + "val events = log.eventsList\n", + "\n", + "println(\"Game configuration:\")\n", + "events.first { it.eventTypeCase == GAME_START }" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Plot `/lagstat` summaries over the session" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import org.emulinker.proto.Event\n", + "import org.jetbrains.kotlinx.dataframe.api.dataFrameOf\n", + "import org.jetbrains.kotlinx.dataframe.api.toDataFrame\n", + "\n", + "val summaries = events.filter { it.eventTypeCase == LAGSTAT_SUMMARY && it.lagstatSummary.gameLagMs < 2000 }\n", + "\n", + "val time = column(\"Timestamp (ns)\")\n", + "val observedLag = column(\"Observed lag (ms)\")\n", + "val player1Lag = column(\"Player 1 attributed lag (ms)\")\n", + "val player2Lag = column(\"Player 2 attributed lag (ms)\")\n", + "\n", + "val df = dataFrameOf(\n", + " time.name() to summaries.map { it.timestampNs },\n", + " observedLag.name() to summaries.map { it.lagstatSummary.gameLagMs },\n", + " player1Lag.name() to summaries.map { it.lagstatSummary.getPlayerAttributedLags(0).attributedLagMs },\n", + " player2Lag.name() to summaries.map { it.lagstatSummary.getPlayerAttributedLags(1).attributedLagMs }\n", + ")\n", + "\n", + "df.plot {\n", + " x(time)\n", + " points {\n", + " y(observedLag) {\n", + " axis.name = \"Lag (ms)\"\n", + " }\n", + " }\n", + " points {\n", + " y(player1Lag)\n", + " color = Color.BLUE\n", + " }\n", + " points {\n", + " y(player2Lag)\n", + " color = Color.RED\n", + " }\n", + " layout {\n", + " title = \"Server-measured lag\"\n", + " subtitle = \"Grey is server, blue is p1, red is p2\"\n", + " }\n", "}" ] } diff --git a/PythonNotebook.ipynb b/PythonNotebook.ipynb new file mode 100644 index 0000000..c55c7ae --- /dev/null +++ b/PythonNotebook.ipynb @@ -0,0 +1,54 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "5d816fab", + "metadata": {}, + "source": [ + "## Read in proto in Python\n", + "\n", + "NOTE: First make sure you run `./gradlew build` which will generate the Python file.\n", + "\n", + "NOTE: This fails at the import line.. Getting this working is left as an exercise to the reader." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ebc30054e5d69323", + "metadata": {}, + "outputs": [], + "source": [ + "import build.generated.sources.proto.main.python.lag_pb2\n", + "\n", + "log = lag_pb2.GameLog()\n", + "\n", + "with open(\"gamelog_1p_1f.bin\", \"rb\") as f:\n", + " log.ParseFromString(f.read())\n", + "\n", + "log" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/buf.yaml b/buf.yaml new file mode 100644 index 0000000..0447698 --- /dev/null +++ b/buf.yaml @@ -0,0 +1,8 @@ +--- +version: "v2" +lint: + use: + - "STANDARD" + except: + - "PACKAGE_VERSION_SUFFIX" + - "PACKAGE_DIRECTORY_MATCH" diff --git a/build.gradle.kts b/build.gradle.kts index 075d6ec..c28c0ec 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,10 +3,10 @@ import com.google.protobuf.gradle.id plugins { id("com.google.protobuf") version "0.9.5" id("build.buf") version "0.10.2" - id("com.diffplug.spotless") version "6.25.0" + id("com.diffplug.spotless") version "7.2.1" application - kotlin("jvm") version "2.0.20" + kotlin("jvm") version "2.2.10" } repositories { @@ -18,10 +18,10 @@ repositories { } dependencies { - api("org.jetbrains.kotlin:kotlin-stdlib:2.0.20") + api("org.jetbrains.kotlin:kotlin-stdlib:2.2.10") - implementation("org.jetbrains.kotlinx:kandy-lets-plot:0.7.0") - implementation("org.jetbrains.kotlinx:kotlin-statistics-jvm:0.3.0") + implementation("org.jetbrains.kotlinx:kandy-lets-plot:0.8.0") + implementation("org.jetbrains.kotlinx:dataframe:1.0.0-Beta2") implementation("com.github.nwillc.ksvg:ksvg:master-SNAPSHOT") @@ -62,30 +62,17 @@ tasks.withType { systemProperty( "flogger.backend_factory", - "org.emulinker.testing.TestLoggingBackendFactory#getInstance" + "org.emulinker.testing.TestLoggingBackendFactory#getInstance", ) } -// Disable formatting via buf plugin directly. We just need it for the binary. -buf { enforceFormat = false } - -tasks.named("bufLint") { enabled = false } +buf { enforceFormat = true } // Formatting/linting. spotless { - // Breaks github CI even though it works locally.. - // protobuf { - // buf("1.46.0") - // .pathToExe( - // - // configurations.getByName(BUF_BINARY_CONFIGURATION_NAME).getSingleFile().getAbsolutePath() - // ) - // target("src/**/*.proto") - // } - kotlin { target("**/*.kt", "**/*.kts") - targetExclude("build/", ".git/", ".idea/", ".mvn", "src/main/java-templates/") + targetExclude("bin/", "build/", ".git/", ".idea/", ".mvn", "src/main/java-templates/") ktfmt().googleStyle() } @@ -104,6 +91,7 @@ protobuf { it.plugins { // Generates Kotlin DSL builders. id("kotlin") {} + id("python") {} } } } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 249e583..8bdaf60 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 065a6c8..d4081da 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,7 @@ -#Sun Sep 01 20:42:20 JST 2024 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip +networkTimeout=10000 +validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index 1b6c787..ef07e01 100755 --- a/gradlew +++ b/gradlew @@ -1,7 +1,7 @@ #!/bin/sh # -# Copyright © 2015-2021 the original authors. +# Copyright © 2015 the original authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,6 +15,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +# SPDX-License-Identifier: Apache-2.0 +# ############################################################################## # @@ -55,7 +57,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/. @@ -80,13 +82,11 @@ do esac done -APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit - -APP_NAME="Gradle" +# This is normally unused +# shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum @@ -114,7 +114,7 @@ case "$( uname )" in #( NONSTOP* ) nonstop=true ;; esac -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar +CLASSPATH="\\\"\\\"" # Determine the Java command to use to start the JVM. @@ -133,22 +133,29 @@ location of your Java installation." fi else JAVACMD=java - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation." + fi fi # Increase the maximum file descriptors if we can. if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then case $MAX_FD in #( max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 MAX_FD=$( ulimit -H -n ) || warn "Could not query maximum file descriptor limit" esac case $MAX_FD in #( '' | soft) :;; #( *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 ulimit -n "$MAX_FD" || warn "Could not set maximum file descriptor limit to $MAX_FD" esac @@ -193,18 +200,28 @@ if "$cygwin" || "$msys" ; then done fi -# Collect all arguments for the java command; -# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of -# shell script including quotes and variable substitutions, so put them in -# double quotes to make sure that they get re-expanded; and -# * put everything else in single quotes, so that it's not re-expanded. + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. set -- \ "-Dorg.gradle.appname=$APP_BASE_NAME" \ -classpath "$CLASSPATH" \ - org.gradle.wrapper.GradleWrapperMain \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ "$@" +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + # Use "xargs" to parse quoted args. # # With -n1 it outputs one arg per line, with the quotes and backslashes removed. diff --git a/gradlew.bat b/gradlew.bat index ac1b06f..5eed7ee 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -13,8 +13,10 @@ @rem See the License for the specific language governing permissions and @rem limitations under the License. @rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem -@if "%DEBUG%" == "" @echo off +@if "%DEBUG%"=="" @echo off @rem ########################################################################## @rem @rem Gradle startup script for Windows @@ -25,7 +27,8 @@ if "%OS%"=="Windows_NT" setlocal set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @@ -40,13 +43,13 @@ if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto execute +if %ERRORLEVEL% equ 0 goto execute -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail @@ -56,32 +59,34 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe if exist "%JAVA_EXE%" goto execute -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail :execute @rem Setup the command line -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar +set CLASSPATH= @rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* :end @rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd +if %ERRORLEVEL% equ 0 goto mainEnd :fail rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% :mainEnd if "%OS%"=="Windows_NT" endlocal diff --git a/src/main/java/com/hopskipnfall/Client.kt b/src/main/java/com/hopskipnfall/Client.kt index e65a943..46fc9b1 100644 --- a/src/main/java/com/hopskipnfall/Client.kt +++ b/src/main/java/com/hopskipnfall/Client.kt @@ -37,7 +37,7 @@ class Client( frameNumberLogger: TimeBasedDataLogger, diagramBuilder: DiagramBuilder, objectiveLagLogger: TimeBasedDataLogger, - timeStep: Duration + timeStep: Duration, ) { // This block ensures that all of the clients are synchronized with respect to the server. // Different frame differences require different respective start times. @@ -97,7 +97,7 @@ class Client( start = timeReceivedDataNecessaryForNextFrame!!, end = now, client = id, - frameNumber = frameNumber + frameNumber = frameNumber, ) } diagramBuilder.registerNewFrame(now, client = id, frameNumber) @@ -107,7 +107,7 @@ class Client( "Frame Number" to frameNumber, "Client" to "Client $id" + if (description == null) "" else " ($description)", "Objective lag in a single frame (ms)" to - max((now - newFrameTimestamp - SINGLE_FRAME_DURATION).toMillisDouble(), 0.0) + max((now - newFrameTimestamp - SINGLE_FRAME_DURATION).toMillisDouble(), 0.0), ) } @@ -144,7 +144,7 @@ class Client( val packet = DelayedPacket( arrivalTime = now + (pingRange.random() / 2), - listOf(FrameData(frameNumber + frameDelay, fromClientId = id)) + listOf(FrameData(frameNumber + frameDelay, fromClientId = id)), ) diagramBuilder.registerPacketToServer(now, client = id, packet) server.incomingPackets += packet diff --git a/src/main/java/com/hopskipnfall/Server.kt b/src/main/java/com/hopskipnfall/Server.kt index d950c5a..44e6f3b 100644 --- a/src/main/java/com/hopskipnfall/Server.kt +++ b/src/main/java/com/hopskipnfall/Server.kt @@ -52,7 +52,6 @@ data class Server(val clients: List) { client.incomingPackets += packet diagramBuilder.registerPacketToClient(now, client = client.id, packet) - if (lastFanOutTime != null) { // Calculate lag. val elapsedSinceReceivingFrameData = now - client.serverData.receivedDataAt @@ -110,18 +109,18 @@ data class Server(val clients: List) { clients.joinToString(separator = "\n") { "${it.id} - Drift: ${it.serverData.totalDrift.toString(DurationUnit.MILLISECONDS)}" }, - now + now, ) log("Overall game drift: ${gameData.totalDrift}", now) log( "Sum of client lags: " + clients.sumOf { it.serverData.totalDrift.toMillisDouble() }.milliseconds, - now + now, ) log( "Client-perceived lag:\n" + clients.joinToString(separator = "\n") { "${it.id} - ${it.clientPerceivedLag}" }, - now + now, ) } diff --git a/src/main/java/com/hopskipnfall/TimeBasedDistributionLogger.kt b/src/main/java/com/hopskipnfall/TimeBasedDistributionLogger.kt index 3c7d9e0..969b00b 100644 --- a/src/main/java/com/hopskipnfall/TimeBasedDistributionLogger.kt +++ b/src/main/java/com/hopskipnfall/TimeBasedDistributionLogger.kt @@ -33,6 +33,6 @@ class TimeBasedDataLogger( fun buildDataFrame() = dataFrameOf( timeName to times, - *(data.map { (columnName, values) -> columnName to values }.toTypedArray()) + *(data.map { (columnName, values) -> columnName to values }.toTypedArray()), ) } diff --git a/src/main/java/com/hopskipnfall/Utils.kt b/src/main/java/com/hopskipnfall/Utils.kt index 28b6f4b..171a52f 100644 --- a/src/main/java/com/hopskipnfall/Utils.kt +++ b/src/main/java/com/hopskipnfall/Utils.kt @@ -24,7 +24,7 @@ fun Duration.toSecondsDouble(): Double = data class LognormalDistribution( val mean: Duration, val stdev: Duration, - private val random: Random + private val random: Random, ) : Distribution { override fun random(): Duration { @@ -57,7 +57,7 @@ data class NormalDistribution(val mean: Duration, val stdev: Duration, private v .asJavaRandom() .nextGaussian(/* mean= */ mean.toMillisDouble(), /* stddev= */ stdev.toMillisDouble()) .milliseconds, - Duration.ZERO + Duration.ZERO, ) } diff --git a/src/main/proto/lag.proto b/src/main/proto/lag.proto index 124fa4b..ff58932 100644 --- a/src/main/proto/lag.proto +++ b/src/main/proto/lag.proto @@ -10,14 +10,14 @@ import "google/protobuf/timestamp.proto"; option java_multiple_files = true; -enum PlayerNumber { +enum Player { // Note: In some languages having multiple enum values with the same // name causes issues so it's good to prefix with the name. - PLAYER_NUMBER_UNKNOWN = 0; - ONE = 1; - TWO = 2; - THREE = 3; - FOUR = 4; + PLAYER_UNSPECIFIED = 0; + PLAYER_ONE = 1; + PLAYER_TWO = 2; + PLAYER_THREE = 3; + PLAYER_FOUR = 4; } message Event { @@ -28,7 +28,7 @@ message Event { message GameStart { message PlayerDetails { - PlayerNumber player_number = 1; + Player player_number = 1; // User's average ping when they joined the server. double ping_ms = 2; @@ -42,7 +42,7 @@ message Event { } message ReceivedGameData { - PlayerNumber received_from = 1; + Player received_from = 1; } // Server has collected data from all parties for the frame and will @@ -59,7 +59,7 @@ message Event { double game_lag_ms = 2; message PlayerAttributedLag { - PlayerNumber player = 1; + Player player = 1; // How much of the drift can be reliably attributed to the // player.