Add Backend to Monorepo

This commit is contained in:
2024-08-18 11:15:54 +02:00
parent b8e50dc139
commit fd7fe8c305
37 changed files with 2703 additions and 26 deletions
@@ -0,0 +1,86 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.data
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.cbor.Cbor
import kotlinx.serialization.decodeFromByteArray
import kotlinx.serialization.encodeToByteArray
@Serializable
data class GroupsData(val groups: MutableList<GroupData>)
@Serializable
data class GroupData(val name: String, val fights: MutableList<Int>)
@OptIn(ExperimentalSerializationApi::class)
class Groups {
companion object {
private var groups: GroupsData = if (kGroupsFile.exists()) {
Cbor.decodeFromByteArray(kGroupsFile.readBytes())
} else {
kGroupsFile.createNewFile()
kGroupsFile.writeBytes(Cbor.encodeToByteArray(GroupsData(mutableListOf())))
GroupsData(mutableListOf())
}
fun getGroup(name: String): GroupData? {
return groups.groups.find { it.name == name }
}
fun getGroup(fight: Int): GroupData? {
return groups.groups.find { it.fights.contains(fight) }
}
fun getOrCreateGroup(name: String): GroupData {
val group = getGroup(name)
if (group != null) {
return group
}
val newGroup = GroupData(name, mutableListOf())
groups.groups.add(newGroup)
return newGroup
}
fun resetGroup(fight: Int, save: Boolean = false) {
val oldGroup = getGroup(fight)
oldGroup?.fights?.remove(fight)
if(oldGroup?.fights?.isEmpty() == true) {
groups.groups.remove(oldGroup)
}
if(save) {
kGroupsFile.writeBytes(Cbor.encodeToByteArray(groups))
}
}
fun setGroup(fight: Int, group: String) {
resetGroup(fight)
val newGroup = getOrCreateGroup(group)
newGroup.fights.add(fight)
kGroupsFile.writeBytes(Cbor.encodeToByteArray(groups))
}
fun getAllGroups(): List<String> {
return groups.groups.map { it.name }
}
}
}
@@ -0,0 +1,30 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.data
import java.io.File
const val kDataFolder: String = "data"
const val kGroupsName: String = "groups.cbor"
val kGroupsFile: File = File(kDataFolder, kGroupsName)
const val kRelationsName = "relations.cbor"
val kRelationsFile: File = File(kDataFolder, kRelationsName)
@@ -0,0 +1,110 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.data
import io.ktor.client.*
import io.ktor.client.engine.java.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.utils.io.jvm.javaio.*
import kotlinx.coroutines.*
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.cbor.Cbor
import kotlinx.serialization.decodeFromByteArray
import kotlinx.serialization.encodeToByteArray
import java.io.File
import java.time.Instant
import java.time.temporal.ChronoUnit
const val kCacheFolder: String = "skins"
val kCacheFolderFile: File = File(kCacheFolder)
const val kCacheConfigName: String = "cache.cbor"
val kCacheConfigFile: File = File(kCacheFolder, kCacheConfigName)
@Serializable
data class CacheConfig(val lastUpdate: MutableMap<String, Long>) {
@OptIn(ExperimentalSerializationApi::class)
companion object {
private var config: CacheConfig = if (kCacheConfigFile.exists()) {
kCacheConfigFile.inputStream().use {
Cbor.decodeFromByteArray(it.readBytes())
}
} else {
kCacheConfigFile.createNewFile()
kCacheConfigFile.outputStream().use {
it.write(Cbor.encodeToByteArray(CacheConfig(mutableMapOf())))
}
CacheConfig(mutableMapOf())
}
private fun save() {
kCacheConfigFile.outputStream().use {
it.write(Cbor.encodeToByteArray(config))
}
}
fun update(uuid: String) {
config.lastUpdate[uuid] = Instant.now().toEpochMilli()
save()
}
fun isOutdated(uuid: String): Boolean {
return config.lastUpdate[uuid]?.let {
it < Instant.now().minus(1, ChronoUnit.DAYS).toEpochMilli()
} ?: true
}
}
}
val client = HttpClient(Java) {
install(ContentNegotiation) {
json()
}
defaultRequest {
header("User-Agent", "SteamWar/1.0")
}
}
suspend fun getCachedSkin(uuid: String): Pair<File, Boolean> {
val file = File(kCacheFolderFile, "$uuid.webp")
if (file.exists()) {
if (CacheConfig.isOutdated(uuid)) {
val skin = client.get("https://visage.surgeplay.com/bust/150/$uuid")
skin.bodyAsChannel().copyTo(file.outputStream())
CacheConfig.update(uuid)
return file to false
}
return file to true
}
withContext(Dispatchers.IO) {
file.createNewFile()
}
val skin = client.get("https://visage.surgeplay.com/bust/150/$uuid")
skin.bodyAsChannel().copyTo(file.outputStream())
CacheConfig.update(uuid)
return file to false
}