-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
242 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 80 additions & 75 deletions
155
gauguin-core/src/main/kotlin/org/piepmeyer/gauguin/game/save/SavedGrid.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,103 @@ | ||
package org.piepmeyer.gauguin.game.save | ||
|
||
import kotlinx.serialization.EncodeDefault | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.Serializable | ||
import org.piepmeyer.gauguin.grid.Grid | ||
import org.piepmeyer.gauguin.grid.GridCage | ||
import kotlin.time.Duration.Companion.milliseconds | ||
|
||
@Serializable | ||
data class SavedGrid( | ||
val version: Int = 1, | ||
val variant: SavedGameVariant, | ||
val savedAtInMilliseconds: Long, | ||
val playTimeInMilliseconds: Long, | ||
val startedToBePlayed: Boolean, | ||
val description: String? = null, | ||
val isActive: Boolean, | ||
val cells: List<SavedCell>, | ||
val selectedCellNumber: Int?, | ||
val invalidCellNumbers: List<Int>, | ||
val cheatedCellNumbers: List<Int>, | ||
val cages: List<SavedCage>, | ||
val undoSteps: List<SavedUndoStep> = emptyList(), | ||
) { | ||
fun toGrid(): Grid { | ||
val grid = Grid(variant.toVariant(), savedAtInMilliseconds) | ||
data class SavedGrid | ||
@OptIn(ExperimentalSerializationApi::class) | ||
constructor( | ||
@EncodeDefault | ||
val version: Int = 2, | ||
val variant: SavedGameVariant, | ||
val savedAtInMilliseconds: Long, | ||
val playTimeInMilliseconds: Long, | ||
val startedToBePlayed: Boolean, | ||
val description: String? = null, | ||
val isActive: Boolean, | ||
val cells: List<SavedCell>, | ||
val selectedCellNumber: Int?, | ||
val invalidCellNumbers: List<Int>, | ||
val cheatedCellNumbers: List<Int>, | ||
val cages: List<SavedCage>, | ||
val undoSteps: List<SavedUndoStep> = emptyList(), | ||
) { | ||
fun toGrid(): Grid { | ||
val grid = Grid(variant.toVariant(), savedAtInMilliseconds) | ||
|
||
grid.isActive = isActive | ||
grid.playTime = playTimeInMilliseconds.milliseconds | ||
grid.startedToBePlayed = startedToBePlayed | ||
grid.description = description | ||
grid.isActive = isActive | ||
grid.playTime = playTimeInMilliseconds.milliseconds | ||
grid.startedToBePlayed = startedToBePlayed | ||
grid.description = description | ||
|
||
cells.forEach { | ||
val cell = grid.getCell(it.cellNumber) | ||
cells.forEach { | ||
val cell = grid.getCell(it.cellNumber) | ||
|
||
cell.value = it.value | ||
cell.userValue = it.userValue | ||
cell.possibles = it.possibles | ||
} | ||
cell.value = it.value | ||
cell.userValue = it.userValue | ||
cell.possibles = it.possibles | ||
} | ||
|
||
selectedCellNumber?.let { | ||
grid.getCell(it).isSelected = true | ||
} | ||
selectedCellNumber?.let { | ||
grid.getCell(it).isSelected = true | ||
} | ||
|
||
invalidCellNumbers.forEach { | ||
grid.getCell(it).isInvalidHighlight = true | ||
} | ||
invalidCellNumbers.forEach { | ||
grid.getCell(it).isInvalidHighlight = true | ||
} | ||
|
||
cheatedCellNumbers.forEach { | ||
grid.getCell(it).isCheated = true | ||
} | ||
cheatedCellNumbers.forEach { | ||
grid.getCell(it).isCheated = true | ||
} | ||
|
||
grid.cages = | ||
cages.map { | ||
val cage = GridCage(it.id, grid.options.showOperators, it.action, it.type) | ||
grid.cages = | ||
cages.map { | ||
val cage = GridCage(it.id, grid.options.showOperators, it.action, it.type) | ||
|
||
cage.result = it.result | ||
it.cellNumbers.forEach { cellNumber -> cage.addCell(grid.getCell(cellNumber)) } | ||
cage.result = it.result | ||
it.cellNumbers.forEach { cellNumber -> cage.addCell(grid.getCell(cellNumber)) } | ||
|
||
cage | ||
} | ||
cage | ||
} | ||
|
||
grid.undoSteps.addAll(undoSteps.map { it.toUndoStep(grid) }) | ||
grid.undoSteps.addAll(undoSteps.map { it.toUndoStep(grid) }) | ||
|
||
return grid | ||
} | ||
return grid | ||
} | ||
|
||
companion object { | ||
fun fromGrid(grid: Grid): SavedGrid { | ||
val savedCells = | ||
grid.cells.map { | ||
SavedCell.fromCell(it) | ||
} | ||
val savedCages = | ||
grid.cages.map { | ||
SavedCage.fromCage(it) | ||
} | ||
val savedUndoSteps = | ||
grid.undoSteps.map { | ||
SavedUndoStep.fromUndoStep(it) | ||
} | ||
companion object { | ||
fun fromGrid(grid: Grid): SavedGrid { | ||
val savedCells = | ||
grid.cells.map { | ||
SavedCell.fromCell(it) | ||
} | ||
val savedCages = | ||
grid.cages.map { | ||
SavedCage.fromCage(it) | ||
} | ||
val savedUndoSteps = | ||
grid.undoSteps.map { | ||
SavedUndoStep.fromUndoStep(it) | ||
} | ||
|
||
return SavedGrid( | ||
variant = SavedGameVariant.fromVariant(grid.variant), | ||
savedAtInMilliseconds = System.currentTimeMillis(), | ||
playTimeInMilliseconds = grid.playTime.inWholeMilliseconds, | ||
startedToBePlayed = grid.startedToBePlayed, | ||
description = grid.description, | ||
isActive = grid.isActive, | ||
cells = savedCells, | ||
selectedCellNumber = grid.selectedCell?.cellNumber, | ||
invalidCellNumbers = grid.invalidsHighlighted().map { it.cellNumber }, | ||
cheatedCellNumbers = grid.cheatedHighlighted().map { it.cellNumber }, | ||
cages = savedCages, | ||
undoSteps = savedUndoSteps, | ||
) | ||
return SavedGrid( | ||
variant = SavedGameVariant.fromVariant(grid.variant), | ||
savedAtInMilliseconds = System.currentTimeMillis(), | ||
playTimeInMilliseconds = grid.playTime.inWholeMilliseconds, | ||
startedToBePlayed = grid.startedToBePlayed, | ||
description = grid.description, | ||
isActive = grid.isActive, | ||
cells = savedCells, | ||
selectedCellNumber = grid.selectedCell?.cellNumber, | ||
invalidCellNumbers = grid.invalidsHighlighted().map { it.cellNumber }, | ||
cheatedCellNumbers = grid.cheatedHighlighted().map { it.cellNumber }, | ||
cages = savedCages, | ||
undoSteps = savedUndoSteps, | ||
) | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
gauguin-core/src/main/kotlin/org/piepmeyer/gauguin/game/save/SavedGridVersion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.piepmeyer.gauguin.game.save | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class SavedGridVersion( | ||
val version: Int = 1, | ||
) |
34 changes: 34 additions & 0 deletions
34
gauguin-core/src/main/kotlin/org/piepmeyer/gauguin/game/save/v1/V1SavedGameOptionsVariant.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.piepmeyer.gauguin.game.save.v1 | ||
|
||
import kotlinx.serialization.Serializable | ||
import org.piepmeyer.gauguin.game.save.SavedDifficultySetting | ||
import org.piepmeyer.gauguin.game.save.SavedGameOptionsVariant | ||
import org.piepmeyer.gauguin.options.DifficultySetting | ||
import org.piepmeyer.gauguin.options.DigitSetting | ||
import org.piepmeyer.gauguin.options.GameOptionsVariant | ||
import org.piepmeyer.gauguin.options.GridCageOperation | ||
import org.piepmeyer.gauguin.options.NumeralSystem | ||
import org.piepmeyer.gauguin.options.SingleCageUsage | ||
|
||
@Serializable | ||
data class V1SavedGameOptionsVariant( | ||
var showOperators: Boolean, | ||
var cageOperation: GridCageOperation, | ||
var digitSetting: DigitSetting, | ||
var difficultySetting: SavedDifficultySetting?, | ||
var difficultiesSetting: Set<DifficultySetting> = emptySet(), | ||
var singleCageUsage: SingleCageUsage, | ||
var numeralSystem: NumeralSystem, | ||
) { | ||
fun toSavedGameOptionsVariant(): SavedGameOptionsVariant = | ||
SavedGameOptionsVariant.fromOptionsVariant( | ||
GameOptionsVariant( | ||
showOperators = showOperators, | ||
cageOperation = cageOperation, | ||
digitSetting = digitSetting, | ||
difficultiesSetting = difficultySetting!!.toDifficultySetting(), | ||
singleCageUsage = singleCageUsage, | ||
numeralSystem = numeralSystem, | ||
), | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
gauguin-core/src/main/kotlin/org/piepmeyer/gauguin/game/save/v1/V1SavedGameVariant.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.piepmeyer.gauguin.game.save.v1 | ||
|
||
import kotlinx.serialization.Serializable | ||
import org.piepmeyer.gauguin.game.save.SavedGameVariant | ||
import org.piepmeyer.gauguin.grid.GridSize | ||
|
||
@Serializable | ||
data class V1SavedGameVariant( | ||
val gridSize: GridSize, | ||
val options: V1SavedGameOptionsVariant, | ||
) { | ||
fun toUpdatedSavedGameVariant(): SavedGameVariant = SavedGameVariant(gridSize, options.toSavedGameOptionsVariant()) | ||
} |
Oops, something went wrong.