-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
176 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
backend/src/main/kotlin/com/retypeme/project/statistic/controller/StatisticController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.retypeme.project.racing.controller | ||
|
||
import com.retypeme.project.statistic.model.Statistic | ||
import com.retypeme.project.statistic.service.StatisticService | ||
import org.springframework.web.bind.annotation.* | ||
import java.time.LocalDate | ||
|
||
@RestController | ||
@RequestMapping("/statistics") | ||
class StatisticController(private val userStatisticService: StatisticService) { | ||
|
||
@GetMapping("/{userId}") | ||
fun getStatistic(@PathVariable userId: String): Statistic? { | ||
return userStatisticService.getUserStatistic(userId) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
backend/src/main/kotlin/com/retypeme/project/statistic/model/Statistic.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.retypeme.project.statistic.model | ||
|
||
import java.time.LocalDate | ||
|
||
class Statistic ( | ||
val userId: String, | ||
var completedDuels: Int = 0, | ||
var averageSpeed: Double = 0.0, | ||
var bestSpeed: Double = 0.0, | ||
var wins: Int = 0, | ||
var maxSpeed: Double = 0.0, | ||
var topSpeeds: MutableList<Double> = mutableListOf(), | ||
var firstDuelDate: LocalDate? = null ) | ||
|
29 changes: 29 additions & 0 deletions
29
backend/src/main/kotlin/com/retypeme/project/statistic/repository/StatisticRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.retypeme.project.statistic.repository | ||
|
||
import com.retypeme.project.statistic.model.Statistic | ||
import org.springframework.stereotype.Repository | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
|
||
interface StatisticRepository { | ||
fun save(userStatistic: Statistic) | ||
fun findById(userId: String): Statistic? | ||
fun findAll(): List<Statistic> | ||
} | ||
|
||
@Repository | ||
class UserStatisticRepositoryImpl : StatisticRepository { | ||
private val userStatistics = ConcurrentHashMap<String, Statistic>() | ||
|
||
override fun save(userStatistic: Statistic) { | ||
userStatistics[userStatistic.userId] = userStatistic | ||
} | ||
|
||
override fun findById(userId: String): Statistic? { | ||
return userStatistics[userId] | ||
} | ||
|
||
override fun findAll(): List<Statistic> { | ||
return userStatistics.values.toList() | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
backend/src/main/kotlin/com/retypeme/project/statistic/service/StatisticService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.retypeme.project.statistic.service | ||
|
||
import com.retypeme.project.statistic.model.Statistic | ||
import com.retypeme.project.statistic.repository.StatisticRepository | ||
import org.springframework.stereotype.Service | ||
import java.time.LocalDate | ||
|
||
@Service | ||
class StatisticService(private val userStatisticRepository: StatisticRepository) { | ||
|
||
fun updateUserStatistic(userId: String, speed: Double, won: Boolean, duelDate: LocalDate) { | ||
val userStatistic = userStatisticRepository.findById(userId) ?: Statistic(userId, firstDuelDate = duelDate) | ||
|
||
userStatistic.completedDuels++ | ||
userStatistic.averageSpeed = | ||
((userStatistic.averageSpeed * (userStatistic.completedDuels - 1)) + speed) / userStatistic.completedDuels | ||
if (speed > userStatistic.bestSpeed) { | ||
userStatistic.bestSpeed = speed | ||
} | ||
if (speed > userStatistic.maxSpeed) { | ||
userStatistic.maxSpeed = speed | ||
} | ||
if (won) { | ||
userStatistic.wins++ | ||
} | ||
userStatistic.topSpeeds.add(speed) | ||
userStatistic.topSpeeds.sortDescending() | ||
if (userStatistic.topSpeeds.size > 10) { | ||
userStatistic.topSpeeds = userStatistic.topSpeeds.take(10).toMutableList() | ||
} | ||
|
||
userStatisticRepository.save(userStatistic) | ||
} | ||
|
||
fun getUserStatistic(userId: String): Statistic? { | ||
return userStatisticRepository.findById(userId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters