diff --git a/CommonCore/SQL/src/de/steamwar/sql/EventGroup.java b/CommonCore/SQL/src/de/steamwar/sql/EventGroup.java index 166e7eb1..68934765 100644 --- a/CommonCore/SQL/src/de/steamwar/sql/EventGroup.java +++ b/CommonCore/SQL/src/de/steamwar/sql/EventGroup.java @@ -23,10 +23,9 @@ import de.steamwar.sql.internal.*; import lombok.Getter; import lombok.Setter; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; @Getter @Setter @@ -94,6 +93,15 @@ public class EventGroup { return EventFight.get(this); } + public Set getTeamsId() { + return getFights().stream().flatMap(fight -> Stream.of(fight.getTeamBlue(), fight.getTeamRed())) + .collect(Collectors.toSet()); + } + + public Set getTeams() { + return getTeamsId().stream().map(Team::get).collect(Collectors.toSet()); + } + public Optional getLastFight() { return EventFight.getLast(this); } @@ -104,13 +112,16 @@ public class EventGroup { public Map calculatePoints() { if (points == null) { - Map teams = new HashMap<>(); - points = new HashMap<>(); + Map p = getTeamsId().stream().collect(Collectors.toMap(team -> team, team -> 0)); for (EventFight fight : getFights()) { int blueTeamAdd = 0; int redTeamAdd = 0; + if (!fight.hasFinished()) { + continue; + } + switch (fight.getErgebnis()) { case 1: blueTeamAdd += pointsPerWin; @@ -128,19 +139,18 @@ public class EventGroup { break; } - Team blueTeam = teams.computeIfAbsent(fight.getTeamBlue(), Team::get); - Team redTeam = teams.computeIfAbsent(fight.getTeamRed(), Team::get); - - points.put(blueTeam, points.getOrDefault(blueTeam, 0) + blueTeamAdd); - points.put(redTeam, points.getOrDefault(redTeam, 0) + redTeamAdd); + p.put(fight.getTeamBlue(), p.get(fight.getTeamBlue()) + blueTeamAdd); + p.put(fight.getTeamRed(), p.get(fight.getTeamRed()) + redTeamAdd); } + + points = p.entrySet().stream().collect(Collectors.toMap(integerIntegerEntry -> Team.get(integerIntegerEntry.getKey()), Map.Entry::getValue)); } return points; } public void update(String name, EventGroupType type, int pointsPerWin, int pointsPerLoss, int pointsPerDraw) { - update.update(id, name, type, pointsPerWin, pointsPerLoss, pointsPerDraw); + update.update(name, type, pointsPerWin, pointsPerLoss, pointsPerDraw, id); this.name = name; this.type = type; this.pointsPerWin = pointsPerWin; diff --git a/WebsiteBackend/src/de/steamwar/routes/EventFights.kt b/WebsiteBackend/src/de/steamwar/routes/EventFights.kt index 4823619f..d1974686 100644 --- a/WebsiteBackend/src/de/steamwar/routes/EventFights.kt +++ b/WebsiteBackend/src/de/steamwar/routes/EventFights.kt @@ -40,7 +40,8 @@ data class ResponseEventFight( val start: Long, val ergebnis: Int, val spectatePort: Int?, - val group: ResponseGroups? + val group: ResponseGroups?, + val hasFinished: Boolean ) { constructor(eventFight: EventFight) : this( eventFight.fightID, @@ -51,7 +52,8 @@ data class ResponseEventFight( eventFight.startTime.time, eventFight.ergebnis, eventFight.spectatePort, - eventFight.group.orElse(null)?.let { ResponseGroups(it) } + eventFight.group.orElse(null)?.let { ResponseGroups(it, short = true) }, + eventFight.hasFinished() ) } diff --git a/WebsiteBackend/src/de/steamwar/routes/Page.kt b/WebsiteBackend/src/de/steamwar/routes/Page.kt index 6f9a9fc4..139814c7 100644 --- a/WebsiteBackend/src/de/steamwar/routes/Page.kt +++ b/WebsiteBackend/src/de/steamwar/routes/Page.kt @@ -24,7 +24,6 @@ import de.steamwar.plugins.SWAuthPrincipal import de.steamwar.plugins.SWPermissionCheck import de.steamwar.sql.UserPerm import io.ktor.client.* -import io.ktor.client.call.* import io.ktor.client.engine.java.* import io.ktor.client.plugins.* import io.ktor.client.plugins.contentnegotiation.* @@ -37,9 +36,7 @@ import io.ktor.server.auth.* import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* -import io.ktor.util.reflect.* import kotlinx.serialization.Serializable -import kotlinx.serialization.encodeToString import kotlinx.serialization.json.* import java.util.Base64 @@ -113,65 +110,42 @@ fun Route.configurePage() { } } + suspend fun filesInDirectory(path: String, branch: String = "master", fileFilter: (name: String) -> Boolean = { true }): List { + val filesToCheck = mutableListOf(path) + val files = mutableListOf() + + while (filesToCheck.isNotEmpty()) { + val path = filesToCheck.removeAt(0) + val res = client.get("repos/SteamWar/Website/contents/$path?ref=$branch") + val fileJson = Json.parseToJsonElement(res.bodyAsText()) + + if (fileJson is JsonArray) { + fileJson.forEach { + val obj = it.jsonObject + if (obj["type"]?.jsonPrimitive?.content == "dir") { + filesToCheck.add(obj["path"]?.jsonPrimitive?.content!!) + } else if (obj["type"]?.jsonPrimitive?.content == "file" && fileFilter(obj["name"]!!.jsonPrimitive.content)) { + files.add(PageResponseList(obj, pathPageIdMap.computeIfAbsent(obj["path"]?.jsonPrimitive?.content!!) { pageId++ })) + } + } + } else { + files.add(PageResponseList(fileJson.jsonObject, pathPageIdMap.computeIfAbsent(fileJson.jsonObject["path"]?.jsonPrimitive?.content!!) { pageId++ })) + } + } + + return files + } + route("page") { install(SWPermissionCheck) { permission = UserPerm.MODERATION } get { val branch = call.request.queryParameters["branch"] ?: "master" - val filesToCheck = mutableListOf("src/content") - val files = mutableListOf() - while (filesToCheck.isNotEmpty()) { - val path = filesToCheck.removeAt(0) - val res = client.get("repos/SteamWar/Website/contents/$path?ref=$branch") - val fileJson = Json.parseToJsonElement(res.bodyAsText()) - - if (fileJson is JsonArray) { - fileJson.forEach { - val obj = it.jsonObject - if (obj["type"]?.jsonPrimitive?.content == "dir") { - filesToCheck.add(obj["path"]?.jsonPrimitive?.content!!) - } else if (obj["type"]?.jsonPrimitive?.content == "file" && (obj["name"]?.jsonPrimitive?.content?.endsWith(".md") == true || obj["name"]?.jsonPrimitive?.content?.endsWith(".json") == true)) { - files.add(PageResponseList(obj, pathPageIdMap.computeIfAbsent(obj["path"]?.jsonPrimitive?.content!!) { pageId++ })) - } - } - } else { - files.add(PageResponseList(fileJson.jsonObject, pathPageIdMap.computeIfAbsent(fileJson.jsonObject["path"]?.jsonPrimitive?.content!!) { pageId++ })) - } - } - - call.respond(files) - } - get("branch") { - val res = client.get("repos/SteamWar/Website/branches") - call.respond(res.status, Json.parseToJsonElement(res.bodyAsText()).jsonArray.map { it.jsonObject["name"]?.jsonPrimitive?.content!! }) - } - post("branch") { - @Serializable - data class CreateGiteaBranchRequest(val new_branch_name: String, val old_branch_name: String) - - val branch = call.receive().branch - val res = client.post("repos/SteamWar/Website/branches") { - contentType(ContentType.Application.Json) - setBody(CreateGiteaBranchRequest(branch, "master")) - } - - - @Serializable - data class CreateGiteaMergeRequest(val base: String, val head: String, val title: String) - - client.post("repos/SteamWar/Website/pulls") { - contentType(ContentType.Application.Json) - setBody(CreateGiteaMergeRequest("master", branch, "Merge branch $branch")) - } - - call.respond(res.status) - } - delete("branch") { - val branch = call.receive().branch - val res = client.delete("repos/SteamWar/Website/branches/$branch") - call.respond(res.status) + call.respond(filesInDirectory("/src/content", branch) { + it.endsWith(".md") || it.endsWith(".json") + }) } post { @Serializable @@ -197,55 +171,94 @@ fun Route.configurePage() { """.trimIndent().toByteArray()), call.request.queryParameters["branch"] ?: "master", Identity(call.principal()!!.user.userName, "admin-tool@steamwar.de" - ))) + ))) } call.respond(res.status) } - get("{id}") { - val id = call.parameters["id"]?.toIntOrNull() ?: return@get call.respond(HttpStatusCode.BadRequest, "Invalid id") - val path = pathPageIdMap.entries.find { it.value == id }?.key ?: return@get call.respond(HttpStatusCode.NotFound, "Page not found") - - val branch = call.request.queryParameters["branch"] ?: "master" - val res = client.get("repos/SteamWar/Website/contents/$path?ref=$branch") - val fileJson = Json.parseToJsonElement(res.bodyAsText()) - if (fileJson is JsonArray) { - return@get call.respond(HttpStatusCode.BadRequest, "Invalid id") + route("branch") { + get { + val res = client.get("repos/SteamWar/Website/branches") + call.respond(res.status, Json.parseToJsonElement(res.bodyAsText()).jsonArray.map { it.jsonObject["name"]?.jsonPrimitive?.content!! }) } + post { + @Serializable + data class CreateGiteaBranchRequest(val new_branch_name: String, val old_branch_name: String) - val file = PageResponse(fileJson.jsonObject, id) - call.respond(file) + val branch = call.receive().branch + val res = client.post("repos/SteamWar/Website/branches") { + contentType(ContentType.Application.Json) + setBody(CreateGiteaBranchRequest(branch, "master")) + } + + + @Serializable + data class CreateGiteaMergeRequest(val base: String, val head: String, val title: String) + + client.post("repos/SteamWar/Website/pulls") { + contentType(ContentType.Application.Json) + setBody(CreateGiteaMergeRequest("master", branch, "Merge branch $branch")) + } + + call.respond(res.status) + } + delete { + val branch = call.receive().branch + val res = client.delete("repos/SteamWar/Website/branches/$branch") + call.respond(res.status) + } } + route("{id}") { + get { + val id = call.parameters["id"]?.toIntOrNull() ?: return@get call.respond(HttpStatusCode.BadRequest, "Invalid id") + val path = pathPageIdMap.entries.find { it.value == id }?.key ?: return@get call.respond(HttpStatusCode.NotFound, "Page not found") - delete("{id}") { - val data = call.receive() + val branch = call.request.queryParameters["branch"] ?: "master" + val res = client.get("repos/SteamWar/Website/contents/$path?ref=$branch") + val fileJson = Json.parseToJsonElement(res.bodyAsText()) + if (fileJson is JsonArray) { + return@get call.respond(HttpStatusCode.BadRequest, "Invalid id") + } - val path = pathPageIdMap.entries.find { it.value == call.parameters["id"]?.toIntOrNull() }?.key ?: return@delete call.respond(HttpStatusCode.NotFound, "Page not found") - val branch = call.request.queryParameters["branch"] ?: "master" - - @Serializable - data class DeleteGiteaPageRequest(val sha: String, val message: String, val branch: String, val author: Identity) - - val res = client.delete("repos/SteamWar/Website/contents/$path") { - contentType(ContentType.Application.Json) - setBody(DeleteGiteaPageRequest(data.sha, data.message, branch, Identity(call.principal()!!.user.userName, "admin-tool@steamwar.de"))) + val file = PageResponse(fileJson.jsonObject, id) + call.respond(file) } + delete { + val data = call.receive() - call.respond(res.status) + val path = pathPageIdMap.entries.find { it.value == call.parameters["id"]?.toIntOrNull() }?.key ?: return@delete call.respond(HttpStatusCode.NotFound, "Page not found") + val branch = call.request.queryParameters["branch"] ?: "master" + + @Serializable + data class DeleteGiteaPageRequest(val sha: String, val message: String, val branch: String, val author: Identity) + + val res = client.delete("repos/SteamWar/Website/contents/$path") { + contentType(ContentType.Application.Json) + setBody(DeleteGiteaPageRequest(data.sha, data.message, branch, Identity(call.principal()!!.user.userName, "admin-tool@steamwar.de"))) + } + + call.respond(res.status) + } + put { + @Serializable + data class UpdateGiteaPageRequest(val content: String, val sha: String, val message: String, val branch: String, val author: Identity) + + val data = call.receive() + val path = pathPageIdMap.entries.find { it.value == call.parameters["id"]?.toIntOrNull() }?.key ?: return@put call.respond(HttpStatusCode.NotFound, "Page not found") + + val res = client.put("repos/SteamWar/Website/contents/$path") { + contentType(ContentType.Application.Json) + setBody(UpdateGiteaPageRequest(data.content, data.sha, data.message, (call.request.queryParameters["branch"] ?: "master"), Identity(call.principal()!!.user.userName, "admin-tool@steamwar.de"))) + } + + call.respond(res.status) + } } + route("images") { + get { + val branch = call.request.queryParameters["branch"] ?: "master" - put("{id}") { - @Serializable - data class UpdateGiteaPageRequest(val content: String, val sha: String, val message: String, val branch: String, val author: Identity) - - val data = call.receive() - val path = pathPageIdMap.entries.find { it.value == call.parameters["id"]?.toIntOrNull() }?.key ?: return@put call.respond(HttpStatusCode.NotFound, "Page not found") - - val res = client.put("repos/SteamWar/Website/contents/$path") { - contentType(ContentType.Application.Json) - setBody(UpdateGiteaPageRequest(data.content, data.sha, data.message, (call.request.queryParameters["branch"] ?: "master"), Identity(call.principal()!!.user.userName, "admin-tool@steamwar.de"))) + call.respond(filesInDirectory("/src/images", branch)) } - - call.respond(res.status) } } } \ No newline at end of file