SquadCompanion/src/main/kotlin/OverviewTemplate.kt

110 lines
3.2 KiB
Kotlin

import io.ktor.server.html.*
import kotlinx.html.*
class OverviewTemplate(
private val players: ListPlayersOutput,
private val squadInfo: Pair<RconAbstraction.SquadListFaction, RconAbstraction.SquadListFaction>
) : Template<HTML> {
private fun TR.makePlayer(player: Player) {
td { +player.role }
td { +player.name }
td { +player.steamId.toString() }
td {
button {//Message
+""
}
button {//Kick
+""
}
button {//Ban
+""
}
}
}
private fun FlowContent.makeSquad(
squad: Map.Entry<Int, List<Player>>,
squadListSquad: RconAbstraction.SquadListSquad
) {
val leader = squad.value.find { it.leader }
div("squad") {
div("squadHeader") {
if (squad.key != 0) {
+squadListSquad.name
} else {
+"Unassigned"
}
if (squadListSquad.locked) {
+" \uD83D\uDD12"
}
if (squadListSquad.creatorSteamId != (leader?.steamId ?: 0) && squadListSquad.creatorSteamId != 0L) {
+" (originally created by ${squadListSquad.creatorName} ${squadListSquad.creatorSteamId})"
}
}
table("squadTable") {
colGroup {
col("playerClass")
col("playerName")
col("playerId")
col("playerButtons")
}
leader?.let {
thead {
tr("leader") {
makePlayer(leader)
}
}
}
squad.value.forEach {
if (!it.leader) {
tr {
makePlayer(it)
}
}
}
}
}
}
private fun FlowContent.makeSide(team: Map<Int, List<Player>>, squadListFaction: RconAbstraction.SquadListFaction) {
div("teamHeader") {
+squadListFaction.name
}
if (team.keys.isEmpty()) {
+"empty"
return
}
team.forEach {
if (it.key != 0) {
makeSquad(it, squadListFaction.squads[it.key]!!)
}
}
team.filter { it.key == 0 }.forEach {
makeSquad(it, RconAbstraction.SquadListSquad("Unassigned", it.value.size, false, "default", 0L))
}
}
override fun HTML.apply() {
val teamA = players.players.filter { it.teamId == 1 }.groupBy { it.squadId }
val teamB = players.players.filter { it.teamId == 2 }.groupBy { it.squadId }
head {
styleLink("/css")
}
body {
div("teamView") {
div("team colorRedFor") {
makeSide(teamA, squadInfo.first)
}
div("team colorBluFor") {
makeSide(teamB, squadInfo.second)
}
}
}
}
}