Skip to content

Commit

Permalink
Use list idx for playerId. Fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonnold committed Aug 23, 2020
1 parent 24c50e9 commit 2a30fb4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ class SummaryService(
}

fun buildAndSaveSummary(data: ReplayData, replay: Replay, player: Player): Mono<Summary> {
val playerSummary = ReplayUtil.findPlayerInReplayDetails(player.profileId, data.details)
val (idx, playerSummary) = ReplayUtil.findPlayerInReplayDetails(player.profileId, data.details)
if (playerSummary == null) {
logger.warn(
"Unable to locate ${player.profileId} in ${replay.slug}! No summary will be generated"
)
return Mono.empty()
}

var workingId: Long = playerSummary["m_workingSetSlotId"]
workingId++ // Increment this value since it's 1 idx everywhere else
val gamePlayerId = idx + 1L

val teamId: Long = playerSummary["m_teamId"]

Expand All @@ -52,11 +51,11 @@ class SummaryService(
val nameBlob: Blob = playerSummary["m_name"]
val name = unescapeName(nameBlob.value)

val jsonPlayer = data.metadata.players.find { p -> p.playerId == workingId }
val jsonPlayer = data.metadata.players.find { p -> p.playerId == gamePlayerId }
val didWin = jsonPlayer?.result == "Win"
val mmr = jsonPlayer?.mmr ?: 0

val summary = Summary(null, replay.id, player.id, workingId, teamId, race, name, didWin, mmr)
val summary = Summary(null, replay.id, player.id, gamePlayerId, teamId, race, name, didWin, mmr)
return this.summaryDAO.save(summary)
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/me/honnold/ladderhero/util/ReplayUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import me.honnold.s2protocol.model.data.Struct

class ReplayUtil {
companion object {
fun findPlayerInReplayDetails(profileId: Long, replayDetails: Struct): Struct? {
fun findPlayerInReplayDetails(profileId: Long, replayDetails: Struct): Pair<Int, Struct?> {
val players: List<Struct> = replayDetails["m_playerList"]

return players.find { player ->
val idx = players.indexOfFirst { player ->
val toon: Struct = player["m_toon"]
val id: Long = toon["m_id"]

profileId == id
}

return if (idx >= 0) Pair(idx, players[idx]) else Pair(idx, null)
}
}
}

0 comments on commit 2a30fb4

Please sign in to comment.