Skip to content

Commit

Permalink
0.8.0
Browse files Browse the repository at this point in the history
* Upgrade jars
* Fix apiversion
* Add shout to moves
  • Loading branch information
pambrose authored Jan 13, 2021
1 parent 7666560 commit c74dcb5
Show file tree
Hide file tree
Showing 22 changed files with 110 additions and 140 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gradlew binary
gradlew.bat binary
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [2017] [Paul Ambrose]
Copyright [2021] [Paul Ambrose]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ versioncheck:
./gradlew dependencyUpdates

upgrade-wrapper:
./gradlew wrapper --gradle-version=6.6.1 --distribution-type=bin
./gradlew wrapper --gradle-version=6.8 --distribution-type=bin
23 changes: 4 additions & 19 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
/*
* Copyright © 2020 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.
*/

plugins {
id 'java'
id 'maven'
id 'org.jetbrains.kotlin.jvm' version '1.4.0'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.0'
id "com.github.ben-manes.versions" version "0.29.0"
id 'org.jetbrains.kotlin.jvm' version '1.4.30-M1'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.30-M1'
id "com.github.ben-manes.versions" version "0.36.0"
}

repositories {
Expand All @@ -38,6 +22,7 @@ version = '0.8.0'

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-core:$serialization_version"

implementation "io.ktor:ktor-server-cio:$ktor_version"
implementation "io.ktor:ktor-server-core:$ktor_version"
Expand Down
24 changes: 5 additions & 19 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
#
# Copyright © 2020 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.
#
kotlin.code.style=official
kotlin.incremental=true
org.gradle.daemon=true
org.gradle.configureondemand=true
org.gradle.parallel=true
org.gradle.caching=true
junit_version=5.6.1
kluent_version=1.61
ktor_version=1.4.0
junit_version=5.7.0
kluent_version=1.65
ktor_version=1.5.0
logback_version=1.2.3
logging_version=1.8.3
logging_version=2.0.4
serialization_version=1.0.1
slf4j_version=1.7.28
17 changes: 16 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
#
# 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.6.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 2 additions & 2 deletions gradlew
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env sh

#
# Copyright 2015 the original author or authors.
# 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
#
# https://www.apache.org/licenses/LICENSE-2.0
# 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,
Expand Down
16 changes: 0 additions & 16 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* Copyright © 2020 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.
*/

pluginManagement {
resolutionStrategy {
eachPlugin {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/battlesnake/core/JavaConstants.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 Paul Ambrose (pambrose@mac.com)
* 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.
Expand Down
14 changes: 6 additions & 8 deletions src/main/kotlin/io/battlesnake/core/AbstractBattleSnake.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 Paul Ambrose (pambrose@mac.com)
* 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.
Expand All @@ -20,13 +20,11 @@ package io.battlesnake.core

import io.battlesnake.core.ktor.installs
import io.battlesnake.core.ktor.routes
import io.ktor.application.Application
import io.ktor.application.ApplicationCall
import io.ktor.features.origin
import io.ktor.request.receive
import io.ktor.request.uri
import io.ktor.server.cio.CIO
import io.ktor.server.engine.embeddedServer
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.request.*
import io.ktor.server.cio.*
import io.ktor.server.engine.*
import mu.KLogging
import java.util.concurrent.ConcurrentHashMap
import kotlin.time.measureTimedValue
Expand Down
9 changes: 3 additions & 6 deletions src/main/kotlin/io/battlesnake/core/AbstractGameStrategy.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 Paul Ambrose (pambrose@mac.com)
* 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.
Expand All @@ -18,11 +18,10 @@

package io.battlesnake.core

import io.ktor.application.ApplicationCall
import io.ktor.application.*
import kotlin.time.Duration

abstract class AbstractGameStrategy<T : SnakeContext>(private val verbose: Boolean = false) : GameStrategy<T>() {

init {
onDescribe { call: ApplicationCall ->
logger.info { describeMsg(call) }
Expand Down Expand Up @@ -50,9 +49,7 @@ abstract class AbstractGameStrategy<T : SnakeContext>(private val verbose: Boole

open fun onDescribe(call: ApplicationCall) = DescribeResponse()

open fun onStart(context: T, request: StartRequest) {
return
}
open fun onStart(context: T, request: StartRequest) {}

abstract fun onMove(context: T, request: MoveRequest): MoveResponse

Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/io/battlesnake/core/GameStrategy.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 Paul Ambrose (pambrose@mac.com)
* 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.
Expand All @@ -22,9 +22,9 @@ import io.battlesnake.core.GameStrategy.Companion.afterTurnMsg
import io.battlesnake.core.GameStrategy.Companion.describeMsg
import io.battlesnake.core.GameStrategy.Companion.endMsg
import io.battlesnake.core.GameStrategy.Companion.startMsg
import io.ktor.application.ApplicationCall
import io.ktor.features.origin
import io.ktor.request.uri
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.request.*
import mu.KLogging
import kotlin.time.Duration
import kotlin.time.milliseconds
Expand Down Expand Up @@ -108,8 +108,7 @@ open class GameStrategy<T : SnakeContext> : KLogging() {
call: ApplicationCall,
gameResponse: GameResponse,
duration: Duration): String =
"Responded to ${call.request.uri} in $duration with: " +
(if (gameResponse is MoveResponse) gameResponse.move.toUpperCase() else "$gameResponse") +
"Responded to ${call.request.uri} in $duration with: $gameResponse" +
(context?.let { " [${context.snakeId}]" } ?: "")
}
}
33 changes: 23 additions & 10 deletions src/main/kotlin/io/battlesnake/core/Json.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 Paul Ambrose (pambrose@mac.com)
* 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.
Expand Down Expand Up @@ -29,14 +29,19 @@ val Int.isOdd get() = this % 2 != 0

private val json = Json { ignoreUnknownKeys = true; isLenient = true }

interface GameResponse
@Serializable
sealed class GameResponse

@Serializable
data class DescribeResponse(val author: String = "",
val color: String = "#888888",
val headType: String = "default",
val tailType: String = "default") : GameResponse {
val apiversion = "1"
data class DescribeResponse private constructor(val author: String,
val color: String,
val head: String,
val tail: String,
val apiversion: String) : GameResponse() {
constructor(author: String = "",
color: String = "#888888",
head: String = "default",
tail: String = "default") : this(author, color, head, tail, "1")

fun toJson() = Json.encodeToString(serializer(), this)

Expand Down Expand Up @@ -68,7 +73,7 @@ data class StartRequest(val board: Board, val game: Game, val turn: Int, val you
}

@Serializable
object StartResponse : GameResponse {
object StartResponse : GameResponse() {
override fun toString() = StartResponse::class.simpleName ?: "StartResponse"
}

Expand Down Expand Up @@ -148,14 +153,22 @@ data class MoveRequest(val board: Board,
}

@Serializable
data class MoveResponse(val move: String) : GameResponse {
data class MoveResponse(val move: String, val shout: String = "") : GameResponse() {
fun toJson() = Json.encodeToString(serializer(), this)

companion object {
fun toObject(s: String) = json.decodeFromString(serializer(), s)
}

override fun toString() =
move.toUpperCase() + if (shout.isNotEmpty()) " ($shout)" else ""
}

fun up(shout: String) = MoveResponse("up", shout)
fun down(shout: String) = MoveResponse("down", shout)
fun left(shout: String) = MoveResponse("left", shout)
fun right(shout: String) = MoveResponse("right", shout)

@Serializable
data class EndRequest(val board: Board,
val game: Game,
Expand All @@ -172,7 +185,7 @@ data class EndRequest(val board: Board,
}

@Serializable
class EndResponse : GameResponse {
class EndResponse : GameResponse() {
override fun toString() = EndResponse::class.simpleName ?: "EndResponse"

companion object {
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/io/battlesnake/core/KotlinConstants.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 Paul Ambrose (pambrose@mac.com)
* 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.
Expand All @@ -25,4 +25,4 @@ const val END = "/end"
val UP = MoveResponse("up")
val DOWN = MoveResponse("down")
val LEFT = MoveResponse("left")
val RIGHT = MoveResponse("right")
val RIGHT = MoveResponse("right")
4 changes: 2 additions & 2 deletions src/main/kotlin/io/battlesnake/core/SnakeContext.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 Paul Ambrose (pambrose@mac.com)
* 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.
Expand All @@ -18,7 +18,7 @@

package io.battlesnake.core

import io.ktor.application.ApplicationCall
import io.ktor.application.*
import kotlin.time.TimeSource
import kotlin.time.seconds

Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/io/battlesnake/core/ktor/Installs.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 Paul Ambrose (pambrose@mac.com)
* 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.
Expand Down Expand Up @@ -43,7 +43,7 @@ fun Application.installs() {

install(ContentNegotiation) {
json(contentType = ContentType.Application.Json,
json = Json { ignoreUnknownKeys = true; prettyPrint = true })
json = Json { ignoreUnknownKeys = true; prettyPrint = true; isLenient = true; })
}

install(StatusPages) {
Expand All @@ -54,7 +54,6 @@ fun Application.installs() {
call.respond(HttpStatusCode.Forbidden)
}
}

}

class AuthenticationException : RuntimeException()
Expand Down
Loading

0 comments on commit c74dcb5

Please sign in to comment.