From 3b6517090c32f6477946599aea0278807205f0b5 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Sat, 10 Feb 2024 05:50:28 -0500 Subject: [PATCH] [+] Add play count to trend --- .../api/controller/sega/game/maimai2/Maimai2New.kt | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/icu/samnyan/aqua/api/controller/sega/game/maimai2/Maimai2New.kt b/src/main/java/icu/samnyan/aqua/api/controller/sega/game/maimai2/Maimai2New.kt index 254ad902..e6bad8dd 100644 --- a/src/main/java/icu/samnyan/aqua/api/controller/sega/game/maimai2/Maimai2New.kt +++ b/src/main/java/icu/samnyan/aqua/api/controller/sega/game/maimai2/Maimai2New.kt @@ -14,17 +14,19 @@ class Maimai2New( private val userPlaylogRepository: UserPlaylogRepository ) { - data class TrendOut(val date: String, val rating: Int) + data class TrendOut(val date: String, val rating: Int, val plays: Int) @GetMapping("trend") fun trend(@RequestParam userId: Long): List { - // O(n log n) sort TODO: It might be possible to optimize this + // O(n log n) sort val d = userPlaylogRepository.findByUser_Card_ExtId(userId).sortedBy { it.playDate }.toList() - // Assume it's sorted by date, map the values O(n) - val map = d.associate { it.playDate to it.afterRating } + // Precompute the play counts for each date in O(n) + val playCounts = d.groupingBy { it.playDate }.eachCount() - // Is sorting here necessary? - return map.map { TrendOut(it.key, it.value) }.sortedBy { it.date } + // Use the precomputed play counts + return d.distinctBy { it.playDate } + .map { TrendOut(it.playDate, it.afterRating, playCounts[it.playDate] ?: 0) } + .sortedBy { it.date } } } \ No newline at end of file