Grid.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. function GridCell(x, y) {
  2. this.px = x;
  3. this.py = y;
  4. this.version = 0;
  5. }
  6. GridCell.prototype.toStatic = function() {
  7. return { x: this.px, y: this.py };
  8. };
  9. function EmptyCell(x, y) {
  10. GridCell.call(this, x, y);
  11. }
  12. EmptyCell.prototype = Object.create(GridCell);
  13. EmptyCell.prototype.constructor = EmptyCell;
  14. EmptyCell.prototype.toStatic = function() {
  15. var ret = GridCell.prototype.toStatic.call(this);
  16. ret["type"] = null;
  17. return ret;
  18. };
  19. function LetterCell(x, y, letter) {
  20. GridCell.call(this, x, y);
  21. this.letter = letter;
  22. this.found = false;
  23. }
  24. LetterCell.prototype = Object.create(GridCell);
  25. LetterCell.prototype.constructor = LetterCell;
  26. LetterCell.prototype.toStatic = function() {
  27. var ret = GridCell.prototype.toStatic.call(this);
  28. if (this.found) {
  29. ret.letter = this.letter;
  30. }
  31. return ret;
  32. };
  33. var debugFound = {};
  34. function Definition(position, alignment, text) {
  35. this.position = position;
  36. this.alignment = alignment;
  37. this.textArray = text;
  38. }
  39. Definition.POSITION_BOTTOM = {};
  40. Definition.POSITION_RIGHT = {};
  41. Definition.ALIGNMENT_HORIZONTAL = {};
  42. Definition.ALIGNMENT_VERTICAL = {};
  43. Definition.prototype.toStatic = function() {
  44. var ret = {
  45. text: this.textArray
  46. };
  47. if (this.position === Definition.POSITION_RIGHT && this.alignment === Definition.ALIGNMENT_HORIZONTAL) {
  48. ret.pos = 1;
  49. } else if (this.position === Definition.POSITION_RIGHT && this.alignment === Definition.ALIGNMENT_VERTICAL) {
  50. ret.pos = 2;
  51. } else if (this.position === Definition.POSITION_BOTTOM && this.alignment === Definition.ALIGNMENT_HORIZONTAL) {
  52. ret.pos = 3;
  53. } else {
  54. ret.pos = 4;
  55. }
  56. return ret;
  57. };
  58. function DefinitionCell(x, y, code, definitionArray, currentDefinition) {
  59. if (debugFound[code]) debugFound[code].push({x:x,y:y}); else debugFound[code] = [{x:x,y:y}];
  60. GridCell.call(this, x, y);
  61. this.definitions = [];
  62. switch (code) {
  63. case 0: // a
  64. this.definitions.push(new Definition(Definition.POSITION_RIGHT, Definition.ALIGNMENT_HORIZONTAL, definitionArray[currentDefinition++]));
  65. break;
  66. case 1: // b
  67. this.definitions.push(new Definition(Definition.POSITION_BOTTOM, Definition.ALIGNMENT_VERTICAL, definitionArray[currentDefinition++]));
  68. break;
  69. case 2: // c
  70. this.definitions.push(new Definition(Definition.POSITION_RIGHT, Definition.ALIGNMENT_VERTICAL, definitionArray[currentDefinition++]));
  71. break;
  72. case 3: // d
  73. this.definitions.push(new Definition(Definition.POSITION_BOTTOM, Definition.ALIGNMENT_HORIZONTAL, definitionArray[currentDefinition++]));
  74. break;
  75. case 4: // e
  76. case 5: // f
  77. case 6: // g
  78. case 7: // h
  79. case 8: // i
  80. this.definitions.push(new Definition(Definition.POSITION_RIGHT, Definition.ALIGNMENT_HORIZONTAL, definitionArray[currentDefinition++]));
  81. this.definitions.push(new Definition(Definition.POSITION_BOTTOM, Definition.ALIGNMENT_VERTICAL, definitionArray[currentDefinition++]));
  82. break;
  83. case 9: // j
  84. case 10: // k
  85. case 11: // l
  86. case 12: // m
  87. case 13: // n
  88. this.definitions.push(new Definition(Definition.POSITION_RIGHT, Definition.ALIGNMENT_VERTICAL, definitionArray[currentDefinition++]));
  89. this.definitions.push(new Definition(Definition.POSITION_BOTTOM, Definition.ALIGNMENT_VERTICAL, definitionArray[currentDefinition++]));
  90. break;
  91. case 14: // o
  92. case 15: // p
  93. case 16: // q
  94. case 17: // r
  95. case 18: // s
  96. this.definitions.push(new Definition(Definition.POSITION_RIGHT, Definition.ALIGNMENT_HORIZONTAL, definitionArray[currentDefinition++]));
  97. this.definitions.push(new Definition(Definition.POSITION_BOTTOM, Definition.ALIGNMENT_HORIZONTAL, definitionArray[currentDefinition++]));
  98. break;
  99. case 19: // t
  100. case 20: // u
  101. case 21: // v
  102. case 22: // w
  103. case 23: // x
  104. this.definitions.push(new Definition(Definition.POSITION_RIGHT, Definition.ALIGNMENT_VERTICAL, definitionArray[currentDefinition++]));
  105. this.definitions.push(new Definition(Definition.POSITION_BOTTOM, Definition.ALIGNMENT_HORIZONTAL, definitionArray[currentDefinition++]));
  106. break;
  107. }
  108. }
  109. DefinitionCell.prototype = Object.create(GridCell);
  110. DefinitionCell.prototype.constructor = DefinitionCell;
  111. DefinitionCell.prototype.toStatic = function() {
  112. var ret = GridCell.prototype.toStatic.call(this);
  113. ret.definitions = [];
  114. this.definitions.forEach((def) => {
  115. ret.definitions.push(def.toStatic());
  116. });
  117. return ret;
  118. };
  119. function parseGrid(grid, definitions, w, h) {
  120. var currentDefinition = 0
  121. ,resultGrid = []
  122. ,firstCharCode = 'a'.charCodeAt(0);
  123. for (let i =0; i < h; i++) {
  124. for (let j =0; j < w; j++) {
  125. let c = grid[i][j];
  126. if (c == c.toUpperCase()) {
  127. resultGrid.push(new LetterCell(j, i, c));
  128. } else if (c == 'z') {
  129. resultGrid.push(new EmptyCell(j, i));
  130. } else {
  131. let cell = new DefinitionCell(j, i, c.charCodeAt(0) -firstCharCode, definitions, currentDefinition);
  132. currentDefinition += cell.definitions.length;
  133. resultGrid.push(cell);
  134. }
  135. }
  136. }
  137. return resultGrid;
  138. }
  139. function Grid(publicId, data) {
  140. this.title = data["titre"];
  141. this.difficulty = data["force"];
  142. this.width = data["nbcaseslargeur"];
  143. this.height = data["nbcaseshauteur"];
  144. this.grid = parseGrid(data["grille"], data["definitions"], this.width, this.height);
  145. this.publicId = publicId;
  146. };
  147. Grid.prototype.getCell = function(x, y) {
  148. for (var i =0, nbCells = this.grid.length; i < nbCells; i++) {
  149. var cell = this.grid[i];
  150. if (cell.px === x && cell.py === y)
  151. return cell;
  152. }
  153. return null;
  154. };
  155. Grid.prototype.toStatic = function(v) {
  156. var ret = {};
  157. if (!v) {
  158. ret.title = this.title;
  159. ret.difficulty = this.difficulty;
  160. ret.w = this.width;
  161. ret.h = this.height;
  162. }
  163. var grid = [];
  164. this.grid.forEach((cell) => {
  165. if ((cell instanceof DefinitionCell || cell instanceof EmptyCell) && !v) {
  166. grid.push(cell.toStatic());
  167. } else if (cell instanceof LetterCell && cell.found && cell.v >= v) {
  168. grid.push(cell.toStatic());
  169. }
  170. });
  171. if (grid.length)
  172. ret.grid = grid;
  173. return grid.length || !v ? ret : null;
  174. };
  175. module.exports.Grid = Grid;
  176. module.exports.LetterCell = LetterCell;