Changeset View
Standalone View
src/activities/calendar/calendar.js
Show All 34 Lines | |||||
35 | var daySelected = 1 | 35 | var daySelected = 1 | ||
36 | var monthSelected = 2 | 36 | var monthSelected = 2 | ||
37 | var yearSelected = 2018 | 37 | var yearSelected = 2018 | ||
38 | var dayOfWeekSelected | 38 | var dayOfWeekSelected | ||
39 | var minRange //sum of min. visible month and year on calendar for navigation bar next/prev button visibility. | 39 | var minRange //sum of min. visible month and year on calendar for navigation bar next/prev button visibility. | ||
40 | var maxRange //sum of max. visible month and year on calendar for navigation bar next/prev button visibility. | 40 | var maxRange //sum of max. visible month and year on calendar for navigation bar next/prev button visibility. | ||
41 | var correctAnswer | 41 | var correctAnswer | ||
42 | var mode | 42 | var mode | ||
43 | var daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | ||||
43 | 44 | | |||
44 | function start(items_, dataset_) { | 45 | function start(items_) { | ||
45 | items = items_ | 46 | items = items_ | ||
46 | dataset = dataset_.get() | 47 | numberOfLevel = items.levels.length | ||
47 | numberOfLevel = dataset.length | | |||
48 | currentLevel = 0 | 48 | currentLevel = 0 | ||
49 | 49 | | |||
50 | if(Qt.locale(GCompris.ApplicationSettings.locale).firstDayOfWeek == Qml.Locale.Monday) { | 50 | if(Qt.locale(GCompris.ApplicationSettings.locale).firstDayOfWeek == Qml.Locale.Monday) { | ||
51 | items.daysOfTheWeekModel.move(0, items.daysOfTheWeekModel.count - 1, 1) | 51 | items.daysOfTheWeekModel.move(0, items.daysOfTheWeekModel.count - 1, 1) | ||
52 | } | 52 | } | ||
53 | initLevel(); | 53 | initLevel(); | ||
54 | } | 54 | } | ||
55 | 55 | | |||
56 | function stop() { | 56 | function stop() { | ||
57 | } | 57 | } | ||
58 | 58 | | |||
59 | function initLevel() { | 59 | function initLevel() { | ||
60 | currentSubLevel = 1; | 60 | currentSubLevel = 1; | ||
61 | items.bar.level = currentLevel + 1 | 61 | items.bar.level = currentLevel + 1 | ||
62 | currentLevelConfig = dataset[currentLevel][0][0] | 62 | currentLevelConfig = items.levels[currentLevel] | ||
63 | setCalendarConfigurations() | 63 | setCalendarConfigurations() | ||
64 | initQuestion(); | 64 | initQuestion(); | ||
65 | } | 65 | } | ||
66 | 66 | | |||
67 | function nextLevel() { | 67 | function nextLevel() { | ||
68 | if(numberOfLevel <= ++currentLevel) { | 68 | if(numberOfLevel <= ++currentLevel) { | ||
69 | currentLevel = 0 | 69 | currentLevel = 0 | ||
70 | } | 70 | } | ||
Show All 16 Lines | 82 | function setCalendarConfigurations() { | |||
87 | items.calendar.minimumDate = currentLevelConfig["minimumDate"] | 87 | items.calendar.minimumDate = currentLevelConfig["minimumDate"] | ||
88 | items.calendar.maximumDate = currentLevelConfig["maximumDate"] | 88 | items.calendar.maximumDate = currentLevelConfig["maximumDate"] | ||
89 | items.calendar.visibleYear = currentLevelConfig["visibleYear"] | 89 | items.calendar.visibleYear = currentLevelConfig["visibleYear"] | ||
90 | yearSelected = currentLevelConfig["visibleYear"] | 90 | yearSelected = currentLevelConfig["visibleYear"] | ||
91 | items.calendar.visibleMonth = currentLevelConfig["visibleMonth"] | 91 | items.calendar.visibleMonth = currentLevelConfig["visibleMonth"] | ||
92 | monthSelected = currentLevelConfig["visibleMonth"] | 92 | monthSelected = currentLevelConfig["visibleMonth"] | ||
93 | items.answerChoices.visible = (mode === "findDayOfWeek") ? true : false | 93 | items.answerChoices.visible = (mode === "findDayOfWeek") ? true : false | ||
94 | items.okButton.visible = !items.answerChoices.visible | 94 | items.okButton.visible = !items.answerChoices.visible | ||
95 | currentDataSet = dataset[currentLevel][1] | 95 | currentDataSet = currentLevelConfig["questionAnswers"] | ||
96 | currentDataSet = Core.shuffle(currentDataSet) | 96 | currentDataSet = Core.shuffle(currentDataSet) | ||
97 | items.score.numberOfSubLevels = currentDataSet.length | 97 | items.score.numberOfSubLevels = currentDataSet.length | ||
98 | items.score.currentSubLevel = currentSubLevel | 98 | items.score.currentSubLevel = currentSubLevel | ||
99 | } | 99 | } | ||
100 | 100 | | |||
101 | function isLeapYear(year) { | ||||
102 | if(year % 100 == 0 && year % 400 != 0) { | ||||
103 | return false | ||||
104 | } else if(year % 4 == 0) { | ||||
105 | return true | ||||
106 | } else { | ||||
107 | return false | ||||
108 | } | ||||
109 | } | ||||
110 | | ||||
111 | function generateRandomYearMonthDay(minimumDate, maximumDate) { | ||||
112 | var minYear = Number(minimumDate.slice(0, 4)) | ||||
jjazeix: qsTr() missing | |||||
scagarwal: Do maintain spaces between elements in an array. The formatting is wrong. | |||||
113 | var maxYear = Number(maximumDate.slice(0, 4)) | ||||
amankumargupta: Indentation | |||||
114 | var minMonth = Number(minimumDate.slice(5, 7)) | ||||
this should be depending on the country. We may want to display month day year on some locales, it needs to be configurable for the translator (and have a comment) jjazeix: this should be depending on the country. We may want to display month day year on some locales… | |||||
115 | var maxMonth = Number(maximumDate.slice(5, 7)) | ||||
116 | var minDate = Number(minimumDate.slice(8, 10)) | ||||
117 | var maxDate = Number(maximumDate.slice(8, 10)) | ||||
118 | var currentYear = minYear + Math.floor(Math.random() * Math.floor((maxYear - minYear))) | ||||
119 | var currentMonth = minMonth + Math.floor(Math.random() * Math.floor((maxMonth - minMonth))) | ||||
120 | var currentDate | ||||
121 | daysInMonths[1] = (isLeapYear(currentYear)) ? 29 : 28; | ||||
122 | currentDate = minDate + Math.floor(Math.random() * Math.floor((daysInMonths[currentMonth - 1] - minDate))) | ||||
123 | return { year: currentYear, month: currentMonth - 1, day: currentDate } | ||||
124 | } | ||||
125 | | ||||
Shouldn't Math.floor((maxMonth - minMonth)) be Math.floor(maxMonth - minMonth + 1)? Let's say minMonth is 3 and maxMonth is 5. I should get any random among 0, 1, 2 so that minMonth + Math.floor(Math.random() * Math.floor((maxMonth - minMonth))) can be anything between 3, 4, 5. But, Math.floor(Math.random() * Math.floor((maxMonth - minMonth))) limits the values among 0, 1 (as minMonth - maxMonth = 2 in our case). So you should probably do maxMonth - minMonth + 1? amankumargupta: Shouldn't Math.floor((maxMonth - minMonth)) be Math.floor(maxMonth - minMonth + 1)?
Let's say… | |||||
126 | function addOffsetToCurrentDate(currentDate) { | ||||
127 | var maxOffset = currentLevelConfig.questionAnswers.maxOffset | ||||
128 | console.log(typeof maxOffset) | ||||
amankumargupta: Same comment about choosing random value as above. | |||||
129 | var offset = Math.floor(maxOffset / 2) + Math.floor(Math.random() * Math.floor(maxOffset)) | ||||
130 | console.log(offset) | ||||
131 | daysInMonths[1] = (isLeapYear(currentDate.year)) ? 29 : 28; | ||||
132 | var currentOffset = offset; | ||||
133 | currentOffset += currentDate.day | ||||
134 | var answerDate = 1; | ||||
135 | var answerMonth = currentDate.month | ||||
136 | var answerYear = currentDate.year | ||||
Why a new variable "currentOffset"? Why not directly use "offset"? It's not used anywhere in this function. amankumargupta: Why a new variable "currentOffset"? Why not directly use "offset"? It's not used anywhere in… | |||||
137 | while(currentOffset > 0) { | ||||
138 | if(currentOffset - daysInMonths[answerMonth] > 0) { | ||||
139 | currentOffset -= daysInMonths[answerMonth] | ||||
140 | answerMonth++; | ||||
141 | } else { | ||||
142 | answerDate = currentOffset; | ||||
143 | currentOffset = 0 | ||||
144 | } | ||||
145 | if(answerMonth > 12) { | ||||
146 | answerYear++; | ||||
147 | daysInMonths[1] = (isLeapYear(answerYear)) ? 29 : 28; | ||||
148 | answerMonth = 0; | ||||
149 | } | ||||
150 | } | ||||
151 | return { year: answerYear, month: answerMonth, day: answerDate, offset: offset } | ||||
152 | } | ||||
153 | | ||||
154 | function getTemplateQuestionText(mode, date) { | ||||
155 | var questionText | ||||
156 | if(mode == "findDayOfWeek") { | ||||
157 | questionText = "Find the weekday on " + date.day.toString() | ||||
158 | } else if(mode == "findDay") { | ||||
159 | questionText = "Select Day " + date.day.toString() | ||||
160 | } else if(mode == "findMonthOnly") { | ||||
jjazeix: no space before ? | |||||
161 | questionText = "Find month number" + (date.month + 1).toString() | ||||
qsTr() + bad format jjazeix: qsTr() + bad format
Can you please check if the format is the same if we change locale? | |||||
@jjazeix When I change the locale only the names of months and years change, not the question text. As we used hardcoded strings for questions. AkshayCHD: @jjazeix When I change the locale only the names of months and years change, not the question… | |||||
162 | } else { | ||||
163 | questionText = "Find the date " + date.offset.toString() + " days after " + date.day.toString() + "-" + | ||||
164 | (date.month + 1).toString() + "-" + date.year.toString(); | ||||
165 | | ||||
166 | } | ||||
167 | return questionText | ||||
168 | } | ||||
Please add translators comment above explaining what the arguments will be. It'll be easier for them to understand while translating. amankumargupta: Please add translators comment above explaining what the arguments will be. It'll be easier for… | |||||
169 | | ||||
101 | function initQuestion() { | 170 | function initQuestion() { | ||
102 | if(currentDataSet.length < currentSubLevel) { | 171 | if(currentDataSet.length < currentSubLevel) { | ||
103 | items.bonus.good("lion") | 172 | items.bonus.good("lion") | ||
104 | } | 173 | } | ||
105 | else { | 174 | else { | ||
175 | if(!currentLevelConfig.questionsExplicitlyGiven) { | ||||
176 | var randomDate = generateRandomYearMonthDay(currentLevelConfig.minimumDate, currentLevelConfig.maximumDate) | ||||
177 | items.score.currentSubLevel = currentSubLevel | ||||
178 | if(currentLevelConfig.mode == "findDayOfWeek") { | ||||
179 | var selectedDate = new Date(randomDate.year, randomDate.month - 1, randomDate.day) | ||||
180 | correctAnswer.dayOfWeek = Number(selectedDate.getDay()) | ||||
181 | | ||||
182 | } else if(currentLevelConfig.mode == "findYearMonthDay") { | ||||
183 | correctAnswer = addOffsetToCurrentDate(randomDate) | ||||
184 | randomDate.offset = correctAnswer.offset | ||||
185 | } else { | ||||
186 | correctAnswer = randomDate | ||||
187 | } | ||||
188 | items.questionItem.text = getTemplateQuestionText(currentLevelConfig.mode, randomDate) | ||||
189 | } else { | ||||
106 | items.score.currentSubLevel = currentSubLevel | 190 | items.score.currentSubLevel = currentSubLevel | ||
107 | items.questionItem.text = currentDataSet[currentSubLevel-1]["question"] | 191 | items.questionItem.text = currentDataSet[currentSubLevel-1]["question"] | ||
108 | correctAnswer = currentDataSet[currentSubLevel-1]["answer"] | 192 | correctAnswer = currentDataSet[currentSubLevel-1]["answer"] | ||
109 | } | 193 | } | ||
110 | } | 194 | } | ||
195 | } | ||||
111 | 196 | | |||
112 | function updateScore(isCorrectAnswer) { | 197 | function updateScore(isCorrectAnswer) { | ||
113 | if(isCorrectAnswer) { | 198 | if(isCorrectAnswer) { | ||
114 | items.questionDelay.start() | 199 | items.questionDelay.start() | ||
115 | items.okButtonParticles.burst(20) | 200 | items.okButtonParticles.burst(20) | ||
116 | items.score.playWinAnimation() | 201 | items.score.playWinAnimation() | ||
117 | currentSubLevel++; | 202 | currentSubLevel++; | ||
118 | } | 203 | } | ||
Show All 10 Lines | 211 | if(mode === "findDayOfWeek") { | |||
129 | } | 214 | } | ||
130 | } | 215 | } | ||
131 | // For levels having question based on month only. | 216 | // For levels having question based on month only. | ||
132 | else if(mode === "findMonthOnly") { | 217 | else if(mode === "findMonthOnly") { | ||
133 | if(correctAnswer["month"].indexOf(monthSelected) >= 0) { | 218 | if(correctAnswer["month"].indexOf(monthSelected) >= 0) { | ||
134 | isCorrectAnswer = true | 219 | isCorrectAnswer = true | ||
135 | } | 220 | } | ||
136 | } | 221 | } | ||
222 | // For levels having question based on day only. | ||||
223 | else if(mode === "findDay") { | ||||
224 | if(daySelected === correctAnswer["day"]) { | ||||
225 | isCorrectAnswer = true | ||||
226 | } | ||||
227 | } | ||||
137 | // For levels having questions based on dayOfWeek, month and year. | 228 | // For levels having questions based on dayOfWeek, month and year. | ||
138 | else if(mode !== "findDayOfWeek") { | 229 | else { | ||
139 | if(monthSelected === correctAnswer["month"] && daySelected === correctAnswer["day"] && yearSelected === correctAnswer["year"]) { | 230 | if(monthSelected === correctAnswer["month"] && daySelected === correctAnswer["day"] && yearSelected === correctAnswer["year"]) { | ||
140 | isCorrectAnswer = true | 231 | isCorrectAnswer = true | ||
141 | } | 232 | } | ||
142 | } | 233 | } | ||
143 | updateScore(isCorrectAnswer) | 234 | updateScore(isCorrectAnswer) | ||
144 | } | 235 | } | ||
145 | 236 | |
qsTr() missing