This repository was archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMostRatedChooser.kt
More file actions
36 lines (34 loc) · 1.4 KB
/
MostRatedChooser.kt
File metadata and controls
36 lines (34 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.github.insanusmokrassar.AutoPostTelegramBot.plugins.choosers
import com.github.insanusmokrassar.AutoPostTelegramBot.base.models.PostId
import com.github.insanusmokrassar.AutoPostTelegramBot.base.plugins.abstractions.RatingPair
import kotlinx.serialization.Serializable
import org.joda.time.DateTime
@Serializable
class MostRatedChooser : RateChooser() {
override suspend fun triggerChoose(time: DateTime, exceptions: List<PostId>): Collection<Int> {
val mostRated = mutableListOf<RatingPair>()
ratingPlugin.getRegisteredPosts().flatMap {
ratingPlugin.getPostRatings(it)
}.forEach {
val postId = ratingPlugin.resolvePostId(it.first) ?: return@forEach
if (postId !in exceptions) {
mostRated.firstOrNull() ?.also { (_, rating) ->
when {
it.second > rating -> {
mostRated.clear()
mostRated.add(it)
}
it.second == rating -> {
mostRated.add(it)
}
}
} ?: mostRated.add(it)
}
}
return mostRated.minBy { (ratingId, _) -> ratingId } ?.let { (ratingId, _) ->
ratingPlugin.resolvePostId(ratingId) ?.let {
listOf(it)
}
} ?: emptyList()
}
}