Skip to content

Commit

Permalink
Added move serialization to Game
Browse files Browse the repository at this point in the history
  • Loading branch information
Garry Passarella committed Jul 10, 2020
1 parent 5be3d60 commit 14c8300
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/Game.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,10 @@ describe("Game tests", () => {
});

it("Records card", () => {
expect(_game.moves[0].card.equals(_cards1[0])).to.be.true;
expect(_game.moves[1].card.equals(_cards2[0])).to.be.true;
expect(_game.moves[2].card.equals(_cards1[1])).to.be.true;
expect(_game.moves[3].card.equals(_cards2[1])).to.be.true;
expect(_game.moves[0].card?.equals(_cards1[0])).to.be.true;
expect(_game.moves[1].card?.equals(_cards2[0])).to.be.true;
expect(_game.moves[2].card?.equals(_cards1[1])).to.be.true;
expect(_game.moves[3].card?.equals(_cards2[1])).to.be.true;
});
});

Expand Down
8 changes: 6 additions & 2 deletions src/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class Game {
return this._moves;
}

private recordMove(card: Card, taken: Card[], player: Player, isScopa: boolean) {
private recordMove(card: Card | null, taken: Card[], player: Player, isScopa: boolean) {
this._moves.push(new MoveLogItem(card, taken, new Date().toISOString(), player, isScopa));
}

Expand Down Expand Up @@ -175,7 +175,11 @@ export class Game {
}

private giveTableToLastTaker() {
this._lastTaker?.capture(this._table.removeAll());
const tableCards = this._table.removeAll();
this._lastTaker?.capture(tableCards);
if(this._lastTaker) {
this.recordMove(null, tableCards, this._lastTaker?.player, false);
}
}

private updateScorecard() {
Expand Down
2 changes: 1 addition & 1 deletion src/dtos/IMoveLogItemDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ICardDto } from "./ICardDto"
import { IPlayerDto } from "./IPlayerDto";

export interface IMoveLogItemDto {
card: ICardDto;
card: ICardDto | null;
taken: ICardDto[];
timestamp: string;
player: IPlayerDto;
Expand Down
2 changes: 1 addition & 1 deletion src/models/MoveLogItem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe("MoveLogItem tests", () => {

it("serializes/deserializes cards", () =>
_moveLogItemAfter.forEach((_, i) =>
expect(_moveLogItemAfter[i].card.equals(_moveLogItem[i].card)).to.be.true));
expect(_moveLogItemAfter[i].card?.equals(_moveLogItem[i].card!)).to.be.true));

it("serializes/deserializes players", () =>
_moveLogItemAfter.forEach((_, i) =>
Expand Down
10 changes: 5 additions & 5 deletions src/models/MoveLogItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { Player } from "./Player";
import { IMoveLogItemDto } from "../dtos/IMoveLogItemDto";

export class MoveLogItem {
private _card: Card;
private _card: Card | null;
private _taken: Card[];
private _timestamp: string;
private _player: Player;
private _isScopa: boolean;

constructor(
card: Card,
card: Card | null,
taken: Card[],
timestamp: string,
player: Player,
Expand All @@ -23,7 +23,7 @@ export class MoveLogItem {
this._isScopa = isScopa;
}

get card(): Card { return this._card; }
get card(): Card | null { return this._card; }
get taken(): Card[] { return this._taken.slice(); }
get timestamp(): string { return this._timestamp; }
get player(): Player { return this._player; }
Expand All @@ -39,7 +39,7 @@ export class MoveLogItem {

static fromDto(dtoObj: IMoveLogItemDto): MoveLogItem {
return new MoveLogItem(
Card.fromDto(dtoObj.card),
dtoObj.card ? Card.fromDto(dtoObj.card) : null,
Card.fromDtoArray(dtoObj.taken),
dtoObj.timestamp,
Player.fromDto(dtoObj.player),
Expand All @@ -49,7 +49,7 @@ export class MoveLogItem {

static toDto(obj: MoveLogItem): IMoveLogItemDto {
return {
card: Card.toDto(obj._card),
card: obj._card ? Card.toDto(obj._card) : null,
isScopa: obj._isScopa,
player: Player.toDto(obj._player),
taken: Card.toDtoArray(obj._taken),
Expand Down

0 comments on commit 14c8300

Please sign in to comment.