File indexing completed on 2024-04-28 15:07:59

0001 /* GCompris - readingh.js
0002  *
0003  * SPDX-FileCopyrightText: 2015 Johnny Jazeix <jazeix@gmail.com>
0004  *
0005  * Authors:
0006  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version)
0007  *   Johnny Jazeix <jazeix@gmail.com> (Qt Quick port)
0008  *   Timothée Giet <animtim@gmail.com> (graphics and improvements)
0009  *
0010  *   SPDX-License-Identifier: GPL-3.0-or-later
0011  */
0012 .pragma library
0013 .import QtQuick 2.12 as Quick
0014 .import GCompris 1.0 as GCompris //for ApplicationInfo
0015 .import "qrc:/gcompris/src/core/core.js" as Core
0016 
0017 var items
0018 var maxLevel
0019 
0020 var url = "qrc:/gcompris/src/activities/readingh/resource/"
0021 var dataSetUrl= "qrc:/gcompris/src/activities/wordsgame/resource/"
0022 
0023 //
0024 var level
0025 // words to display
0026 var words
0027 
0028 function start(items_) {
0029     items = items_
0030     items.score.currentSubLevel = 0
0031     var locale = items.locale == "system" ? "$LOCALE" : items.locale
0032 
0033     items.wordlist.loadFromFile(GCompris.ApplicationInfo.getLocaleFilePath(
0034             dataSetUrl + "default-"+locale+".json"));
0035     // If wordlist is empty, we try to load from short locale and if not present again, we switch to default one
0036     var localeUnderscoreIndex = locale.indexOf('_')
0037     // probably exist a better way to see if the list is empty
0038     if(items.wordlist.maxLevel == 0) {
0039         var localeShort;
0040         // We will first look again for locale xx (without _XX if exist)
0041         if(localeUnderscoreIndex > 0) {
0042             localeShort = locale.substring(0, localeUnderscoreIndex)
0043         }
0044         else {
0045             localeShort = locale;
0046         }
0047         // If not found, we will use the default file
0048         items.wordlist.useDefault = true
0049         items.wordlist.loadFromFile(GCompris.ApplicationInfo.getLocaleFilePath(
0050         dataSetUrl + "default-"+localeShort+".json"));
0051         // We remove the using of default file for next time we enter this function
0052         items.wordlist.useDefault = false
0053     }
0054     maxLevel = items.wordlist.maxLevel;
0055     items.currentLevel = Core.getInitialLevel(maxLevel);
0056     initLevel();
0057 }
0058 
0059 function stop() {
0060     items.wordDropTimer.stop();
0061 }
0062 
0063 function initLevel() {
0064     items.score.currentSubLevel = 0
0065     items.wordDropTimer.stop();
0066     items.answerButtonsFlow.visible = false;
0067 
0068     // initialize level
0069     level = items.wordlist.getLevelWordList(items.currentLevel + 1);
0070     items.wordlist.initRandomWord(items.currentLevel + 1)
0071     items.score.numberOfSubLevels = Math.min(10, Math.floor(level.words.length))
0072     initSubLevel();
0073 }
0074 
0075 function initSubLevel() {
0076     items.answerButtonsFlow.visible = false;
0077     items.textToFind = items.wordlist.getRandomWord()
0078     Core.shuffle(level.words)
0079     words = level.words.slice(0, 15)
0080     // add 1/2 probablity for yes/no answer
0081     var probability = Math.random()
0082     if(probability > 0.5) {     // answer should be yes
0083         if(words.indexOf(items.textToFind) == -1) {
0084             words.pop()
0085             words.push(items.textToFind)
0086         }
0087     } else if(words.indexOf(items.textToFind) > -1) {    // answer should be no
0088         var wordIndex = words.indexOf(items.textToFind)  // if word is included, remove it
0089         words.splice(wordIndex, 1)
0090         for(var i=level.words.length - 1; i == 0; i--) { // try to get a word from the whole list different from textToFind and not yet in words
0091             if(level.words[i] != items.textToFind && words.indexOf(level.words[i]) == -1) {
0092                 var wordToAdd = level.words[i]
0093                 words.push(wordToAdd)
0094                 break
0095             }
0096         }
0097     }
0098     Core.shuffle(words)
0099 
0100     items.currentIndex = -1
0101 
0102     items.wordDisplayRepeater.model = words
0103     items.wordDisplayRepeater.idToHideBecauseOverflow = 0
0104     items.answerButtonFound.isCorrectAnswer = words.indexOf(items.textToFind) != -1
0105     items.iAmReady.visible = true
0106     items.buttonsBlocked = false
0107 }
0108 
0109 function retrySubLevel() {
0110     items.wordlist.appendRandomWord(items.textToFind);
0111     initSubLevel();
0112 }
0113 
0114 function nextSubLevel() {
0115     if(items.score.currentSubLevel >= items.score.numberOfSubLevels) {
0116         items.bonus.good("flower")
0117     } else {
0118         initSubLevel();
0119     }
0120 }
0121 
0122 function nextLevel() {
0123     items.currentLevel = Core.getNextLevel(items.currentLevel, maxLevel);
0124     initLevel();
0125 }
0126 
0127 function previousLevel() {
0128     items.currentLevel = Core.getPreviousLevel(items.currentLevel, maxLevel);
0129     initLevel();
0130 }
0131 
0132 function run() {
0133     items.wordDropTimer.start();
0134 }
0135 
0136 function dropWord() {
0137     if(++items.currentIndex < words.length) {
0138         // Display next word
0139     }
0140     else {
0141         items.wordDropTimer.stop();
0142         items.answerButtonsFlow.visible = true
0143     }
0144 }