GameMoves.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. class GameMove {
  2. constructor(type, data) {
  3. this.type = type;
  4. this.data = data;
  5. }
  6. verify() {}
  7. performMove() {}
  8. }
  9. GameMove.TYPES = {
  10. PLACE: 1,
  11. REVEAL: 2,
  12. SELECT_CARDS: 0
  13. };
  14. class PlaceMove extends GameMove {
  15. constructor(card, position, player) {
  16. super(GameMove.TYPES.PLACE, {});
  17. this.position = position;
  18. this.card = card;
  19. this.player = player;
  20. }
  21. verify(state) {
  22. if (!state.board.isEmpty(this.position)) {
  23. throw Error('Not a valid move, there is already a card there!');
  24. }
  25. // Verify the hash of the card to be sure it was there from the beginning
  26. return true;
  27. }
  28. performMove(board) {
  29. if(!board.isEmpty(this.position)) {
  30. console.log(this.position);
  31. console.log('Board');
  32. console.log(board);
  33. let err = { msg: 'Tried to place a card on occupied holder!' };
  34. throw err;
  35. }
  36. board.putCard(this.card, this.position, this.player);
  37. }
  38. }
  39. class RevealMove extends GameMove {
  40. constructor(card, player) {
  41. super(GameMove.TYPES.REVEAL, {});
  42. this.card = card;
  43. this.player = player;
  44. }
  45. verify(state) {
  46. if (state.stack.length != 9) {
  47. throw Error('Not a valid move, Need to play all cards to reveal!');
  48. }
  49. // TODO - Verify the last card from hashes
  50. return true;
  51. }
  52. performMove() {
  53. console.log('Reveal move processed');
  54. }
  55. }
  56. module.exports = {
  57. PlaceMove,
  58. RevealMove
  59. };