File indexing completed on 2024-05-05 15:53:19

0001 /* GCompris - share.js
0002  *
0003  * SPDX-FileCopyrightText: 2016 Stefan Toncu <stefan.toncu29@gmail.com>
0004  *
0005  *   SPDX-License-Identifier: GPL-3.0-or-later
0006  */
0007 
0008 .pragma library
0009 .import QtQuick 2.12 as Quick
0010 .import GCompris 1.0 as GCompris
0011 .import "qrc:/gcompris/src/core/core.js" as Core
0012 
0013 var numberOfLevel
0014 var items
0015 
0016 var savedTotalBoys
0017 var savedTotalGirls
0018 var savedTotalCandies
0019 var savedPlacedInGirls
0020 var savedPlacedInBoys
0021 var savedCurrentCandies
0022 var subLevelData
0023 
0024 function start(items_) {
0025     items = items_
0026     items.currentLevel = Core.getInitialLevel(numberOfLevel)
0027     initLevel()
0028 }
0029 
0030 function stop() {
0031 }
0032 
0033 function initLevel() {
0034     setUp()
0035 }
0036 
0037 function setUp() {
0038     items.errorRectangle.resetState()
0039     var levelData = items.levels
0040     numberOfLevel = items.levels.length
0041     subLevelData = levelData[items.currentLevel][items.score.currentSubLevel];
0042     // use board levels
0043     if (!subLevelData["randomisedInputData"]) {
0044         items.totalBoys = subLevelData.totalBoys
0045         items.totalGirls = subLevelData.totalGirls
0046         items.totalCandies = subLevelData.totalCandies
0047 
0048         items.instruction.text = subLevelData.instruction
0049         items.nbSubLevel = levelData[items.currentLevel].length
0050 
0051         items.background.currentCandies = items.totalGirls * subLevelData.placedInGirls +
0052                 items.totalBoys * subLevelData.placedInBoys
0053 
0054         items.background.placedInGirls = subLevelData.placedInGirls
0055         items.background.placedInBoys = subLevelData.placedInBoys
0056 
0057         items.background.rest = items.totalCandies -
0058                 Math.floor(items.totalCandies / items.totalChildren) * (items.totalBoys+items.totalGirls)
0059         items.basketWidget.element.opacity = (subLevelData.forceShowBasket === true ||
0060                                               items.background.rest !== 0) ? 1 : 0
0061         items.background.wrongMove.visible = false
0062     }
0063     else {
0064         // create random (guided) levels
0065         // get a random number between 1 and max for boys, girls and candies
0066         var maxBoys = subLevelData.maxBoys
0067         var maxGirls = subLevelData.maxGirls
0068         var maxCandies = subLevelData.maxCandies
0069 
0070         items.totalBoys = Math.floor(Math.random() * maxBoys) + 1
0071         items.totalGirls = Math.floor(Math.random() * maxGirls) + 1
0072         var sum = items.totalBoys + items.totalGirls
0073         // use sum * 6 as top margin (max 6 candies per rectangle)
0074         items.totalCandies = Math.floor(Math.random() * (5 * sum + 1)) + sum
0075         items.nbSubLevel = levelData[items.currentLevel].length
0076         // stay within the max margin
0077         if (items.totalCandies > maxCandies)
0078             items.totalCandies = maxCandies
0079 
0080         // depending on the levels configuration, add candies from start in a child rectangle
0081         if (subLevelData.alreadyPlaced === false) {
0082             items.background.placedInGirls = 0
0083             items.background.placedInBoys = 0
0084             items.background.currentCandies = 0
0085         }
0086         else {
0087             items.background.currentCandies = items.totalCandies * 2
0088             // Place randomly between 0 and 3 candies for each child
0089             while (items.background.currentCandies > items.totalCandies / 3) {
0090                 items.background.placedInGirls = Math.floor(Math.random() * 3)
0091                 items.background.placedInBoys = Math.floor(Math.random() * 3)
0092                 items.background.currentCandies = items.totalGirls * items.background.placedInGirls
0093                         + items.totalBoys * items.background.placedInBoys
0094             }
0095         }
0096         //~ singular "Place %n boy "
0097         //~ plural "Place %n boys "
0098         items.instruction.text = qsTr("Place %n boy(s) ", "First part of Place %n boy(s) and %n girl(s) in the center. Then split %n pieces of candy equally between them.", items.totalBoys);
0099 
0100         //~ singular "and %n girl in the center. "
0101         //~ plural "and %n girls in the center. "
0102         items.instruction.text += qsTr("and %n girl(s) in the center. ", "Second part of Place %n boy(s) and %n girl(s) in the center. Then split %n pieces of candy equally between them.", items.totalGirls);
0103 
0104         //~ singular Then split %n candy equally between them.
0105         //~ plural Then split %n candies equally between them.
0106         items.instruction.text += qsTr("Then split %n pieces of candy equally between them.", "Third part of Place %n boy(s) and %n girl(s) in the center. Then split %n pieces of candy equally between them.", items.totalCandies - items.background.currentCandies);
0107 
0108 
0109         items.background.rest = items.totalCandies -
0110                 Math.floor(items.totalCandies / items.totalChildren) * (items.totalBoys+items.totalGirls)
0111 
0112         items.basketWidget.element.opacity = 1
0113 
0114         items.background.wrongMove.visible = false;
0115 
0116         saveVariables()
0117     }
0118     resetBoard()
0119 }
0120 
0121 function resetBoard() {
0122     items.background.currentGirls = 0
0123     items.background.currentBoys = 0
0124     items.background.resetCandy()
0125 
0126     items.acceptCandy = false
0127     items.instruction.opacity = 1
0128     items.listModel.clear()
0129 
0130     items.girlWidget.current = 0
0131     items.girlWidget.canDrag = true
0132     items.girlWidget.element.opacity = 1
0133 
0134     items.boyWidget.current = 0
0135     items.boyWidget.canDrag = true
0136     items.boyWidget.element.opacity = 1
0137 
0138     items.candyWidget.canDrag = true
0139     items.candyWidget.element.opacity = 1
0140     if (items.totalCandies - items.background.currentCandies == 0)
0141         items.candyWidget.element.opacity = 0.6
0142 
0143     items.basketWidget.canDrag = true
0144     items.buttonsBlocked = false
0145 }
0146 
0147 function saveVariables() {
0148     savedTotalBoys = items.totalBoys
0149     savedTotalGirls = items.totalGirls
0150     savedTotalCandies = items.totalCandies
0151     savedPlacedInGirls = items.background.placedInGirls
0152     savedPlacedInBoys = items.background.placedInBoys
0153     savedCurrentCandies = items.background.currentCandies
0154 }
0155 
0156 function loadVariables() {
0157     items.totalBoys = savedTotalBoys
0158     items.totalGirls = savedTotalGirls
0159     items.totalCandies = savedTotalCandies
0160     items.background.placedInGirls = savedPlacedInGirls
0161     items.background.placedInBoys = savedPlacedInBoys
0162     items.background.currentCandies = savedCurrentCandies
0163 }
0164 
0165 function reloadRandom() {
0166     if (!subLevelData["randomisedInputData"]) {
0167         initLevel()
0168     }
0169     else {
0170         loadVariables()
0171         resetBoard()
0172 
0173         items.background.rest = items.totalCandies -
0174                 Math.floor(items.totalCandies / items.totalChildren) * (items.totalBoys+items.totalGirls)
0175         items.basketWidget.element.opacity = 1
0176     }
0177 }
0178 
0179 function nextSubLevel() {
0180     if (items.score.currentSubLevel >= items.nbSubLevel) {
0181         items.bonus.good("tux")
0182     }
0183     else {
0184         setUp()
0185     }
0186 }
0187 
0188 function nextLevel() {
0189     items.score.stopWinAnimation();
0190     items.currentLevel = Core.getNextLevel(items.currentLevel, numberOfLevel);
0191     items.score.currentSubLevel = 0;
0192     initLevel();
0193 }
0194 
0195 function previousLevel() {
0196     items.score.stopWinAnimation();
0197     items.currentLevel = Core.getPreviousLevel(items.currentLevel, numberOfLevel);
0198     items.score.currentSubLevel = 0;
0199     initLevel();
0200 }