Skip to content

Commit

Permalink
Merge pull request #997 from notaphplover/feat/update-game-db-entitie…
Browse files Browse the repository at this point in the history
…s-with-game-history-ones

Update game db entities with game history ones
  • Loading branch information
notaphplover authored Feb 24, 2024
2 parents 493e197 + 9cbedc8 commit 784d3a5
Show file tree
Hide file tree
Showing 28 changed files with 423 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ components:
paths:
/v1/auth:
post:
deprecated: true
summary: Create an authorization token
operationId: createAuth
requestBody:
Expand Down Expand Up @@ -629,6 +630,18 @@ paths:
requestBody:
content:
application/json:
examples:
authByCode:
summary: User code
value:
code: 0077df18851946fb67e552b83f34d58283548c38c4c5f144e6b655280b773528
kind: code
authByLogin:
summary: User credentials
value:
email: [email protected]
kind: login
password: sample-password
schema:
$ref: 'https://onegame.schemas/api/v2/auth/auth-create-query.json'
required: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddGameHistory1708646400000 implements MigrationInterface {
public readonly name: string = 'AddGameHistory1708646400000';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "GameAction" ("id" character varying(36) NOT NULL, "payload" json NOT NULL, "position" smallint NOT NULL, "turn" smallint NOT NULL, "game_id" character varying(36) NOT NULL, CONSTRAINT "PK_504414b3ee4d764e8df48118c0e" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE INDEX "IDX_030557ed95c9537ffb33536d49" ON "GameAction" ("game_id", "turn")`,
);
await queryRunner.query(
`CREATE INDEX "IDX_7ed74975e34bbce332c8b58525" ON "GameAction" ("game_id", "position")`,
);
await queryRunner.query(
`CREATE TABLE "GameInitialSnapshotSlot" ("cards" json NOT NULL, "id" character varying(36) NOT NULL, "position" smallint, "user_id" character varying(36) NOT NULL, "game_initial_snapshot_id" character varying(36) NOT NULL, CONSTRAINT "PK_ab9464398b172ed7ff9c00a397d" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "GameInitialSnapshot" ("current_card" bit(16) NOT NULL, "current_color" bit(16) NOT NULL, "current_direction" character varying(32) NOT NULL, "current_playing_slot" smallint NOT NULL, "deck" json NOT NULL, "draw_count" smallint NOT NULL, "id" character varying(36) NOT NULL, "game_id" character varying(36) NOT NULL, CONSTRAINT "PK_79de72d7a2e49d332bd7ad3ec41" PRIMARY KEY ("id"))`,
);
await queryRunner.query(`ALTER TABLE "Game" ADD "turn" smallint`);
await queryRunner.query(
`ALTER TABLE "GameAction" ADD CONSTRAINT "FK_4ccd5dc231685b49a5355edaa46" FOREIGN KEY ("game_id") REFERENCES "Game"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "GameInitialSnapshotSlot" ADD CONSTRAINT "FK_46af31c84986ba21bf4837025a5" FOREIGN KEY ("game_initial_snapshot_id") REFERENCES "GameInitialSnapshot"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "GameInitialSnapshot" ADD CONSTRAINT "FK_be95245a47a081f63be111e22b0" FOREIGN KEY ("game_id") REFERENCES "Game"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "GameInitialSnapshot" DROP CONSTRAINT "FK_be95245a47a081f63be111e22b0"`,
);
await queryRunner.query(
`ALTER TABLE "GameInitialSnapshotSlot" DROP CONSTRAINT "FK_46af31c84986ba21bf4837025a5"`,
);
await queryRunner.query(
`ALTER TABLE "GameAction" DROP CONSTRAINT "FK_4ccd5dc231685b49a5355edaa46"`,
);
await queryRunner.query(`ALTER TABLE "Game" DROP COLUMN "turn"`);
await queryRunner.query(`DROP TABLE "GameInitialSnapshot"`);
await queryRunner.query(`DROP TABLE "GameInitialSnapshotSlot"`);
await queryRunner.query(
`DROP INDEX "public"."IDX_7ed74975e34bbce332c8b58525"`,
);
await queryRunner.query(
`DROP INDEX "public"."IDX_030557ed95c9537ffb33536d49"`,
);
await queryRunner.query(`DROP TABLE "GameAction"`);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { GameActionDb } from '../../../../../games/adapter/typeorm/models/GameActionDb';
import { GameDb } from '../../../../../games/adapter/typeorm/models/GameDb';
import { GameInitialSnapshotDb } from '../../../../../games/adapter/typeorm/models/GameInitialSnapshotDb';
import { GameInitialSnapshotSlotDb } from '../../../../../games/adapter/typeorm/models/GameInitialSnapshotSlotDb';
import { GameSlotDb } from '../../../../../games/adapter/typeorm/models/GameSlotDb';
import { GameSpecDb } from '../../../../../games/adapter/typeorm/models/GameSpecDb';

// eslint-disable-next-line @typescript-eslint/ban-types
export const typeOrmEntities: Function[] = [GameDb, GameSlotDb, GameSpecDb];
export const typeOrmEntities: Function[] = [
GameActionDb,
GameDb,
GameInitialSnapshotDb,
GameInitialSnapshotSlotDb,
GameSlotDb,
GameSpecDb,
];
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ describe(GameFromGameDbBuilder.name, () => {
drawCount: gameDbFixture.drawCount as number,
slots: [gameSlotFixture],
status: GameStatus.active,
turn: gameDbFixture.turn as number,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ export class GameFromGameDbBuilder implements Builder<Game, [GameDb]> {
gameDb.currentPlayingSlotIndex === null ||
gameDb.currentTurnCardsPlayed === null ||
gameDb.deck === null ||
gameDb.drawCount === null
gameDb.drawCount === null ||
gameDb.turn === null
) {
throw new AppError(AppErrorKind.unknown, 'Unexpected card spec db entry');
}
Expand All @@ -146,6 +147,7 @@ export class GameFromGameDbBuilder implements Builder<Game, [GameDb]> {
drawCount: gameDb.drawCount,
slots: gameSlots,
status: GameStatus.active,
turn: gameDb.turn,
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,5 +384,37 @@ describe(GameSetQueryTypeOrmFromGameUpdateQueryBuilder.name, () => {
});
});
});

describe('having a GameUpdateQuery with turn', () => {
let gameUpdateQueryFixture: GameUpdateQuery;

beforeAll(() => {
gameUpdateQueryFixture = GameUpdateQueryFixtures.withTurn;
});

describe('when called', () => {
let result: unknown;

beforeAll(() => {
result = gameSetQueryTypeOrmFromGameUpdateQueryBuilder.build(
gameUpdateQueryFixture,
);
});

afterAll(() => {
jest.clearAllMocks();
});

it('should return a QueryDeepPartialEntity<GameDb>', () => {
const expectedProperties: Partial<QueryDeepPartialEntity<GameDb>> = {
turn: gameUpdateQueryFixture.turn as number,
};

expect(result).toStrictEqual(
expect.objectContaining(expectedProperties),
);
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export class GameSetQueryTypeOrmFromGameUpdateQueryBuilder
gameSetQueryTypeOrm.drawCount = gameUpdateQuery.drawCount;
}

if (gameUpdateQuery.turn !== undefined) {
gameSetQueryTypeOrm.turn = gameUpdateQuery.turn;
}

return gameSetQueryTypeOrm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class GameDbFixtures {
fixture.gameSpecDb = GameSpecDbFixtures.any;
fixture.id = '6fbcdb6c-b03c-4754-94c1-9f664f036cde';
fixture.status = GameStatusDb.active;
fixture.turn = 1;

return fixture;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
Column,
Entity,
Index,
JoinColumn,
ManyToOne,
PrimaryColumn,
RelationId,
} from 'typeorm';

import { GameDb } from './GameDb';

@Entity({
name: 'GameAction',
})
@Index(['game', 'position'])
@Index(['game', 'turn'])
export class GameActionDb {
@ManyToOne(() => GameDb, undefined, { nullable: false })
@JoinColumn({ name: 'game_id' })
public readonly game!: GameDb;

@RelationId((gameAction: GameActionDb) => gameAction.game)
public readonly gameId!: string;

@PrimaryColumn({
length: 36,
name: 'id',
type: 'varchar',
})
public readonly id!: string;

@Column({
name: 'payload',
nullable: false,
type: 'json',
})
public readonly payload!: string;

@Column({
name: 'position',
nullable: false,
type: 'smallint',
})
public readonly position!: number;

@Column({
name: 'turn',
nullable: false,
type: 'smallint',
})
public readonly turn!: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum GameActionKindDb {
passTurn = 'passTurn',
playCards = 'playCards',
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class GameDb {
type: 'smallint',
width: 4,
})
public drawCount!: number | null;
public readonly drawCount!: number | null;

@PrimaryColumn({
length: 36,
Expand Down Expand Up @@ -122,4 +122,11 @@ export class GameDb {
width: 2,
})
public readonly status!: GameStatusDb;

@Column({
name: 'turn',
nullable: true,
type: 'smallint',
})
public readonly turn!: number | null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
PrimaryColumn,
RelationId,
} from 'typeorm';

import { BinaryToNumberTransformer } from '../../../../foundation/db/adapter/typeorm/transformers/BinaryToNumberTransformer';
import { GameDb } from './GameDb';
import { GameDirectionDb } from './GameDirectionDb';
import { GameInitialSnapshotSlotDb } from './GameInitialSnapshotSlotDb';

@Entity({
name: 'GameInitialSnapshot',
})
export class GameInitialSnapshotDb {
@Column({
length: 16,
name: 'current_card',
nullable: false,
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
transformer: new BinaryToNumberTransformer(16),
type: 'bit',
})
public readonly currentCard!: number;

@Column({
length: 16,
name: 'current_color',
nullable: false,
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
transformer: new BinaryToNumberTransformer(16),
type: 'bit',
})
public readonly currentColor!: number;

@Column({
length: 32,
name: 'current_direction',
nullable: false,
type: 'varchar',
})
public readonly currentDirection!: GameDirectionDb;

@Column({
name: 'current_playing_slot',
nullable: false,
type: 'smallint',
})
public readonly currentPlayingSlotIndex!: number;

@Column({
name: 'deck',
nullable: false,
type: 'json',
})
public readonly deck!: string;

@Column({
name: 'draw_count',
nullable: false,
type: 'smallint',
width: 4,
})
public readonly drawCount!: number;

@ManyToOne(() => GameDb, undefined, { nullable: false })
@JoinColumn({ name: 'game_id' })
public readonly game!: GameDb;

@RelationId(
(gameInitialSnapshot: GameInitialSnapshotDb) => gameInitialSnapshot.game,
)
public readonly gameId!: string;

@OneToMany(
() => GameInitialSnapshotSlotDb,
(gameSlotDb: GameInitialSnapshotSlotDb): GameInitialSnapshotDb =>
gameSlotDb.gameInitialSnapshot,
{
eager: true,
},
)
public readonly gameSlotsDb!: GameInitialSnapshotSlotDb[];

@PrimaryColumn({
length: 36,
name: 'id',
type: 'varchar',
})
public readonly id!: string;
}
Loading

0 comments on commit 784d3a5

Please sign in to comment.