Skip to content

Commit

Permalink
fix: sort the order after sort by date
Browse files Browse the repository at this point in the history
  • Loading branch information
SettingDust committed Oct 7, 2024
1 parent 80665fb commit db43413
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ object CurseForge : Platform(
): List<CfModModel.File> = this
.filter { file ->
file.gameVersions.any { it in mcVersions } && file.sortableGameVersions
.filter { it.gameVersionTypeId == LOADER_VERSION_TYPE_ID } // Filter to loader only
.filter { it.gameVersionTypeId == LOADER_VERSION_TYPE_ID }
.takeIf { it.isNotEmpty() }
?.map { it.gameVersionName.lowercase() }?.any {
it in loaders || it in validLoaders // Check default valid loaders
} ?: true // If no loaders found, accept model
}.sortedWith { a, b ->
it in loaders || it in validLoaders
} ?: true
}

private fun List<CfModModel.File>.sortByLoaders(loaders: List<String>) = this.sortedWith { a, b ->
val aVersions = a.sortableGameVersions.filter { it.gameVersionTypeId == LOADER_VERSION_TYPE_ID }
.map { it.gameVersionName.lowercase() }
val bVersions = b.sortableGameVersions.filter { it.gameVersionTypeId == LOADER_VERSION_TYPE_ID }
Expand Down Expand Up @@ -198,6 +200,7 @@ object CurseForge : Platform(
this.requestProjectBody(requestUrl) ?: return mutableSetOf()
).data
.filterFileModels(mcVersions, loaders)
.sortByLoaders(loaders)
.map { it.toProjectFile(gameVersionTypeIds) }
.debugIfEmpty {
println("${this::class.simpleName}#requestProjectFiles: file is null")
Expand Down Expand Up @@ -229,6 +232,7 @@ object CurseForge : Platform(
).data
.filterFileModels(mcVersions, loaders)
.sortedByDescending { it.fileDate }
.sortByLoaders(loaders)
.map { it.toProjectFile(gameVersionTypeIds) }
.debugIfEmpty {
println("${this::class.simpleName}#requestMultipleProjectFiles: file is null")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import teksturepako.pakku.api.models.mr.GetVersionsFromHashesRequest
import teksturepako.pakku.api.models.mr.MrProjectModel
import teksturepako.pakku.api.models.mr.MrVersionModel
import teksturepako.pakku.api.projects.*
import teksturepako.pakku.debug
import teksturepako.pakku.debugIfEmpty
import kotlin.system.exitProcess
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -142,9 +143,10 @@ object Modrinth : Platform(
it in loaders || it in validLoaders // Check default valid loaders
} ?: true // If no loaders found, accept model
}
.sortedWith { a, b ->
loaders.indexOfFirst { it in a.loaders } - loaders.indexOfFirst { it in b.loaders }
}

private fun List<MrVersionModel>.sortByLoaders(loaders: List<String>) = this.sortedWith { a, b ->
loaders.indexOfFirst { it in a.loaders } - loaders.indexOfFirst { it in b.loaders }
}

private fun MrVersionModel.toProjectFiles(): List<ProjectFile>
{
Expand Down Expand Up @@ -185,6 +187,7 @@ object Modrinth : Platform(
this.requestProjectBody("project/$projectId/version") ?: return mutableSetOf()
)
.filterFileModels(mcVersions, loaders)
.sortByLoaders(loaders)
.flatMap { version -> version.toProjectFiles() }
.debugIfEmpty {
println("${this::class.simpleName}#requestProjectFiles: file is null")
Expand Down Expand Up @@ -216,8 +219,9 @@ object Modrinth : Platform(
}
.awaitAll()
.flatten()
.sortedByDescending { it.datePublished }
.filterFileModels(mcVersions, loaders)
.sortedByDescending { it.datePublished }
.sortByLoaders(loaders)
.flatMap { version -> version.toProjectFiles() }
.toMutableSet()
}
Expand Down

0 comments on commit db43413

Please sign in to comment.