From 7d9b331133b8d8ec2ab7c788d5ae9bb263090b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Molero?= Date: Thu, 15 Mar 2018 15:57:29 +0100 Subject: [PATCH] #4 full serialization and local storage to save full state --- src/Board.ts | 70 +++++++++++++++++++++++++----- src/Deal.ts | 10 ++--- src/Hand.ts | 2 +- src/Player.ts | 4 +- src/components/BoardComponent.tsx | 5 ++- src/components/SimpleBlackJack.tsx | 4 ++ src/tests/Board.test.ts | 7 +++ src/tests/Deal.test.ts | 4 +- 8 files changed, 82 insertions(+), 24 deletions(-) create mode 100644 src/tests/Board.test.ts diff --git a/src/Board.ts b/src/Board.ts index a85230d..e13afb3 100644 --- a/src/Board.ts +++ b/src/Board.ts @@ -1,19 +1,50 @@ -import Player from './Player'; -import Deal from './Deal'; +import Player, { PlayerJsonInterface } from './Player'; +import Deal, { DealJsonInterface } from './Deal'; import House from './House'; import Hand from './Hand'; import Card from './Card'; import CardDeck from './CardDeck'; +export interface BoardJsonInterface { + deal: DealJsonInterface; + house: PlayerJsonInterface; + player1: PlayerJsonInterface; +} + export default class Board { + private static playerWins: number = 0; + private static houseWins: number = 0; private readonly SCORE_THRESHOLD: number = 21; private deal: Deal; - private playerWins: number = 0; - private houseWins: number = 0; + private house: House; private player1: Player; + public static get PlayerWins(): number { + return this.playerWins; + } + + public static get HouseWins(): number { + return this.houseWins; + } + + public static set PlayerWins(wins: number) { + this.playerWins = wins; + } + + public static set HouseWins(wins: number) { + this.houseWins = wins; + } + + public static sumPlayerWins(): number { + return ++this.playerWins; + } + + public static sumHouseWins(): number { + return ++this.houseWins; + } + public static newGame(): Board { let deal: Deal = new Deal(CardDeck.createStandard52CardDeck()); let playerHand: Hand = new Hand([]); @@ -24,6 +55,29 @@ export default class Board { return new Board(deal, house, player1); } + public static fromJSON(json: BoardJsonInterface | string): Board { + if (typeof json === 'string') { + return JSON.parse(json, Board.reviver); + } + return new Board( + Deal.fromJSON(json.deal), + House.fromJSON(json.house), + Player.fromJSON(json.player1) + ); + } + + public static reviver(key: string, value: BoardJsonInterface): Board | BoardJsonInterface { + return key === '' ? Board.fromJSON(value) : value; + } + + public toJSON(): BoardJsonInterface { + return { + deal: this.deal.toJSON(), + house: this.house.toJSON(), + player1: this.player1.toJSON() + }; + } + public newDeal(): Board { let deal: Deal = new Deal(CardDeck.createStandard52CardDeck()); let playerHand: Hand = new Hand([deal.pullCard()]); @@ -57,14 +111,6 @@ export default class Board { return this.deal; } - public getPlayerWins(): number { - return this.playerWins; - } - - public getHouseWins(): number { - return this.houseWins; - } - public getHouse(): House { return this.house; } diff --git a/src/Deal.ts b/src/Deal.ts index a68b47c..4eb0f0a 100644 --- a/src/Deal.ts +++ b/src/Deal.ts @@ -2,16 +2,12 @@ import Card from './Card'; import CardDeck, { CardDeckJsonInterface } from './CardDeck'; export interface DealJsonInterface { - cardDeck: CardDeckJsonInterface + cardDeck: CardDeckJsonInterface; } export default class Deal { private cardDeck: CardDeck; - public constructor(cardDeck?: CardDeck) { - this.cardDeck = cardDeck || CardDeck.createStandard52CardDeck(); - } - public static fromJSON(json: DealJsonInterface | string): Deal { if (typeof json === 'string') { return JSON.parse(json, Deal.reviver); @@ -29,6 +25,10 @@ export default class Deal { }; } + public constructor(cardDeck?: CardDeck) { + this.cardDeck = cardDeck || CardDeck.createStandard52CardDeck(); + } + public pullCard(): Card { try { return this.cardDeck.popCard(); diff --git a/src/Hand.ts b/src/Hand.ts index 7d30867..bec28e3 100644 --- a/src/Hand.ts +++ b/src/Hand.ts @@ -1,7 +1,7 @@ import Card, { CardJsonInterface } from './Card'; export interface HandJsonInterface { - cards: Array, + cards: Array; } export default class Hand { diff --git a/src/Player.ts b/src/Player.ts index 5e766b9..ff028b5 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -1,8 +1,8 @@ import Hand, { HandJsonInterface } from './Hand'; import Card from './Card'; -interface PlayerJsonInterface { - hand: HandJsonInterface, +export interface PlayerJsonInterface { + hand: HandJsonInterface; } export default class Player { diff --git a/src/components/BoardComponent.tsx b/src/components/BoardComponent.tsx index 0e1594c..ecf12bd 100644 --- a/src/components/BoardComponent.tsx +++ b/src/components/BoardComponent.tsx @@ -1,6 +1,6 @@ import * as React from 'react'; import DealComponent from './DealComponent'; -import Board from '../Board'; +import Board, { BoardJsonInterface } from '../Board'; export interface BoardStateInterface { board: Board; @@ -20,6 +20,7 @@ interface BoardPropsInterface { } export interface JsonStateInterface { + board: BoardJsonInterface; handNumber: number; playerScore: number; houseScore: number; @@ -42,7 +43,7 @@ export class BoardComponent extends React.Component { return JSON.parse(jsonState); } } + return { + board: Board.newGame().toJSON(), handNumber: 0, playerScore: 0, houseScore: 0, @@ -30,6 +33,7 @@ class SimpleBlackJack extends React.Component { saveGame(state: BoardStateInterface) { let jsonSate: JsonStateInterface = { + board: state.board.toJSON(), handNumber: state.handNumber, playerScore: state.playerScore, houseScore: state.houseScore, diff --git a/src/tests/Board.test.ts b/src/tests/Board.test.ts new file mode 100644 index 0000000..5201c03 --- /dev/null +++ b/src/tests/Board.test.ts @@ -0,0 +1,7 @@ +import Board from './../Board'; + +test('A Board is serialized', () => { + const board: Board = Board.newGame(); + const jsonBoard: string = JSON.stringify(board); + return expect(JSON.parse(jsonBoard, Board.reviver)).toEqual(board); +}); \ No newline at end of file diff --git a/src/tests/Deal.test.ts b/src/tests/Deal.test.ts index ad3331a..3c16a1d 100644 --- a/src/tests/Deal.test.ts +++ b/src/tests/Deal.test.ts @@ -1,5 +1,5 @@ -import Deal from "./../Deal"; -import CardDeck from "./../CardDeck"; +import Deal from './../Deal'; +import CardDeck from './../CardDeck'; test('A deal is serialized', () => { const deal: Deal = new Deal(CardDeck.createStandard52CardDeck());