diff --git a/Makefile b/Makefile index 646af1d..b24d5d3 100644 --- a/Makefile +++ b/Makefile @@ -15,4 +15,4 @@ versioncheck: ./gradlew dependencyUpdates upgrade-wrapper: - ./gradlew wrapper --gradle-version=6.8 --distribution-type=bin \ No newline at end of file + ./gradlew wrapper --gradle-version=6.8.1 --distribution-type=bin \ No newline at end of file diff --git a/build.gradle b/build.gradle index 19c81c5..c9267fb 100644 --- a/build.gradle +++ b/build.gradle @@ -1,8 +1,8 @@ plugins { id 'java' id 'maven' - id 'org.jetbrains.kotlin.jvm' version '1.4.30-M1' - id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.30-M1' + id 'org.jetbrains.kotlin.jvm' version '1.4.30' + id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.30' id "com.github.ben-manes.versions" version "0.36.0" } @@ -18,7 +18,7 @@ sourceCompatibility = 1.8 description = 'battlesnake-quickstart' group = 'io.battlesnake' -version = '0.9.0' +version = '0.10.0' dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index ce24709..28ff446 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,20 +1,5 @@ -# -# Copyright © 2021 Paul Ambrose (pambrose@mac.com) -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index cb06363..4f906e0 100755 --- a/gradlew +++ b/gradlew @@ -1,13 +1,13 @@ #!/usr/bin/env sh # -# Copyright © 2021 Paul Ambrose (pambrose@mac.com) +# Copyright 2015 the original author or authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/gradlew.bat b/gradlew.bat index 107acd3..ac1b06f 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,89 +1,89 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "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. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -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. - -goto fail - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="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 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "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. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +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. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="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 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/requests/Requests.http b/requests/Requests.http index 07aaa99..87edae3 100644 --- a/requests/Requests.http +++ b/requests/Requests.http @@ -1,3 +1,9 @@ +GET http://localhost:8080/ + +### +GET http://localhost:8080/info + +### POST http://localhost:8080/start Content-Type: application/json @@ -165,4 +171,16 @@ Content-Type: application/json "length": 3, "shout": "" } -} \ No newline at end of file +} + +### +GET http://localhost:8080/left + +### +GET http://localhost:8080/right + +### +GET http://localhost:8080/up + +### +GET http://localhost:8080/down diff --git a/src/main/kotlin/io/battlesnake/core/AbstractBattleSnake.kt b/src/main/kotlin/io/battlesnake/core/AbstractBattleSnake.kt index 3972d3a..e137fc2 100644 --- a/src/main/kotlin/io/battlesnake/core/AbstractBattleSnake.kt +++ b/src/main/kotlin/io/battlesnake/core/AbstractBattleSnake.kt @@ -70,7 +70,7 @@ abstract class AbstractBattleSnake : KLogging() { snakeContext() .let { context -> val startRequest = call.receive() - logger.info { "Creating new snake context for ${startRequest.gameId}" } + //logger.info { "Creating new snake context for ${startRequest.gameId}" } context.resetStartTime() context.assignIds(startRequest.gameId, startRequest.you.id) context.assignRequestResponse(call) diff --git a/src/main/kotlin/io/battlesnake/core/ktor/Routes.kt b/src/main/kotlin/io/battlesnake/core/ktor/Routes.kt index c54695e..da788ad 100644 --- a/src/main/kotlin/io/battlesnake/core/ktor/Routes.kt +++ b/src/main/kotlin/io/battlesnake/core/ktor/Routes.kt @@ -32,7 +32,7 @@ import io.battlesnake.core.right import io.battlesnake.core.up import io.ktor.application.* import io.ktor.html.* -import io.ktor.http.* +import io.ktor.http.ContentType.Application.Json import io.ktor.response.* import io.ktor.routing.* import kotlinx.html.Entities.nbsp @@ -47,14 +47,9 @@ import kotlinx.html.id import kotlinx.html.script import kotlinx.html.span import kotlinx.html.unsafe -import mu.KLogging - -object Routes : KLogging() fun Application.routes(snake: AbstractBattleSnake<*>) { - routing { - get(INFO) { call.respondHtml { head {} @@ -76,53 +71,49 @@ fun Application.routes(snake: AbstractBattleSnake<*>) { } get(DESCRIBE) { + // We call response.toJson() to avoid ktor adding the "type" property to the json val response = snake.process(call) as DescribeResponse - call.respondText(response.toJson(), ContentType.Application.Json) + call.respondText(response.toJson(), Json) } post(DESCRIBE) { val response = snake.process(call) as DescribeResponse - call.respondText(response.toJson(), ContentType.Application.Json) + call.respondText(response.toJson(), Json) } post(START) { - call.response.headers.append(HttpHeaders.CacheControl, "no-cache, no-store") val response = snake.process(call) as StartResponse - call.respondText(response.toJson(), ContentType.Application.Json) + call.respondText(response.toJson(), Json) } post(MOVE) { - call.response.headers.append(HttpHeaders.CacheControl, "no-cache, no-store") val response = snake.process(call) as MoveResponse - val json = response.toJson() - Routes.logger.info { "Return value:\n$json" } - call.respondText(json, ContentType.Application.Json) + call.respondText(response.toJson(), Json) } post(END) { - call.response.headers.append(HttpHeaders.CacheControl, "no-cache, no-store") val response = snake.process(call) as EndResponse - call.respondText(response.toJson(), ContentType.Application.Json) + call.respondText(response.toJson(), Json) } get("/left") { val response = left("Going left") - call.respond(response) + call.respondText(response.toJson(), Json) } get("/right") { val response = right("Going right") - call.respond(response) + call.respondText(response.toJson(), Json) } get("/up") { val response = up("Going up") - call.respond(response) + call.respondText(response.toJson(), Json) } get("/down") { val response = down("Going down") - call.respond(response) + call.respondText(response.toJson(), Json) } } }