Initial Commit

This commit is contained in:
2025-10-25 12:50:47 +02:00
commit 28f6921788
27 changed files with 853 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
package de.steamwar.commands
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.findOrSetObject
import com.github.ajalt.mordant.rendering.TextStyles
import de.steamwar.db.Database
class SteamWar: CliktCommand(name = "sw") {
val db by findOrSetObject { Database() }
override fun run() {
echo("${TextStyles.bold("SteamWar-CLI")} (${db.database})")
}
}

View File

@@ -0,0 +1,22 @@
package de.steamwar.commands.database
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.CliktError
import com.github.ajalt.clikt.core.Context
import com.github.ajalt.clikt.core.requireObject
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import de.steamwar.db.Database
class DatabaseCommand: CliktCommand(name = "db") {
val useProduction by option().flag()
val db by requireObject<Database>()
override fun help(context: Context): String = "Run database commands"
override fun run() {
if (!useProduction && db.database == "production") {
throw CliktError("You should not use the production database!")
}
}
}

View File

@@ -0,0 +1,23 @@
package de.steamwar.commands.database
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.requireObject
import com.github.ajalt.mordant.table.table
import de.steamwar.db.Database
class InfoCommand: CliktCommand() {
val db by requireObject<Database>()
override fun run() {
val tables = db.execute("SHOW TABLES") { it.getString(1) }
echo(
table {
header { row("Name") }
body {
tables.map { row(it) }
}
}
)
}
}

View File

@@ -0,0 +1,31 @@
package de.steamwar.commands.database
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.CliktError
import com.github.ajalt.clikt.core.requireObject
import com.github.ajalt.mordant.rendering.TextColors
import com.github.ajalt.mordant.rendering.TextStyles
import de.steamwar.db.Database
import java.io.File
class ResetCommand: CliktCommand() {
val db by requireObject<Database>()
override fun run() {
val schemaFile = File("/var/Schema.sql")
if (!schemaFile.exists()) {
throw CliktError("Schema file not found!")
}
val schema = schemaFile.readText()
val tables = db.execute("SHOW TABLES;") { it.getString(1) }
for (table in tables) {
db.execute("DROP TABLE IF EXISTS $table;") { }
}
db.execute(schema) { }
echo(TextColors.brightGreen(TextStyles.bold("Database reset!")))
}
}

View File

@@ -0,0 +1,26 @@
package de.steamwar.commands.user
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.CliktError
import com.github.ajalt.clikt.core.findOrSetObject
import com.github.ajalt.clikt.parameters.arguments.argument
import de.steamwar.db.schema.SteamwarUser
import de.steamwar.db.schema.SteamwarUserTable
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.core.or
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
class UserCommand: CliktCommand("user") {
val userId by argument()
val user by findOrSetObject("user") {
transaction {
SteamwarUser.find { (SteamwarUserTable.id eq userId.toIntOrNull()) or (SteamwarUserTable.username eq userId) }
.firstOrNull()
?.let { return@transaction it } ?: throw CliktError("User not found!")
}
}
override fun run() {
user.id
}
}

View File

@@ -0,0 +1,50 @@
package de.steamwar.commands.user
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.requireObject
import com.github.ajalt.mordant.table.table
import de.steamwar.db.schema.Session
import de.steamwar.db.schema.SteamwarUser
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.jdbc.selectAll
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime
class UsesrInfoCommand: CliktCommand("info") {
val user by requireObject<SteamwarUser>("user")
@OptIn(ExperimentalTime::class)
override fun run() {
transaction {
val sessions = Session.selectAll().where { Session.user eq user.id.value }.map { it[Session.start] to it[Session.end] }
val totalPlayed = sessions.map { it.second - it.first }.sumOf { it.toDouble(DurationUnit.HOURS) }
val firstJoin = sessions.minByOrNull { it.first }?.first
val lastJoin = sessions.maxByOrNull { it.second }?.second
echo(
table {
body {
row("Name", user.username)
row("UUID", user.uuid)
row("Team", user.team.name)
row("Leader", user.leader)
row("Locale", user.locale)
row("Beigetreten am", firstJoin)
row("Zuletzt gesehen am", lastJoin)
row("Spielzeit", totalPlayed.toString() + "h")
row("Punishments", table {
header { row("Typ", "Ersteller", "Von", "Bis", "Grund") }
body {
user.punishments.map {
row(it.type, it.punisher.username, it.starttime.toString(), if (it.perma) "Perma" else it.endtime.toString(), it.reason)
}
}
})
}
}
)
}
}
}