Pairgoth json export is now complete

This commit is contained in:
Claude Brisson
2024-03-30 06:06:06 +01:00
parent aefc567d29
commit 1cd59d54ba
5 changed files with 20 additions and 10 deletions

View File

@@ -8,6 +8,7 @@ import org.jeudego.pairgoth.ext.OpenGotha
import org.jeudego.pairgoth.model.TeamTournament import org.jeudego.pairgoth.model.TeamTournament
import org.jeudego.pairgoth.model.Tournament import org.jeudego.pairgoth.model.Tournament
import org.jeudego.pairgoth.model.fromJson import org.jeudego.pairgoth.model.fromJson
import org.jeudego.pairgoth.model.toFullJson
import org.jeudego.pairgoth.model.toJson import org.jeudego.pairgoth.model.toJson
import org.jeudego.pairgoth.server.ApiServlet import org.jeudego.pairgoth.server.ApiServlet
import org.jeudego.pairgoth.server.Event.* import org.jeudego.pairgoth.server.Event.*
@@ -24,12 +25,20 @@ object TournamentHandler: PairgothApiHandler {
null -> getStore(request).getTournaments().toJsonObject() null -> getStore(request).getTournaments().toJsonObject()
else -> else ->
when { 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) -> { ApiServlet.isXml(accept) -> {
val export = getStore(request).getTournament(id)?.let { OpenGotha.export(it) } ?: badRequest("no tournament with id #${id}") val export = getStore(request).getTournament(id)?.let { OpenGotha.export(it) } ?: badRequest("no tournament with id #${id}")
response.contentType = "application/xml; charset=UTF-8" response.contentType = "application/xml; charset=UTF-8"
response.writer.write(export) 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") else -> badRequest("unhandled Accept header: $accept")
} }
@@ -48,7 +57,7 @@ object TournamentHandler: PairgothApiHandler {
} }
override fun put(request: HttpServletRequest, response: HttpServletResponse): Json { 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 tournament = getTournament(request)
val payload = getObjectPayload(request) val payload = getObjectPayload(request)
// disallow changing type // disallow changing type

View File

@@ -299,7 +299,7 @@ class ApiServlet: HttpServlet() {
private var logger = LoggerFactory.getLogger("api") private var logger = LoggerFactory.getLogger("api")
private const val EXPECTED_CHARSET = "utf8" private const val EXPECTED_CHARSET = "utf8"
const val USER_KEY = "pairgoth-user" 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") fun isXml(mimeType: String) = "text/xml" == mimeType || "application/xml" == mimeType || mimeType.endsWith("+xml")
} }
} }

View File

@@ -45,10 +45,10 @@ class LoadTest: TestBase() {
"rating" to rating, "rating" to rating,
"rank" to (rating - 2050)/100, "rank" to (rating - 2050)/100,
"final" to true, "final" to true,
"skip" to (1..ROUNDS).map { "skip" to (0..ROUNDS - 1).map {
rand.nextDouble() < SKIP_RATIO rand.nextDouble() < SKIP_RATIO
}.mapIndexedNotNullTo(Json.MutableArray()) { index, skip -> }.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) { repeat(PLAYERS) {
TestAPI.post("/api/tour/$tour/part", generatePlayer()) 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")) it.println(TestAPI.getJson("/api/tour/$tour"))
} }
repeat(ROUNDS) { repeat(ROUNDS) {
@@ -97,13 +97,14 @@ class LoadTest: TestBase() {
games.map { (it as Json.Object).getInt("id")!! }.forEach { gameId -> games.map { (it as Json.Object).getInt("id")!! }.forEach { gameId ->
TestAPI.put("/api/tour/$tour/res/$round", Json.Object( TestAPI.put("/api/tour/$tour/res/$round", Json.Object(
"id" to gameId, "id" to gameId,
// TODO - credible probabilistic results based on scores
"result" to if (rand.nextBoolean()) "w" else "b" "result" to if (rand.nextBoolean()) "w" else "b"
)) ))
} }
} }
// val standings = TestAPI.get("/api/tour/$tour/standings/$ROUNDS") // val standings = TestAPI.get("/api/tour/$tour/standings/$ROUNDS")
// logger.info(standings.toString()) // logger.info(standings.toString())
getOutputFile("verybig.json").printWriter().use { getOutputFile("verybig.tour").printWriter().use {
it.println(TestAPI.getJson("/api/tour/$tour")) it.println(TestAPI.getJson("/api/tour/$tour"))
} }
} finally { } finally {

View File

@@ -82,7 +82,7 @@ object TestAPI {
fun get(uri: String): Json = Json.parse(testRequest<Void>("GET", uri)) ?: throw Error("no payload") fun get(uri: String): Json = Json.parse(testRequest<Void>("GET", uri)) ?: throw Error("no payload")
fun getXml(uri: String): String = testRequest<Void>("GET", uri, "application/xml") fun getXml(uri: String): String = testRequest<Void>("GET", uri, "application/xml")
fun getJson(uri: String): String = testRequest<Void>("GET", uri, "application/json") fun getJson(uri: String): String = testRequest<Void>("GET", uri, "application/pairgoth")
fun getCSV(uri: String): String = testRequest<Void>("GET", uri, "text/csv") fun getCSV(uri: String): String = testRequest<Void>("GET", uri, "text/csv")
fun getFFG(uri: String): String = testRequest<Void>("GET", uri, "application/ffg") fun getFFG(uri: String): String = testRequest<Void>("GET", uri, "application/ffg")
fun <T> post(uri: String, payload: T) = Json.parse(testRequest("POST", uri, payload = payload)) ?: throw Error("no payload") fun <T> post(uri: String, payload: T) = Json.parse(testRequest("POST", uri, payload = payload)) ?: throw Error("no payload")

View File

@@ -123,7 +123,7 @@ onLoad(() => {
let form = $('#tournament-infos')[0]; let form = $('#tournament-infos')[0];
let shortName = form.val('shortName'); let shortName = form.val('shortName');
let hdrs = headers(); let hdrs = headers();
hdrs['Accept'] = 'application/json'; hdrs['Accept'] = 'application/pairgoth';
fetch(`${base}tour/${tour_id}`, { fetch(`${base}tour/${tour_id}`, {
headers: hdrs headers: hdrs
}).then(resp => { }).then(resp => {