Grid.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * @constructor
  3. **/
  4. function Definition(data) {
  5. /** @type {Array.<string>} */
  6. this.text = data["text"];
  7. /** @type {number} */
  8. this.direction = data["pos"];
  9. /** @type {Array.<Array.<number>>|null} */
  10. this.word = null;
  11. }
  12. /** @const */
  13. Definition.RIGHT_HORIZONTAL = 1;
  14. /** @const */
  15. Definition.RIGHT_VERTICAL = 2;
  16. /** @const */
  17. Definition.BOTTOM_HORIZONTAL = 3;
  18. /** @const */
  19. Definition.BOTTOM_VERTICAL = 4;
  20. /**
  21. * @constructor
  22. **/
  23. function Cell() {
  24. /** @type {boolean} */
  25. this.isBlack = false;
  26. /** @type {Array.<Definition>} */
  27. this.definitions = null;
  28. /** @type {Player|null} */
  29. this.found = null;
  30. /** @type {string|null} */
  31. this.letter = null;
  32. }
  33. /**
  34. * @param {*} data
  35. * @param {Object.<string, Player>} players
  36. **/
  37. Cell.prototype.update = function(data, players) {
  38. if (data["type"] === null) {
  39. this.isBlack = true;
  40. } else if (data["definitions"] !== undefined) {
  41. this.definitions = [];
  42. data["definitions"].forEach(function(definition) {
  43. this.definitions.push(new Definition(definition));
  44. }.bind(this));
  45. } else if (data["letter"]) {
  46. this.letter = data["letter"];
  47. this.found = players[data["found"]];
  48. return data["v"];
  49. }
  50. return 0;
  51. };
  52. /**
  53. * @constructor
  54. **/
  55. function Grid(data, pseudo) {
  56. /** @const @type {string} */
  57. this.title = data["title"] || "";
  58. /** @const @type {number} */
  59. this.difficulty = data["difficulty"];
  60. /** @const @type {number} */
  61. this.width = data["w"];
  62. /** @const @type {number} */
  63. this.height = data["h"];
  64. /** @const @type {number} */
  65. this.startTime = data["startTime"] || 0;
  66. /** @type {Object.<string, Player>} */
  67. this.players = {};
  68. /** @type {Player|null} */
  69. this.playerSelf = null;
  70. /** @const @type {string} */
  71. this.playerSelfId = pseudo;
  72. this.words = [];
  73. this.grid = [];
  74. for (var i =0; i < this.width; i++) {
  75. this.grid[i] = [];
  76. for (var j =0; j < this.height; j++) {
  77. this.grid[i][j] = new Cell();
  78. }
  79. }
  80. /** @type {number|null} */
  81. this.gridTime = null;
  82. }
  83. Grid.prototype.computeWord = function(x, y, dx, dy) {
  84. if (!this.grid[x +dx] || !this.grid[x +dx][y +dy] || this.grid[x +dx][y +dy].definitions || this.grid[x +dx][y +dy].isBlack) {
  85. return [[x, y]];
  86. }
  87. var word = this.computeWord(x +dx, y +dy, dx, dy);
  88. word.unshift([x, y]);
  89. return word;
  90. };
  91. Grid.prototype.getWord = function(x, y) {
  92. var words = [];
  93. this.words.forEach(function(word) {
  94. for (var i =0, nbLetters =word.length; i < nbLetters; i++) {
  95. if (word[i][0] == x && word[i][1] == y) {
  96. words.push(word);
  97. break;
  98. }
  99. }
  100. });
  101. return words;
  102. };
  103. Grid.prototype.updatePlayers = function(playerData) {
  104. var maxVersion = 0;
  105. playerData.forEach(function(player) {
  106. var localPlayer = this.players[player["name"]];
  107. if (!localPlayer)
  108. localPlayer = this.players[player["name"]] = new Player(player);
  109. maxVersion = Math.max(maxVersion, localPlayer.update(player));
  110. }.bind(this));
  111. return maxVersion;
  112. };
  113. /**
  114. * @return {number|null}
  115. **/
  116. Grid.prototype.update = function(data) {
  117. var maxVersion = null
  118. ,topologyUpdated = false;
  119. data.forEach(function(cellData) {
  120. var updateResult = this.grid[cellData["x"]][cellData["y"]].update(cellData, this.players);
  121. maxVersion = Math.max(maxVersion || 0, updateResult);
  122. if (updateResult === 0) {
  123. topologyUpdated = true;
  124. }
  125. }.bind(this));
  126. if (topologyUpdated) {
  127. var words = [];
  128. for (var i =0; i < this.width; i++) {
  129. for (var j =0; j < this.height; j++) {
  130. if (this.grid[i][j].definitions) {
  131. this.grid[i][j].definitions.forEach(function(definition) {
  132. var word;
  133. switch (definition.direction) {
  134. case Definition.RIGHT_VERTICAL:
  135. word = this.computeWord(i +1, j, 0, 1);
  136. break;
  137. case Definition.RIGHT_HORIZONTAL:
  138. word = this.computeWord(i +1, j, 1, 0);
  139. break;
  140. case Definition.BOTTOM_VERTICAL:
  141. word = this.computeWord(i, j +1, 0, 1);
  142. break;
  143. case Definition.BOTTOM_HORIZONTAL:
  144. word = this.computeWord(i, j +1, 1, 0);
  145. break;
  146. }
  147. words.push(word);
  148. definition.word = word;
  149. }.bind(this));
  150. }
  151. }
  152. }
  153. this.words = words;
  154. }
  155. return maxVersion;
  156. };