Compare commits
2 Commits
5a53fd5f68
...
a9ea232e13
| Author | SHA1 | Date | |
|---|---|---|---|
| a9ea232e13 | |||
| be03f5fb96 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -183,3 +183,4 @@ gradle-app.setting
|
||||
# End of https://www.toptal.com/developers/gitignore/api/intellij,java,gradle,kotlin
|
||||
|
||||
data.json
|
||||
token
|
||||
22
src/main/java/Discord.kt
Normal file
22
src/main/java/Discord.kt
Normal file
@ -0,0 +1,22 @@
|
||||
import dev.kord.core.Kord
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
object Discord {
|
||||
|
||||
private lateinit var kord: Kord
|
||||
suspend fun init(token: String) {
|
||||
|
||||
kord = Kord(token)
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
kord.login()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun sendMessage(message: String) {
|
||||
kord.rest.channel.createMessage(Settings.instance.discordChannelId) {
|
||||
this.content = "<@&${Settings.instance.discordGroupId}>$message"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,15 +1,18 @@
|
||||
import dev.kord.common.entity.Snowflake
|
||||
import dev.kord.core.Kord
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.cio.*
|
||||
import io.ktor.server.engine.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.nio.file.Files
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.io.path.Path
|
||||
|
||||
|
||||
suspend fun main() {
|
||||
Settings.load()
|
||||
val path = Path("data.json")
|
||||
|
||||
if (Files.isRegularFile(path)) {
|
||||
@ -19,25 +22,32 @@ suspend fun main() {
|
||||
RaceHolder.save(path)
|
||||
})
|
||||
|
||||
val kord = Kord("MTE3MTIwODc1MDE5MTg5MDQ4Mw.GOUedL.i3zD6IG5B6fFRvaSOotWwJ5KBRK2whC9xr0vL8")
|
||||
Discord.init(withContext(Dispatchers.IO) {
|
||||
Files.readString(Path("token"))
|
||||
})
|
||||
|
||||
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate({
|
||||
runBlocking {
|
||||
RaceHolder.races.forEach {
|
||||
it.rescanAll()
|
||||
}
|
||||
}
|
||||
}, 0, 5, TimeUnit.MINUTES)
|
||||
|
||||
val server = embeddedServer(CIO, port = 8080){
|
||||
routing {
|
||||
get("/"){
|
||||
call.respondRedirect("/search")
|
||||
}
|
||||
settingsPage()
|
||||
searchPage()
|
||||
trackPage()
|
||||
racePage()
|
||||
get("/hi") {
|
||||
kord.rest.channel.createMessage(Snowflake("1040400994355392522")) {
|
||||
this.content = "<@&978289601506586624> Herro!"
|
||||
}
|
||||
Discord.sendMessage("Herro!")
|
||||
}
|
||||
}
|
||||
}
|
||||
println("connection to localhost:8080 now possible.")
|
||||
server.start(false)
|
||||
kord.login()
|
||||
|
||||
server.start(true)
|
||||
}
|
||||
@ -1,3 +1,7 @@
|
||||
import kotlinx.datetime.Clock
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
import kotlinx.datetime.TimeZone
|
||||
import kotlinx.datetime.toInstant
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
@ -5,49 +9,57 @@ import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.StandardOpenOption
|
||||
|
||||
@Serializable
|
||||
data class Track(
|
||||
val trackId: Long,
|
||||
val endTime: LocalDateTime? = null,
|
||||
var leaderboard: Velocidrone.Leaderboard = Velocidrone.Leaderboard(
|
||||
true, "", "",
|
||||
arrayOf()
|
||||
)
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class RaceData(var name: String, var description: String) {
|
||||
private val trackList = arrayListOf<Long>()
|
||||
private val leaderboardMap = hashMapOf<Long, Velocidrone.Leaderboard>()
|
||||
private val trackList = arrayListOf<Track>()
|
||||
private var totalScores = arrayListOf<Pair<String, Long>>()
|
||||
|
||||
fun addTrack(trackId: Long) {
|
||||
trackList.add(trackId)
|
||||
fun addTrack(track: Track) {
|
||||
trackList.add(track)
|
||||
}
|
||||
|
||||
fun removeTrack(trackId: Long) {
|
||||
trackList.remove(trackId)
|
||||
leaderboardMap.remove(trackId)
|
||||
fun removeTrack(track: Track) {
|
||||
trackList.remove(track)
|
||||
}
|
||||
|
||||
private suspend fun rescanLeaderboard(trackId: Long, newLeaderboardCallback: (Velocidrone.Leaderboard) -> Unit) {
|
||||
if (trackList.contains(trackId)) {
|
||||
Velocidrone.getLeaderboardForId(trackId).getOrNull()?.let { newLeaderboard ->
|
||||
if (leaderboardMap[trackId]?.equals(newLeaderboard) == false) {
|
||||
leaderboardMap[trackId] = newLeaderboard
|
||||
private suspend fun rescanLeaderboard(track: Track, newLeaderboardCallback: (Velocidrone.Leaderboard) -> Unit) {
|
||||
Velocidrone.getLeaderboardForId(track.trackId).getOrNull()?.let { newLeaderboard ->
|
||||
if (track.leaderboard != newLeaderboard) {
|
||||
track.leaderboard = newLeaderboard
|
||||
newLeaderboardCallback(newLeaderboard)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun rescanAll() {
|
||||
var changes = false
|
||||
trackList.forEach {
|
||||
if ((it.endTime?.toInstant(TimeZone.UTC)?.compareTo(Clock.System.now()) ?: -1) < 0) {
|
||||
rescanLeaderboard(it) {
|
||||
changes = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if (changes) {
|
||||
calculateTotalScores()
|
||||
}
|
||||
}
|
||||
|
||||
fun calculateTotalScores() {
|
||||
private fun calculateTotalScores() {
|
||||
val scoreMap = hashMapOf<String, Long>()
|
||||
leaderboardMap.forEach { leaderboardEntry ->
|
||||
leaderboardEntry.value.tracktimes.sortedBy { it.lap_time }.forEach {
|
||||
scoreMap[it.playername] = scoreMap.getOrDefault(it.playername, 0L) + 1
|
||||
trackList.forEach { track ->
|
||||
track.leaderboard.trackTimes.sortedBy { it.lapTime }.forEachIndexed { index, it ->
|
||||
scoreMap[it.playerName] = scoreMap.getOrDefault(it.playerName, 0L) + 1 + index
|
||||
}
|
||||
}
|
||||
synchronized(totalScores) {
|
||||
@ -55,6 +67,10 @@ data class RaceData(var name: String, var description: String) {
|
||||
scoreMap.asIterable().sortedByDescending { it.value }.map { it.toPair() }.toCollection(totalScores)
|
||||
}
|
||||
}
|
||||
|
||||
fun getTracks(): ArrayList<Track> {
|
||||
return trackList
|
||||
}
|
||||
}
|
||||
|
||||
object RaceHolder {
|
||||
|
||||
@ -9,13 +9,33 @@ fun Routing.racePage() {
|
||||
get("/race/list") {
|
||||
raceOverviewPage(RaceHolder.races)
|
||||
}
|
||||
|
||||
get("/races/{id}/show") {
|
||||
|
||||
}
|
||||
|
||||
get("/races/{raceId}/remove/{trackId}") {
|
||||
val raceId = call.parameters["raceId"]
|
||||
val trackId = call.parameters["trackId"]?.toLong()
|
||||
|
||||
RaceHolder.races.find { it.name == raceId }?.let { raceData ->
|
||||
val toBeRemovedTrack = raceData.getTracks().find { it.trackId == trackId }
|
||||
toBeRemovedTrack?.let {
|
||||
raceData.removeTrack(it)
|
||||
}
|
||||
}
|
||||
call.respondRedirect("/races/$raceId/show")
|
||||
}
|
||||
|
||||
get("/race/new") {
|
||||
newRacePage()
|
||||
}
|
||||
|
||||
post("/race/new") {
|
||||
val parameters = call.receiveParameters()
|
||||
val name = parameters["name"]
|
||||
val description = parameters["description"]
|
||||
|
||||
if (name == null || description == null) {
|
||||
newRacePage("Not all parameters given.", name ?: "", description ?: "")
|
||||
return@post
|
||||
@ -24,6 +44,7 @@ fun Routing.racePage() {
|
||||
newRacePage("Race with the same name already exists.", name, description)
|
||||
return@post
|
||||
}
|
||||
|
||||
RaceHolder.races.add(RaceData(name, description))
|
||||
call.respondRedirect("/race/list")
|
||||
}
|
||||
|
||||
@ -68,7 +68,7 @@ private suspend fun PipelineContext<Unit,ApplicationCall>.searchResultPage(track
|
||||
}
|
||||
}
|
||||
}
|
||||
trackResults.user_tracks.forEach {
|
||||
trackResults.userTracks.forEach {
|
||||
tr {
|
||||
td {
|
||||
form("/track/" + it.id + "/add") {
|
||||
@ -81,10 +81,10 @@ private suspend fun PipelineContext<Unit,ApplicationCall>.searchResultPage(track
|
||||
}
|
||||
}
|
||||
td {
|
||||
+it.track_name
|
||||
+it.trackName
|
||||
}
|
||||
td {
|
||||
+it.playername
|
||||
+it.playerName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
32
src/main/java/Settings.kt
Normal file
32
src/main/java/Settings.kt
Normal file
@ -0,0 +1,32 @@
|
||||
import dev.kord.common.entity.Snowflake
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.nio.file.Files
|
||||
import kotlin.io.path.Path
|
||||
|
||||
const val SETTINGS = "settings.json"
|
||||
|
||||
@Serializable
|
||||
class Settings {
|
||||
companion object {
|
||||
var instance = Settings()
|
||||
|
||||
fun load() {
|
||||
if (Files.isRegularFile(Path(SETTINGS))) {
|
||||
val reader = Files.newBufferedReader(Path(SETTINGS))
|
||||
instance = Json.decodeFromString<Settings>(reader.readText())
|
||||
reader.close()
|
||||
}
|
||||
}
|
||||
|
||||
fun save() {
|
||||
val writer = Files.newBufferedWriter(Path(SETTINGS))
|
||||
writer.write(Json.encodeToString(instance))
|
||||
writer.close()
|
||||
}
|
||||
}
|
||||
|
||||
var discordChannelId = Snowflake(0)
|
||||
var discordGroupId = Snowflake(0)
|
||||
}
|
||||
41
src/main/java/SettingsPage.kt
Normal file
41
src/main/java/SettingsPage.kt
Normal file
@ -0,0 +1,41 @@
|
||||
import dev.kord.common.entity.Snowflake
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.html.*
|
||||
|
||||
fun Routing.settingsPage() {
|
||||
get("/settings") {
|
||||
respondThemedHtml("Settings") {
|
||||
form("/settings", encType = FormEncType.applicationXWwwFormUrlEncoded, method = FormMethod.post) {
|
||||
p {
|
||||
+"GroupID:"
|
||||
textInput(name = "groupId") {
|
||||
value = Settings.instance.discordGroupId.toString()
|
||||
}
|
||||
}
|
||||
p {
|
||||
+"ChannelID:"
|
||||
textInput(name = "channelId") {
|
||||
value = Settings.instance.discordChannelId.toString()
|
||||
}
|
||||
}
|
||||
|
||||
submitInput { }
|
||||
}
|
||||
}
|
||||
}
|
||||
post("/settings") {
|
||||
val params = call.receiveParameters()
|
||||
params["groupId"]?.let { Settings.instance.discordGroupId = Snowflake(it) }
|
||||
params["channelId"]?.let { Settings.instance.discordChannelId = Snowflake(it) }
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
Settings.save()
|
||||
}
|
||||
call.respondRedirect("/settings")
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,7 @@ import io.ktor.server.html.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.util.pipeline.*
|
||||
import kotlinx.datetime.toLocalDateTime
|
||||
import kotlinx.html.*
|
||||
import java.net.URLDecoder
|
||||
import java.net.URLEncoder
|
||||
@ -22,11 +23,12 @@ fun Routing.trackPage() {
|
||||
}
|
||||
}
|
||||
post("/track/{id}/add") {
|
||||
val endTime = call.receiveParameters()["endTime"]?.toLocalDateTime()
|
||||
call.parameters["id"]?.toLong()?.let { trackId ->
|
||||
val param = call.receiveParameters()
|
||||
param["race"]?.let { raceString ->
|
||||
val decoded = URLDecoder.decode(raceString, Charsets.UTF_8)
|
||||
RaceHolder.races.find { it.name == decoded }?.addTrack(trackId)
|
||||
RaceHolder.races.find { it.name == decoded }?.addTrack(Track(trackId, endTime))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -47,10 +49,10 @@ private suspend fun PipelineContext<Unit, ApplicationCall>.trackPage(leaderboard
|
||||
td { +"Name" }
|
||||
}
|
||||
}
|
||||
leaderboard.tracktimes.sortedBy { it.lap_time }.forEach {
|
||||
leaderboard.trackTimes.sortedBy { it.lapTime }.forEach {
|
||||
tr {
|
||||
td { +it.lap_time.toString() }
|
||||
td { +it.playername }
|
||||
td { +it.lapTime.toString() }
|
||||
td { +it.playerName }
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -70,6 +72,10 @@ private suspend fun PipelineContext<Unit, ApplicationCall>.addTrackPage(trackId:
|
||||
}
|
||||
}
|
||||
}
|
||||
p {
|
||||
+"Optional end time:"
|
||||
dateTimeLocalInput(name = "endTime")
|
||||
}
|
||||
submitInput()
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ import io.ktor.client.*
|
||||
import io.ktor.client.call.*
|
||||
import io.ktor.client.engine.cio.*
|
||||
import io.ktor.client.request.*
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.net.URLEncoder
|
||||
@ -13,38 +14,67 @@ import javax.crypto.spec.SecretKeySpec
|
||||
object Velocidrone{
|
||||
private val key = "BatCaveGGevaCtaB".toByteArray(Charset.defaultCharset())
|
||||
|
||||
fun decodeVeloBase64(input: ByteArray): Result<String> {
|
||||
private fun decodeVeloBase64(input: ByteArray): Result<String> {
|
||||
val decoded = Base64.getDecoder().decode(input)
|
||||
val cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
|
||||
cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec(key, "AES"))
|
||||
return Result.success(String(cipher.doFinal(decoded)))
|
||||
}
|
||||
|
||||
fun encodeVeloBase64(input: String): ByteArray {
|
||||
private fun encodeVeloBase64(input: String): ByteArray {
|
||||
val encoded: ByteArray = input.toByteArray()
|
||||
val cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
|
||||
cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(key, "AES"))
|
||||
return Base64.getEncoder().encode(cipher.doFinal(encoded))
|
||||
}
|
||||
|
||||
val client = HttpClient(CIO)
|
||||
private val client = HttpClient(CIO)
|
||||
|
||||
@Serializable
|
||||
data class Leaderboard(
|
||||
val success:Boolean,
|
||||
val message_title:String,
|
||||
@SerialName("message_title")
|
||||
val messageTitle: String,
|
||||
val message:String,
|
||||
val tracktimes:Array<TrackTime>
|
||||
)
|
||||
@SerialName("tracktimes")
|
||||
val trackTimes: Array<TrackTime>
|
||||
) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as Leaderboard
|
||||
|
||||
if (success != other.success) return false
|
||||
if (messageTitle != other.messageTitle) return false
|
||||
if (message != other.message) return false
|
||||
if (!trackTimes.contentEquals(other.trackTimes)) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = success.hashCode()
|
||||
result = 31 * result + messageTitle.hashCode()
|
||||
result = 31 * result + message.hashCode()
|
||||
result = 31 * result + trackTimes.contentHashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class TrackTime(
|
||||
val lap_time:Float,
|
||||
val playername: String,
|
||||
val model_id: Long,
|
||||
@SerialName("lap_time")
|
||||
val lapTime: Float,
|
||||
@SerialName("playername")
|
||||
val playerName: String,
|
||||
@SerialName("model_id")
|
||||
val modelId: Long,
|
||||
val country: String,
|
||||
val sim_version: String,
|
||||
val device_type: Long
|
||||
@SerialName("sim_version")
|
||||
val simVersion: String,
|
||||
@SerialName("device_type")
|
||||
val deviceType: Long
|
||||
)
|
||||
|
||||
suspend fun getLeaderboardForId(id:Long): Result<Leaderboard> {
|
||||
@ -66,17 +96,44 @@ object Velocidrone{
|
||||
data class TrackResults(
|
||||
val success:Boolean,
|
||||
val detail:String,
|
||||
val user_tracks:Array<Track>
|
||||
)
|
||||
@SerialName("user_tracks")
|
||||
val userTracks: Array<Track>
|
||||
) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as TrackResults
|
||||
|
||||
if (success != other.success) return false
|
||||
if (detail != other.detail) return false
|
||||
if (!userTracks.contentEquals(other.userTracks)) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = success.hashCode()
|
||||
result = 31 * result + detail.hashCode()
|
||||
result = 31 * result + userTracks.contentHashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class Track(
|
||||
val id:Long,
|
||||
val scenery_id:Long,
|
||||
val track_name:String,
|
||||
val track_type:String,
|
||||
val playername:String,
|
||||
@SerialName("scenery_id")
|
||||
val sceneryId: Long,
|
||||
@SerialName("track_name")
|
||||
val trackName: String,
|
||||
@SerialName("track_type")
|
||||
val trackType: String,
|
||||
@SerialName("playername")
|
||||
val playerName: String,
|
||||
val rating:Float,
|
||||
val total_ratings:Long,
|
||||
@SerialName("total_ratings")
|
||||
val totalRatings: Long,
|
||||
val date:String
|
||||
)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user