This commit is contained in:
2024-08-18 13:03:24 +02:00
parent f69f7ae294
commit 6d648b9a71
11 changed files with 61 additions and 118 deletions
+46 -40
View File
@@ -44,10 +44,10 @@ import java.util.UUID
data class ResponseSchematicType(val name: String, val db: String)
@Serializable
data class ResponseUser(val id: Int, val name: String, val uuid: String, val prefix: String, val perms: List<String>) {
constructor(user: SteamwarUser) : this(user.id, user.userName, user.uuid.toString(), user.prefix().chatPrefix, user.perms().map { it.name }) {
data class ResponseUser(val name: String, val uuid: String, val prefix: String, val perms: List<String>) {
constructor(user: SteamwarUser) : this(user.userName, user.uuid.toString(), user.prefix().chatPrefix, user.perms().map { it.name }) {
synchronized(cache) {
cache[id] = this
cache[user.id] = this
}
}
@@ -73,32 +73,51 @@ fun Route.configureDataRoutes() {
get {
call.respondText("Hello World!")
}
get("/schematicTypes") {
val types = mutableListOf<SchematicType>()
loadSchematicTypes(types, mutableMapOf())
call.respond(types.filter { !it.check() }.map { ResponseSchematicType(it.name(), it.toDB()) })
}
get("/gamemodes") {
call.respond(
File("/configs/GameModes/").listFiles()!!
.filter { it.name.endsWith(".yml") && !it.name.endsWith(".kits.yml") }
.map { it.nameWithoutExtension })
}
get("/gamemodes/{gamemode}/maps") {
val gamemode = call.parameters["gamemode"]
if (gamemode == null) {
call.respond(HttpStatusCode.BadRequest, ResponseError("Invalid gamemode"))
return@get
route("/admin") {
install(SWPermissionCheck) {
mustAuth = true
permission = UserPerm.PREFIX_MODERATOR
}
val file = File("/configs/GameModes/$gamemode.yml")
if (!file.exists()) {
call.respond(HttpStatusCode.NotFound, ResponseError("Gamemode not found"))
return@get
get("/users") {
call.respond(SteamwarUser.getAll().map { ResponseUser(it) })
}
get("/schematicTypes") {
val types = mutableListOf<SchematicType>()
loadSchematicTypes(types, mutableMapOf())
call.respond(types.filter { !it.check() }.map { ResponseSchematicType(it.name(), it.toDB()) })
}
get("/gamemodes") {
call.respond(
File("/configs/GameModes/").listFiles()!!
.filter { it.name.endsWith(".yml") && !it.name.endsWith(".kits.yml") }
.map { it.nameWithoutExtension })
}
get("/gamemodes/{gamemode}/maps") {
val gamemode = call.parameters["gamemode"]
if (gamemode == null) {
call.respond(HttpStatusCode.BadRequest, ResponseError("Invalid gamemode"))
return@get
}
val file = File("/configs/GameModes/$gamemode.yml")
if (!file.exists()) {
call.respond(HttpStatusCode.NotFound, ResponseError("Gamemode not found"))
return@get
}
call.respond(YamlConfiguration.loadConfiguration(file).getStringList("Server.Maps"))
}
get("/groups") {
call.respond(Groups.getAllGroups())
}
get("/server") {
try {
val server = fetchData(InetSocketAddress("steamwar.de", 25565), 100)
call.respond(server)
} catch (e: Exception) {
e.printStackTrace()
call.respond(HttpStatusCode.InternalServerError, ResponseError(e.message ?: "Unknown error"))
return@get
}
}
call.respond(YamlConfiguration.loadConfiguration(file).getStringList("Server.Maps"))
}
get("/users") {
call.respond(SteamwarUser.getAll().map { ResponseUser(it) })
}
get("/team") {
call.respond(
@@ -108,19 +127,6 @@ fun Route.configureDataRoutes() {
.mapValues { it.value.map { ResponseUser(it) } }
)
}
get("/groups") {
call.respond(Groups.getAllGroups())
}
get("/server") {
try {
val server = fetchData(InetSocketAddress("steamwar.de", 25565), 100)
call.respond(server)
} catch (e: Exception) {
e.printStackTrace()
call.respond(HttpStatusCode.InternalServerError, ResponseError(e.message ?: "Unknown error"))
return@get
}
}
get("/skin/{uuid}") {
val uuid = call.parameters["uuid"]
if (uuid == null || catchException { UUID.fromString(uuid) } == null) {
@@ -50,8 +50,8 @@ data class ResponseSchematicLong(val members: List<ResponseUser>, val path: Stri
}
@Serializable
data class ResponseSchematicList(val breadcrumbs: List<ResponseBreadcrumb>, val schematics: List<ResponseSchematic>, val players: Map<Int, ResponseUser>) {
constructor(schematics: List<ResponseSchematic>, breadcrumbs: List<ResponseBreadcrumb>) : this(breadcrumbs, schematics, schematics.map { it.owner }.distinct().map { ResponseUser.get(it) }.associateBy { it.id })
data class ResponseSchematicList(val breadcrumbs: List<ResponseBreadcrumb>, val schematics: List<ResponseSchematic>, val players: Map<String, ResponseUser>) {
constructor(schematics: List<ResponseSchematic>, breadcrumbs: List<ResponseBreadcrumb>) : this(breadcrumbs, schematics, schematics.map { it.owner }.distinct().map { ResponseUser.get(it) }.associateBy { it.uuid })
}
@Serializable
@@ -132,7 +132,7 @@ fun Route.configureSchematic() {
}
route("/schem") {
install(SWPermissionCheck)
get {
/*get {
val user = call.principal<SWAuthPrincipal>()!!.user
call.respond(ResponseSchematicList(SchematicNode.list(user, null).filter { it.name != "//copy" }.sortedWith { o1, o2 ->
if (o1.isDir || o2.isDir) {
@@ -141,7 +141,7 @@ fun Route.configureSchematic() {
o1.name.compareTo(o2.name)
}
}.map { ResponseSchematic(it) }, listOf()))
}
}*/
post {
val file = call.receive<UploadSchematic>()
@@ -166,7 +166,7 @@ fun Route.configureSchematic() {
call.respond(ResponseSchematic(node))
}
/*
route("/{id}") {
get {
val user = call.principal<SWAuthPrincipal>()!!.user
@@ -224,6 +224,6 @@ fun Route.configureSchematic() {
}
}.map { ResponseSchematic(it) }, parent.generateBreadcrumbsMap(user).map { ResponseBreadcrumb(it.key, it.value) }))
}
}
}*/
}
}