Skip to content

Commit

Permalink
#4 full serialization and local storage to save full state
Browse files Browse the repository at this point in the history
  • Loading branch information
vmolero committed Mar 15, 2018
1 parent 80628d3 commit 7d9b331
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 24 deletions.
70 changes: 58 additions & 12 deletions src/Board.ts
Original file line number Diff line number Diff line change
@@ -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([]);
Expand All @@ -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()]);
Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Deal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Hand.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Card, { CardJsonInterface } from './Card';

export interface HandJsonInterface {
cards: Array<CardJsonInterface>,
cards: Array<CardJsonInterface>;
}

export default class Hand {
Expand Down
4 changes: 2 additions & 2 deletions src/Player.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions src/components/BoardComponent.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,6 +20,7 @@ interface BoardPropsInterface {
}

export interface JsonStateInterface {
board: BoardJsonInterface;
handNumber: number;
playerScore: number;
houseScore: number;
Expand All @@ -42,7 +43,7 @@ export class BoardComponent extends React.Component<BoardPropsInterface, BoardSt

public fromSavedState(stateObject: JsonStateInterface): BoardStateInterface {
let state: BoardStateInterface = {
board: Board.newGame(),
board: Board.fromJSON(stateObject.board),
handNumber: stateObject.handNumber,
playerScore: stateObject.playerScore,
houseScore: stateObject.houseScore,
Expand Down
4 changes: 4 additions & 0 deletions src/components/SimpleBlackJack.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { BoardComponent, BoardStateInterface, JsonStateInterface } from './BoardComponent';
import Board from 'Board';

interface SimpleBlackJackPropsInterface {
localStorage: Storage | null;
Expand All @@ -21,7 +22,9 @@ class SimpleBlackJack extends React.Component<SimpleBlackJackPropsInterface> {
return JSON.parse(jsonState);
}
}

return {
board: Board.newGame().toJSON(),
handNumber: 0,
playerScore: 0,
houseScore: 0,
Expand All @@ -30,6 +33,7 @@ class SimpleBlackJack extends React.Component<SimpleBlackJackPropsInterface> {

saveGame(state: BoardStateInterface) {
let jsonSate: JsonStateInterface = {
board: state.board.toJSON(),
handNumber: state.handNumber,
playerScore: state.playerScore,
houseScore: state.houseScore,
Expand Down
7 changes: 7 additions & 0 deletions src/tests/Board.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
4 changes: 2 additions & 2 deletions src/tests/Deal.test.ts
Original file line number Diff line number Diff line change
@@ -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());
Expand Down

0 comments on commit 7d9b331

Please sign in to comment.