Skip to content

Commit

Permalink
Reduce slowness in getting names when updating dynmap, closes #1604
Browse files Browse the repository at this point in the history
  • Loading branch information
renbinden committed Nov 14, 2022
1 parent daae497 commit 47362c0
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class MfDynmapService(private val plugin: MedievalFactions) {
factionMarkersByFactionId[faction.id]?.forEach { marker -> marker.deleteMarker() }
val claimService = plugin.services.claimService
val claims = claimService.getClaims(faction.id)
val factionInfo = buildFactionInfo(faction)
claims.groupBy { it.worldId }.forEach { (worldId, worldClaims) ->
val world = plugin.server.getWorld(worldId)
if (world != null) {
Expand All @@ -51,7 +52,7 @@ class MfDynmapService(private val plugin: MedievalFactions) {
val color = Integer.decode(faction.flags[plugin.flags.color])
areaMarker.setFillStyle(0.0, color)
areaMarker.setLineStyle(1, 1.0, color)
areaMarker.description = buildFactionInfo(faction)
areaMarker.description = factionInfo
val factionMarkers = factionMarkersByFactionId[faction.id] ?: listOf()
factionMarkersByFactionId[faction.id] = factionMarkers + areaMarker
}
Expand All @@ -68,7 +69,7 @@ class MfDynmapService(private val plugin: MedievalFactions) {
val color = Integer.decode(faction.flags[plugin.flags.color])
areaMarker.setFillStyle(0.3, color)
areaMarker.setLineStyle(0, 0.0, color)
areaMarker.description = buildFactionInfo(faction)
areaMarker.description = factionInfo
val factionMarkers = factionMarkersByFactionId[faction.id] ?: listOf()
factionMarkersByFactionId[faction.id] = factionMarkers + areaMarker
}
Expand Down Expand Up @@ -97,7 +98,7 @@ class MfDynmapService(private val plugin: MedievalFactions) {
val color = Integer.decode(faction.flags[plugin.flags.color])
areaMarker.setFillStyle(0.0, color)
areaMarker.setLineStyle(4, 1.0, color)
areaMarker.description = buildFactionInfo(faction)
areaMarker.description = factionInfo
val factionMarkers = factionMarkersByFactionId[faction.id] ?: listOf()
factionMarkersByFactionId[faction.id] = factionMarkers + areaMarker
}
Expand Down Expand Up @@ -194,6 +195,7 @@ class MfDynmapService(private val plugin: MedievalFactions) {
val factionService = plugin.services.factionService
val relationshipService = plugin.services.factionRelationshipService
val claimService = plugin.services.claimService
val playerService = plugin.services.playerService
return buildString {
append("<h1>${faction.name}</h1>")
append("<h2>Description</h2>")
Expand All @@ -203,7 +205,7 @@ class MfDynmapService(private val plugin: MedievalFactions) {
faction.members.groupBy { it.role }.map { (role, members) ->
"""
<h3>${role.name} (${faction.members.count { it.role.id == role.id }})</h3>
${members.map { member -> member.playerId.toBukkitPlayer().name }.joinToString()}
${members.joinToString { member -> playerService.getPlayer(member.playerId)?.name ?: plugin.language["UnknownPlayer"] }}
""".trimIndent()
}.joinToString("<br />")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ class AsyncPlayerPreLoginListener(private val plugin: MedievalFactions) : Listen
val playerService = plugin.services.playerService
val playerId = MfPlayerId(event.uniqueId.toString())
val player = playerService.getPlayer(playerId)
?: playerService.save(MfPlayer(plugin, playerId))
?: playerService.save(MfPlayer(plugin, playerId, event.name))
.onFailure {
plugin.logger.log(SEVERE, "Failed to save player: ${it.reason.message}", it.reason.cause)
return
}
if (player.name != event.name) {
playerService.save(player.copy(name = event.name)).onFailure {
plugin.logger.log(SEVERE, "Failed to save player: ${it.reason.message}", it.reason.cause)
return
}
}

val interactionService = plugin.services.interactionService
interactionService.loadInteractionStatus(player.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ class JooqMfPlayerRepository(private val plugin: MedievalFactions, private val d
val rowCount = dsl.insertInto(MF_PLAYER)
.set(MF_PLAYER.ID, player.id.value)
.set(MF_PLAYER.VERSION, 1)
.set(MF_PLAYER.NAME, player.name)
.set(MF_PLAYER.POWER, player.power)
.set(MF_PLAYER.POWER_AT_LOGOUT, player.powerAtLogout)
.set(MF_PLAYER.BYPASS_ENABLED, player.isBypassEnabled)
.set(MF_PLAYER.CHAT_CHANNEL, player.chatChannel?.name)
.onConflict(MF_PLAYER.ID).doUpdate()
.set(MF_PLAYER.NAME, player.name)
.set(MF_PLAYER.POWER, player.power)
.set(MF_PLAYER.POWER_AT_LOGOUT, player.powerAtLogout)
.set(MF_PLAYER.BYPASS_ENABLED, player.isBypassEnabled)
Expand Down Expand Up @@ -97,6 +99,7 @@ class JooqMfPlayerRepository(private val plugin: MedievalFactions, private val d
private fun MfPlayerRecord.toDomain() = MfPlayer(
MfPlayerId(id),
version,
name,
power,
powerAtLogout,
bypassEnabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ data class MfPlayer(
@get:JvmName("getId")
val id: MfPlayerId,
val version: Int = 0,
val name: String? = null,
val power: Double = 0.0,
val powerAtLogout: Double = 0.0,
val isBypassEnabled: Boolean = false,
Expand All @@ -17,9 +18,11 @@ data class MfPlayer(

constructor(
plugin: MedievalFactions,
id: MfPlayerId
id: MfPlayerId,
name: String?
): this(
id,
name = name,
power = plugin.config.getDouble("players.initialPower"),
powerAtLogout = plugin.config.getDouble("players.initialPower")
)
Expand All @@ -34,6 +37,7 @@ data class MfPlayer(
): this(
MfPlayerId.fromBukkitPlayer(player),
version,
player.name,
power,
powerOnLogout,
isBypassEnabled,
Expand All @@ -45,6 +49,7 @@ data class MfPlayer(
player: OfflinePlayer
): this(
MfPlayerId.fromBukkitPlayer(player),
name = player.name,
power = plugin.config.getDouble("players.initialPower"),
powerAtLogout = plugin.config.getDouble("players.initialPower")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import com.dansplugins.factionsystem.failure.ServiceFailureType.CONFLICT
import com.dansplugins.factionsystem.failure.ServiceFailureType.GENERAL
import dev.forkhandles.result4k.Result4k
import dev.forkhandles.result4k.mapFailure
import dev.forkhandles.result4k.onFailure
import dev.forkhandles.result4k.resultFrom
import org.bukkit.OfflinePlayer
import java.util.concurrent.ConcurrentHashMap
import java.util.logging.Level.SEVERE
import kotlin.collections.set

class MfPlayerService(private val plugin: MedievalFactions, private val playerRepository: MfPlayerRepository) {
Expand All @@ -21,6 +23,18 @@ class MfPlayerService(private val plugin: MedievalFactions, private val playerRe
plugin.logger.info("Loading players...")
val startTime = System.currentTimeMillis()
playersById.putAll(playerRepository.getPlayers().associateBy(MfPlayer::id))
val playersToUpdate = playersById.values
.associateWith(MfPlayer::toBukkit)
.filter { (mfPlayer, bukkitPlayer) -> bukkitPlayer.name != mfPlayer.name }
playersToUpdate.forEach { (mfPlayer, bukkitPlayer) ->
val updatedPlayer = resultFrom {
playerRepository.upsert(mfPlayer.copy(name = bukkitPlayer.name))
}.onFailure {
plugin.logger.log(SEVERE, "Failed to update player: ${it.reason.message}", it.reason.cause)
return@forEach
}
playersById[updatedPlayer.id] = updatedPlayer
}
plugin.logger.info("${playersById.size} players loaded (${System.currentTimeMillis() - startTime}ms)")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table `mf_player`
add `name` varchar(16);

0 comments on commit 47362c0

Please sign in to comment.