|
@@ -5,6 +5,10 @@ function GridCell(x, y) {
|
|
|
this.version = 0;
|
|
this.version = 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+GridCell.prototype.toStatic = function() {
|
|
|
|
|
+ return { x: this.px, y: this.py };
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
function LetterCell(x, y, letter) {
|
|
function LetterCell(x, y, letter) {
|
|
|
GridCell.call(this, x, y);
|
|
GridCell.call(this, x, y);
|
|
|
this.letter = letter;
|
|
this.letter = letter;
|
|
@@ -13,55 +17,164 @@ function LetterCell(x, y, letter) {
|
|
|
LetterCell.prototype = Object.create(GridCell);
|
|
LetterCell.prototype = Object.create(GridCell);
|
|
|
LetterCell.prototype.constructor = LetterCell;
|
|
LetterCell.prototype.constructor = LetterCell;
|
|
|
|
|
|
|
|
|
|
+LetterCell.prototype.toStatic = function() {
|
|
|
|
|
+ var ret = GridCell.prototype.toStatic.call(this);
|
|
|
|
|
+ if (this.found) {
|
|
|
|
|
+ ret.letter = this.letter;
|
|
|
|
|
+ }
|
|
|
|
|
+ return ret;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
var debugFound = {};
|
|
var debugFound = {};
|
|
|
|
|
|
|
|
function Definition(position, alignment, text) {
|
|
function Definition(position, alignment, text) {
|
|
|
|
|
+ this.position = position;
|
|
|
|
|
+ this.alignment = alignment;
|
|
|
|
|
+ this.textArray = text;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-Definition.POSITION_TOP = {};
|
|
|
|
|
Definition.POSITION_BOTTOM = {};
|
|
Definition.POSITION_BOTTOM = {};
|
|
|
Definition.POSITION_RIGHT = {};
|
|
Definition.POSITION_RIGHT = {};
|
|
|
-Definition.POSITION_LEFT = {};
|
|
|
|
|
|
|
|
|
|
Definition.ALIGNMENT_HORIZONTAL = {};
|
|
Definition.ALIGNMENT_HORIZONTAL = {};
|
|
|
Definition.ALIGNMENT_VERTICAL = {};
|
|
Definition.ALIGNMENT_VERTICAL = {};
|
|
|
|
|
|
|
|
|
|
+Definition.prototype.toStatic = function() {
|
|
|
|
|
+ var ret = {
|
|
|
|
|
+ text: this.textArray
|
|
|
|
|
+ };
|
|
|
|
|
+ if (this.position === Definition.POSITION_RIGHT && this.alignment === Definition.ALIGNMENT_HORIZONTAL) {
|
|
|
|
|
+ ret.pos = 1;
|
|
|
|
|
+ } else if (this.position === Definition.POSITION_RIGHT && this.alignment === Definition.ALIGNMENT_VERTICAL) {
|
|
|
|
|
+ ret.pos = 2;
|
|
|
|
|
+ } else if (this.position === Definition.POSITION_BOTTOM && this.alignment === Definition.ALIGNMENT_HORIZONTAL) {
|
|
|
|
|
+ ret.pos = 3;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ ret.pos = 4;
|
|
|
|
|
+ }
|
|
|
|
|
+ return ret;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
function DefinitionCell(x, y, code, definitionArray, currentDefinition) {
|
|
function DefinitionCell(x, y, code, definitionArray, currentDefinition) {
|
|
|
if (debugFound[code]) debugFound[code].push({x:x,y:y}); else debugFound[code] = [{x:x,y:y}];
|
|
if (debugFound[code]) debugFound[code].push({x:x,y:y}); else debugFound[code] = [{x:x,y:y}];
|
|
|
GridCell.call(this, x, y);
|
|
GridCell.call(this, x, y);
|
|
|
this.definitions = [];
|
|
this.definitions = [];
|
|
|
- if ()
|
|
|
|
|
|
|
+ switch (code) {
|
|
|
|
|
+ case 0: // a
|
|
|
|
|
+ this.definitions.push(new Definition(Definition.POSITION_RIGHT, Definition.ALIGNMENT_HORIZONTAL, definitionArray[currentDefinition++]));
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 1: // b
|
|
|
|
|
+ this.definitions.push(new Definition(Definition.POSITION_BOTTOM, Definition.ALIGNMENT_VERTICAL, definitionArray[currentDefinition++]));
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 2: // c
|
|
|
|
|
+ this.definitions.push(new Definition(Definition.POSITION_RIGHT, Definition.ALIGNMENT_VERTICAL, definitionArray[currentDefinition++]));
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 3: // d
|
|
|
|
|
+ this.definitions.push(new Definition(Definition.POSITION_BOTTOM, Definition.ALIGNMENT_HORIZONTAL, definitionArray[currentDefinition++]));
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 4: // e
|
|
|
|
|
+ case 5: // f
|
|
|
|
|
+ case 6: // g
|
|
|
|
|
+ case 7: // h
|
|
|
|
|
+ case 8: // i
|
|
|
|
|
+ this.definitions.push(new Definition(Definition.POSITION_RIGHT, Definition.ALIGNMENT_HORIZONTAL, definitionArray[currentDefinition++]));
|
|
|
|
|
+ this.definitions.push(new Definition(Definition.POSITION_BOTTOM, Definition.ALIGNMENT_VERTICAL, definitionArray[currentDefinition++]));
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 9: // j
|
|
|
|
|
+ case 10: // k
|
|
|
|
|
+ case 11: // l
|
|
|
|
|
+ case 12: // m
|
|
|
|
|
+ case 13: // n
|
|
|
|
|
+ this.definitions.push(new Definition(Definition.POSITION_RIGHT, Definition.ALIGNMENT_VERTICAL, definitionArray[currentDefinition++]));
|
|
|
|
|
+ this.definitions.push(new Definition(Definition.POSITION_BOTTOM, Definition.ALIGNMENT_VERTICAL, definitionArray[currentDefinition++]));
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 14: // o
|
|
|
|
|
+ case 15: // p
|
|
|
|
|
+ case 16: // q
|
|
|
|
|
+ case 17: // r
|
|
|
|
|
+ case 18: // s
|
|
|
|
|
+ this.definitions.push(new Definition(Definition.POSITION_RIGHT, Definition.ALIGNMENT_HORIZONTAL, definitionArray[currentDefinition++]));
|
|
|
|
|
+ this.definitions.push(new Definition(Definition.POSITION_BOTTOM, Definition.ALIGNMENT_HORIZONTAL, definitionArray[currentDefinition++]));
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 19: // t
|
|
|
|
|
+ case 20: // u
|
|
|
|
|
+ case 21: // v
|
|
|
|
|
+ case 22: // w
|
|
|
|
|
+ case 23: // x
|
|
|
|
|
+ this.definitions.push(new Definition(Definition.POSITION_RIGHT, Definition.ALIGNMENT_VERTICAL, definitionArray[currentDefinition++]));
|
|
|
|
|
+ this.definitions.push(new Definition(Definition.POSITION_BOTTOM, Definition.ALIGNMENT_HORIZONTAL, definitionArray[currentDefinition++]));
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
DefinitionCell.prototype = Object.create(GridCell);
|
|
DefinitionCell.prototype = Object.create(GridCell);
|
|
|
DefinitionCell.prototype.constructor = DefinitionCell;
|
|
DefinitionCell.prototype.constructor = DefinitionCell;
|
|
|
|
|
|
|
|
|
|
+DefinitionCell.prototype.toStatic = function() {
|
|
|
|
|
+ var ret = GridCell.prototype.toStatic.call(this);
|
|
|
|
|
+ ret.definitions = [];
|
|
|
|
|
+ this.definitions.forEach((def) => {
|
|
|
|
|
+ ret.definitions.push(def.toStatic());
|
|
|
|
|
+ });
|
|
|
|
|
+ return ret;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
function parseGrid(grid, definitions, w, h) {
|
|
function parseGrid(grid, definitions, w, h) {
|
|
|
var currentDefinition = 0
|
|
var currentDefinition = 0
|
|
|
,resultGrid = []
|
|
,resultGrid = []
|
|
|
,firstCharCode = 'a'.charCodeAt(0);
|
|
,firstCharCode = 'a'.charCodeAt(0);
|
|
|
|
|
|
|
|
- for (var i =0; i < h; i++) {
|
|
|
|
|
- for (var j =0; j < w; j++) {
|
|
|
|
|
- var c = grid[i][j];
|
|
|
|
|
|
|
+ for (let i =0; i < h; i++) {
|
|
|
|
|
+ for (let j =0; j < w; j++) {
|
|
|
|
|
+ let c = grid[i][j];
|
|
|
if (c == c.toUpperCase()) {
|
|
if (c == c.toUpperCase()) {
|
|
|
- resultGrid.push(new LetterCell(i, j, c));
|
|
|
|
|
|
|
+ resultGrid.push(new LetterCell(j, i, c));
|
|
|
|
|
+ } else if (c == 'z') {
|
|
|
|
|
+ resultGrid.push(null);
|
|
|
} else {
|
|
} else {
|
|
|
- var cell = new DefinitionCell(i, j, c.charCodeAt(0) -firstCharCode, definitions, currentDefinition);
|
|
|
|
|
|
|
+ let cell = new DefinitionCell(j, i, c.charCodeAt(0) -firstCharCode, definitions, currentDefinition);
|
|
|
currentDefinition += cell.definitions.length;
|
|
currentDefinition += cell.definitions.length;
|
|
|
resultGrid.push(cell);
|
|
resultGrid.push(cell);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- console.log(debugFound);
|
|
|
|
|
return resultGrid;
|
|
return resultGrid;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function Grid(data) {
|
|
|
|
|
|
|
+function Grid(publicId, data) {
|
|
|
this.title = data["titre"];
|
|
this.title = data["titre"];
|
|
|
this.difficulty = data["force"];
|
|
this.difficulty = data["force"];
|
|
|
this.width = data["nbcaseslargeur"];
|
|
this.width = data["nbcaseslargeur"];
|
|
|
this.height = data["nbcaseshauteur"];
|
|
this.height = data["nbcaseshauteur"];
|
|
|
this.grid = parseGrid(data["grille"], data["definitions"], this.width, this.height);
|
|
this.grid = parseGrid(data["grille"], data["definitions"], this.width, this.height);
|
|
|
|
|
+ this.publicId = publicId;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+Grid.prototype.toStatic = function(v) {
|
|
|
|
|
+ var ret = {};
|
|
|
|
|
+ if (!v) {
|
|
|
|
|
+ ret.title = this.title;
|
|
|
|
|
+ ret.difficulty = this.difficulty;
|
|
|
|
|
+ ret.w = this.width;
|
|
|
|
|
+ ret.h = this.height;
|
|
|
|
|
+ }
|
|
|
|
|
+ var grid = [];
|
|
|
|
|
+ this.grid.forEach((cell) => {
|
|
|
|
|
+ if (cell instanceof DefinitionCell && !v) {
|
|
|
|
|
+ grid.push(cell.toStatic());
|
|
|
|
|
+ } else if (cell instanceof LetterCell && cell.found && cell.v >= v) {
|
|
|
|
|
+ grid.push(cell.toStatic());
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ if (grid.length)
|
|
|
|
|
+ ret.grid = grid;
|
|
|
|
|
+ return grid.length || !v ? ret : null;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
module.exports.Grid = Grid;
|
|
module.exports.Grid = Grid;
|