From 1cd59d54bafa0c72a4e98ef237f09bd20f6048f3 Mon Sep 17 00:00:00 2001 From: Claude Brisson Date: Sat, 30 Mar 2024 06:06:06 +0100 Subject: [PATCH] Pairgoth json export is now complete --- .../org/jeudego/pairgoth/api/TournamentHandler.kt | 15 ++++++++++++--- .../org/jeudego/pairgoth/server/ApiServlet.kt | 2 +- api-webapp/src/test/kotlin/LoadTest.kt | 9 +++++---- api-webapp/src/test/kotlin/TestUtils.kt | 2 +- .../src/main/webapp/js/tour-information.inc.js | 2 +- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/api/TournamentHandler.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/api/TournamentHandler.kt index 7ae8537..0134d86 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/api/TournamentHandler.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/api/TournamentHandler.kt @@ -8,6 +8,7 @@ import org.jeudego.pairgoth.ext.OpenGotha import org.jeudego.pairgoth.model.TeamTournament import org.jeudego.pairgoth.model.Tournament import org.jeudego.pairgoth.model.fromJson +import org.jeudego.pairgoth.model.toFullJson import org.jeudego.pairgoth.model.toJson import org.jeudego.pairgoth.server.ApiServlet import org.jeudego.pairgoth.server.Event.* @@ -24,12 +25,20 @@ object TournamentHandler: PairgothApiHandler { null -> getStore(request).getTournaments().toJsonObject() else -> when { - ApiServlet.isJson(accept) -> getStore(request).getTournament(id)?.toJson() ?: badRequest("no tournament with id #${id}") + ApiServlet.isJson(accept) -> { + getStore(request).getTournament(id)?.let { + if (accept == "application/pairgoth") { + it.toFullJson() + } else { + it.toJson() + } + } ?: badRequest("no tournament with id #${id}") + } ApiServlet.isXml(accept) -> { val export = getStore(request).getTournament(id)?.let { OpenGotha.export(it) } ?: badRequest("no tournament with id #${id}") response.contentType = "application/xml; charset=UTF-8" response.writer.write(export) - null // return null to indicate that we handled the response ourself + null // return null to indicate that we handled the response ourselves } else -> badRequest("unhandled Accept header: $accept") } @@ -48,7 +57,7 @@ object TournamentHandler: PairgothApiHandler { } override fun put(request: HttpServletRequest, response: HttpServletResponse): Json { - // BC TODO - some checks are needed here (cannot lower rounds number if games have been played in removed rounds, for instance) + // CB TODO - some checks are needed here (cannot lower rounds number if games have been played in removed rounds, for instance) val tournament = getTournament(request) val payload = getObjectPayload(request) // disallow changing type diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/server/ApiServlet.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/server/ApiServlet.kt index 3991e6c..8dba6b7 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/server/ApiServlet.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/server/ApiServlet.kt @@ -299,7 +299,7 @@ class ApiServlet: HttpServlet() { private var logger = LoggerFactory.getLogger("api") private const val EXPECTED_CHARSET = "utf8" const val USER_KEY = "pairgoth-user" - fun isJson(mimeType: String) = "text/json" == mimeType || "application/json" == mimeType || mimeType.endsWith("+json") + fun isJson(mimeType: String) = "text/json" == mimeType || "application/json" == mimeType || mimeType.endsWith("+json") || "application/pairgoth" == mimeType fun isXml(mimeType: String) = "text/xml" == mimeType || "application/xml" == mimeType || mimeType.endsWith("+xml") } } diff --git a/api-webapp/src/test/kotlin/LoadTest.kt b/api-webapp/src/test/kotlin/LoadTest.kt index fbd69ae..b3e9239 100644 --- a/api-webapp/src/test/kotlin/LoadTest.kt +++ b/api-webapp/src/test/kotlin/LoadTest.kt @@ -45,10 +45,10 @@ class LoadTest: TestBase() { "rating" to rating, "rank" to (rating - 2050)/100, "final" to true, - "skip" to (1..ROUNDS).map { + "skip" to (0..ROUNDS - 1).map { rand.nextDouble() < SKIP_RATIO }.mapIndexedNotNullTo(Json.MutableArray()) { index, skip -> - if (skip) index else null + if (skip) (index + 1) else null } ) } @@ -84,7 +84,7 @@ class LoadTest: TestBase() { repeat(PLAYERS) { TestAPI.post("/api/tour/$tour/part", generatePlayer()) } - getOutputFile("verybig-nopairing.json").printWriter().use { + getOutputFile("verybig-nopairing.tour").printWriter().use { it.println(TestAPI.getJson("/api/tour/$tour")) } repeat(ROUNDS) { @@ -97,13 +97,14 @@ class LoadTest: TestBase() { games.map { (it as Json.Object).getInt("id")!! }.forEach { gameId -> TestAPI.put("/api/tour/$tour/res/$round", Json.Object( "id" to gameId, + // TODO - credible probabilistic results based on scores "result" to if (rand.nextBoolean()) "w" else "b" )) } } // val standings = TestAPI.get("/api/tour/$tour/standings/$ROUNDS") // logger.info(standings.toString()) - getOutputFile("verybig.json").printWriter().use { + getOutputFile("verybig.tour").printWriter().use { it.println(TestAPI.getJson("/api/tour/$tour")) } } finally { diff --git a/api-webapp/src/test/kotlin/TestUtils.kt b/api-webapp/src/test/kotlin/TestUtils.kt index 12a6e5c..56bae48 100644 --- a/api-webapp/src/test/kotlin/TestUtils.kt +++ b/api-webapp/src/test/kotlin/TestUtils.kt @@ -82,7 +82,7 @@ object TestAPI { fun get(uri: String): Json = Json.parse(testRequest("GET", uri)) ?: throw Error("no payload") fun getXml(uri: String): String = testRequest("GET", uri, "application/xml") - fun getJson(uri: String): String = testRequest("GET", uri, "application/json") + fun getJson(uri: String): String = testRequest("GET", uri, "application/pairgoth") fun getCSV(uri: String): String = testRequest("GET", uri, "text/csv") fun getFFG(uri: String): String = testRequest("GET", uri, "application/ffg") fun post(uri: String, payload: T) = Json.parse(testRequest("POST", uri, payload = payload)) ?: throw Error("no payload") diff --git a/view-webapp/src/main/webapp/js/tour-information.inc.js b/view-webapp/src/main/webapp/js/tour-information.inc.js index a7d6e58..a26b3fe 100644 --- a/view-webapp/src/main/webapp/js/tour-information.inc.js +++ b/view-webapp/src/main/webapp/js/tour-information.inc.js @@ -123,7 +123,7 @@ onLoad(() => { let form = $('#tournament-infos')[0]; let shortName = form.val('shortName'); let hdrs = headers(); - hdrs['Accept'] = 'application/json'; + hdrs['Accept'] = 'application/pairgoth'; fetch(`${base}tour/${tour_id}`, { headers: hdrs }).then(resp => {