diff --git a/common/src/commonMain/kotlin/repositories/DbCardRepository.kt b/common/src/commonMain/kotlin/repositories/DbCardRepository.kt index 0bd88be1..552ec856 100644 --- a/common/src/commonMain/kotlin/repositories/DbCardRepository.kt +++ b/common/src/commonMain/kotlin/repositories/DbCardRepository.kt @@ -5,7 +5,6 @@ import com.gitlab.sszuev.flashcards.model.common.AppUserId import com.gitlab.sszuev.flashcards.model.domain.CardEntity import com.gitlab.sszuev.flashcards.model.domain.CardFilter import com.gitlab.sszuev.flashcards.model.domain.CardId -import com.gitlab.sszuev.flashcards.model.domain.CardLearn import com.gitlab.sszuev.flashcards.model.domain.DictionaryEntity import com.gitlab.sszuev.flashcards.model.domain.DictionaryId @@ -44,11 +43,6 @@ interface DbCardRepository { */ fun updateCards(userId: AppUserId, cardIds: Iterable, update: (CardEntity) -> CardEntity): CardsDbResponse - /** - * Updates cards details. - */ - fun learnCards(userId: AppUserId, cardLearns: List): CardsDbResponse - /** * Resets status. */ diff --git a/common/src/commonMain/kotlin/repositories/NoOpDbCardRepository.kt b/common/src/commonMain/kotlin/repositories/NoOpDbCardRepository.kt index b3852194..3645c3ef 100644 --- a/common/src/commonMain/kotlin/repositories/NoOpDbCardRepository.kt +++ b/common/src/commonMain/kotlin/repositories/NoOpDbCardRepository.kt @@ -4,7 +4,6 @@ import com.gitlab.sszuev.flashcards.model.common.AppUserId import com.gitlab.sszuev.flashcards.model.domain.CardEntity import com.gitlab.sszuev.flashcards.model.domain.CardFilter import com.gitlab.sszuev.flashcards.model.domain.CardId -import com.gitlab.sszuev.flashcards.model.domain.CardLearn import com.gitlab.sszuev.flashcards.model.domain.DictionaryId object NoOpDbCardRepository : DbCardRepository { @@ -36,10 +35,6 @@ object NoOpDbCardRepository : DbCardRepository { noOp() } - override fun learnCards(userId: AppUserId, cardLearns: List): CardsDbResponse { - noOp() - } - override fun resetCard(userId: AppUserId, cardId: CardId): CardDbResponse { noOp() } diff --git a/core/src/main/kotlin/processes/CardProcessWorkers.kt b/core/src/main/kotlin/processes/CardProcessWorkers.kt index 0bdef07b..a21c8140 100644 --- a/core/src/main/kotlin/processes/CardProcessWorkers.kt +++ b/core/src/main/kotlin/processes/CardProcessWorkers.kt @@ -107,10 +107,7 @@ fun ChainDSL.processLearnCards() = worker { this.status == AppStatus.RUN } process { - val userId = this.contextUserEntity.id - val res = - this.repositories.cardRepository(this.workMode).learnCards(userId, this.normalizedRequestCardLearnList) - this.postProcess(res) + this.postProcess(this.learnCards()) } onException { this.handleThrowable(CardOperation.LEARN_CARDS, it) diff --git a/core/src/main/kotlin/processes/UpdateCardsHelper.kt b/core/src/main/kotlin/processes/UpdateCardsHelper.kt new file mode 100644 index 00000000..0c721c0c --- /dev/null +++ b/core/src/main/kotlin/processes/UpdateCardsHelper.kt @@ -0,0 +1,22 @@ +package com.gitlab.sszuev.flashcards.core.processes + +import com.gitlab.sszuev.flashcards.CardContext +import com.gitlab.sszuev.flashcards.repositories.CardsDbResponse + +fun CardContext.learnCards(): CardsDbResponse { + val cards = this.normalizedRequestCardLearnList.associateBy { it.cardId } + return this.repositories.cardRepository(this.workMode).updateCards(this.contextUserEntity.id, cards.keys) { card -> + val learn = checkNotNull(cards[card.cardId]) + var answered = card.answered?.toLong() ?: 0L + val details = card.stats.toMutableMap() + learn.details.forEach { + answered += it.value.toInt() + require(answered < Int.MAX_VALUE && answered > Int.MIN_VALUE) + if (answered < 0) { + answered = 0 + } + details.merge(it.key, it.value) { a, b -> a + b } + } + card.copy(stats = details, answered = answered.toInt()) + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/validators/CardValidateWorkers.kt b/core/src/main/kotlin/validators/CardValidateWorkers.kt index db312a59..c1755682 100644 --- a/core/src/main/kotlin/validators/CardValidateWorkers.kt +++ b/core/src/main/kotlin/validators/CardValidateWorkers.kt @@ -91,8 +91,8 @@ fun ChainDSL.validateCardLearnListDetails(getCardLearn: (CardContex } } ) { (_, score) -> - // right not just check score is positive and not big - score <= 0 || score > 42 + // right not just check score is not big + score < -42 || score > 42 } private fun ChainDSL.validateIds( diff --git a/core/src/test/kotlin/CardCorProcessorRunCardsTest.kt b/core/src/test/kotlin/CardCorProcessorRunCardsTest.kt index 78c6231f..49b6a870 100644 --- a/core/src/test/kotlin/CardCorProcessorRunCardsTest.kt +++ b/core/src/test/kotlin/CardCorProcessorRunCardsTest.kt @@ -33,7 +33,6 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.EnumSource -@Suppress("OPT_IN_USAGE") internal class CardCorProcessorRunCardsTest { companion object { private val testUser = AppUserEntity(AppUserId("42"), AppAuthId("00000000-0000-0000-0000-000000000000")) @@ -225,10 +224,6 @@ internal class CardCorProcessorRunCardsTest { val context = testContext(CardOperation.CREATE_CARD, repository) context.requestCardEntity = testRequestEntity -context.errors.forEach { // TODO - println(it) -} - CardCorProcessor().execute(context) Assertions.assertTrue(wasCalled) @@ -358,10 +353,10 @@ context.errors.forEach { // TODO var wasCalled = false val repository = MockDbCardRepository( - invokeLearnCards = { _, it -> + invokeUpdateCards = { _, givenIds, _ -> wasCalled = true CardsDbResponse( - cards = if (it == testLearn) testResponseEntities else emptyList(), + cards = if (givenIds == setOf(stubCard.cardId)) testResponseEntities else emptyList(), ) } ) @@ -384,6 +379,7 @@ context.errors.forEach { // TODO CardLearn(cardId = CardId("1"), details = mapOf(Stage.SELF_TEST to 42)), CardLearn(cardId = CardId("2"), details = mapOf(Stage.OPTIONS to 2, Stage.MOSAIC to 3)) ) + val ids = testLearn.map { it.cardId }.toSet() val testResponseEntities = stubCards val testResponseErrors = listOf( @@ -392,11 +388,11 @@ context.errors.forEach { // TODO var wasCalled = false val repository = MockDbCardRepository( - invokeLearnCards = { _, it -> + invokeUpdateCards = { _, givenIds, _ -> wasCalled = true CardsDbResponse( - cards = if (it == testLearn) testResponseEntities else emptyList(), - errors = if (it == testLearn) testResponseErrors else emptyList() + cards = if (givenIds == ids) testResponseEntities else emptyList(), + errors = if (givenIds == ids) testResponseErrors else emptyList() ) } ) diff --git a/core/src/test/kotlin/CardCorProcessorValidationTest.kt b/core/src/test/kotlin/CardCorProcessorValidationTest.kt index 82009ccc..f36de508 100644 --- a/core/src/test/kotlin/CardCorProcessorValidationTest.kt +++ b/core/src/test/kotlin/CardCorProcessorValidationTest.kt @@ -5,7 +5,16 @@ import com.gitlab.sszuev.flashcards.model.common.AppAuthId import com.gitlab.sszuev.flashcards.model.common.AppError import com.gitlab.sszuev.flashcards.model.common.AppMode import com.gitlab.sszuev.flashcards.model.common.AppRequestId -import com.gitlab.sszuev.flashcards.model.domain.* +import com.gitlab.sszuev.flashcards.model.domain.CardEntity +import com.gitlab.sszuev.flashcards.model.domain.CardFilter +import com.gitlab.sszuev.flashcards.model.domain.CardId +import com.gitlab.sszuev.flashcards.model.domain.CardLearn +import com.gitlab.sszuev.flashcards.model.domain.CardOperation +import com.gitlab.sszuev.flashcards.model.domain.CardWordEntity +import com.gitlab.sszuev.flashcards.model.domain.DictionaryId +import com.gitlab.sszuev.flashcards.model.domain.LangId +import com.gitlab.sszuev.flashcards.model.domain.Stage +import com.gitlab.sszuev.flashcards.model.domain.TTSResourceGet import com.gitlab.sszuev.flashcards.stubs.stubCard import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest @@ -15,7 +24,7 @@ import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.Arguments import org.junit.jupiter.params.provider.EnumSource import org.junit.jupiter.params.provider.MethodSource -import java.util.* +import java.util.UUID @OptIn(ExperimentalCoroutinesApi::class) internal class CardCorProcessorValidationTest { @@ -314,7 +323,7 @@ internal class CardCorProcessorValidationTest { val context = testContext(CardOperation.LEARN_CARDS) context.requestCardLearnList = listOf( testCardLearn.copy(cardId = CardId("42"), details = mapOf(Stage.OPTIONS to 4200, Stage.WRITING to 42)), - testCardLearn.copy(cardId = CardId("21"), details = mapOf(Stage.MOSAIC to -12, Stage.SELF_TEST to 0)), + testCardLearn.copy(cardId = CardId("21"), details = mapOf(Stage.MOSAIC to -4200, Stage.SELF_TEST to -4200)), ) processor.execute(context) Assertions.assertEquals(3, context.errors.size) diff --git a/core/src/test/kotlin/SearchCardsHelperTest.kt b/core/src/test/kotlin/SearchCardsHelperTest.kt index 89ab5d53..f45ab0cf 100644 --- a/core/src/test/kotlin/SearchCardsHelperTest.kt +++ b/core/src/test/kotlin/SearchCardsHelperTest.kt @@ -35,4 +35,19 @@ internal class SearchCardsHelperTest { val card2 = CardEntity(words = listOf(CardWordEntity("word", translations = listOf(listOf("b", "slo"))))) Assertions.assertTrue(card1.isSimilar(card2)) } + + @Test + fun `test isSimilar #5`() { + val card1 = CardEntity( + words = listOf( + CardWordEntity("moist", translations = listOf(listOf("сырой"), listOf("влажный"), listOf("мокрый"))) + ) + ) + val card2 = CardEntity( + words = listOf( + CardWordEntity("wet", translations = listOf(listOf("влажный"), listOf("сырой"))) + ) + ) + Assertions.assertTrue(card1.isSimilar(card2)) + } } \ No newline at end of file diff --git a/db-common/src/testFixtures/kotlin/DbCardRepositoryTest.kt b/db-common/src/testFixtures/kotlin/DbCardRepositoryTest.kt index 6e226030..00a1735d 100644 --- a/db-common/src/testFixtures/kotlin/DbCardRepositoryTest.kt +++ b/db-common/src/testFixtures/kotlin/DbCardRepositoryTest.kt @@ -7,7 +7,6 @@ import com.gitlab.sszuev.flashcards.model.common.NONE import com.gitlab.sszuev.flashcards.model.domain.CardEntity import com.gitlab.sszuev.flashcards.model.domain.CardFilter import com.gitlab.sszuev.flashcards.model.domain.CardId -import com.gitlab.sszuev.flashcards.model.domain.CardLearn import com.gitlab.sszuev.flashcards.model.domain.CardWordEntity import com.gitlab.sszuev.flashcards.model.domain.CardWordExampleEntity import com.gitlab.sszuev.flashcards.model.domain.DictionaryId @@ -128,25 +127,6 @@ abstract class DbCardRepositoryTest { ), ) - private val rainCardEntity = CardEntity( - cardId = CardId("248"), - dictionaryId = DictionaryId("2"), - words = listOf( - CardWordEntity( - word = "rain", - transcription = "rein", - partOfSpeech = "noun", - translations = listOf(listOf("дождь")), - examples = listOf( - CardWordExampleEntity(text = "It rains.", translation = "Идет дождь."), - CardWordExampleEntity(text = "heavy rain", translation = "проливной дождь, ливень"), - CardWordExampleEntity(text = "drizzling rain", translation = "изморось"), - CardWordExampleEntity(text = "torrential rain", translation = "проливной дождь"), - ), - ), - ), - ) - private val newMurkyCardEntity = CardEntity( dictionaryId = DictionaryId("2"), words = listOf( @@ -375,21 +355,6 @@ abstract class DbCardRepositoryTest { ) } - @Order(9) - @Test - fun `test learn cards success`() { - val request = CardLearn( - cardId = rainCardEntity.cardId, - details = mapOf(Stage.SELF_TEST to 3, Stage.WRITING to 4), - ) - val res = repository.learnCards(userId, listOf(request)) - Assertions.assertEquals(0, res.errors.size) { "Has errors: ${res.errors}" } - Assertions.assertEquals(1, res.cards.size) - val card = res.cards[0] - val expectedCard = rainCardEntity.copy(stats = request.details) - assertCard(expected = expectedCard, actual = card, ignoreChangeAt = true, ignoreId = false) - } - @Order(10) @Test fun `test get card & reset card success`() { diff --git a/db-common/src/testFixtures/kotlin/mocks/MockDbCardRepository.kt b/db-common/src/testFixtures/kotlin/mocks/MockDbCardRepository.kt index f94138b6..33938374 100644 --- a/db-common/src/testFixtures/kotlin/mocks/MockDbCardRepository.kt +++ b/db-common/src/testFixtures/kotlin/mocks/MockDbCardRepository.kt @@ -4,7 +4,6 @@ import com.gitlab.sszuev.flashcards.model.common.AppUserId import com.gitlab.sszuev.flashcards.model.domain.CardEntity import com.gitlab.sszuev.flashcards.model.domain.CardFilter import com.gitlab.sszuev.flashcards.model.domain.CardId -import com.gitlab.sszuev.flashcards.model.domain.CardLearn import com.gitlab.sszuev.flashcards.model.domain.DictionaryId import com.gitlab.sszuev.flashcards.repositories.CardDbResponse import com.gitlab.sszuev.flashcards.repositories.CardsDbResponse @@ -22,7 +21,6 @@ class MockDbCardRepository( private val invokeCreateCard: (AppUserId, CardEntity) -> CardDbResponse = { _, _ -> CardDbResponse.EMPTY }, private val invokeUpdateCard: (AppUserId, CardEntity) -> CardDbResponse = { _, _ -> CardDbResponse.EMPTY }, private val invokeUpdateCards: (AppUserId, Iterable, (CardEntity) -> CardEntity) -> CardsDbResponse = { _, _, _ -> CardsDbResponse.EMPTY }, - private val invokeLearnCards: (AppUserId, List) -> CardsDbResponse = { _, _ -> CardsDbResponse.EMPTY }, private val invokeResetCard: (AppUserId, CardId) -> CardDbResponse = { _, _ -> CardDbResponse.EMPTY }, private val invokeDeleteCard: (AppUserId, CardId) -> RemoveCardDbResponse = { _, _ -> RemoveCardDbResponse.EMPTY }, ) : DbCardRepository { @@ -55,10 +53,6 @@ class MockDbCardRepository( return invokeUpdateCards(userId, cardIds, update) } - override fun learnCards(userId: AppUserId, cardLearns: List): CardsDbResponse { - return invokeLearnCards(userId, cardLearns) - } - override fun resetCard(userId: AppUserId, cardId: CardId): CardDbResponse { return invokeResetCard(userId, cardId) } diff --git a/db-mem/src/main/kotlin/MemDbCardRepository.kt b/db-mem/src/main/kotlin/MemDbCardRepository.kt index 1e500265..3dccc0bd 100644 --- a/db-mem/src/main/kotlin/MemDbCardRepository.kt +++ b/db-mem/src/main/kotlin/MemDbCardRepository.kt @@ -11,8 +11,6 @@ import com.gitlab.sszuev.flashcards.common.status import com.gitlab.sszuev.flashcards.common.systemNow import com.gitlab.sszuev.flashcards.common.validateCardEntityForCreate import com.gitlab.sszuev.flashcards.common.validateCardEntityForUpdate -import com.gitlab.sszuev.flashcards.common.validateCardLearns -import com.gitlab.sszuev.flashcards.dbmem.dao.MemDbCard import com.gitlab.sszuev.flashcards.dbmem.dao.MemDbDictionary import com.gitlab.sszuev.flashcards.model.Id import com.gitlab.sszuev.flashcards.model.common.AppError @@ -20,13 +18,11 @@ import com.gitlab.sszuev.flashcards.model.common.AppUserId import com.gitlab.sszuev.flashcards.model.domain.CardEntity import com.gitlab.sszuev.flashcards.model.domain.CardFilter import com.gitlab.sszuev.flashcards.model.domain.CardId -import com.gitlab.sszuev.flashcards.model.domain.CardLearn import com.gitlab.sszuev.flashcards.model.domain.DictionaryId import com.gitlab.sszuev.flashcards.repositories.CardDbResponse import com.gitlab.sszuev.flashcards.repositories.CardsDbResponse import com.gitlab.sszuev.flashcards.repositories.DbCardRepository import com.gitlab.sszuev.flashcards.repositories.RemoveCardDbResponse -import java.time.LocalDateTime import kotlin.random.Random class MemDbCardRepository( @@ -160,17 +156,6 @@ class MemDbCardRepository( ) } - override fun learnCards(userId: AppUserId, cardLearns: List): CardsDbResponse { - validateCardLearns(cardLearns) - val timestamp = systemNow() - val errors = mutableListOf() - val cards = cardLearns.mapNotNull { learnCard(it, userId, errors, timestamp) } - if (errors.isNotEmpty()) { - return CardsDbResponse(errors = errors) - } - return CardsDbResponse(cards = cards.map { it.toCardEntity() }, errors = errors) - } - override fun resetCard(userId: AppUserId, cardId: CardId): CardDbResponse { val timestamp = systemNow() val card = @@ -199,24 +184,6 @@ class MemDbCardRepository( return RemoveCardDbResponse(card = card.copy(changedAt = timestamp).toCardEntity()) } - private fun learnCard( - learn: CardLearn, - userId: AppUserId, - errors: MutableList, - timestamp: LocalDateTime, - ): MemDbCard? { - val cardId = learn.cardId - val card = database.findCardById(cardId.asLong()) - if (card == null) { - errors.add(noCardFoundDbError("learnCard", cardId)) - return null - } - if (checkDictionaryUser("learnCard", userId, card.dictionaryId.asDictionaryId(), cardId, errors) == null) { - return null - } - return database.saveCard(card.copy(details = learn.details.toMemDbCardDetails(), changedAt = timestamp)) - } - @Suppress("DuplicatedCode") private fun checkDictionaryUser( operation: String, diff --git a/db-mem/src/main/kotlin/MemDbEntityMapper.kt b/db-mem/src/main/kotlin/MemDbEntityMapper.kt index b0009889..1a483f70 100644 --- a/db-mem/src/main/kotlin/MemDbEntityMapper.kt +++ b/db-mem/src/main/kotlin/MemDbEntityMapper.kt @@ -20,7 +20,6 @@ import com.gitlab.sszuev.flashcards.common.parseUserDetailsJson import com.gitlab.sszuev.flashcards.common.toCardEntityDetails import com.gitlab.sszuev.flashcards.common.toCardEntityStats import com.gitlab.sszuev.flashcards.common.toCardWordEntity -import com.gitlab.sszuev.flashcards.common.toCommonCardDtoDetails import com.gitlab.sszuev.flashcards.common.toCommonWordDtoList import com.gitlab.sszuev.flashcards.common.toDocumentExamples import com.gitlab.sszuev.flashcards.common.toDocumentTranslations @@ -41,7 +40,6 @@ import com.gitlab.sszuev.flashcards.model.domain.DictionaryEntity import com.gitlab.sszuev.flashcards.model.domain.DictionaryId import com.gitlab.sszuev.flashcards.model.domain.LangEntity import com.gitlab.sszuev.flashcards.model.domain.LangId -import com.gitlab.sszuev.flashcards.model.domain.Stage import java.util.UUID internal fun MemDbUser.detailsAsJsonString(): String { @@ -65,7 +63,7 @@ internal fun MemDbCard.detailsAsJsonString(): String { } internal fun fromJsonStringToMemDbCardDetails(json: String): Map { - return parseCardDetailsJson(json).mapValues { it.toString() } + return parseCardDetailsJson(json).mapValues { it.value.toString() } } internal fun MemDbCard.wordsAsJsonString(): String { @@ -216,8 +214,6 @@ internal fun MemDbCard.detailsAsCommonCardDetailsDto(): CommonCardDetailsDto = C private fun CommonCardDetailsDto.toMemDbCardDetails(): Map = this.mapValues { it.value.toString() } -internal fun Map.toMemDbCardDetails(): Map = toCommonCardDtoDetails().toMemDbCardDetails() - private fun Long.asUserId(): AppUserId = AppUserId(toString()) private fun String.asLangId(): LangId = LangId(this) diff --git a/db-pg/src/main/kotlin/PgDbCardRepository.kt b/db-pg/src/main/kotlin/PgDbCardRepository.kt index 38df2b12..d84ca34a 100644 --- a/db-pg/src/main/kotlin/PgDbCardRepository.kt +++ b/db-pg/src/main/kotlin/PgDbCardRepository.kt @@ -9,7 +9,6 @@ import com.gitlab.sszuev.flashcards.common.noDictionaryFoundDbError import com.gitlab.sszuev.flashcards.common.systemNow import com.gitlab.sszuev.flashcards.common.validateCardEntityForCreate import com.gitlab.sszuev.flashcards.common.validateCardEntityForUpdate -import com.gitlab.sszuev.flashcards.common.validateCardLearns import com.gitlab.sszuev.flashcards.dbpg.dao.Cards import com.gitlab.sszuev.flashcards.dbpg.dao.Dictionaries import com.gitlab.sszuev.flashcards.dbpg.dao.PgDbCard @@ -20,7 +19,6 @@ import com.gitlab.sszuev.flashcards.model.common.AppUserId import com.gitlab.sszuev.flashcards.model.domain.CardEntity import com.gitlab.sszuev.flashcards.model.domain.CardFilter import com.gitlab.sszuev.flashcards.model.domain.CardId -import com.gitlab.sszuev.flashcards.model.domain.CardLearn import com.gitlab.sszuev.flashcards.model.domain.DictionaryId import com.gitlab.sszuev.flashcards.repositories.CardDbResponse import com.gitlab.sszuev.flashcards.repositories.CardsDbResponse @@ -191,36 +189,6 @@ class PgDbCardRepository( } } - override fun learnCards(userId: AppUserId, cardLearns: List): CardsDbResponse { - validateCardLearns(cardLearns) - return connection.execute { - val learns = cardLearns.associateBy { it.cardId.asLong() } - val found = PgDbCard.find { Cards.id inList learns.keys }.associateBy { it.id.value } - val errors = mutableListOf() - cardLearns.filterNot { it.cardId.asLong() in found.keys }.forEach { - errors.add(noCardFoundDbError(operation = "learnCards", id = it.cardId)) - } - val dictionaries = mutableMapOf() - found.forEach { - val dictionary = dictionaries.computeIfAbsent(it.value.dictionaryId.value) { k -> - checkNotNull(PgDbDictionary.findById(k)) - } - if (dictionary.userId.value != userId.asLong()) { - errors.add(forbiddenEntityDbError("learnCards", it.key.asCardId(), userId)) - } - } - if (errors.isNotEmpty()) { - return@execute CardsDbResponse(errors = errors) - } - val cards = learns.values.map { learn -> - val record = checkNotNull(found[learn.cardId.asLong()]) - record.details = learn.details.toPgDbCardDetailsJson() - record.toCardEntity() - } - CardsDbResponse(cards = cards, errors = errors) - } - } - override fun resetCard(userId: AppUserId, cardId: CardId): CardDbResponse { return connection.execute { val timestamp = systemNow() diff --git a/frontend/src/main/resources/static/data.js b/frontend/src/main/resources/static/data.js index 972a4325..55eb2ba5 100644 --- a/frontend/src/main/resources/static/data.js +++ b/frontend/src/main/resources/static/data.js @@ -56,14 +56,14 @@ function findById(cards, cardId) { } function rememberAnswer(card, stage, booleanAnswer) { - if (card.currentDetails == null) { - card.currentDetails = {} + if (card.stageStats == null) { + card.stageStats = {} } - card.currentDetails[stage] = booleanAnswer ? 1 : -1 + card.stageStats[stage] = booleanAnswer ? 1 : -1 } function hasStage(card, stage) { - return card.currentDetails != null && card.currentDetails[stage] != null + return card.stageStats != null && card.stageStats[stage] != null } /** @@ -73,7 +73,7 @@ function hasStage(card, stage) { * @returns {boolean|undefined} */ function isAnsweredRight(card) { - const details = card.currentDetails + const details = card.sessionStats if (details == null || !Object.keys(details).length) { return undefined } @@ -94,7 +94,7 @@ function isAnsweredRight(card) { * @returns {number} */ function sumAnswers(card) { - const details = card.currentDetails + const details = card.stageStats if (details == null || !Object.keys(details).length) { return 0 } diff --git a/frontend/src/main/resources/static/tutor.js b/frontend/src/main/resources/static/tutor.js index 889afca7..206e60c5 100644 --- a/frontend/src/main/resources/static/tutor.js +++ b/frontend/src/main/resources/static/tutor.js @@ -387,7 +387,7 @@ function drawResultCardPage() { $('#result-learned').html(learned); // remove state details flashcards.forEach(function (item) { - delete item.currentDetails + delete item.stageStats }); } @@ -448,15 +448,19 @@ function updateCardAndCallNext(cards, nextStageCallback) { if (card.answered === undefined) { card.answered = 0 } + if (card.sessionStats === undefined) { + card.sessionStats = {} + } card.answered += sumAnswers(card) if (card.answered < 0) { card.answered = 0 } const learn = {} learn['cardId'] = card.cardId - learn['details'] = card.currentDetails + learn['details'] = card.stageStats res.push(learn) - card.currentDetails = {} + card.sessionStats = {...card.sessionStats, ...card.stageStats} + card.stageStats = {} }) learnCard(res, () => nextStageCallback()) } \ No newline at end of file