Skip to content

Commit

Permalink
wait for mojang api rate limit #86
Browse files Browse the repository at this point in the history
  • Loading branch information
l3nnartt committed Jun 20, 2024
2 parents 00d5c15 + 676a451 commit c5a037a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/team/teamler.json
Original file line number Diff line number Diff line change
Expand Up @@ -3105,7 +3105,7 @@
"rank_history": {
"initial": "spieler",
"20.11.2022": "developer",
"09.05.2024": "native"
"09.05.2024": "native_mvp"
}
},
{
Expand Down
10 changes: 6 additions & 4 deletions howto-dapp/src/main/java/de/timolia/howto/Dapp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ import de.timolia.howto.generator.FileWriter
import de.timolia.howto.generator.PageRankChange
import de.timolia.howto.generator.PageResponsibilities
import de.timolia.howto.generator.PageTeamMembers
import de.timolia.howto.minecraft.MojangService
import de.timolia.howto.teamler.Teamler
import de.timolia.howto.translate.Translate
import kotlinx.coroutines.runBlocking
import java.nio.file.Files
import java.nio.file.Path
import java.util.*
import kotlin.Throws
import kotlin.jvm.JvmStatic
import java.util.logging.Logger

object Dapp {
val translate: Translate = Translate()
val gson: Gson = GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create()
private val logger: Logger = Logger.getLogger(this::class.simpleName)

@Throws(Exception::class)
@JvmStatic
Expand All @@ -32,10 +30,14 @@ object Dapp {
val json = Files.readString(path.resolve("teamler.json"))
val teamlers = gson.fromJson(json, Array<Teamler>::class.java)
val teamlerList = listOf(*teamlers)
logger.info("Update team member names...")
teamlerList.forEach(Teamler::updateName)
with(FileWriter(path, translate)) {
logger.info("Generate team changes...")
writeFile("changes", PageRankChange.generate(teamlerList))
logger.info("Generate team page members...")
writeFile("members", PageTeamMembers(teamlerList).generate())
logger.info("Generate team responsibilities...")
writeFile("responsibilities", PageResponsibilities.generate(teamlerList))
}
if(args.any { it.equals("init", ignoreCase = true) }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,33 @@ package de.timolia.howto.minecraft
import com.google.gson.Gson
import fuel.httpGet
import kotlinx.coroutines.runBlocking
import java.util.logging.Logger

object MojangService {
data class ProfileInfo(val id: String, val name: String? = null)
val resolved = HashMap<String, String>()
private val resolved = HashMap<String, String>()
private val logger = Logger.getLogger(this::class.simpleName)

fun nameFromUUid(uuid: String) = resolved.computeIfAbsent(uuid, ::resolve)
/**
* Get the player name of an uuid from the Mojang api or local cache
*
* @param uuid the uuid of the player the name should be looked up
* @param fallback the name of player if the Mojang api can't look up the player name
* @return the player name
*/
fun nameFromUUid(uuid: String, fallback: () -> String) = resolved.computeIfAbsent(uuid) {
resolve(uuid) ?: fallback()
}

fun resolve(uuid: String): String = runBlocking {
val base_url = "https://sessionserver.mojang.com/session/minecraft/profile/$uuid"
private fun resolve(uuid: String): String? = runBlocking {
val baseUrl = "https://sessionserver.mojang.com/session/minecraft/profile/$uuid"

val response = base_url.httpGet()
val response = baseUrl.httpGet()
try {
Gson().fromJson(response.body, ProfileInfo::class.java).name!!
} catch (exception: Exception) {
println(response)
throw exception
logger.warning(response.body)
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class TeamlerRankChange {

constructor(name: String?, uuid: UUID, rankFrom: Rank, rankTo: Rank, date: String, hidden: Boolean) {
this.name = try {
MojangService.nameFromUUid(uuid.toString())
MojangService.nameFromUUid(uuid.toString()) {
name!!
}
} catch (exception: Exception) {
exception.printStackTrace()
name!!
Expand Down
11 changes: 5 additions & 6 deletions howto-dapp/src/main/java/de/timolia/howto/teamler/Teamler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class Teamler(
if (rankHistory.isNullOrEmpty()) {
return rankChanges
}
for (i in 0 until rankHistory.size) {
val rank = rankHistory.values.toTypedArray()[i]
Validate.notNull(rank, "Der " + (i + 1) + ". Rang von '" + name + "' existiert nicht")
val ranks = rankHistory.values.toTypedArray()
ranks.forEachIndexed { index, rank ->
Validate.notNull(rank, "Der " + (index + 1) + ". Rang von '" + name + "' existiert nicht")
}
val dates = rankHistory.keys
.map { s -> s.replace("hidden-", "") }
Expand Down Expand Up @@ -108,9 +108,8 @@ class Teamler(
}

fun updateName() {
name = MojangService.nameFromUUid(uuid.toString())
if (name == null) {
name = SQLApi.getName(uuid).toString()
name = MojangService.nameFromUUid(uuid.toString()) {
SQLApi.getName(uuid).toString()
}
}

Expand Down

0 comments on commit c5a037a

Please sign in to comment.