Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom
import edu.ntnu.idi.idatt.backend.classroom.domain.ClassroomPupilStatus
import edu.ntnu.idi.idatt.backend.classroom.domain.ClassroomStatus
import edu.ntnu.idi.idatt.backend.classroom.infrastructure.ClassroomRepository
import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgress
import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgressId
import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository
import edu.ntnu.idi.idatt.backend.game.gameProgress.application.PupilGameProgressService
import edu.ntnu.idi.idatt.backend.iam.application.ClassroomUserResolver
import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver
import edu.ntnu.idi.idatt.backend.iam.infrastructure.UserRepository
Expand All @@ -25,8 +23,9 @@ import java.util.*
* @property classroomRepository repository for classroom access.
* @property currentUserResolver shared resolver for the authenticated user and role guard.
* @property userRepository repository for user access.
* @property classroomUserResolver repository for classroom access.
* @property pupilGameProgressRepository pupil repository for game progress.
* @property classroomUserResolver resolver for classroom access control.
* @property pupilGameProgressService service for classroom-scoped game progress entries.
* @property taskRepository repository used to count classroom tasks.
* @property domainEventPublisher publisher for application events.
*/
@Service
Expand All @@ -35,7 +34,7 @@ class ClassroomService(
private val currentUserResolver: CurrentUserResolver,
private val userRepository: UserRepository,
private val classroomUserResolver: ClassroomUserResolver,
private val pupilGameProgressRepository: PupilGameProgressRepository,
private val pupilGameProgressService: PupilGameProgressService,
private val taskRepository: TaskRepository,
private val domainEventPublisher: DomainEventPublisher,
) {
Expand Down Expand Up @@ -124,15 +123,9 @@ class ClassroomService(

classroom.addPupilMembership(user)
classroomRepository.save(classroom)
pupilGameProgressRepository.save(
PupilGameProgress(
id = PupilGameProgressId(
pupilUserId = userId,
classroomId = classroom.id,
),
pupil = user,
classroom = classroom,
),
pupilGameProgressService.createPupilGameProgressEntry(
pupilId = userId,
classroomId = requireNotNull(classroom.id) { "Classroom is missing an id" },
)
domainEventPublisher.publish(PupilJoinedClassroomEvent(pupil = user, classroom = classroom))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package edu.ntnu.idi.idatt.backend.game.gameProgress.application

import edu.ntnu.idi.idatt.backend.classroom.infrastructure.ClassroomRepository
import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgress
import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgressId
import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository
import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver
import edu.ntnu.idi.idatt.backend.iam.infrastructure.UserRepository
import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.server.ResponseStatusException
import java.time.Instant

/**
* Application service for classroom-scoped pupil game progress.
*
* The service centralizes reads and writes for the aggregated progress row tied
* to one pupil and one classroom. It is used by XP awarding, classroom join
* flows, and profile/notebook lookups that need progress-derived values.
*
* @property pupilGameProgressRepository repository for persisted pupil game-progress rows
* @property userRepository repository used to resolve pupils when creating new progress rows
* @property classroomRepository repository used to resolve classrooms when creating new progress rows
* @property currentUserResolver resolver used to derive the active pupil and classroom from the authenticated user
*/
@Service
class PupilGameProgressService(
private val pupilGameProgressRepository: PupilGameProgressRepository,
private val userRepository: UserRepository,
private val classroomRepository: ClassroomRepository,
private val currentUserResolver: CurrentUserResolver,
) {
companion object {
private const val XP_PER_LEVEL = 150
}

/**
* Updates the authenticated pupil's classroom-scoped XP total and derived level.
*
* @param currentUser authenticated pupil whose progress should be updated
* @param totalXp recalculated classroom-scoped total XP
*
* If no progress row exists for the active classroom, the update is skipped.
*/
@Transactional
fun updateUserXpAndLevel(currentUser: CurrentUser, totalXp: Int) {
val pupil = currentUserResolver.requirePupil(currentUser.subject)
val pupilId = requireNotNull(pupil.id) { "Pupil is missing an id" }
val classroom = currentUserResolver.getClassroomByUser(currentUser)
val classroomId = requireNotNull(classroom.id) { "Classroom is missing an id" }
val pupilGameProgress = pupilGameProgressRepository.findByPupilIdAndClassroomId(pupilId, classroomId)
?: return

val currentLevel = calculateLevelFromXp(totalXp)

pupilGameProgress.updateProgress(
totalScore = totalXp,
totalXp = totalXp,
currentLevel = currentLevel,
completedStopsCount = pupilGameProgress.completedStopsCount,
completedTasksCount = pupilGameProgress.completedTasksCount,
finalBossCompleted = pupilGameProgress.finalBossCompleted,
lastActivityAt = Instant.now(),
)

pupilGameProgressRepository.save(pupilGameProgress)
}

@Transactional(readOnly = true)
fun getUserCompletedStopsCount(currentUser: CurrentUser): Int {
val pupil = currentUserResolver.requirePupil(currentUser.subject)
val pupilId = requireNotNull(pupil.id) { "Authenticated pupil is missing an id" }

val classroom = currentUserResolver.getClassroomByUser(currentUser)
val classroomId = requireNotNull(classroom.id) { "Active classroom is missing an id" }

val gameProgress = pupilGameProgressRepository.findByPupilIdAndClassroomId(pupilId, classroomId)
val completedStopsCount = gameProgress?.completedStopsCount ?: 0

return completedStopsCount
}

/**
* Returns the persisted progress row for one pupil in one classroom.
*
* @param pupilId persisted pupil user id
* @param classroomId persisted classroom id
* @return classroom-scoped progress row, or `null` when none exists
*/
@Transactional(readOnly = true)
fun getPupilProgress(pupilId: Long, classroomId: Long): PupilGameProgress? =
pupilGameProgressRepository.findByPupilIdAndClassroomId(pupilId, classroomId)

/**
* Creates an empty classroom-scoped progress row for a pupil if it does not already exist.
*
* @param pupilId persisted pupil user id
* @param classroomId persisted classroom id
* @throws ResponseStatusException when the pupil or classroom does not exist
*/
@Transactional
fun createPupilGameProgressEntry(pupilId: Long, classroomId: Long) {
if (pupilGameProgressRepository.findByPupilIdAndClassroomId(pupilId, classroomId) != null) {
return
}

val pupil = userRepository.findById(pupilId).orElseThrow {
ResponseStatusException(HttpStatus.NOT_FOUND, "Pupil not found")
}
val classroom = classroomRepository.findById(classroomId).orElseThrow {
ResponseStatusException(HttpStatus.NOT_FOUND, "Classroom not found")
}

pupilGameProgressRepository.save(
PupilGameProgress(
id = PupilGameProgressId(
pupilUserId = pupilId,
classroomId = requireNotNull(classroom.id) { "Classroom is missing an id" },
),
pupil = pupil,
classroom = classroom,
),
)
}

/**
* Maps accumulated XP to a 1-based level.
*
* Each full [XP_PER_LEVEL] XP increases the pupil's level by one.
*
* @param totalXp classroom-scoped accumulated XP
* @return derived 1-based level.
*/
private fun calculateLevelFromXp(totalXp: Int): Int =
(totalXp / XP_PER_LEVEL) + 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package edu.ntnu.idi.idatt.backend.game.tasks.domain
* Coarse content categories for authored tasks.
*/
enum class TaskType {
EMAIL_REVIEW,
NEWS_COMPARISON,
IMAGE_ASSESSMENT,
IMAGE_SELECT,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package edu.ntnu.idi.idatt.backend.notebook.application

import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository
import edu.ntnu.idi.idatt.backend.game.gameProgress.application.PupilGameProgressService
import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository
import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver
import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser
Expand All @@ -20,13 +20,15 @@ import org.springframework.web.server.ResponseStatusException
* @property notebookEntryRepository repository used to load and persist notebook entries
* @property currentUserResolver resolver used to enforce that only pupil users access notebook entries
* @property mapStopsRepository repository used to validate referenced map stops during creation
* @property pupilGameProgressService service used to resolve notebook visibility from pupil progress
* @property xpService service used to award XP for notebook activity
*/
@Service
class NotebookService(
private val notebookEntryRepository: NotebookEntryRepository,
private val currentUserResolver: CurrentUserResolver,
private val mapStopsRepository: MapStopsRepository,
private val pupilGameProgressRepository: PupilGameProgressRepository,
private val pupilGameProgressService: PupilGameProgressService,
private val xpService: XpService,
) {
/**
Expand All @@ -43,8 +45,7 @@ class NotebookService(
val classroom = currentUserResolver.getClassroomByUser(currentUser)
val classroomId = requireNotNull(classroom.id) { "Active classroom is missing an id" }

val gameProgress = pupilGameProgressRepository.findByPupilIdAndClassroomId(pupilId, classroomId)
val completedStopsCount = gameProgress?.completedStopsCount ?: 0
val completedStopsCount = pupilGameProgressService.getUserCompletedStopsCount(currentUser)

return notebookEntryRepository.findAllVisibleToPupil(pupilId, classroomId, completedStopsCount)
}
Expand All @@ -58,7 +59,6 @@ class NotebookService(
@Transactional
fun createNotebookEntry(command: CreateNotebookEntryCommand): NotebookEntry {
val pupil = currentUserResolver.requirePupil(command.currentUser.subject)
val pupilId = requireNotNull(pupil.id) { "Authenticated pupil is missing an id" }
val classroom = currentUserResolver.getClassroomByUser(command.currentUser)

if (command.sourceType == NotebookSourceType.GAME) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.ntnu.idi.idatt.backend.users.profile.application

import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository
import edu.ntnu.idi.idatt.backend.game.gameProgress.application.PupilGameProgressService
import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgress
import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver
import edu.ntnu.idi.idatt.backend.iam.domain.UserRole
import edu.ntnu.idi.idatt.backend.users.profile.domain.PupilProfile
Expand All @@ -16,14 +17,14 @@ import org.springframework.web.server.ResponseStatusException
*
* @property currentUserResolver resolver to authenticate users
* @property pupilProfileRepository repository used to load pupil profile data
* @property pupilGameProgressRepository repository used to load classroom-scoped game progress
* @property pupilGameProgressService service used to load classroom-scoped game progress
* @property teacherProfileRepository repository used to load the teacher profile data
*/
@Service
class CurrentUserProfileService(
private val currentUserResolver: CurrentUserResolver,
private val pupilProfileRepository: PupilProfileRepository,
private val pupilGameProgressRepository: PupilGameProgressRepository,
private val pupilGameProgressService: PupilGameProgressService,
private val teacherProfileRepository: TeacherProfileRepository,
) {
/**
Expand Down Expand Up @@ -120,8 +121,15 @@ class CurrentUserProfileService(
ResponseStatusException(HttpStatus.NOT_FOUND, "Pupil profile was not found")
}

private fun getCurrentClassroomProgress(userId: Long, classroomId: Long?) =
classroomId?.let { pupilGameProgressRepository.findByPupilIdAndClassroomId(userId, it) }
/**
* Loads the classroom-scoped progress row for the supplied pupil if a current classroom is set.
*
* @param userId persisted pupil user id
* @param classroomId current classroom id from the pupil profile, or `null`
* @return classroom-scoped progress row, or `null` when no classroom or progress row exists
*/
private fun getCurrentClassroomProgress(userId: Long, classroomId: Long?): PupilGameProgress? =
classroomId?.let { pupilGameProgressService.getPupilProgress(userId, it) }
}

/**
Expand All @@ -145,6 +153,11 @@ data class CurrentUserProfile(

/**
* Internal pupil profile projection.
*
* @property displayName pupil-facing display name
* @property currentClassroomId active classroom id, or `null` when the pupil has none selected
* @property totalXp classroom-scoped accumulated XP for the active classroom
* @property currentLevel classroom-scoped level derived from the active classroom XP total
*/
data class PupilProfileDetails(
val displayName: String,
Expand All @@ -155,6 +168,9 @@ data class PupilProfileDetails(

/**
* Internal teacher profile projection reserved for future support.
*
* @property teacherName teacher full name shown in the product
* @property schoolName school name associated with the teacher
*/
data class TeacherProfileDetails(
val teacherName: String,
Expand Down
Loading