Skip to content

Commit

Permalink
Updated version to 1.0.0
Browse files Browse the repository at this point in the history
Finished GameMechanic logic
  • Loading branch information
DrCorchit committed Nov 10, 2023
1 parent b8fd1a4 commit 7d6341e
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<artifactId>justice-core</artifactId>
<groupId>com.drcorchit</groupId>
<version>1.0-SNAPSHOT</version>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>justice-core</name>
Expand Down Expand Up @@ -90,7 +90,7 @@
<dependency>
<groupId>com.github.DrCorchit</groupId>
<artifactId>justice-utils</artifactId>
<version>1.1.3</version>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ abstract class AbstractElement(private val parent: AbstractMechanic<*>, name: St
}

override fun sync(info: JsonObject) {
this.info = info.deepCopy()
preSync(info)
this.info = info
postSync(info)
touch()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.drcorchit.justice.games

import com.drcorchit.utils.Logger
import com.drcorchit.utils.json.get
import com.drcorchit.utils.json.getOrDefault
import com.google.gson.JsonObject

private val log = Logger.getLogger(AbstractMechanic::class.java)
Expand Down Expand Up @@ -47,6 +47,7 @@ abstract class AbstractMechanic<T : AbstractElement>(game: Game, info: JsonObjec
val message = "Mechanic info for $name is missing \"elements\". Found keys: ${info.keySet()}"
throw IllegalArgumentException(message)
}
preSync(info)

elementsByName.clear()

Expand All @@ -59,7 +60,7 @@ abstract class AbstractMechanic<T : AbstractElement>(game: Game, info: JsonObjec

//Use "key" if present, otherwise default to name
val name = eleInfo["name"].asString
val key = eleInfo["key", { nextId() }, { it.asInt }]
val key = eleInfo.getOrDefault("key", { nextId() }, { it.asInt })

var element: T
if (has(key)) {
Expand All @@ -85,6 +86,7 @@ abstract class AbstractMechanic<T : AbstractElement>(game: Game, info: JsonObjec

//Remove all elements still marked for deletion
deleteThese.forEach { elementsById.remove(it.key) }
postSync(info)
val message = String.format("Synced AbstractMechanic $name", name)
log.info("sync", message)
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/com/drcorchit/justice/games/GameElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.drcorchit.utils.normalize
import com.google.gson.JsonElement
import com.google.gson.JsonObject

interface GameElement {
interface GameElement : Syncable<JsonObject> {
//The name of the object as seen by the client
//The name is encouraged to be unique. It may also change from time to time.
fun name(): String
Expand All @@ -28,8 +28,6 @@ interface GameElement {
parent().touch()
}

fun sync(info: JsonObject)

//Serializes the object so it can be recreated
fun serialize(): JsonElement {
return GSON.toJsonTree(this)
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/com/drcorchit/justice/games/GameMechanic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.drcorchit.justice.games
import com.drcorchit.utils.json.GSON
import com.google.gson.JsonObject

interface GameMechanic<T : GameElement> : Iterable<T> {
interface GameMechanic<T : GameElement> : Syncable<JsonObject>, Iterable<T> {
val name: String get() = javaClass.simpleName

val parent: Game
Expand All @@ -23,8 +23,6 @@ interface GameMechanic<T : GameElement> : Iterable<T> {
//Call this method whenever the mechanic is modified. This should also update lastModified
fun touch()

fun sync(info: JsonObject)

fun serialize(): JsonObject {
return GSON.toJsonTree(this).asJsonObject
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/kotlin/com/drcorchit/justice/games/GridElement.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.drcorchit.justice.games

import com.drcorchit.utils.json.getString
import com.drcorchit.utils.math.Space
import com.google.gson.JsonObject

abstract class GridElement(val parent: GridMechanic<*>, val coord: Space.Coordinate) : GameElement {

protected var info = JsonObject()

override fun name(): String {
return coord.toString()
}

override fun description(): String {
return info.getString("description", "No description is available.")
}

override fun parent(): GridMechanic<*> {
return parent
}

override fun sync(info: JsonObject) {
preSync(info)
this.info = info
postSync(info)
touch()
}
}
106 changes: 106 additions & 0 deletions src/main/kotlin/com/drcorchit/justice/games/GridMechanic.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.drcorchit.justice.games

import com.drcorchit.utils.Logger
import com.drcorchit.utils.json.getBool
import com.drcorchit.utils.json.getOrDefault
import com.drcorchit.utils.math.Grid
import com.drcorchit.utils.math.Layout
import com.drcorchit.utils.math.Space
import com.google.gson.JsonObject

private val log = Logger.getLogger(GridMechanic::class.java)

abstract class GridMechanic<T : GridElement>(game: Game, info: JsonObject, date: Long) : GameMechanic<T> {

init {
sync(info)
}

override val parent: Game = game

override var lastModified: Long = date

override var defaultElement: T? = null

override fun size(): Int {
return grid.space.size
}

override fun has(key: Any): Boolean {
return if (key is Space.Coordinate) {
grid.space.within(key.x, key.y)
} else throw java.lang.IllegalArgumentException("Supplied key is not a valid coordinate")
}

override fun get(key: Any): T {
if (key is Space.Coordinate) {
return grid.get(key)
} else {
throw IllegalArgumentException("Supplied key is not a valid coordinate")
}
}

override fun touch() {
lastModified = System.currentTimeMillis()
}

final override fun sync(info: JsonObject) {
if (!info.has("elements")) {
val message = "Mechanic info for $name is missing \"elements\". Found keys: ${info.keySet()}"
throw IllegalArgumentException(message)
}

preSync(info)

val rows = info.getAsJsonArray("elements").asJsonArray
if (rows.size() != grid.height) {
throw IllegalArgumentException("Incorrect grid size; expected ${grid.height} but got ${rows.size()}")
}

for ((j, row) in rows.withIndex()) {
val actualWidth = row.asJsonArray.size()
if (actualWidth != grid.width) {
throw java.lang.IllegalArgumentException("Incorrect grid size; expected ${grid.width} but got $actualWidth")
}
for ((i, ele) in row.asJsonArray.withIndex()) {
val eleInfo = ele.asJsonObject
val element: T = grid[i, j] ?: create(grid.space.coordinate(i, j))
element.sync(eleInfo)
}
}

defaultElement =
if (info.has("default")) {
val rawKey = info["default"].asJsonPrimitive
if (rawKey.isString) get(rawKey.asString)
else if (rawKey.isNumber) get(rawKey.asInt)
else throw IllegalArgumentException("That type of JsonPrimitive cannot specify a default element")
} else {
null
}

postSync(info)
val message = String.format("Synced GridMechanic $name", name)
log.info("sync", message)
}


override fun iterator(): Iterator<T> {
return grid.iterator()
}

//New (non-inherited) functionality

val grid: Grid<T> by lazy {
val w = info["width"].asInt
val h = info["height"].asInt
val wrapH = info.getBool("wrapHoriz", false)
val wrapV = info.getBool("wrapVert", false)
val layout = info.getOrDefault("layout", { Layout.CARTESIAN }, { Layout.valueOf(it.asString) })
val space = Space(w, h, wrapH, wrapV, layout)
Grid(space)
}

abstract fun create(coordinate: Space.Coordinate): T

}
13 changes: 13 additions & 0 deletions src/main/kotlin/com/drcorchit/justice/games/Syncable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.drcorchit.justice.games

interface Syncable<T: Any> {

//This method is not intended to be called except by sync()
fun preSync(info: T)

fun sync(info: T)

//This method is not intended to be called except by sync()
fun postSync(info: T)

}

0 comments on commit 7d6341e

Please sign in to comment.