forked from SteamWar/SteamWar
Format code
This commit is contained in:
+9
-8
@@ -12,11 +12,12 @@ import de.steamwar.commands.user.UserCommand
|
||||
import de.steamwar.commands.user.UserInfoCommand
|
||||
import de.steamwar.commands.user.UserSearchCommand
|
||||
|
||||
fun main(args: Array<String>) = SteamWar()
|
||||
.subcommands(
|
||||
DatabaseCommand().subcommands(InfoCommand(), ResetCommand()),
|
||||
UserCommand().subcommands(UserInfoCommand(), UserSearchCommand()),
|
||||
DevCommand(),
|
||||
ProfilerCommand()
|
||||
)
|
||||
.main(args)
|
||||
fun main(args: Array<String>) =
|
||||
SteamWar()
|
||||
.subcommands(
|
||||
DatabaseCommand().subcommands(InfoCommand(), ResetCommand()),
|
||||
UserCommand().subcommands(UserInfoCommand(), UserSearchCommand()),
|
||||
DevCommand(),
|
||||
ProfilerCommand()
|
||||
)
|
||||
.main(args)
|
||||
@@ -3,7 +3,7 @@ package de.steamwar.commands
|
||||
import com.github.ajalt.clikt.core.CliktCommand
|
||||
import com.github.ajalt.mordant.rendering.TextStyles
|
||||
|
||||
class SteamWar: CliktCommand(name = "sw") {
|
||||
class SteamWar : CliktCommand(name = "sw") {
|
||||
override fun run() {
|
||||
echo(TextStyles.bold("SteamWar-CLI"))
|
||||
}
|
||||
|
||||
@@ -8,11 +8,12 @@ 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") {
|
||||
class DatabaseCommand : CliktCommand(name = "db") {
|
||||
val useProduction by option().flag()
|
||||
val db by findOrSetObject { Database }
|
||||
|
||||
override fun help(context: Context): String = "Run database commands"
|
||||
override fun help(context: Context): String =
|
||||
"Run database commands"
|
||||
|
||||
override fun run() {
|
||||
if (!useProduction && db.database == "production") {
|
||||
|
||||
@@ -7,19 +7,20 @@ import de.steamwar.db.Database
|
||||
import de.steamwar.db.execute
|
||||
import de.steamwar.db.useDb
|
||||
|
||||
class InfoCommand: CliktCommand() {
|
||||
class InfoCommand : CliktCommand() {
|
||||
val db by requireObject<Database>()
|
||||
|
||||
override fun run() = useDb {
|
||||
val tables = execute("SHOW TABLES") { it.getString(1) }
|
||||
override fun run() =
|
||||
useDb {
|
||||
val tables = execute("SHOW TABLES") { it.getString(1) }
|
||||
|
||||
echo(
|
||||
table {
|
||||
header { row("Name") }
|
||||
body {
|
||||
tables.map { row(it) }
|
||||
echo(
|
||||
table {
|
||||
header { row("Name") }
|
||||
body {
|
||||
tables.map { row(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -10,24 +10,25 @@ import de.steamwar.db.execute
|
||||
import de.steamwar.db.useDb
|
||||
import java.io.File
|
||||
|
||||
class ResetCommand: CliktCommand() {
|
||||
class ResetCommand : CliktCommand() {
|
||||
val db by requireObject<Database>()
|
||||
|
||||
override fun run() = useDb {
|
||||
val schemaFile = File("/var/Schema.sql")
|
||||
if (!schemaFile.exists()) {
|
||||
throw CliktError("Schema file not found!")
|
||||
override fun run() =
|
||||
useDb {
|
||||
val schemaFile = File("/var/Schema.sql")
|
||||
if (!schemaFile.exists()) {
|
||||
throw CliktError("Schema file not found!")
|
||||
}
|
||||
|
||||
val schema = schemaFile.readText()
|
||||
|
||||
val tables = execute("SHOW TABLES;") { it.getString(1) }
|
||||
for (table in tables) {
|
||||
execute("DROP TABLE IF EXISTS $table;") { }
|
||||
}
|
||||
|
||||
execute(schema) { }
|
||||
|
||||
echo(TextColors.brightGreen(TextStyles.bold("Database reset!")))
|
||||
}
|
||||
|
||||
val schema = schemaFile.readText()
|
||||
|
||||
val tables = execute("SHOW TABLES;") { it.getString(1) }
|
||||
for (table in tables) {
|
||||
execute("DROP TABLE IF EXISTS $table;") { }
|
||||
}
|
||||
|
||||
execute(schema) { }
|
||||
|
||||
echo(TextColors.brightGreen(TextStyles.bold("Database reset!")))
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,8 @@ const val LOG4J_CONFIG = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
</Configuration>"""
|
||||
|
||||
class DevCommand : CliktCommand("dev") {
|
||||
override fun help(context: Context): String = "Start a dev Server"
|
||||
override fun help(context: Context): String =
|
||||
"Start a dev Server"
|
||||
|
||||
override val treatUnknownOptionsAsArgs = true
|
||||
|
||||
@@ -73,13 +74,22 @@ class DevCommand : CliktCommand("dev") {
|
||||
if (serverDirectory.exists() && serverDirectory.isDirectory) serverDirectory else File(workingDir, server)
|
||||
|
||||
if (isVelocity(server)) {
|
||||
runServer(args, jvmArgs, listOf(jar?.absolutePath ?: File("/jar/Velocity.jar").absolutePath), serverDir)
|
||||
runServer(
|
||||
args, jvmArgs, listOf(
|
||||
jar?.absolutePath
|
||||
?: File("/jar/Velocity.jar").absolutePath
|
||||
), serverDir
|
||||
)
|
||||
} else {
|
||||
setLogConfig(args)
|
||||
val version = findVersion(server) ?: throw CliktError("Unknown Server Version")
|
||||
val worldFile = world?.absolute()?.toFile() ?: File(serverDir, "devtempworld")
|
||||
val jarFile = jar?.absolutePath ?: additionalVersions[server]?.let { supportedVersionJars[it] } ?: supportedVersionJars[version]
|
||||
?: throw CliktError("Unknown Server Version")
|
||||
val version = findVersion(server)
|
||||
?: throw CliktError("Unknown Server Version")
|
||||
val worldFile = world?.absolute()?.toFile()
|
||||
?: File(serverDir, "devtempworld")
|
||||
val jarFile = jar?.absolutePath
|
||||
?: additionalVersions[server]?.let { supportedVersionJars[it] }
|
||||
?: supportedVersionJars[version]
|
||||
?: throw CliktError("Unknown Server Version")
|
||||
|
||||
if (!worldFile.exists()) {
|
||||
val templateFile = File(serverDir, "Bauwelt")
|
||||
@@ -108,7 +118,8 @@ class DevCommand : CliktCommand("dev") {
|
||||
|
||||
try {
|
||||
devFile.delete()
|
||||
} catch (_: Exception) { /* ignored */ }
|
||||
} catch (_: Exception) { /* ignored */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,11 +156,15 @@ class DevCommand : CliktCommand("dev") {
|
||||
"Lobby" to 20
|
||||
)
|
||||
|
||||
fun findVersion(server: String): Int? = server.dropWhile { !it.isDigit() }.toIntOrNull()
|
||||
fun findVersion(server: String): Int? =
|
||||
server.dropWhile { !it.isDigit() }.toIntOrNull()
|
||||
|
||||
fun isJava8(server: String): Boolean = findVersion(server)?.let { it <= 10 } ?: false
|
||||
fun isJava8(server: String): Boolean =
|
||||
findVersion(server)?.let { it <= 10 }
|
||||
?: false
|
||||
|
||||
fun isVelocity(server: String): Boolean = server.endsWith("Velocity")
|
||||
fun isVelocity(server: String): Boolean =
|
||||
server.endsWith("Velocity")
|
||||
|
||||
fun setLogConfig(args: MutableList<String>) {
|
||||
args += "-DlogPath=${workingDir.absolutePath}/logs"
|
||||
@@ -162,7 +177,8 @@ class DevCommand : CliktCommand("dev") {
|
||||
|
||||
fun runServer(args: List<String>, jvmArgs: List<String>, cmd: List<String>, serverDir: File) {
|
||||
val process = ProcessBuilder(
|
||||
jvm?.absolutePath ?: if (isJava8(server)) "/usr/lib/jvm/openj9-8/bin/java" else "java",
|
||||
jvm?.absolutePath
|
||||
?: if (isJava8(server)) "/usr/lib/jvm/openj9-8/bin/java" else "java",
|
||||
*jvmArgs.toTypedArray(),
|
||||
*args.toTypedArray(),
|
||||
*jvmDefaultParams,
|
||||
|
||||
@@ -11,7 +11,7 @@ import com.github.ajalt.clikt.parameters.types.int
|
||||
|
||||
const val SPARK = "/jars/spark.jar"
|
||||
|
||||
class ProfilerCommand: CliktCommand("profiler") {
|
||||
class ProfilerCommand : CliktCommand("profiler") {
|
||||
val pid by argument().help("Process id").int().optional()
|
||||
val port by option("--port", "-p").int().default(8543)
|
||||
|
||||
@@ -38,5 +38,6 @@ class ProfilerCommand: CliktCommand("profiler") {
|
||||
}
|
||||
}
|
||||
|
||||
override fun help(context: Context): String = "Start a profiler"
|
||||
override fun help(context: Context): String =
|
||||
"Start a profiler"
|
||||
}
|
||||
@@ -3,7 +3,10 @@ package de.steamwar.commands.user
|
||||
import com.github.ajalt.clikt.core.CliktCommand
|
||||
import com.github.ajalt.clikt.core.Context
|
||||
|
||||
class UserCommand: CliktCommand("user") {
|
||||
override fun run() = Unit
|
||||
override fun help(context: Context): String = "User related commands"
|
||||
class UserCommand : CliktCommand("user") {
|
||||
override fun run() =
|
||||
Unit
|
||||
|
||||
override fun help(context: Context): String =
|
||||
"User related commands"
|
||||
}
|
||||
@@ -17,49 +17,54 @@ import java.time.Duration
|
||||
|
||||
class UserInfoCommand : CliktCommand("info") {
|
||||
val userId by argument().help("Id, Name, UUID or DiscordId")
|
||||
val user by lazy { findUser(userId) ?: throw CliktError("User not found") }
|
||||
val user by lazy {
|
||||
findUser(userId)
|
||||
?: throw CliktError("User not found")
|
||||
}
|
||||
|
||||
override val printHelpOnEmptyArgs = true
|
||||
|
||||
override fun run() = useDb {
|
||||
val sessions =
|
||||
SessionTable.selectAll().where { SessionTable.userId eq user.id.value }
|
||||
.map { it[SessionTable.startTime] to it[SessionTable.endTime] }
|
||||
override fun run() =
|
||||
useDb {
|
||||
val sessions =
|
||||
SessionTable.selectAll().where { SessionTable.userId eq user.id.value }
|
||||
.map { it[SessionTable.startTime] to it[SessionTable.endTime] }
|
||||
|
||||
val totalPlayed = sessions.sumOf { Duration.between(it.first, it.second).toMinutes() } / 60.0
|
||||
val firstJoin = sessions.minByOrNull { it.first }?.first
|
||||
val lastJoin = sessions.maxByOrNull { it.second }?.second
|
||||
val totalPlayed = sessions.sumOf { Duration.between(it.first, it.second).toMinutes() } / 60.0
|
||||
val firstJoin = sessions.minByOrNull { it.first }?.first
|
||||
val lastJoin = sessions.maxByOrNull { it.second }?.second
|
||||
|
||||
val punishments = Punishment.getAllPunishmentsOfPlayer(user.id.value)
|
||||
val punishments = Punishment.getAllPunishmentsOfPlayer(user.id.value)
|
||||
|
||||
echo(
|
||||
table {
|
||||
body {
|
||||
row("Name", user.userName)
|
||||
row("UUID", user.uuid)
|
||||
row("Team", Team.byId(user.team).teamName)
|
||||
row("Leader", user.leader)
|
||||
row("Locale", user.locale)
|
||||
row("Beigetreten am", firstJoin)
|
||||
row("Zuletzt gesehen am", lastJoin)
|
||||
row("Spielzeit", totalPlayed.toString() + "h")
|
||||
row("Punishments", if (punishments.isEmpty()) "Keine" else table {
|
||||
header { row("Typ", "Ersteller", "Von", "Bis", "Grund") }
|
||||
body {
|
||||
punishments.map {
|
||||
row(
|
||||
it.type,
|
||||
SteamwarUser.byId(it.punisher)?.userName ?: it.punisher,
|
||||
it.startTime.toString(),
|
||||
if (it.perma) "Perma" else it.endTime.toString(),
|
||||
it.reason
|
||||
)
|
||||
echo(
|
||||
table {
|
||||
body {
|
||||
row("Name", user.userName)
|
||||
row("UUID", user.uuid)
|
||||
row("Team", Team.byId(user.team).teamName)
|
||||
row("Leader", user.leader)
|
||||
row("Locale", user.locale)
|
||||
row("Beigetreten am", firstJoin)
|
||||
row("Zuletzt gesehen am", lastJoin)
|
||||
row("Spielzeit", totalPlayed.toString() + "h")
|
||||
row("Punishments", if (punishments.isEmpty()) "Keine" else table {
|
||||
header { row("Typ", "Ersteller", "Von", "Bis", "Grund") }
|
||||
body {
|
||||
punishments.map {
|
||||
row(
|
||||
it.type,
|
||||
SteamwarUser.byId(it.punisher)?.userName
|
||||
?: it.punisher,
|
||||
it.startTime.toString(),
|
||||
if (it.perma) "Perma" else it.endTime.toString(),
|
||||
it.reason
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,25 +18,27 @@ class UserSearchCommand : CliktCommand("search") {
|
||||
|
||||
override val printHelpOnEmptyArgs = true
|
||||
|
||||
override fun help(context: Context): String = "Search for users"
|
||||
override fun help(context: Context): String =
|
||||
"Search for users"
|
||||
|
||||
override fun run() = useDb {
|
||||
val users = SteamwarUser.find {
|
||||
joinedOr(
|
||||
SteamwarUserTable.username like "%$query%",
|
||||
SteamwarUserTable.uuid like "%$query%",
|
||||
query.toLongOrNull()?.let { SteamwarUserTable.discordId eq it },
|
||||
query.toIntOrNull()?.let { SteamwarUserTable.id eq it }
|
||||
)
|
||||
}
|
||||
|
||||
val teams = mutableMapOf<Int, Team>()
|
||||
|
||||
echo(table {
|
||||
header { row("Id", "Username", "UUID", "Team", "DiscordId") }
|
||||
body {
|
||||
users.map { row(it.id.value, it.userName, it.uuid, teams.computeIfAbsent(it.team) { teamId -> Team.byId(teamId) }.teamName, it.discordId) }
|
||||
override fun run() =
|
||||
useDb {
|
||||
val users = SteamwarUser.find {
|
||||
joinedOr(
|
||||
SteamwarUserTable.username like "%$query%",
|
||||
SteamwarUserTable.uuid like "%$query%",
|
||||
query.toLongOrNull()?.let { SteamwarUserTable.discordId eq it },
|
||||
query.toIntOrNull()?.let { SteamwarUserTable.id eq it }
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
val teams = mutableMapOf<Int, Team>()
|
||||
|
||||
echo(table {
|
||||
header { row("Id", "Username", "UUID", "Team", "DiscordId") }
|
||||
body {
|
||||
users.map { row(it.id.value, it.userName, it.uuid, teams.computeIfAbsent(it.team) { teamId -> Team.byId(teamId) }.teamName, it.discordId) }
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,11 +49,12 @@ object Database {
|
||||
}
|
||||
}
|
||||
|
||||
fun <T: BaseCliktCommand<T>> BaseCliktCommand<T>.findUser(query: String): SteamwarUser? = transaction {
|
||||
SteamwarUser.find { joinedOr(query.toIntOrNull()?.let { SteamwarUserTable.id eq it }, (SteamwarUserTable.username eq query), SteamwarUserTable.uuid eq query, query.toLongOrNull()?.let { SteamwarUserTable.discordId eq it }) }
|
||||
.firstOrNull()
|
||||
?.let { return@transaction it }
|
||||
}
|
||||
fun <T : BaseCliktCommand<T>> BaseCliktCommand<T>.findUser(query: String): SteamwarUser? =
|
||||
transaction {
|
||||
SteamwarUser.find { joinedOr(query.toIntOrNull()?.let { SteamwarUserTable.id eq it }, (SteamwarUserTable.username eq query), SteamwarUserTable.uuid eq query, query.toLongOrNull()?.let { SteamwarUserTable.discordId eq it }) }
|
||||
.firstOrNull()
|
||||
?.let { return@transaction it }
|
||||
}
|
||||
|
||||
fun joinedOr(vararg expressions: Expression<Boolean>?): Op<Boolean> =
|
||||
expressions.filterNotNull().reduce { acc, expression -> acc or expression } as Op<Boolean>
|
||||
|
||||
+1
-1
@@ -6,6 +6,6 @@
|
||||
</appender>
|
||||
|
||||
<root level="WARN">
|
||||
<appender-ref ref="STDOUT" />
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user