Grid.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. }
  81. Grid.prototype.computeWord = function(x, y, dx, dy) {
  82. 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) {
  83. return [[x, y]];
  84. }
  85. var word = this.computeWord(x +dx, y +dy, dx, dy);
  86. word.unshift([x, y]);
  87. return word;
  88. };
  89. Grid.prototype.getWord = function(x, y) {
  90. var words = [];
  91. this.words.forEach(function(word) {
  92. for (var i =0, nbLetters =word.length; i < nbLetters; i++) {
  93. if (word[i][0] == x && word[i][1] == y) {
  94. words.push(word);
  95. break;
  96. }
  97. }
  98. });
  99. return words;
  100. };
  101. Grid.prototype.updatePlayers = function(playerData) {
  102. var maxVersion = 0;
  103. playerData.forEach(function(player) {
  104. var localPlayer = this.players[player["name"]];
  105. if (!localPlayer)
  106. localPlayer = this.players[player["name"]] = new Player(player);
  107. maxVersion = Math.max(maxVersion, localPlayer.update(player));
  108. }.bind(this));
  109. return maxVersion;
  110. };
  111. /**
  112. * @return {number|null}
  113. **/
  114. Grid.prototype.update = function(data) {
  115. var maxVersion = null
  116. ,topologyUpdated = false;
  117. data.forEach(function(cellData) {
  118. var updateResult = this.grid[cellData["x"]][cellData["y"]].update(cellData, this.players);
  119. maxVersion = Math.max(maxVersion || 0, updateResult);
  120. if (updateResult === 0) {
  121. topologyUpdated = true;
  122. }
  123. }.bind(this));
  124. if (topologyUpdated) {
  125. var words = [];
  126. for (var i =0; i < this.width; i++) {
  127. for (var j =0; j < this.height; j++) {
  128. if (this.grid[i][j].definitions) {
  129. this.grid[i][j].definitions.forEach(function(definition) {
  130. var word;
  131. switch (definition.direction) {
  132. case Definition.RIGHT_VERTICAL:
  133. word = this.computeWord(i +1, j, 0, 1);
  134. break;
  135. case Definition.RIGHT_HORIZONTAL:
  136. word = this.computeWord(i +1, j, 1, 0);
  137. break;
  138. case Definition.BOTTOM_VERTICAL:
  139. word = this.computeWord(i, j +1, 0, 1);
  140. break;
  141. case Definition.BOTTOM_HORIZONTAL:
  142. word = this.computeWord(i, j +1, 1, 0);
  143. break;
  144. }
  145. words.push(word);
  146. definition.word = word;
  147. }.bind(this));
  148. }
  149. }
  150. }
  151. this.words = words;
  152. }
  153. return maxVersion;
  154. };