Skip to content

Commit

Permalink
Revert "Make multiple riddles solvable from the same category"
Browse files Browse the repository at this point in the history
This reverts commit 39ed3d3.
  • Loading branch information
Isti01 committed Sep 23, 2024
1 parent 1cbdba7 commit c5033b5
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.springframework.transaction.annotation.Isolation
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional
import java.sql.SQLException
import kotlin.collections.none

@Service
@ConditionalOnBean(RiddleComponent::class)
Expand Down Expand Up @@ -52,12 +51,17 @@ open class RiddleBusinessLogicService(
) = categories.map { category ->
val riddles = riddleCacheManager.findAllRiddleByCategoryId(category.categoryId)
val total = riddles.size
val nextRiddleIds = findNextTo(category.categoryId, submissions[category.categoryId] ?: listOf())
val nextId = riddles
.filter { filter ->
submissions[category.categoryId]?.none { it.riddleId == filter.id } ?: true
}
.minByOrNull { it.order }
?.id

RiddleCategoryDto(
categoryId = category.categoryId,
title = category.title,
nextRiddles = nextRiddleIds,
nextRiddle = nextId,
completed = submissions[category.categoryId]?.size ?: 0,
total = total
)
Expand All @@ -70,8 +74,7 @@ open class RiddleBusinessLogicService(
?: return null

val submissions = riddleCacheManager.findAllMappingByOwnerUserIdAndRiddleCategoryId(user.id, riddle.categoryId)
val nextRiddles = findNextTo(riddle.categoryId, submissions)
return nextRiddles.find { it.id == riddle.id }
return mapRiddleView(submissions, riddleId, riddle)
}

@Transactional(readOnly = true)
Expand All @@ -84,19 +87,17 @@ open class RiddleBusinessLogicService(
?: return null

val submissions = riddleCacheManager.findAllMappingByGroupUserIdAndRiddleCategoryId(groupId, riddle.categoryId)
val nextRiddles = findNextTo(riddle.categoryId, submissions)
return nextRiddles.find { it.id == riddle.id }
return mapRiddleView(submissions, riddleId, riddle)
}

private fun mapRiddleView(
submissions: List<RiddleMappingEntity>,
riddleId: Int,
riddle: RiddleEntity
): RiddleView {
): RiddleView? {
val submission = submissions.find { it.riddleId == riddleId }
if (submission != null)
return RiddleView(
id = riddle.id,
imageUrl = riddle.imageUrl,
title = riddle.title,
hint = if (submission.hintUsed) riddle.hint else null,
Expand All @@ -108,8 +109,15 @@ open class RiddleBusinessLogicService(
skipPermitted = !submission.completed && !cannotSkip(riddle)
)

val riddles = riddleCacheManager.findAllRiddleByCategoryId(riddle.categoryId)
val nextId = riddles
.filter { filter -> submissions.none { it.completed && it.riddleId == filter.id } }
.minByOrNull { it.order }
?.id

if (nextId != riddle.id)
return null
return RiddleView(
id = riddle.id,
imageUrl = riddle.imageUrl,
title = riddle.title,
hint = null,
Expand All @@ -134,8 +142,8 @@ open class RiddleBusinessLogicService(
submission.hintUsed = true
riddleCacheManager.updateMapping(submission)
} else {
val nextRiddles = getNextRiddlesUser(user, riddle)
if (nextRiddles.find { it.id == riddle.id } == null)
val nextId = getNextIdUser(user, riddle)
if (nextId != riddle.id)
return null

val userEntity = userService.getById(user.internalId)
Expand All @@ -162,8 +170,8 @@ open class RiddleBusinessLogicService(
submission.hintUsed = true
riddleCacheManager.updateMapping(submission)
} else {
val nextRiddles = getNextRiddlesGroup(groupId, riddle)
if (nextRiddles.find { it.id == riddle.id } == null)
val nextId = getNextIdGroup(groupId, riddle)
if (nextId != riddle.id)
return null

riddleCacheManager.createNewMapping(
Expand All @@ -184,17 +192,17 @@ open class RiddleBusinessLogicService(
): RiddleSubmissionView? {
val banStatus = riddleModerationService.getUserAndGroupBanStatus(user.internalId, user.groupId?.toString())
if (banStatus == SubmissionModerationStatus.HARD_BAN) {
return RiddleSubmissionView(status = RiddleSubmissionStatus.SUBMITTER_BANNED)
return RiddleSubmissionView(status = RiddleSubmissionStatus.SUBMITTER_BANNED, null)
}
val riddle = riddleCacheManager.getRiddleById(riddleId) ?: return null
riddleCacheManager.findCategoryByCategoryIdAndVisibleTrueAndMinRoleAtMost(riddle.categoryId, user.role)
?: return null

if (skip && (cannotSkip(riddle) || !riddleComponent.skipEnabled.isValueTrue()))
return RiddleSubmissionView(status = RiddleSubmissionStatus.CANNOT_SKIP)
return RiddleSubmissionView(status = RiddleSubmissionStatus.CANNOT_SKIP, null)

if (banStatus == SubmissionModerationStatus.SHADOW_BAN) {
return RiddleSubmissionView(status = RiddleSubmissionStatus.WRONG)
return RiddleSubmissionView(status = RiddleSubmissionStatus.WRONG, null)
}
val submission = riddleCacheManager.findMappingByOwnerUserIdAndRiddleId(user.id, riddleId)
if (submission != null) {
Expand All @@ -204,7 +212,7 @@ open class RiddleBusinessLogicService(
submission.completedAt = clock.getTimeInSeconds()
riddleCacheManager.updateMapping(submission, lazyPersist = true)
}
return RiddleSubmissionView(status = RiddleSubmissionStatus.WRONG)
return RiddleSubmissionView(status = RiddleSubmissionStatus.WRONG, null)
}

if (!submission.completed) {
Expand All @@ -218,11 +226,11 @@ open class RiddleBusinessLogicService(
riddleCacheManager.updateRiddle(riddle)
}
}
return RiddleSubmissionView(status = RiddleSubmissionStatus.CORRECT, getNextRiddlesUser(user, riddle))
return RiddleSubmissionView(status = RiddleSubmissionStatus.CORRECT, getNextIdUser(user, riddle))

} else {
val nextRiddles = getNextRiddlesUser(user, riddle)
if (nextRiddles.find { it.id == riddle.id } == null)
val nextId = getNextIdUser(user, riddle)
if (nextId != riddle.id)
return null
val userEntity = userService.getById(user.internalId)
if (!skip && checkSolutionIsWrong(solution, riddle)) {
Expand All @@ -235,7 +243,7 @@ open class RiddleBusinessLogicService(
)
)
}
return RiddleSubmissionView(status = RiddleSubmissionStatus.WRONG)
return RiddleSubmissionView(status = RiddleSubmissionStatus.WRONG, null)
}

riddleCacheManager.createNewMapping(
Expand All @@ -247,7 +255,7 @@ open class RiddleBusinessLogicService(
riddle.firstSolver = user.userName
riddleCacheManager.updateRiddle(riddle)
}
return RiddleSubmissionView(status = RiddleSubmissionStatus.CORRECT, nextRiddles)
return RiddleSubmissionView(status = RiddleSubmissionStatus.CORRECT, getNextIdUser(user, riddle))
}
}

Expand All @@ -266,18 +274,18 @@ open class RiddleBusinessLogicService(

val banStatus = riddleModerationService.getUserAndGroupBanStatus(user.internalId, groupId.toString())
if (banStatus == SubmissionModerationStatus.HARD_BAN) {
return RiddleSubmissionView(status = RiddleSubmissionStatus.SUBMITTER_BANNED)
return RiddleSubmissionView(status = RiddleSubmissionStatus.SUBMITTER_BANNED, null)
}

val riddle = riddleCacheManager.getRiddleById(riddleId) ?: return null
riddleCacheManager.findCategoryByCategoryIdAndVisibleTrueAndMinRoleAtMost(riddle.categoryId, user.role)
?: return null

if (skip && (cannotSkip(riddle) || !riddleComponent.skipEnabled.isValueTrue()))
return RiddleSubmissionView(status = RiddleSubmissionStatus.CANNOT_SKIP)
return RiddleSubmissionView(status = RiddleSubmissionStatus.CANNOT_SKIP, null)

if (banStatus == SubmissionModerationStatus.SHADOW_BAN) {
return RiddleSubmissionView(status = RiddleSubmissionStatus.WRONG)
return RiddleSubmissionView(status = RiddleSubmissionStatus.WRONG, null)
}

val submission = riddleCacheManager.findMappingByOwnerGroupIdAndRiddleId(groupId, riddleId)
Expand All @@ -288,7 +296,7 @@ open class RiddleBusinessLogicService(
submission.completedAt = clock.getTimeInSeconds()
riddleCacheManager.updateMapping(submission, lazyPersist = true)
}
return RiddleSubmissionView(status = RiddleSubmissionStatus.WRONG)
return RiddleSubmissionView(status = RiddleSubmissionStatus.WRONG, null)
}

if (!submission.completed) {
Expand All @@ -302,11 +310,11 @@ open class RiddleBusinessLogicService(
riddleCacheManager.updateRiddle(riddle)
}
}
return RiddleSubmissionView(status = RiddleSubmissionStatus.CORRECT, getNextRiddlesGroup(groupId, riddle))
return RiddleSubmissionView(status = RiddleSubmissionStatus.CORRECT, getNextIdGroup(groupId, riddle))

} else {
val nextRiddles = getNextRiddlesGroup(groupId, riddle)
if (nextRiddles.find { it.id == riddle.id } == null)
val nextId = getNextIdGroup(groupId, riddle)
if (nextId != riddle.id)
return null
if (!skip && checkSolutionIsWrong(solution, riddle)) {
if (riddleComponent.saveFailedAttempts.isValueTrue()) {
Expand All @@ -318,7 +326,7 @@ open class RiddleBusinessLogicService(
)
)
}
return RiddleSubmissionView(status = RiddleSubmissionStatus.WRONG)
return RiddleSubmissionView(status = RiddleSubmissionStatus.WRONG, null)
}

riddleCacheManager.createNewMapping(
Expand All @@ -331,7 +339,7 @@ open class RiddleBusinessLogicService(
riddle.firstSolver = groupName
riddleCacheManager.updateRiddle(riddle)
}
return RiddleSubmissionView(status = RiddleSubmissionStatus.CORRECT, getNextRiddlesGroup(groupId, riddle))
return RiddleSubmissionView(status = RiddleSubmissionStatus.CORRECT, getNextIdGroup(groupId, riddle))
}
}

Expand Down Expand Up @@ -390,26 +398,25 @@ open class RiddleBusinessLogicService(
.replace("ü", "u")
.replace("ű", "u")

private fun getNextRiddlesUser(user: CmschUser, riddle: RiddleEntity): List<RiddleView> {
private fun getNextIdUser(user: CmschUser, riddle: RiddleEntity): Int? {
val submissions = riddleCacheManager.findAllMappingByOwnerUserIdAndRiddleCategoryId(user.id, riddle.categoryId)
return findNextTo(riddle.categoryId, submissions)
return findNextTo(riddle, submissions)
}

private fun getNextRiddlesGroup(groupId: Int, riddle: RiddleEntity): List<RiddleView> {
private fun getNextIdGroup(groupId: Int, riddle: RiddleEntity): Int? {
val submissions = riddleCacheManager.findAllMappingByGroupUserIdAndRiddleCategoryId(groupId, riddle.categoryId)
return findNextTo(riddle.categoryId, submissions)
return findNextTo(riddle, submissions)
}

private fun findNextTo(
categoryId: Int,
riddle: RiddleEntity,
submissions: List<RiddleMappingEntity>
): List<RiddleView> {
val riddles = riddleCacheManager.findAllRiddleByCategoryId(categoryId)
): Int? {
val riddles = riddleCacheManager.findAllRiddleByCategoryId(riddle.categoryId)
return riddles
.filter { filter -> submissions.none { it.riddleId == filter.id } }
.sortedBy { it.order }
.take(riddleComponent.visibleRiddlesPerCategory.getIntValue(1))
.map { mapRiddleView(submissions, it.id, it) }
.filter { filter -> submissions.none { it.completed && it.riddleId == filter.id } }
.minByOrNull { it.order }
?.id
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data class RiddleCategoryDto(
var title: String = "",

@field:JsonView(Preview::class)
var nextRiddles: List<RiddleView> = listOf(),
var nextRiddle: Int? = null,

@field:JsonView(Preview::class)
var completed: Int = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class RiddleComponent(
listOf(
riddleGroup,
title, menuDisplayName, minRole,
visibleRiddlesPerCategory,

shadowBanModerationGroup,
userShadowBanList,
Expand Down Expand Up @@ -82,11 +81,6 @@ class RiddleComponent(
fieldName = "Jogosultságok", description = "Melyik roleokkal nyitható meg az oldal"
)

val visibleRiddlesPerCategory = SettingProxy(componentSettingService, component,
"visibleRiddlesPerCategory", "3", type = SettingType.NUMBER, fieldName = "Egyidőben mutatott riddleök száma",
description = "Kategóriánként ennyi kerül megjelenítésre egyidőben, ennyi közül lehet választani"
)

/// -------------------------------------------------------------------------------------------------------------------

val shadowBanModerationGroup = SettingProxy(componentSettingService, component,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ data class RiddleSubmissionView(
var status: RiddleSubmissionStatus = RiddleSubmissionStatus.WRONG,

@field:JsonView(FullDetails::class)
var nextRiddles: List<RiddleView> = listOf(),
var nextId: Int? = null

)
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import hu.bme.sch.cmsch.dto.FullDetails

data class RiddleView(

@field:JsonView(FullDetails::class)
var id: Int,

@field:JsonView(FullDetails::class)
var imageUrl: String = "",

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/riddle/riddleList.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const RiddleCategoryList = () => {
<RiddleCategoryListItem
category={riddleCategory}
key={riddleCategory.categoryId}
onClick={() => onRiddleCategoryClick(riddleCategory.nextRiddles[0]?.id)}
onClick={() => onRiddleCategoryClick(riddleCategory.nextRiddle)}
/>
))
) : (
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/util/views/riddle.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface RiddleCategory {
title: string
completed: number
total: number
nextRiddles: Riddle[]
nextRiddle?: number
}

export enum RiddleSubmissionStatus {
Expand All @@ -36,7 +36,7 @@ export enum RiddleSubmissionStatus {

export interface RiddleSubmissionResult {
status: RiddleSubmissionStatus
nextRiddles: Riddle[]
nextId?: number
}

export interface Hint {
Expand Down

0 comments on commit c5033b5

Please sign in to comment.