Skip to content

Commit

Permalink
0.9.0
Browse files Browse the repository at this point in the history
* Add snake name to info endpoint
* Remove json prettyprint
  • Loading branch information
pambrose authored Jan 14, 2021
1 parent c74dcb5 commit 6e1499b
Show file tree
Hide file tree
Showing 10 changed files with 633 additions and 47 deletions.
10 changes: 6 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ plugins {
}

repositories {
maven { url 'https://kotlin.bintray.com/ktor' }
maven { url "https://kotlin.bintray.com/kotlinx" }
maven { url 'https://kotlin.bintray.com/kotlin-js-wrappers' }
maven { url = 'https://kotlin.bintray.com/ktor' }
maven { url = "https://kotlin.bintray.com/kotlinx" }
maven { url = 'https://kotlin.bintray.com/kotlin-js-wrappers' }
maven { url = 'https://maven-central.storage-download.googleapis.com/repos/central/data/' }
jcenter()
}
Expand All @@ -18,7 +18,7 @@ sourceCompatibility = 1.8

description = 'battlesnake-quickstart'
group = 'io.battlesnake'
version = '0.8.0'
version = '0.9.0'

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
Expand Down Expand Up @@ -52,12 +52,14 @@ artifacts {
compileKotlin {
kotlinOptions.jvmTarget = '1.8'
kotlinOptions.freeCompilerArgs += ['-Xuse-experimental=kotlin.time.ExperimentalTime',
'-Xuse-experimental=kotlin.ExperimentalStdlibApi',
'-Xuse-experimental=io.ktor.util.KtorExperimentalAPI']
}

compileTestKotlin {
kotlinOptions.jvmTarget = '1.8'
kotlinOptions.freeCompilerArgs += ['-Xuse-experimental=kotlin.time.ExperimentalTime',
'-Xuse-experimental=kotlin.ExperimentalStdlibApi',
'-Xuse-experimental=io.ktor.util.KtorExperimentalAPI']
}

Expand Down
168 changes: 168 additions & 0 deletions requests/Requests.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
POST http://localhost:8080/start
Content-Type: application/json

{
"game": {
"id": "76f989bf-9db6-4e4b-af17-62ca02b26f7d",
"ruleset": {
"name": "solo",
"version": "v1.0.15"
},
"timeout": 500
},
"turn": 0,
"board": {
"height": 7,
"width": 7,
"snakes": [
{
"id": "gs_7jvbwFT93wgFQ4f3mVdR8GtK",
"name": "PaulTest",
"latency": "",
"health": 100,
"body": [
{
"x": 3,
"y": 1
},
{
"x": 3,
"y": 1
},
{
"x": 3,
"y": 1
}
],
"head": {
"x": 3,
"y": 1
},
"length": 3,
"shout": ""
}
],
"food": [
{
"x": 4,
"y": 0
},
{
"x": 3,
"y": 3
}
],
"hazards": []
},
"you": {
"id": "gs_7jvbwFT93wgFQ4f3mVdR8GtK",
"name": "PaulTest",
"latency": "",
"health": 100,
"body": [
{
"x": 3,
"y": 1
},
{
"x": 3,
"y": 1
},
{
"x": 3,
"y": 1
}
],
"head": {
"x": 3,
"y": 1
},
"length": 3,
"shout": ""
}
}

###
POST http://localhost:8080/move
Content-Type: application/json

{
"game": {
"id": "76f989bf-9db6-4e4b-af17-62ca02b26f7d",
"ruleset": {
"name": "solo",
"version": "v1.0.15"
},
"timeout": 500
},
"turn": 0,
"board": {
"height": 7,
"width": 7,
"snakes": [
{
"id": "gs_7jvbwFT93wgFQ4f3mVdR8GtK",
"name": "PaulTest",
"latency": "",
"health": 100,
"body": [
{
"x": 3,
"y": 1
},
{
"x": 3,
"y": 1
},
{
"x": 3,
"y": 1
}
],
"head": {
"x": 3,
"y": 1
},
"length": 3,
"shout": ""
}
],
"food": [
{
"x": 4,
"y": 0
},
{
"x": 3,
"y": 3
}
],
"hazards": []
},
"you": {
"id": "gs_7jvbwFT93wgFQ4f3mVdR8GtK",
"name": "PaulTest",
"latency": "",
"health": 100,
"body": [
{
"x": 3,
"y": 1
},
{
"x": 3,
"y": 1
},
{
"x": 3,
"y": 1
}
],
"head": {
"x": 3,
"y": 1
},
"length": 3,
"shout": ""
}
}
5 changes: 4 additions & 1 deletion src/main/kotlin/io/battlesnake/core/AbstractBattleSnake.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ abstract class AbstractBattleSnake<T : SnakeContext> : KLogging() {
snakeContext()
.let { context ->
val startRequest = call.receive<StartRequest>()
logger.info { "Creating new snake context for ${startRequest.gameId}" }
context.resetStartTime()
context.assignIds(startRequest.gameId, startRequest.you.id)
context.assignRequestResponse(call)
Expand All @@ -88,7 +89,9 @@ abstract class AbstractBattleSnake<T : SnakeContext> : KLogging() {

val (response, duration) =
measureTimedValue {
strategy.moveActions.map { it.invoke(context, moveRequest) }.lastOrNull() ?: RIGHT
strategy.moveActions
.map { it.invoke(context, moveRequest) }
.lastOrNull() ?: throw IllegalStateException("Missing move action")
}

context.apply {
Expand Down
39 changes: 25 additions & 14 deletions src/main/kotlin/io/battlesnake/core/Json.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import kotlin.math.abs
val Int.isEven get() = this % 2 == 0
val Int.isOdd get() = this % 2 != 0

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

@Serializable
sealed class GameResponse
Expand All @@ -43,7 +43,7 @@ data class DescribeResponse private constructor(val author: String,
head: String = "default",
tail: String = "default") : this(author, color, head, tail, "1")

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

companion object {
fun toObject(s: String) = json.decodeFromString(serializer(), s)
Expand All @@ -55,15 +55,15 @@ data class StartRequest(val board: Board, val game: Game, val turn: Int, val you
val gameId
get() = game.id

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

companion object {
fun primeClassLoader() {
val start =
StartRequest(Board(3, 4, emptyList(), emptyList(), emptyList()),
Game(""),
Game("", Ruleset("", ""), 500),
1,
You("", "", emptyList(), 3, ""))
You(name = "", id = "", health = 3, body = emptyList(), latency = "", shout = ""))
val json = start.toJson()
toObject(json)
}
Expand All @@ -74,6 +74,8 @@ data class StartRequest(val board: Board, val game: Game, val turn: Int, val you

@Serializable
object StartResponse : GameResponse() {
fun toJson() = json.encodeToString(StartResponse.serializer(), this)

override fun toString() = StartResponse::class.simpleName ?: "StartResponse"
}

Expand All @@ -82,7 +84,6 @@ data class MoveRequest(val board: Board,
val game: Game,
val turn: Int,
val you: You) {

val gameId
get() = game.id

Expand Down Expand Up @@ -145,7 +146,7 @@ data class MoveRequest(val board: Board,
val headPosition
get() = you.headPosition

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

companion object {
fun toObject(s: String) = json.decodeFromString(serializer(), s)
Expand All @@ -154,7 +155,7 @@ data class MoveRequest(val board: Board,

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

companion object {
fun toObject(s: String) = json.decodeFromString(serializer(), s)
Expand All @@ -177,7 +178,7 @@ data class EndRequest(val board: Board,
val gameId
get() = game.id

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

companion object {
fun toObject(s: String) = json.decodeFromString(serializer(), s)
Expand All @@ -186,6 +187,8 @@ data class EndRequest(val board: Board,

@Serializable
class EndResponse : GameResponse() {
fun toJson() = json.encodeToString(serializer(), this)

override fun toString() = EndResponse::class.simpleName ?: "EndResponse"

companion object {
Expand All @@ -194,7 +197,10 @@ class EndResponse : GameResponse() {
}

@Serializable
data class Game(val id: String, val timeOutMillis: Int = 500)
data class Ruleset(val name: String, val version: String)

@Serializable
data class Game(val id: String, val ruleset: Ruleset, val timeout: Int)

@Serializable
data class Board(val height: Int,
Expand Down Expand Up @@ -228,7 +234,9 @@ data class Snake(val name: String,
val id: String,
val health: Int,
val body: List<Body>,
val shout: String) {
val latency: String,
val shout: String,
val squad: String = "") {
val headPosition
get() = bodyPosition(0)

Expand All @@ -241,10 +249,13 @@ data class Snake(val name: String,
@Serializable
data class You(val name: String,
val id: String,
val body: List<Body>,
val health: Int,
val shout: String) {
val headPosition by lazy { bodyPosition(0) }
val body: List<Body>,
val latency: String,
val shout: String,
val squad: String = "") {
val headPosition
get() = bodyPosition(0)

fun bodyPosition(pos: Int) = body[pos].position

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/battlesnake/core/ktor/Installs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fun Application.installs() {

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

install(StatusPages) {
Expand Down
Loading

0 comments on commit 6e1499b

Please sign in to comment.