Skip to content

Commit

Permalink
Player serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
vmolero committed Mar 15, 2018
1 parent 3cb6437 commit 80628d3
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Player.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
import Hand from './Hand';
import Hand, { HandJsonInterface } from './Hand';
import Card from './Card';

interface PlayerJsonInterface {
hand: HandJsonInterface,
}

export default class Player {
private hand: Hand;

public static fromJSON(json: PlayerJsonInterface | string): Player {
if (typeof json === 'string') {
return JSON.parse(json, Player.reviver);
}
return new this(Hand.fromJSON(json.hand));
}

public static reviver(key: string, value: PlayerJsonInterface): Player | PlayerJsonInterface {
return key === '' ? this.fromJSON(value) : value;
}

public toJSON(): PlayerJsonInterface {
return {
hand: this.hand.toJSON(),
};
}

public constructor(hand: Hand) {
this.hand = hand;
}
Expand Down

0 comments on commit 80628d3

Please sign in to comment.