Skip to content

Commit

Permalink
Merge pull request #49 from SettingDust/feature/update-project-loaders
Browse files Browse the repository at this point in the history
feat: prefer current loaders of project files
  • Loading branch information
juraj-hrivnak authored Oct 16, 2024
2 parents 0178e88 + 3ef129b commit 6ff15b4
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/teksturepako/pakku/Version.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

package teksturepako.pakku

const val VERSION = "0.20.1"
const val VERSION = "0.20.2"
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,29 @@ suspend fun updateMultipleProjectsWithFiles(

private fun combineProjects(accProject: Project, newProject: Project, platformName: String, numberOfFiles: Int): Project
{
val accPublished = accProject.files
.filter { projectFile ->
projectFile.type == platformName
val accFile = accProject.files.filter { projectFile ->
projectFile.type == platformName
}.maxByOrNull { it.datePublished }
val accPublished = accFile?.datePublished ?: Instant.DISTANT_PAST
val newFiles =
if (accFile == null)
{
newProject.files
}
else
{
val accLoaders = accFile.loaders
newProject.files.sortedWith(compareBy { file ->
val fileLoaders = file.loaders
accLoaders.indexOfFirst { it in fileLoaders }.let { if (it == -1) accLoaders.size else it }
})
}
.maxOfOrNull { it.datePublished }
?: Instant.DISTANT_PAST

val updatedFiles = (newProject.files.take(numberOfFiles) + accProject.files)
.filterNot { projectFile ->
val updatedFiles = (newFiles.take(numberOfFiles) + accProject.files).filterNot { projectFile ->
projectFile.type == platformName && projectFile.datePublished < accPublished
}
.toMutableSet()

return (accProject + newProject).get()?.copy(files = updatedFiles)
?.takeIf { it.hasFiles() } ?: accProject
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,10 @@ object CurseForge : Platform(
} ?: true // If no loaders found, accept model
}

internal fun List<CfModModel.File>.sortByLoaders(loaders: List<String>) = this.sortedWith { fileA, fileB ->
val aLoaders = fileA.sortableGameVersions.filter { it.gameVersionTypeId == LOADER_VERSION_TYPE_ID }
internal fun compareByLoaders(loaders: List<String>) = { file: CfModModel.File ->
val fileLoaders = file.sortableGameVersions.filter { it.gameVersionTypeId == LOADER_VERSION_TYPE_ID }
.map { it.gameVersionName.lowercase() }

val bLoaders = fileB.sortableGameVersions.filter { it.gameVersionTypeId == LOADER_VERSION_TYPE_ID }
.map { it.gameVersionName.lowercase() }

loaders.indexOfFirst { it in aLoaders }.let { if (it == -1) loaders.size else it }
- loaders.indexOfFirst { it in bLoaders }.let { if (it == -1) loaders.size else it }
loaders.indexOfFirst { it in fileLoaders }.let { if (it == -1) loaders.size else it }
}

private fun CfModModel.File.toProjectFile(gameVersionTypeIds: List<Int>): ProjectFile
Expand Down Expand Up @@ -205,7 +200,7 @@ object CurseForge : Platform(
this.requestProjectBody(requestUrl) ?: return mutableSetOf()
).data
.filterFileModels(mcVersions, loaders)
.sortByLoaders(loaders)
.sortedWith(compareBy(compareByLoaders(loaders)))
.map { it.toProjectFile(gameVersionTypeIds) }
.debugIfEmpty {
println("${this::class.simpleName}#requestProjectFiles: file is null")
Expand Down Expand Up @@ -236,8 +231,7 @@ object CurseForge : Platform(
this.requestProjectBody("mods/files", MultipleFilesRequest(ids.map(String::toInt))) ?: return mutableSetOf()
).data
.filterFileModels(mcVersions, loaders)
.sortedByDescending { Instant.parse(it.fileDate) }
.sortByLoaders(loaders)
.sortedWith(compareByDescending<CfModModel.File> { Instant.parse(it.fileDate) }.thenBy(compareByLoaders(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 @@ -144,9 +144,8 @@ object Modrinth : Platform(
} ?: true // If no loaders found, accept model
}

internal fun List<MrVersionModel>.sortByLoaders(loaders: List<String>) = this.sortedWith { aVersion, bVersion ->
loaders.indexOfFirst { it in aVersion.loaders }.let { if (it == -1) loaders.size else it }
.minus(loaders.indexOfFirst { it in bVersion.loaders }.let { if (it == -1) loaders.size else it })
internal fun compareByLoaders(loaders: List<String>) = { version: MrVersionModel ->
loaders.indexOfFirst { it in version.loaders }.let { if (it == -1) loaders.size else it }
}

private fun MrVersionModel.toProjectFiles(): List<ProjectFile>
Expand Down Expand Up @@ -189,7 +188,7 @@ object Modrinth : Platform(
this.requestProjectBody("project/$projectId/version") ?: return mutableSetOf()
)
.filterFileModels(mcVersions, loaders)
.sortByLoaders(loaders)
.sortedWith(compareBy(compareByLoaders(loaders)))
.flatMap { version -> version.toProjectFiles() }
.debugIfEmpty {
println("${this::class.simpleName}#requestProjectFiles: file is null")
Expand Down Expand Up @@ -222,8 +221,7 @@ object Modrinth : Platform(
.awaitAll()
.flatten()
.filterFileModels(mcVersions, loaders)
.sortedByDescending { Instant.parse(it.datePublished) }
.sortByLoaders(loaders)
.sortedWith(compareBy ({ Instant.parse(it.datePublished) }, compareByLoaders(loaders)))
.flatMap { version -> version.toProjectFiles() }
.toMutableSet()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.mockk.every
import io.mockk.mockk
import teksturepako.pakku.api.models.cf.CfModModel
import teksturepako.pakku.api.platforms.CurseForge.LOADER_VERSION_TYPE_ID
import teksturepako.pakku.api.platforms.CurseForge.sortByLoaders
import teksturepako.pakku.api.platforms.CurseForge.compareByLoaders
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
Expand All @@ -17,7 +17,7 @@ class CurseForgeTest
}

@Test
fun sortByLoaders_WithValidLoaders_ShouldSortCorrectly() {
fun sortByLoaders_WithValidLoaders_ShouldCompareCorrectly() {
val files = listOf(
mockk<CfModModel.File> {
every { sortableGameVersions } returns listOf(
Expand Down Expand Up @@ -70,13 +70,13 @@ class CurseForgeTest
)

val loaders = listOf("loadera", "loaderb", "loaderc")
val sortedFiles = files.toList().sortByLoaders(loaders)
val sortedFiles = files.toList().sortedWith(compareBy(compareByLoaders(loaders)))

assertContentEquals(listOf(files[1], files[3], files[0], files[2]), sortedFiles)
}

@Test
fun sortByLoaders_WithNoMatchingLoaders_ShouldNotChangeOrder() {
fun compareByLoaders_WithNoMatchingLoaders_ShouldNotChangeOrder() {
val files = listOf(
mockk<CfModModel.File> {
every { sortableGameVersions } returns listOf(
Expand All @@ -97,12 +97,12 @@ class CurseForgeTest
)

val loaders = listOf("loader1", "loader2")
val sortedFiles = files.toList().sortByLoaders(loaders)
val sortedFiles = files.toList().sortedWith(compareBy(compareByLoaders(loaders)))
assertContentEquals(files, sortedFiles)
}

@Test
fun sortByLoaders_WithSomeMatchingLoaders_ShouldSortCorrectly() {
fun sortByLoaders_WithSomeMatchingLoaders_ShouldCompareCorrectly() {
val files = listOf(
mockk<CfModModel.File> {
every { sortableGameVersions } returns listOf(
Expand All @@ -123,7 +123,7 @@ class CurseForgeTest
)

val loaders = listOf("loader1", "loader2")
val sortedFiles = files.toList().sortByLoaders(loaders)
val sortedFiles = files.toList().sortedWith(compareBy(compareByLoaders(loaders)))
assertEquals(files[0], sortedFiles[1])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package teksturepako.pakku.api.platforms
import io.mockk.every
import io.mockk.mockk
import teksturepako.pakku.api.models.mr.MrVersionModel
import teksturepako.pakku.api.platforms.Modrinth.sortByLoaders
import teksturepako.pakku.api.platforms.Modrinth.compareByLoaders
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals

class ModrinthTest
{
@Test
fun sortByLoaders_WithValidLoaders_ShouldSortCorrectly() {
fun compareByLoaders_WithValidLoaders_ShouldSortCorrectly() {
val versions = listOf(
mockk<MrVersionModel> {
every { loaders } returns listOf("loaderb", "loaderc")
Expand All @@ -28,13 +28,13 @@ class ModrinthTest
)

val loaders = listOf("loadera", "loaderb", "loaderc")
val sortedVersions = versions.toList().sortByLoaders(loaders)
val sortedVersions = versions.toList().sortedWith(compareBy(compareByLoaders(loaders)))

assertContentEquals(listOf(versions[1], versions[3], versions[0], versions[2]), sortedVersions)
}

@Test
fun sortByLoaders_WithNoMatchingLoaders_ShouldNotChangeOrder() {
fun compareByLoaders_ShouldNotChangeOrder() {
val versions = listOf(
mockk<MrVersionModel> {
every { loaders } returns listOf("loaderB")
Expand All @@ -45,13 +45,13 @@ class ModrinthTest
)

val loaders = listOf("loader1", "loader2")
val sortedVersions = versions.toList().sortByLoaders(loaders)
val sortedVersions = versions.toList().sortedWith(compareBy(compareByLoaders(loaders)))

assertContentEquals(versions, sortedVersions)
}

@Test
fun sortByLoaders_WithSomeMatchingLoaders_ShouldSortCorrectly() {
fun compareByLoaders_WithSomeMatchingLoaders_ShouldSortCorrectly() {
val versions = listOf(
mockk<MrVersionModel> {
every { loaders } returns listOf("loadera")
Expand All @@ -62,7 +62,7 @@ class ModrinthTest
)

val loaders = listOf("loader1", "loader2")
val sortedVersions = versions.toList().sortByLoaders(loaders)
val sortedVersions = versions.toList().sortedWith(compareBy(compareByLoaders(loaders)))

assertEquals(versions[0], sortedVersions[1])
}
Expand Down

0 comments on commit 6ff15b4

Please sign in to comment.