GameMoves.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const Card = require('./Card.js');
  2. class GameMove {
  3. constructor(type, data) {
  4. this.type = type;
  5. this.data = data;
  6. }
  7. verify() {}
  8. performMove() {}
  9. export() {}
  10. }
  11. GameMove.TYPES = {
  12. PLACE: 1,
  13. REVEAL: 2,
  14. SELECT_CARDS: 0
  15. };
  16. class PlaceMove extends GameMove {
  17. constructor(card, position, player) {
  18. super(GameMove.TYPES.PLACE, {});
  19. this.position = position;
  20. this.card = card;
  21. this.player = player;
  22. }
  23. export() {
  24. return {
  25. type : GameMove.TYPES.PLACE,
  26. id : this.card.id,
  27. salt : this.card.salt,
  28. signature : this.player,
  29. position : this.position
  30. };
  31. // Add signature!
  32. }
  33. getPlayer() { return this.player; }
  34. verify(state) {
  35. if (!state.board.isEmpty(this.position)) {
  36. throw Error('Not a valid move, there is already a card there!');
  37. }
  38. // Verify the hash of the card to be sure it was there from the beginning
  39. return true;
  40. }
  41. performMove(board) {
  42. if(!board.isEmpty(this.position)) {
  43. console.log(this.position);
  44. console.log('Board');
  45. console.log(board);
  46. let err = { msg: 'Tried to place a card on occupied holder!' };
  47. throw err;
  48. }
  49. board.putCard(this.card, this.position, this.player);
  50. }
  51. }
  52. class RevealMove extends GameMove {
  53. constructor(card, player) {
  54. super(GameMove.TYPES.REVEAL, {});
  55. this.card = card;
  56. this.player = player;
  57. }
  58. export() {
  59. return {
  60. type : GameMove.TYPES.REVEAL,
  61. id : this.card.id,
  62. salt : this.card.salt,
  63. signature : this.player
  64. };
  65. }
  66. getPlayer() { return this.player; }
  67. verify(state) {
  68. if (state.stacks.hashes.length !== 9) {
  69. console.log(state);
  70. throw Error('Not a valid move, Need to play all cards to reveal!');
  71. }
  72. // TODO - Verify the last card from hashes
  73. return true;
  74. }
  75. performMove() {
  76. console.log('Reveal move processed');
  77. }
  78. }
  79. function Factory(move) {
  80. let type = move.type;
  81. switch(type) {
  82. case GameMove.TYPES.PLACE:
  83. return new PlaceMove(new Card(move.id, move.salt), move.position, move.signature);
  84. case GameMove.TYPES.REVEAL:
  85. return new RevealMove(new Card(move.id, move.salt), move.signature);
  86. default: return {};
  87. }
  88. }
  89. module.exports = {
  90. PlaceMove,
  91. RevealMove,
  92. Factory,
  93. TYPES: GameMove.TYPES
  94. };