Skip to content

Commit 98dccbf

Browse files
#122: Fixed more issues with the Survival Score and Last Survivor Bonus due to as the ScoreAndDamage needed to be reset after each round.
1 parent a42cac7 commit 98dccbf

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

server/src/main/kotlin/dev/robocode/tankroyale/server/score/ScoreAndDamage.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ class ScoreAndDamage {
1919
var lastSurvivorCount: Int = 0
2020
private set
2121

22+
/** Clears all registered scores and damages when a new round is started. */
23+
fun clear() {
24+
bulletDamage.clear()
25+
ramHits.clear()
26+
bulletKillEnemyIds.clear()
27+
ramKillEnemyIds.clear()
28+
survivalCount = 0
29+
lastSurvivorCount = 0
30+
}
31+
2232
/** The total bullet damage dealt by this bot to other bots. */
2333
fun getTotalBulletDamage() = bulletDamage.keys.sumOf { getBulletDamage(it) }
2434

@@ -33,23 +43,28 @@ class ScoreAndDamage {
3343

3444
/**
3545
* Returns the bullet damage dealt by this bot to specific bot.
36-
* @param enemyId is the enemy bot to retrieve the damage for.
46+
* @param enemyId is the identifier of the specific enemy bot
3747
* @return the bullet damage dealt to a specific bot.
3848
*/
3949
private fun getBulletDamage(enemyId: ParticipantId): Double = bulletDamage[enemyId] ?: 0.0
4050

4151
/**
4252
* Returns the number of times ram damage has been dealt by this bot to specific bot.
43-
* @param enemyId is the enemy bot to retrieve the ram count for.
53+
* @param enemyId is the identifier of the specific enemy bot
4454
* @return the ram count for a specific bot.
4555
*/
4656
private fun getRamHits(enemyId: ParticipantId): Int = ramHits[enemyId] ?: 0
4757

58+
/**
59+
* Returns the total damage dealt to a specific bot.
60+
* @param enemyId is the identifier of the specific enemy bot
61+
* @return the total damage dealt to the bot.
62+
*/
4863
fun getTotalDamage(enemyId: ParticipantId): Double = getBulletDamage(enemyId) + getRamHits(enemyId) * RAM_DAMAGE
4964

5065
/**
5166
* Adds bullet damage to a specific enemy bot.
52-
* @param enemyId is the identifier of the enemy bot
67+
* @param enemyId is the identifier of the specific enemy bot
5368
* @param damage is the amount of damage that the enemy bot has received
5469
*/
5570
fun addBulletDamage(enemyId: ParticipantId, damage: Double) {

server/src/main/kotlin/dev/robocode/tankroyale/server/score/ScoreTracker.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ScoreTracker(private val participantIds: Set<ParticipantId>) {
3030
fun clear() {
3131
aliveParticipants.apply { clear(); addAll(participantIds) }
3232
lastSurvivors = null
33+
scoreAndDamages.values.forEach { it.clear() }
3334
}
3435

3536
/**
@@ -39,17 +40,19 @@ class ScoreTracker(private val participantIds: Set<ParticipantId>) {
3940
*/
4041
fun calculateScore(participantId: ParticipantId): Score {
4142
getScoreAndDamage(participantId).apply {
42-
val isAlive = aliveParticipants.any { it == participantId }
43-
val isLastSurvivor = lastSurvivors?.any { it == participantId } ?: false
43+
val scoreAndDamage = scoreAndDamages[participantId]
44+
45+
val survivalCount = scoreAndDamage?.survivalCount ?: 0
46+
val lastSurvivorCount = scoreAndDamage?.lastSurvivorCount ?: 0
4447

4548
return Score(
4649
participantId = participantId,
4750
bulletDamageScore = SCORE_PER_BULLET_DAMAGE * getTotalBulletDamage(),
4851
bulletKillBonus = BONUS_PER_BULLET_KILL * getBulletKillEnemyIds().sumOf { getTotalDamage(it) },
4952
ramDamageScore = SCORE_PER_RAM_DAMAGE * getTotalRamDamage(),
5053
ramKillBonus = BONUS_PER_RAM_KILL * getRamKillEnemyIds().sumOf { getTotalDamage(it) },
51-
survivalScore = SCORE_PER_SURVIVAL * if (isAlive) 1 else 0,
52-
lastSurvivorBonus = BONUS_PER_LAST_SURVIVOR * if (isLastSurvivor) 1 else 0,
54+
survivalScore = SCORE_PER_SURVIVAL * survivalCount,
55+
lastSurvivorBonus = BONUS_PER_LAST_SURVIVOR * lastSurvivorCount,
5356
)
5457
}
5558
}

0 commit comments

Comments
 (0)