groundwork for race list

This commit is contained in:
yenon 2023-11-08 18:51:12 +01:00
parent bdcac0f494
commit 428226dcd4
6 changed files with 113 additions and 2 deletions

View File

@ -23,6 +23,7 @@ dependencies {
implementation("io.ktor:ktor-client-core:2.3.5")
implementation("io.ktor:ktor-client-cio:2.3.5")
implementation("io.ktor:ktor-server-html-builder:2.3.5")
implementation("dev.kord:kord-core:0.11.1")
}
tasks.test {

View File

@ -0,0 +1,24 @@
import io.ktor.server.application.*
import io.ktor.server.html.*
import io.ktor.util.pipeline.*
import kotlinx.html.*
suspend fun PipelineContext<Unit, ApplicationCall>.respondThemedHtml(
title: String,
css: List<String> = listOf("/css/default.css"),
body: BODY.() -> Unit
) {
call.respondHtml {
head {
title {
+title
}
css.forEach {
styleLink(it)
}
}
body {
body(this)
}
}
}

View File

@ -1,10 +1,22 @@
import dev.kord.core.Kord
import dev.kord.core.event.message.MessageCreateEvent
import dev.kord.core.on
import io.ktor.server.application.*
import io.ktor.server.cio.*
import io.ktor.server.engine.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
fun main(){
suspend fun main() {
val kord = Kord("MTE3MTIwODc1MDE5MTg5MDQ4Mw.GOUedL.i3zD6IG5B6fFRvaSOotWwJ5KBRK2whC9xr0vL8")
kord.on<MessageCreateEvent> {
if (message.author?.isBot == false) {
this.message.channel.createMessage("I exist!")
}
}
val server = embeddedServer(CIO, port = 8080){
routing {
get("/"){
@ -12,8 +24,10 @@ fun main(){
}
searchPage()
trackPage()
racePage()
}
}
println("connection to localhost:8080 now possible.")
server.start(true)
server.start(false)
kord.login()
}

View File

@ -0,0 +1,41 @@
import kotlinx.serialization.Serializable
@Serializable
data class RaceData(var name: String, var description: String) {
val trackList = arrayListOf<Long>()
val leaderboardMap = hashMapOf<Long, Velocidrone.Leaderboard>()
fun addTrack(trackId: Long) {
trackList.add(trackId)
}
fun removeTrack(trackId: Long) {
trackList.remove(trackId)
leaderboardMap.remove(trackId)
}
suspend fun rescanLeaderboard(trackId: Long, newLeaderboardCallback: (Velocidrone.Leaderboard) -> Unit) {
if (trackList.contains(trackId)) {
Velocidrone.getLeaderboardForId(trackId).getOrNull()?.let { newLeaderboard ->
if (leaderboardMap[trackId]?.equals(newLeaderboard) == false) {
leaderboardMap[trackId] = newLeaderboard
newLeaderboardCallback(newLeaderboard)
}
}
}
}
fun getTotalScores(): List<Pair<String, Long>> {
val scoreMap = hashMapOf<String, Long>()
leaderboardMap.forEach { leaderboardEntry ->
val sortedTimes = leaderboardEntry.value.tracktimes.sortedBy { it.lap_time }.forEach {
scoreMap[it.playername] = scoreMap.getOrDefault(it.playername, 0L) + 1
}
}
return scoreMap.toList().sortedByDescending { it.second }
}
}
object RaceHolder {
val races = arrayListOf<RaceData>()
}

25
src/main/java/RacePage.kt Normal file
View File

@ -0,0 +1,25 @@
import io.ktor.server.application.*
import io.ktor.server.routing.*
import io.ktor.util.pipeline.*
import kotlinx.html.div
import kotlinx.html.h1
import kotlinx.html.p
fun Routing.racePage() {
get("/races") {
raceOverviewPage(RaceHolder.races)
}
}
private suspend fun PipelineContext<Unit, ApplicationCall>.raceOverviewPage(races: ArrayList<RaceData>) {
respondThemedHtml("Race Overview") {
div("raceList") {
races.forEach {
div("raceEntry") {
h1 { +it.name }
p { +it.description }
}
}
}
}
}

View File

@ -0,0 +1,6 @@
table tr:nth-child(odd) td{
background:#ccc;
}
table tr:nth-child(even) td{
background:#fff;
}