Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #172 from Z-Jais/master
Browse files Browse the repository at this point in the history
Refactor decode method and simplify simulcast logic
  • Loading branch information
Ziedelth authored Oct 1, 2023
2 parents f73eee8 + 793fc95 commit 9a6b76f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 37 deletions.
15 changes: 9 additions & 6 deletions src/main/kotlin/fr/ziedelth/controllers/AbstractController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ const val MISSING_PARAMETERS_MESSAGE_ERROR = "Missing parameters"

open class AbstractController<T : Serializable>(open val prefix: String) {
data class FilterData(
val animes: List<UUID> = listOf(),
val episodes: List<UUID> = listOf(),
val episodeTypes: List<UUID> = listOf(),
val langTypes: List<UUID> = listOf(),
val animes: List<UUID> = listOf(), // Animes in watchlist
val episodes: List<UUID> = listOf(), // Episodes seen
val episodeTypes: List<UUID> = listOf(), // Episode types wanted to see
val langTypes: List<UUID> = listOf(), // Lang types wanted to see
)

val entityName: String =
((javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<*>).simpleName
val uuidRequest: UUID = UUID.randomUUID()

fun decode(watchlist: String): FilterData =
Constant.gson.fromJson(Decoder.fromGzip(watchlist), FilterData::class.java)
fun decode(watchlist: String): FilterData {
val filterData = Constant.gson.fromJson(Decoder.fromGzip(watchlist), FilterData::class.java)
println("$watchlist - Episodes: ${filterData.episodes.size} - Animes: ${filterData.animes.size}")
return filterData
}

suspend fun printError(call: ApplicationCall, e: Exception) {
e.printStackTrace()
Expand Down
55 changes: 24 additions & 31 deletions src/main/kotlin/fr/ziedelth/controllers/EpisodeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,43 +125,36 @@ class EpisodeController : AttachmentController<Episode>("/episodes") {
episode.number = episodeRepository.getLastNumber(episode) + 1
}

val releaseDatePlus15Days = CalendarConverter.toUTCCalendar(episode.releaseDate)
releaseDatePlus15Days.add(Calendar.DAY_OF_YEAR, 15)
val releaseDateMinus15Days = CalendarConverter.toUTCCalendar(episode.releaseDate)
releaseDateMinus15Days.add(Calendar.DAY_OF_YEAR, -15)
val tmpPreviousSimulcast = Simulcast.getSimulcastFrom(releaseDateMinus15Days.toISO8601())
val tmpCurrentSimulcast = Simulcast.getSimulcastFrom(episode.releaseDate)
val tmpNextSimulcast = Simulcast.getSimulcastFrom(releaseDatePlus15Days.toISO8601())
val adjustedDates = listOf(-15, 0, 15).map { days ->
CalendarConverter.toUTCCalendar(episode.releaseDate).also { it.add(Calendar.DAY_OF_YEAR, days) }
}

val simulcasts = adjustedDates.map {
Simulcast.getSimulcastFrom(it.toISO8601())
}

val tmpPreviousSimulcast = simulcasts[0]
val tmpCurrentSimulcast = simulcasts[1]
val tmpNextSimulcast = simulcasts[2]

val isAnimeReleaseDateBeforeMinus15Days = CalendarConverter.toUTCLocalDateTime(episode.anime!!.releaseDate)
.isBefore(CalendarConverter.calendarToLocalDateTime(releaseDateMinus15Days))
.isBefore(CalendarConverter.calendarToLocalDateTime(adjustedDates[0]))

if (episode.number!! <= 1 && !tmpCurrentSimulcast.equalsWithoutUUID(tmpNextSimulcast)) {
val simulcast = simulcastRepository.findBySeasonAndYear(tmpNextSimulcast.season!!, tmpNextSimulcast.year!!)
?: tmpNextSimulcast
val chosenSimulcast =
when {
episode.number!! <= 1 && !tmpCurrentSimulcast.equalsWithoutUUID(tmpNextSimulcast) -> tmpNextSimulcast
episode.number!! > 1 && isAnimeReleaseDateBeforeMinus15Days && !tmpCurrentSimulcast.equalsWithoutUUID(
tmpPreviousSimulcast
) -> tmpPreviousSimulcast

if (episode.anime!!.simulcasts.isEmpty() || episode.anime!!.simulcasts.none { it.uuid == simulcast.uuid }) {
episode.anime!!.simulcasts.add(simulcast)
else -> tmpCurrentSimulcast
}
} else if (episode.number!! > 1 && isAnimeReleaseDateBeforeMinus15Days && !tmpCurrentSimulcast.equalsWithoutUUID(
tmpPreviousSimulcast
)
) {
val simulcast =
simulcastRepository.findBySeasonAndYear(tmpPreviousSimulcast.season!!, tmpPreviousSimulcast.year!!)
?: tmpPreviousSimulcast

if (episode.anime!!.simulcasts.isEmpty() || episode.anime!!.simulcasts.none { it.uuid == simulcast.uuid }) {
episode.anime!!.simulcasts.add(simulcast)
}
} else {
val simulcast =
simulcastRepository.findBySeasonAndYear(tmpCurrentSimulcast.season!!, tmpCurrentSimulcast.year!!)
?: tmpCurrentSimulcast

if (episode.anime!!.simulcasts.isEmpty() || episode.anime!!.simulcasts.none { it.uuid == simulcast.uuid }) {
episode.anime!!.simulcasts.add(simulcast)
}
val simulcast =
simulcastRepository.findBySeasonAndYear(chosenSimulcast.season!!, chosenSimulcast.year!!) ?: chosenSimulcast

if (episode.anime!!.simulcasts.isEmpty() || episode.anime!!.simulcasts.none { it.uuid == simulcast.uuid }) {
episode.anime!!.simulcasts.add(simulcast)
}
}

Expand Down

0 comments on commit 9a6b76f

Please sign in to comment.