pipes: detekt clean (103 tests)
- Commit
a64978c170fc31a092f5253178361686c2b8d75c- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-14 23:23 UTC (21 days ago)
- Parents
da1b4b2
Fix all detekt findings in pipes by fixing code, not config (detekt.yml untouched, no baselines added): - UndocumentedPublicClass: KDoc for 25 types. - LongMethod/CyclomaticComplexMethod: levelFromJson and the test PackFixtures.levelFrom decomposed into named section parsers (cells/adjacency/pairs/merge/render/ endpoints), same parsing behaviour. - MaxLineLength: wrapped the LevelData construction. Verified: ./gradlew :games:pipes:detekt :games:pipes:testDevUnitTest :games:pipes:lint --max-workers=32 all pass (103 unit tests).
Diff
games/pipes/src/main/java/com/vayunmathur/games/pipes/MainActivity.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/MainActivity.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/MainActivity.ktindex d6daf1ca14..7004df651b 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/MainActivity.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/MainActivity.kt@@ -7,6 +7,7 @@ import com.vayunmathur.games.pipes.platform.PipesViewModel import com.vayunmathur.games.pipes.ui.PipesTheme import com.vayunmathur.library.ui.MaActivity +/** Main Activity: a ma activity implementation for main activity. */ class MainActivity : MaActivity() { override fun onCreateBeforeContent() { LevelPack.init(this)
games/pipes/src/main/java/com/vayunmathur/games/pipes/Route.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/Route.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/Route.ktindex 21d0718b03..04fc285a3d 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/Route.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/Route.kt@@ -3,20 +3,28 @@ package com.vayunmathur.games.pipes import com.vayunmathur.library.util.NavKey import kotlinx.serialization.Serializable +/** Contract for route. */ @Serializable sealed interface Route : NavKey {+ /** Singleton holding pack selector state and operations. */ @Serializable data object PackSelector : Route+ /** Immutable value describing level selector; carries packIndex. */ @Serializable data class LevelSelector(val packIndex: Int) : Route+ /** Immutable value describing game; carries packIndex, levelIndex. */ @Serializable data class Game(val packIndex: Int, val levelIndex: Int) : Route+ /** Singleton holding daily selector state and operations. */ @Serializable data object DailySelector : Route+ /** Immutable value describing daily game; carries levelIndex. */ @Serializable data class DailyGame(val levelIndex: Int) : Route+ /** Singleton holding game center state and operations. */ @Serializable data object GameCenter : Route+ /** Singleton holding settings state and operations. */ @Serializable data object Settings : Route }
games/pipes/src/main/java/com/vayunmathur/games/pipes/data/CompletedLevelsRepository.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/CompletedLevelsRepository.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/CompletedLevelsRepository.ktindex b2d850a750..33897c0619 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/CompletedLevelsRepository.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/CompletedLevelsRepository.kt@@ -3,6 +3,7 @@ package com.vayunmathur.games.pipes.data import android.content.Context import com.vayunmathur.library.util.LevelStatsRepository +/** Completed Levels Repository: a context implementation for completed levels repository. */ class CompletedLevelsRepository(context: Context) : LevelStatsRepository(context) { fun incrementTotalPipesPlaced() = incrementCounter("total_pipes") fun getTotalPipesPlaced(): Int = getCounter("total_pipes")
games/pipes/src/main/java/com/vayunmathur/games/pipes/data/PipeModels.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/PipeModels.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/PipeModels.ktindex fbbd8eb6aa..d289103f2f 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/PipeModels.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/PipeModels.kt@@ -9,12 +9,16 @@ import kotlinx.serialization.json.jsonArray import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonPrimitive +/** Immutable value describing cell pos; carries row, col. */ data class CellPos(val row: Int, val col: Int) +/** Immutable value describing render pos; carries x, y. */ data class RenderPos(val x: Float, val y: Float) +/** Immutable value describing endpoint pair; carries colorIndex, cells. */ data class EndpointPair(val colorIndex: Int, val cells: List<CellPos>) +/** Immutable value describing level data. */ data class LevelData( val id: String, val rows: Int,@@ -29,11 +33,13 @@ data class LevelData( val grid: String = "square" ) +/** Immutable value describing level pack. */ data class LevelPack( val name: String, val shape: String, val levels: List<LevelData> ) {+ /** Shared constants and helpers for LevelPack. */ companion object { private val PACK_FILES = listOf( "packs/5x5.json",@@ -145,65 +151,99 @@ private fun levelFromJson(json: kotlinx.serialization.json.JsonObject): LevelDat // "grid": "hex" derives odd-r six-neighbour adjacency + staggered centers, so hex packs only // declare cells + endpoints. Anything else is square with an optional explicit adjacency. val grid = json["grid"]?.jsonPrimitive?.content ?: "square"-- val cells = json["cells"]?.jsonArray?.map {- val arr = it.jsonArray- CellPos(arr[0].jsonPrimitive.int, arr[1].jsonPrimitive.int)- }?.toSet() ?: rectangularCells(rows, cols)-- val adjacency = json["adjacency"]?.jsonObject?.map { (key, value) ->- val parts = key.split(",")- val cell = CellPos(parts[0].toInt(), parts[1].toInt())- val neighbors = value.jsonArray.map {- val arr = it.jsonArray- CellPos(arr[0].jsonPrimitive.int, arr[1].jsonPrimitive.int)- }- cell to neighbors- }?.toMap() ?: if (grid == "hex") computeHexAdjacency(cells) else computeAdjacency(cells)-+ val cells = cellsFromJson(json, rows, cols)+ val adjacency = adjacencyFromJson(json, cells, grid) // Portals as paired cells: "portals": [[[r,c],[r,c]], ...]. Each pair is bidirectional. // Transit is a normal adjacency step: the links are unioned into the adjacency below, and // isValidStep accepts them like any other edge.- val portals = json["portals"]?.jsonArray?.flatMap { pair ->- val ends = pair.jsonArray.map { c ->- val arr = c.jsonArray- CellPos(arr[0].jsonPrimitive.int, arr[1].jsonPrimitive.int)- }- if (ends.size == 2) listOf(ends[0] to ends[1], ends[1] to ends[0]) else emptyList()- }?.toMap() ?: emptyMap()-+ val portals = pairsFromJson(json, "portals").toMap() // Extra long-range edges (wrap/torus): "extraEdges": [[[r,c],[r,c]], ...], bidirectional.- val extraEdges = json["extraEdges"]?.jsonArray?.flatMap { pair ->- val ends = pair.jsonArray.map { c ->- val arr = c.jsonArray- CellPos(arr[0].jsonPrimitive.int, arr[1].jsonPrimitive.int)- }- if (ends.size == 2) listOf(ends[0] to ends[1], ends[1] to ends[0]) else emptyList()- } ?: emptyList()-- val fullAdjacency = if (portals.isEmpty() && extraEdges.isEmpty()) {- adjacency- } else {- val merged = adjacency.mapValues { (_, neighbors) -> neighbors.toMutableSet() }.toMutableMap()- for ((a, b) in portals) {- merged.getOrPut(a) { mutableSetOf() }.add(b)- }- for ((a, b) in extraEdges) {- merged.getOrPut(a) { mutableSetOf() }.add(b)- }- for (cell in cells) merged.putIfAbsent(cell, mutableSetOf())- merged.mapValues { (_, neighbors) -> neighbors.toList() }- }+ val extraEdges = pairsFromJson(json, "extraEdges")+ val fullAdjacency = mergedAdjacency(adjacency, cells, portals, extraEdges)+ val renderPositions = renderPositionsFromJson(json, cells, grid)+ val endpoints = endpointsFromJson(json)+ val bridges = json["bridges"]?.jsonArray?.map {+ val arr = it.jsonArray+ CellPos(arr[0].jsonPrimitive.int, arr[1].jsonPrimitive.int)+ }?.toSet() ?: emptySet()+ val optimalMoves = json["optimalMoves"]!!.jsonPrimitive.int+ return LevelData(+ id, rows, cols, cells, fullAdjacency, renderPositions, endpoints, bridges, optimalMoves, portals, grid,+ )+} - val renderPositions = json["renderPositions"]?.jsonObject?.map { (key, value) ->- val parts = key.split(",")- val cell = CellPos(parts[0].toInt(), parts[1].toInt())- val posObj = value.jsonObject- val pos = RenderPos(posObj["x"]!!.jsonPrimitive.float, posObj["y"]!!.jsonPrimitive.float)- cell to pos- }?.toMap() ?: if (grid == "hex") hexRenderPositions(cells) else null+/** Declared cells, or the full rows x cols rectangle when omitted. */+private fun cellsFromJson(+ json: kotlinx.serialization.json.JsonObject,+ rows: Int,+ cols: Int,+): Set<CellPos> = json["cells"]?.jsonArray?.map {+ val arr = it.jsonArray+ CellPos(arr[0].jsonPrimitive.int, arr[1].jsonPrimitive.int)+}?.toSet() ?: rectangularCells(rows, cols)++/** Declared adjacency, or the grid-derived adjacency when omitted. */+private fun adjacencyFromJson(+ json: kotlinx.serialization.json.JsonObject,+ cells: Set<CellPos>,+ grid: String,+): Map<CellPos, List<CellPos>> = json["adjacency"]?.jsonObject?.map { (key, value) ->+ val parts = key.split(",")+ val cell = CellPos(parts[0].toInt(), parts[1].toInt())+ val neighbors = value.jsonArray.map {+ val arr = it.jsonArray+ CellPos(arr[0].jsonPrimitive.int, arr[1].jsonPrimitive.int)+ }+ cell to neighbors+}?.toMap() ?: if (grid == "hex") computeHexAdjacency(cells) else computeAdjacency(cells)++/** Bidirectional cell pairs declared under [key] ("portals" / "extraEdges"). */+private fun pairsFromJson(+ json: kotlinx.serialization.json.JsonObject,+ key: String,+): List<Pair<CellPos, CellPos>> = json[key]?.jsonArray?.flatMap { pair ->+ val ends = pair.jsonArray.map { c ->+ val arr = c.jsonArray+ CellPos(arr[0].jsonPrimitive.int, arr[1].jsonPrimitive.int)+ }+ if (ends.size == 2) listOf(ends[0] to ends[1], ends[1] to ends[0]) else emptyList()+} ?: emptyList()++/** Base adjacency unioned with portals and extra edges. */+private fun mergedAdjacency(+ adjacency: Map<CellPos, List<CellPos>>,+ cells: Set<CellPos>,+ portals: Map<CellPos, CellPos>,+ extraEdges: List<Pair<CellPos, CellPos>>,+): Map<CellPos, List<CellPos>> {+ if (portals.isEmpty() && extraEdges.isEmpty()) return adjacency+ val merged = adjacency.mapValues { (_, neighbors) -> neighbors.toMutableSet() }.toMutableMap()+ for ((a, b) in portals) {+ merged.getOrPut(a) { mutableSetOf() }.add(b)+ }+ for ((a, b) in extraEdges) {+ merged.getOrPut(a) { mutableSetOf() }.add(b)+ }+ for (cell in cells) merged.putIfAbsent(cell, mutableSetOf())+ return merged.mapValues { (_, neighbors) -> neighbors.toList() }+} - val endpoints = json["endpoints"]!!.jsonArray.mapIndexed { index, endpoint ->+/** Declared render positions, or hex-derived ones for hex grids. */+private fun renderPositionsFromJson(+ json: kotlinx.serialization.json.JsonObject,+ cells: Set<CellPos>,+ grid: String,+): Map<CellPos, RenderPos>? = json["renderPositions"]?.jsonObject?.map { (key, value) ->+ val parts = key.split(",")+ val cell = CellPos(parts[0].toInt(), parts[1].toInt())+ val posObj = value.jsonObject+ val pos = RenderPos(posObj["x"]!!.jsonPrimitive.float, posObj["y"]!!.jsonPrimitive.float)+ cell to pos+}?.toMap() ?: if (grid == "hex") hexRenderPositions(cells) else null++/** Endpoint pairs, colour defaulting to declaration order. */+private fun endpointsFromJson(json: kotlinx.serialization.json.JsonObject): List<EndpointPair> =+ json["endpoints"]!!.jsonArray.mapIndexed { index, endpoint -> val epObj = endpoint.jsonObject val colorIndex = epObj["color"]?.jsonPrimitive?.intOrNull ?: index val epCells = epObj["cells"]!!.jsonArray.map { c ->@@ -212,13 +252,3 @@ private fun levelFromJson(json: kotlinx.serialization.json.JsonObject): LevelDat } EndpointPair(colorIndex, epCells) }-- val bridges = json["bridges"]?.jsonArray?.map {- val arr = it.jsonArray- CellPos(arr[0].jsonPrimitive.int, arr[1].jsonPrimitive.int)- }?.toSet() ?: emptySet()-- val optimalMoves = json["optimalMoves"]!!.jsonPrimitive.int-- return LevelData(id, rows, cols, cells, fullAdjacency, renderPositions, endpoints, bridges, optimalMoves, portals, grid)-}
games/pipes/src/main/java/com/vayunmathur/games/pipes/data/ProgressRepository.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/ProgressRepository.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/ProgressRepository.ktindex 5635268fd2..70a32a3e38 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/ProgressRepository.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/ProgressRepository.kt@@ -44,6 +44,7 @@ class ProgressRepository(context: Context) { private fun key(levelId: String) = "level_$levelId" + /** Shared constants and helpers for ProgressRepository. */ companion object { const val PREFS_NAME = "pipes_progress" private const val ORDER_KEY = "order"
games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/AppBackupAgent.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/AppBackupAgent.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/AppBackupAgent.ktindex a4ec9b9661..2c87e7d13e 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/AppBackupAgent.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/AppBackupAgent.kt@@ -2,6 +2,7 @@ package com.vayunmathur.games.pipes.platform import com.vayunmathur.library.util.BaseBackupAgent +/** App Backup Agent: a base backup agent implementation for app backup agent. */ class AppBackupAgent : BaseBackupAgent() { override val prefNames: List<String> get() = listOf("level_stats", "daily_stats", "pipes_progress")
games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesAchievementsManager.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesAchievementsManager.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesAchievementsManager.ktindex 28ee68ac19..6d9e9e5fb2 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesAchievementsManager.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesAchievementsManager.kt@@ -8,6 +8,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +/** Manages pipes achievements. */ class PipesAchievementsManager( context: Context, json: String,
games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesUiContract.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesUiContract.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesUiContract.ktindex b3d4550fb1..a32133b850 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesUiContract.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesUiContract.kt@@ -60,6 +60,7 @@ interface PipesActions { fun commitDraw() {} fun onUndo() {} fun onRestart() {}+ /** Shared constants and helpers for PipesActions. */ companion object { val Noop: PipesActions = object : PipesActions {} }@@ -79,6 +80,7 @@ interface SettingsActions { fun setReminderEnabled(enabled: Boolean) {} fun setReminderTime(hour: Int, minute: Int) {} + /** Shared constants and helpers for SettingsActions. */ companion object { val Noop: SettingsActions = object : SettingsActions {} }
games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.ktindex 12fcb10725..896b7794e4 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt@@ -37,6 +37,7 @@ import kotlinx.coroutines.withContext /** The board position the screens draw; the pure model lives in domain/. */ typealias PipesGameState = BoardState +/** Immutable value describing pipes ui state. */ data class PipesUiState( val packIndex: Int = -1, val levelIndex: Int = -1,@@ -54,6 +55,7 @@ data class PipesUiState( val leftStart: Boolean = false, ) +/** Pipes View Model: a application implementation for pipes view model. */ class PipesViewModel(application: Application) : AndroidViewModel(application), PipesActions { val repository: CompletedLevelsRepository = CompletedLevelsRepository(application)@@ -425,6 +427,7 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), achievementsManager.dismissNotification() } + /** Shared constants and helpers for PipesViewModel. */ companion object { const val KEY_COLORBLIND = "pipes_colorblind"
games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/PipeRenderer.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/PipeRenderer.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/PipeRenderer.ktindex 79f68ff292..6afbe0520d 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/PipeRenderer.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/PipeRenderer.kt@@ -9,6 +9,7 @@ import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.drawscope.Stroke import androidx.compose.ui.graphics.nativeCanvas +/** Kinds of direction. */ enum class Direction { UP, DOWN, LEFT, RIGHT } /** Six pipe exits for pointy-top hex cells (screen coords, y down): E=0°, SE=60°, SW=120°, W=180°, NW=240°, NE=300°. */
games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/PackFixtures.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/PackFixtures.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/PackFixtures.ktindex 59b5ce5783..61c3943647 100644--- a/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/PackFixtures.kt+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/PackFixtures.kt@@ -35,51 +35,13 @@ object PackFixtures { val rows = json["rows"]!!.jsonPrimitive.int val cols = json["cols"]!!.jsonPrimitive.int val grid = json["grid"]?.jsonPrimitive?.content ?: "square"- val cells = json["cells"]?.jsonArray?.map { cellFrom(it) }?.toSet()- ?: buildSet { for (r in 0 until rows) for (c in 0 until cols) add(CellPos(r, c)) }- val adjacency = json["adjacency"]?.jsonObject?.map { (key, value) ->- val parts = key.split(",")- val cell = CellPos(parts[0].toInt(), parts[1].toInt())- cell to value.jsonArray.map { cellFrom(it) }- }?.toMap() ?: if (grid == "hex") {- com.vayunmathur.games.pipes.data.computeHexAdjacency(cells)- } else {- computeAdjacency(cells)- }- val portals = json["portals"]?.jsonArray?.flatMap { pair ->- val ends = pair.jsonArray.map { cellFrom(it) }- if (ends.size == 2) listOf(ends[0] to ends[1], ends[1] to ends[0]) else emptyList()- }?.toMap() ?: emptyMap()- val extraEdges = json["extraEdges"]?.jsonArray?.flatMap { pair ->- val ends = pair.jsonArray.map { cellFrom(it) }- if (ends.size == 2) listOf(ends[0] to ends[1], ends[1] to ends[0]) else emptyList()- } ?: emptyList()- val fullAdjacency = if (portals.isEmpty() && extraEdges.isEmpty()) {- adjacency- } else {- val merged = adjacency.mapValues { (_, neighbors) -> neighbors.toMutableSet() }.toMutableMap()- for ((a, b) in portals) merged.getOrPut(a) { mutableSetOf() }.add(b)- for ((a, b) in extraEdges) merged.getOrPut(a) { mutableSetOf() }.add(b)- for (cell in cells) merged.putIfAbsent(cell, mutableSetOf())- merged.mapValues { (_, neighbors) -> neighbors.toList() }- }- val renderPositions = json["renderPositions"]?.jsonObject?.map { (key, value) ->- val parts = key.split(",")- val cell = CellPos(parts[0].toInt(), parts[1].toInt())- val posObj = value.jsonObject- cell to RenderPos(posObj["x"]!!.jsonPrimitive.float, posObj["y"]!!.jsonPrimitive.float)- }?.toMap() ?: if (grid == "hex") {- com.vayunmathur.games.pipes.data.hexRenderPositions(cells)- } else {- null- }- val endpoints = json["endpoints"]!!.jsonArray.mapIndexed { index, element ->- val ep = element.jsonObject- EndpointPair(- ep["color"]?.jsonPrimitive?.intOrNull ?: index,- ep["cells"]!!.jsonArray.map { cellFrom(it) },- )- }+ val cells = cellsFrom(json, rows, cols)+ val adjacency = adjacencyFrom(json, cells, grid)+ val portals = pairsFrom(json, "portals").toMap()+ val extraEdges = pairsFrom(json, "extraEdges")+ val fullAdjacency = mergedAdjacency(adjacency, cells, portals, extraEdges)+ val renderPositions = renderPositionsFrom(json, cells, grid)+ val endpoints = endpointsFrom(json) val bridges = json["bridges"]?.jsonArray?.map { cellFrom(it) }?.toSet() ?: emptySet() return LevelData( id = json["id"]!!.jsonPrimitive.content,@@ -96,6 +58,79 @@ object PackFixtures { ) } + /** Declared cells, or the full rows x cols rectangle when omitted. */+ private fun cellsFrom(+ json: kotlinx.serialization.json.JsonObject,+ rows: Int,+ cols: Int,+ ): Set<CellPos> = json["cells"]?.jsonArray?.map { cellFrom(it) }?.toSet()+ ?: buildSet { for (r in 0 until rows) for (c in 0 until cols) add(CellPos(r, c)) }++ /** Declared adjacency, or the grid-derived adjacency when omitted. */+ private fun adjacencyFrom(+ json: kotlinx.serialization.json.JsonObject,+ cells: Set<CellPos>,+ grid: String,+ ): Map<CellPos, List<CellPos>> = json["adjacency"]?.jsonObject?.map { (key, value) ->+ val parts = key.split(",")+ val cell = CellPos(parts[0].toInt(), parts[1].toInt())+ cell to value.jsonArray.map { cellFrom(it) }+ }?.toMap() ?: if (grid == "hex") {+ com.vayunmathur.games.pipes.data.computeHexAdjacency(cells)+ } else {+ computeAdjacency(cells)+ }++ /** Bidirectional cell pairs declared under [key] ("portals" / "extraEdges"). */+ private fun pairsFrom(+ json: kotlinx.serialization.json.JsonObject,+ key: String,+ ): List<Pair<CellPos, CellPos>> = json[key]?.jsonArray?.flatMap { pair ->+ val ends = pair.jsonArray.map { cellFrom(it) }+ if (ends.size == 2) listOf(ends[0] to ends[1], ends[1] to ends[0]) else emptyList()+ } ?: emptyList()++ /** Base adjacency unioned with portals and extra edges. */+ private fun mergedAdjacency(+ adjacency: Map<CellPos, List<CellPos>>,+ cells: Set<CellPos>,+ portals: Map<CellPos, CellPos>,+ extraEdges: List<Pair<CellPos, CellPos>>,+ ): Map<CellPos, List<CellPos>> {+ if (portals.isEmpty() && extraEdges.isEmpty()) return adjacency+ val merged = adjacency.mapValues { (_, neighbors) -> neighbors.toMutableSet() }.toMutableMap()+ for ((a, b) in portals) merged.getOrPut(a) { mutableSetOf() }.add(b)+ for ((a, b) in extraEdges) merged.getOrPut(a) { mutableSetOf() }.add(b)+ for (cell in cells) merged.putIfAbsent(cell, mutableSetOf())+ return merged.mapValues { (_, neighbors) -> neighbors.toList() }+ }++ /** Declared render positions, or hex-derived ones for hex grids. */+ private fun renderPositionsFrom(+ json: kotlinx.serialization.json.JsonObject,+ cells: Set<CellPos>,+ grid: String,+ ): Map<CellPos, RenderPos>? = json["renderPositions"]?.jsonObject?.map { (key, value) ->+ val parts = key.split(",")+ val cell = CellPos(parts[0].toInt(), parts[1].toInt())+ val posObj = value.jsonObject+ cell to RenderPos(posObj["x"]!!.jsonPrimitive.float, posObj["y"]!!.jsonPrimitive.float)+ }?.toMap() ?: if (grid == "hex") {+ com.vayunmathur.games.pipes.data.hexRenderPositions(cells)+ } else {+ null+ }++ /** Endpoint pairs, colour defaulting to declaration order. */+ private fun endpointsFrom(json: kotlinx.serialization.json.JsonObject): List<EndpointPair> =+ json["endpoints"]!!.jsonArray.mapIndexed { index, element ->+ val ep = element.jsonObject+ EndpointPair(+ ep["color"]?.jsonPrimitive?.intOrNull ?: index,+ ep["cells"]!!.jsonArray.map { cellFrom(it) },+ )+ }+ private fun cellFrom(element: kotlinx.serialization.json.JsonElement): CellPos { val pair = element.jsonArray return CellPos(pair[0].jsonPrimitive.int, pair[1].jsonPrimitive.int)