Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import edu.ntnu.idi.idatt.backend.game.mapstops.domain.MapStop
import edu.ntnu.idi.idatt.backend.game.mapstops.domain.StopProgressStatus
import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository
import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.PupilStopProgressRepository
import edu.ntnu.idi.idatt.backend.game.tasks.infrastructure.TaskRepository
import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
Expand All @@ -23,6 +24,7 @@ class MapStopService(
private val mapStopsRepository: MapStopsRepository,
private val pupilStopProgressRepository: PupilStopProgressRepository,
private val gameAccessContextResolver: GameAccessContextResolver,
private val taskRepository: TaskRepository,
) {
/**
* Returns all active map stops sorted by display order.
Expand Down Expand Up @@ -66,10 +68,12 @@ class MapStopService(
val progressByStopId = pupilStopProgressRepository
.findAllByIdPupilUserIdAndIdClassroomId(accessContext.pupilUserId, classroomId)
.associateBy { it.id.stopId }
val maxScoreByStopId = loadPublishedStopMaxScores(classroomId)

return stops.mapNotNull { stop ->
val stopId = stop.id ?: return@mapNotNull null
val progress = progressByStopId[stop.id!!]
val maxScore = maxScoreByStopId[stopId] ?: 0
MapStopOverviewEntry(
stopId = stopId,
slug = stop.slug,
Expand All @@ -78,7 +82,7 @@ class MapStopService(
displayOrder = stop.displayOrder ?: 0,
isFinalBoss = stop.isFinalBoss ?: false,
status = progress?.status ?: StopProgressStatus.LOCKED,
stars = progress?.let { computeStars(it.bestScore, stop.maxScore ?: 0) } ?: 0,
stars = progress?.let { computeStars(it.bestScore, maxScore) } ?: 0,
medalAwarded = progress?.medalAwarded ?: false,
)
}
Expand Down Expand Up @@ -106,6 +110,7 @@ class MapStopService(
}
val stopId = stop.id
?: throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Map stop has no id")
val maxScoreByStopId = loadPublishedStopMaxScores(classroomId)

val progress = pupilStopProgressRepository
.findByIdPupilUserIdAndIdClassroomIdAndIdStopId(accessContext.pupilUserId, classroomId, stopId)
Expand All @@ -118,11 +123,18 @@ class MapStopService(
displayOrder = stop.displayOrder ?: 0,
isFinalBoss = stop.isFinalBoss ?: false,
status = progress?.status ?: StopProgressStatus.LOCKED,
stars = progress?.let { computeStars(it.bestScore, stop.maxScore ?: 0) } ?: 0,
stars = progress?.let { computeStars(it.bestScore, maxScoreByStopId[stopId] ?: 0) } ?: 0,
medalAwarded = progress?.medalAwarded ?: false,
)
}

private fun loadPublishedStopMaxScores(classroomId: Long): Map<Long, Int> =
taskRepository.findAllByClassroomIdOrderByStopIdAscDifficultyLevelAsc(classroomId)
.asSequence()
.filter { it.isPublished }
.groupBy { it.stopId }
.mapValues { (_, tasks) -> tasks.sumOf { it.maxScore } }

private fun computeStars(bestScore: Int, maxScore: Int): Int {
if (maxScore == 0) return 0
val ratio = bestScore.toDouble() / maxScore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class PupilStopProgressService(
progress.status = next

if (next == StopProgressStatus.COMPLETED && progress.completedAt == null) {
progress.currentTaskId = null
progress.completedAt = Instant.now()
unlockNextStop(pupilUserId, classroomId, stopId, activeStops)
}
Expand Down Expand Up @@ -183,4 +184,4 @@ class PupilStopProgressService(
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package edu.ntnu.idi.idatt.backend.game.tasks.api

import edu.ntnu.idi.idatt.backend.game.tasks.application.LocalizedOptionalText
import edu.ntnu.idi.idatt.backend.game.tasks.application.LocalizedText
import edu.ntnu.idi.idatt.backend.game.tasks.application.StopSubmissionResult
import edu.ntnu.idi.idatt.backend.game.tasks.application.TaskDetails
import edu.ntnu.idi.idatt.backend.game.tasks.application.TaskSubmissionResult
import edu.ntnu.idi.idatt.backend.game.tasks.application.TaskSummary

/**
Expand Down Expand Up @@ -68,3 +70,24 @@ private fun LocalizedText.toResponse(): LocalizedTextResponse =

private fun LocalizedOptionalText.toResponse(): LocalizedOptionalTextResponse =
LocalizedOptionalTextResponse(nb = nb, en = en)

fun StopSubmissionResult.toResponse(): StopSubmissionResponse =
StopSubmissionResponse(
stopId = stopId,
passed = passed,
score = score,
maxScore = maxScore,
stopStatus = stopStatus,
completedAt = completedAt,
taskResults = taskResults.map { it.toResponse() },
)

private fun TaskSubmissionResult.toResponse(): TaskSubmissionResultResponse =
TaskSubmissionResultResponse(
taskId = taskId,
title = title.toResponse(),
passed = passed,
score = score,
maxScore = maxScore,
feedbackText = feedbackText?.toResponse(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ class TaskController(
.getTask(currentUser, classroomId, taskId)
.let { it.task.toResponse(includeAnswers = it.includeAnswers) }

@PostMapping("/submit-stop")
fun submitStopAnswers(
@PathVariable classroomId: Long,
@RequestBody @Valid request: SubmitStopAnswersRequest,
@AuthenticationPrincipal currentUser: CurrentUser,
): StopSubmissionResponse =
taskService.submitStopAnswers(request.toCommand(classroomId, currentUser.subject)).toResponse()

/**
* Creates a new draft task owned by the classroom's teacher.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ data class TaskItemRequest(
val options: List<TaskOptionRequest> = emptyList(),
)

data class SubmitStopAnswersRequest(
@field:Min(1)
val stopId: Long,
@field:Valid
val taskAnswers: List<SubmitTaskAnswerRequest> = emptyList(),
)

data class SubmitTaskAnswerRequest(
@field:Min(1)
val taskId: Long,
@field:Valid
val itemAnswers: List<SubmitItemAnswerRequest> = emptyList(),
)

data class SubmitItemAnswerRequest(
@field:Min(1)
val itemId: Long,
@field:Min(1)
val selectedOptionId: Long,
)

/**
* Request payload for creating a new task inside one classroom.
*/
Expand Down Expand Up @@ -185,3 +206,23 @@ private fun LocalizedRequiredTextRequest.toLocalizedText(): LocalizedText =

private fun LocalizedOptionalTextRequest.toLocalizedOptionalText(): LocalizedOptionalText =
LocalizedOptionalText(nb = nb, en = en)

fun SubmitStopAnswersRequest.toCommand(
classroomId: Long,
authenticatedSubject: String,
) = edu.ntnu.idi.idatt.backend.game.tasks.application.SubmitStopAnswersCommand(
authenticatedSubject = authenticatedSubject,
classroomId = classroomId,
stopId = stopId,
taskAnswers = taskAnswers.map { taskAnswer ->
edu.ntnu.idi.idatt.backend.game.tasks.application.SubmitTaskAnswerCommand(
taskId = taskAnswer.taskId,
itemAnswers = taskAnswer.itemAnswers.map { itemAnswer ->
edu.ntnu.idi.idatt.backend.game.tasks.application.SubmitItemAnswerCommand(
itemId = itemAnswer.itemId,
selectedOptionId = itemAnswer.selectedOptionId,
)
},
)
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ data class TaskOptionResponse(
/**
* Serialized prompt item returned from task endpoints.
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
data class TaskItemResponse(
val id: Long,
val itemOrder: Int,
Expand Down Expand Up @@ -80,3 +81,22 @@ data class TaskSummaryResponse(
val itemCount: Int,
val updatedAt: Instant?,
)

data class TaskSubmissionResultResponse(
val taskId: Long,
val title: LocalizedTextResponse,
val passed: Boolean,
val score: Int,
val maxScore: Int,
val feedbackText: LocalizedOptionalTextResponse?,
)

data class StopSubmissionResponse(
val stopId: Long,
val passed: Boolean,
val score: Int,
val maxScore: Int,
val stopStatus: String,
val completedAt: Instant?,
val taskResults: List<TaskSubmissionResultResponse>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,39 @@ data class TaskSummary(
val itemCount: Int,
val updatedAt: Instant?,
)

data class SubmitStopAnswersCommand(
val authenticatedSubject: String,
val classroomId: Long,
val stopId: Long,
val taskAnswers: List<SubmitTaskAnswerCommand>,
)

data class SubmitTaskAnswerCommand(
val taskId: Long,
val itemAnswers: List<SubmitItemAnswerCommand>,
)

data class SubmitItemAnswerCommand(
val itemId: Long,
val selectedOptionId: Long,
)

data class StopSubmissionResult(
val stopId: Long,
val passed: Boolean,
val score: Int,
val maxScore: Int,
val stopStatus: String,
val completedAt: Instant?,
val taskResults: List<TaskSubmissionResult>,
)

data class TaskSubmissionResult(
val taskId: Long,
val title: LocalizedText,
val passed: Boolean,
val score: Int,
val maxScore: Int,
val feedbackText: LocalizedOptionalText?,
)
Loading