Commit fc51c473 authored by Arnaud Bey's avatar Arnaud Bey
Browse files

WIP js objectif morpho

parent 131d8f89
......@@ -109,7 +109,7 @@ class Grid implements \JsonSerializable
foreach ($foundable->getWords() as $word) {
$root = $word->getRoot();
foreach ($word->getFeatures() as $feature) {
$jsonArray['inflections'][$form]['features'][$feature->getId()] = $feature->getValue();
$jsonArray['inflections'][$form]['features'][] = $feature->getId();
}
$jsonArray['inflections'][$form]['ids'][] = $word->getId();
$rootId = $root->getId();
......
......@@ -286,9 +286,10 @@ class Round implements \JsonSerializable
foreach ($this->getConstraints() as $c) {
$constraint = [];
$constraint["id"] = $c->getId();
$constraint["features"] = [];
$constraint["numberToFind"] = $c->getNumberToFind();
foreach ($c->getFeatures() as $feature) {
$constraint[$feature->getLabel()] = $feature->getValue();
$constraint["features"][$feature->getId()] = ["label" => $feature->getValue(), "value" => $feature->getValue()];
}
$jsonArray['constraints'][] = $constraint;
......
......@@ -2,26 +2,39 @@ var objectiveConstraint = {
properties: ["category", "number", "gender", "person", "mood", "tense"],
constraintRealized: [],
add: function(inflection) {
var infos = gridJSON.inflections[inflection.toLowerCase()].features;
var features = Object.values(gridJSON.inflections[inflection.toLowerCase()].features);
var incrementedObjs = [];
for (var i = 0; i < infos.length; i++) {
currentInflectionInfos = infos[i];
for (var j = 0; j < roundJSON.constraints.length; j++) {
var objective = roundJSON.constraints[j];
if (this.isPertinent(objective, currentInflectionInfos)) {
if ($.inArray(objective.id, incrementedObjs) == -1) {
objectives.updateProgress(objective.id);
this.constraintRealized.push(objective.id);
incrementedObjs.push(objective.id);
if (this.checkCompletion(objective)) {
objectives.considerAsDone(objective.id);
activity.sendObjectiveDone(objective.id);
}
//on boucle sur les features
//for (var i = 0; i < features.length; i++) {
//var currentFeature = features[i];
// on boucle sur les objectifs
var constraints = Object.entries(roundJSON.constraints);
for (var j = 0; j < constraints.length; j++) {
var objective = constraints[j];
var objectiveId = objective[1].id;
// on teste si les propriétés du mot match avec l'objectif
if (this.isPertinent(objective, features)) {
console.log(objective);
// Si l'objectif est pas dans incrementedObjs
console.log("isPertinent");
if (incrementedObjs.indexOf(objectiveId) === -1) {
console.log("updateProgress");
objectives.updateProgress(objectiveId);
// on ajoute l'objectif aux constraintes réalisées
this.constraintRealized.push(objectiveId);
incrementedObjs.push(objectiveId);
// on compte si le compte est bon...
if (this.checkCompletion(objective)) {
objectives.considerAsDone(objectiveId);
activity.sendObjectiveDone(objectiveId);
}
}
}
}
//}
},
countByObjective: function(objectiveId) {
......@@ -35,20 +48,22 @@ var objectiveConstraint = {
return count;
},
isPertinent: function(objective, infos) {
for (var i = 0; i < this.properties.length; i++) {
var property = this.properties[i];
if (objective[property] != null && objective[property] != infos[property]) {
isPertinent: function(objective, features) {
var objectiveFeatures = Object.keys(objective[1].features);
for (var i = 0; i < objectiveFeatures.length; i++) {
var objectiveFeature = objectiveFeatures[i];
if (features.indexOf(parseInt(objectiveFeature, 10)) === -1) {
return false;
};
}
}
return true;
},
checkCompletion: function(objective) {
if (this.countByObjective(objective.id) == objective.numberToFind) {
var objectiveId = objective[1].id;
var numberToFind = objective[1].numberToFind;
if (this.countByObjective(objectiveId) >= numberToFind) {
return true;
}
......
var objectives = {
done: 0,
doable: Object.keys(roundJSON.findWords).length + Object.keys(roundJSON.combos).length + Object.keys(roundJSON.constraints).length,
score: 0,
doneByType: {
'combo':0,
'findword':0,
'constraint':0
},
toDoByType: {
'combo': Object.keys(roundJSON.combos).length,
'findword': Object.keys(roundJSON.findWords).length,
'constraint': Object.keys(roundJSON.constraints).length
},
done: 0,
doable: Object.keys(roundJSON.findWords).length + Object.keys(roundJSON.combos).length + Object.keys(roundJSON.constraints).length,
score: 0,
doneByType: {
'combo': 0,
'findword': 0,
'constraint': 0
},
toDoByType: {
'combo': Object.keys(roundJSON.combos).length,
'findword': Object.keys(roundJSON.findWords).length,
'constraint': Object.keys(roundJSON.constraints).length
},
considerAsDone: function(objectiveId){
var objective = $(".objective-"+objectiveId);
var type = objective.data("type");
considerAsDone: function(objectiveId) {
var objective = $(".objective-" + objectiveId);
var type = objective.data("type");
if (objectives.doneByType[type] < objectives.toDoByType[type]) {
this.updateDone(type);
sound.play(sound.objectiveProgress);
this.done++;
objective.hide();
this.checkCompletion();
this.checkTypeCompletion(type);
}
objectives.displayDone();
},
if (objectives.doneByType[type] < objectives.toDoByType[type]) {
this.updateDone(type);
sound.play(sound.objectiveProgress);
this.done++;
objective.hide();
this.checkCompletion();
this.checkTypeCompletion(type);
}
displayDone: function(){
document.getElementById("points").innerHTML = this.done + " / " + this.doable;
},
objectives.displayDone();
},
checkCompletion: function(){
if(this.done == this.doable){
sound.play(sound.objectiveDone);
activity.end(0);
}
},
displayDone: function() {
document.getElementById("points").innerHTML = this.done + " / " + this.doable;
},
updateDone:function(type){
objectives.doneByType[type]++;
document.getElementById(type+"-done").innerHTML = objectives.doneByType[type];
},
checkCompletion: function() {
if (this.done == this.doable) {
sound.play(sound.objectiveDone);
activity.end(0);
}
},
updateProgress: function(objectiveId){
$(".objective-"+objectiveId).find(".objective-progress").append("*");
},
updateDone: function(type) {
objectives.doneByType[type]++;
document.getElementById(type + "-done").innerHTML = objectives.doneByType[type];
},
click: function(objectiveType){
if (objectiveType.classList.contains("disabled")) {
return;
}
updateProgress: function(objectiveId) {
$(".objective-" + objectiveId).find(".objective-progress").append("*");
},
if (objectiveType.classList.contains("active")) {
if( objectiveType.scrollTop + objectiveType.offsetHeight === objectiveType.scrollHeight ){
objectiveType.scrollTop = 0;
} else {
var offset = document.getElementById('findwords-container').offsetHeight;
objectiveType.scrollTop += offset/2;
}
click: function(objectiveType) {
if (objectiveType.classList.contains("disabled")) {
return;
}
} else {
objectives.deactivateAll();
objectiveType.classList.add("active");
}
},
if (objectiveType.classList.contains("active")) {
if (objectiveType.scrollTop + objectiveType.offsetHeight === objectiveType.scrollHeight) {
objectiveType.scrollTop = 0;
} else {
var offset = document.getElementById('findwords-container').offsetHeight;
objectiveType.scrollTop += offset / 2;
}
checkTypeCompletion: function(type){
if (objectives.doneByType[type] >= objectives.toDoByType[type]) {
var needToFindNext = $("[data-type='"+type+"']").hasClass("active");
objectives.disable(type);
if (needToFindNext) objectives.findActivable(type);
}
},
} else {
objectives.deactivateAll();
objectiveType.classList.add("active");
}
},
deactivateAll: function(){
$('.objectives').removeClass("active");
},
checkTypeCompletion: function(type) {
if (objectives.doneByType[type] >= objectives.toDoByType[type]) {
var needToFindNext = $("[data-type='" + type + "']").hasClass("active");
objectives.disable(type);
if (needToFindNext) objectives.findActivable(type);
}
},
disable: function(type){
$("[data-type='"+type+"']").addClass("disabled").removeClass("active");
},
deactivateAll: function() {
$('.objectives').removeClass("active");
},
findActivable: function(type){
if (!$("[data-type='"+type+"']").hasClass("active")) {
$(".objectives").not(".disabled").first().addClass("active");
}
disable: function(type) {
$("[data-type='" + type + "']").addClass("disabled").removeClass("active");
},
return;
findActivable: function(type) {
if (!$("[data-type='" + type + "']").hasClass("active")) {
$(".objectives").not(".disabled").first().addClass("active");
}
return;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment