Fix import/export test

This commit is contained in:
Claude Brisson
2023-10-02 10:10:09 +02:00
parent c2d1e53f3f
commit dcd752040c
3 changed files with 18 additions and 10 deletions

View File

@@ -203,3 +203,13 @@ fun Tournament<*>.toJson() = Json.Object(
"rounds" to rounds,
"pairing" to pairing.toJson()
)
fun Tournament<*>.toFullJson(): Json.Object {
val json = Json.MutableObject(toJson())
json["players"] = Json.Array(players.values.map { it.toJson() })
if (this is TeamTournament) {
json["teams"] = Json.Array(teams.values.map { it.toJson() })
}
json["games"] = Json.Array((1..lastRound()).mapTo(Json.MutableArray()) { round -> games(round).values.mapTo(Json.MutableArray()) { it.toJson() } });
return json
}

View File

@@ -8,6 +8,7 @@ import org.jeudego.pairgoth.model.TeamTournament
import org.jeudego.pairgoth.model.Tournament
import org.jeudego.pairgoth.model.fromJson
import org.jeudego.pairgoth.model.getID
import org.jeudego.pairgoth.model.toFullJson
import org.jeudego.pairgoth.model.toID
import org.jeudego.pairgoth.model.toJson
import java.nio.file.Path
@@ -49,12 +50,7 @@ class FileStore(pathStr: String): StoreImplementation {
val filename = tournament.filename()
val file = path.resolve(filename).toFile()
if (file.exists()) throw Error("File $filename already exists")
val json = Json.MutableObject(tournament.toJson())
json["players"] = Json.Array(tournament.players.values.map { it.toJson() })
if (tournament is TeamTournament) {
json["teams"] = Json.Array(tournament.teams.values.map { it.toJson() })
}
json["games"] = Json.Array((1..tournament.lastRound()).map { round -> tournament.games(round).values.map { it.toJson() } });
val json = tournament.toFullJson()
file.printWriter().use { out ->
out.println(json.toPrettyString())
}