Skip to content

Commit

Permalink
change totalReward to BigInt (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
t0lia authored May 25, 2024
1 parent e22569c commit ad9ad4b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ class SmartContractService(
return
}
val updatedStatistic: StatisticModel = existingStatistic?.copy(
totalReward = existingStatistic.totalReward + event.winnings.toInt(),
totalReward = existingStatistic.totalReward.add(event.winnings),
overallWinsInDuels = existingStatistic.overallWinsInDuels + 1
)
?: StatisticModel(
userId = event.winner,
completedDuels = 0,
averageSpeed = 0.0,
totalReward = event.winnings.toInt(),
totalReward = event.winnings,
overallWinsInDuels = 1,
maxSpeed = 0.0,
topSpeeds = mutableListOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.retypeme.project.statistic.service.StatisticService
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.math.BigInteger

@RestController
@RequestMapping("/statistics")
Expand Down Expand Up @@ -48,7 +49,7 @@ class StatisticController(
userId = statistic.userId!!,
completedDuels = statistic.completedDuels!!,
averageSpeed = statistic.averageSpeed!!,
totalReward = statistic.totalReward!!,
totalReward = BigInteger(statistic.totalReward!!),
overallWinsInDuels = statistic.overallWinsInDuels!!,
maxSpeed = statistic.maxSpeed!!,
topSpeeds = statistic.topSpeeds?.toMutableList() ?: mutableListOf()
Expand All @@ -60,7 +61,7 @@ class StatisticController(
userId = statisticModel.userId,
completedDuels = statisticModel.completedDuels,
averageSpeed = statisticModel.averageSpeed,
totalReward = statisticModel.totalReward,
totalReward = statisticModel.totalReward.toString(),
overallWinsInDuels = statisticModel.overallWinsInDuels,
maxSpeed = statisticModel.maxSpeed,
topSpeeds = statisticModel.topSpeeds
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.retypeme.project.statistic.model

import java.io.Serializable
import java.math.BigInteger

data class StatisticModel(
val userId: String,
val completedDuels: Int,
val averageSpeed: Double,
val totalReward: Int,
val totalReward: BigInteger,
val overallWinsInDuels: Int,
val maxSpeed: Double,
val topSpeeds: List<Double>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.retypeme.project.statistic.service
import com.retypeme.project.statistic.model.StatisticModel
import com.retypeme.project.statistic.repository.StatisticRepository
import org.springframework.stereotype.Service
import java.math.BigInteger

@Service
class StatisticService(private val userStatisticRepository: StatisticRepository) {
Expand All @@ -14,7 +15,7 @@ class StatisticService(private val userStatisticRepository: StatisticRepository)
val totalSpeed = (existingStatisticModel?.averageSpeed ?: 0.0) * (completedDuels - 1) + speed
val averageSpeed = totalSpeed / completedDuels
val maxSpeed = maxOf(existingStatisticModel?.maxSpeed ?: 0.0, speed)
val totalReward = existingStatisticModel?.totalReward ?: 0
val totalReward = existingStatisticModel?.totalReward ?: BigInteger.ZERO
val overallWinsInDuels = existingStatisticModel?.overallWinsInDuels ?: 0
val topSpeeds = existingStatisticModel?.let { getTopSpeeds(it, speed) } ?: mutableListOf()

Expand Down
3 changes: 2 additions & 1 deletion backend/src/main/resources/api-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ components:
type: number
format: double
totalReward:
type: integer
type: string
format: biginteger
overallWinsInDuels:
type: integer
maxSpeed:
Expand Down

0 comments on commit ad9ad4b

Please sign in to comment.