Skip to content

Commit

Permalink
Add score columns
Browse files Browse the repository at this point in the history
  • Loading branch information
littlewhywhat committed May 14, 2020
1 parent 9e56d77 commit e418ae0
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 53 deletions.
11 changes: 11 additions & 0 deletions backend/db_migrations/1589491059800_add-score-columns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable @typescript-eslint/camelcase */
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate'

export const shorthands: ColumnDefinitions | undefined = undefined

export async function up(pgm: MigrationBuilder): Promise<void> {
pgm.addColumns('Matches', {
Team1Score: { type: 'integer' },
Team2Score: { type: 'integer' },
})
}
7 changes: 7 additions & 0 deletions backend/db_migrations/1589492439775_set-basic-score.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
UPDATE "Matches"
SET "Team1Score" = '1', "Team2Score" = '0'
WHERE "Team1Won" === 'true';

UPDATE "Matches"
SET "Team1Score" = '0', "Team2Score" = '1'
WHERE "Team1Won" === 'false';
14 changes: 14 additions & 0 deletions backend/db_migrations/1589492662936_drop-team1won.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable @typescript-eslint/camelcase */
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate'

export const shorthands: ColumnDefinitions | undefined = undefined

export async function up(pgm: MigrationBuilder): Promise<void> {
pgm.dropColumns('Matches', ['Team1Won'])
pgm.alterColumn('Matches', 'Team1Score', {
notNull: true,
})
pgm.alterColumn('Matches', 'Team2Score', {
notNull: true,
})
}
2 changes: 1 addition & 1 deletion backend/repositories/MatchRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Promise<Match> => {
return new Match(
team1,
team2,
matchDescription.team1Won,
matchDescription.team1Won? { team1Score: 1, team2Score: 0 } : { team1Score: 0, team2Score: 1 },
date,
winningTeamRatingChange,
losingTeamRatingChange,
Expand Down
3 changes: 2 additions & 1 deletion backend/storage/StorageContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,9 @@ describe('StorageContext', () => {
FOOSBALL_MATCH.date,
FOOSBALL_MATCH.winningTeamRatingChange,
FOOSBALL_MATCH.losingTeamRatingChange,
FOOSBALL_MATCH.team1Won,
FOOSBALL_MATCH.gameId,
FOOSBALL_MATCH.score.team1Score,
FOOSBALL_MATCH.score.team2Score,
])
})
it('returns match with id', () => {
Expand Down
4 changes: 2 additions & 2 deletions backend/storage/StorageContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ export class StorageContext {
const query = dbQueries.insertMatch
const values = [team1Player1.id, team1Player1.rating, team1Player2.id, team1Player2.rating,
team2Player1.id, team2Player1.rating, team2Player2.id, team2Player2.rating,
match.date, match.winningTeamRatingChange, match.losingTeamRatingChange, match.team1Won,
match.gameId]
match.date, match.winningTeamRatingChange, match.losingTeamRatingChange,
match.gameId, match.score.team1Score, match.score.team2Score]

let row
try {
Expand Down
5 changes: 3 additions & 2 deletions backend/storage/db/db-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ export const insertMatch = oneLine`
INSERT INTO "Matches"(
"Team1Player1Id", "Team1Player1Rating", "Team1Player2Id", "Team1Player2Rating",
"Team2Player1Id", "Team2Player1Rating", "Team2Player2Id", "Team2Player2Rating",
"Date", "WinningTeamRatingChange", "LosingTeamRatingChange", "Team1Won", "GameId"
"Date", "WinningTeamRatingChange", "LosingTeamRatingChange", "GameId",
"Team1Score", "Team2Score"
)
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) RETURNING *
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) RETURNING *
`

export const selectAllMatches = 'SELECT * FROM "Matches"'
Expand Down
18 changes: 9 additions & 9 deletions backend/storage/db/db-transformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,27 @@ export const createMatchFromDbRow = (matchRow: QueryResultRow): MatchWithId => {
matchRating: Number(matchRow.Team2Player2Rating),
}] : []

return {
id: Number(matchRow.Id),
team1: [
return new MatchWithId(
Number(matchRow.Id),
[
{
id: Number(matchRow.Team1Player1Id),
matchRating: Number(matchRow.Team1Player1Rating),
},
...team1Player2Array,
],
team2: [
[
{
id: Number(matchRow.Team2Player1Id),
matchRating: Number(matchRow.Team2Player1Rating),
},
...team2Player2Array,
],
date: matchRow.Date,
winningTeamRatingChange: Number(matchRow.WinningTeamRatingChange),
losingTeamRatingChange: Number(matchRow.LosingTeamRatingChange),
team1Won: Boolean(matchRow.Team1Won),
}
{ team1Score: matchRow.Team1Score, team2Score: matchRow.Team2Score },
matchRow.Date,
Number(matchRow.WinningTeamRatingChange),
Number(matchRow.LosingTeamRatingChange),
)
}

export const createGameFromDbRow = (gameRow: QueryResultRow): Game => {
Expand Down
65 changes: 33 additions & 32 deletions backend/tests/TestData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MatchWithId, Match } from '../types/Match'
import { Player } from '../types/Player'
import { MatchDescription } from '../types/MatchDescription'
import * as moment from 'moment'
import { UserRow } from '../types/Database'
import { UserRow, MatchRow } from '../types/Database'

const NOW_MOMENT = moment('2020-03-25 10:00:00')
export const NOW = NOW_MOMENT.toDate()
Expand All @@ -25,21 +25,22 @@ export const FOOSBALL_GAME = {
...FOOSBALL_DATA,
}

export const FOOSBALL_MATCH_ROW = {
export const FOOSBALL_MATCH_ROW: MatchRow = {
Id: '2',
Team1Player1Id: '1',
Team1Player1Rating: '1001',
Team1Player2Id: '2',
Team1Player2Rating: '1002',
Team2Player1Id: '3',
Team2Player1Rating: '1003',
Team2Player2Id: '4',
Team2Player2Rating: '1004',
Team1Player1Id: 1,
Team1Player1Rating: 1001,
Team1Player2Id: 2,
Team1Player2Rating: 1002,
Team2Player1Id: 3,
Team2Player1Rating: 1003,
Team2Player2Id: 4,
Team2Player2Rating: 1004,
Date: NOW,
WinningTeamRatingChange: '16',
LosingTeamRatingChange: '-16',
Team1Won: 'true',
GameId: '1',
WinningTeamRatingChange: 16,
LosingTeamRatingChange: -16,
Team1Score: 1,
Team2Score: 0,
GameId: 1,
}

export const FOOSBALL_MATCH_DESCRIPTION: MatchDescription = {
Expand All @@ -48,15 +49,15 @@ export const FOOSBALL_MATCH_DESCRIPTION: MatchDescription = {
team1Won: true,
}

export const FOOSBALL_MATCH_WITH_ID: MatchWithId = {
id: 2,
team1:[ { id: 1, matchRating: 1001 }, { id: 2, matchRating: 1002 }],
team2:[ { id: 3, matchRating: 1003 }, { id: 4, matchRating: 1004 }],
team1Won: true,
date: NOW,
winningTeamRatingChange: 16,
losingTeamRatingChange: -16,
}
export const FOOSBALL_MATCH_WITH_ID = new MatchWithId(
2,
[ { id: 1, matchRating: 1001 }, { id: 2, matchRating: 1002 }],
[ { id: 3, matchRating: 1003 }, { id: 4, matchRating: 1004 }],
{ team1Score: 1, team2Score: 0 },
NOW,
16,
-16,
)

export const TONDA_PLAYER_ROW = {
Id: '3',
Expand Down Expand Up @@ -106,15 +107,15 @@ export const PETR_PLAYER: Player = {
initialRating: 1200,
}

export const FOOSBALL_MATCH: Match = {
team1: [ TONDA_PLAYER ],
team2: [ RADEK_PLAYER, PETR_PLAYER],
team1Won: true,
date: FOOSBALL_MATCH_ROW.Date,
winningTeamRatingChange: 16,
losingTeamRatingChange: -16,
gameId: 1,
}
export const FOOSBALL_MATCH = new Match(
[ TONDA_PLAYER ],
[ RADEK_PLAYER, PETR_PLAYER],
{ team1Score: 1, team2Score: 0 },
FOOSBALL_MATCH_ROW.Date,
16,
-16,
1,
)

export const TONDA_USER_ROW: UserRow = {
Id: 4,
Expand Down
6 changes: 4 additions & 2 deletions backend/types/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ export interface MatchRow {
Date: Date;
WinningTeamRatingChange: number;
LosingTeamRatingChange: number;
Team1Won: boolean;
GameId: number;
Team1Score: number;
Team2Score: number;
}

export const isValidMatchRow = (queryRow: QueryResultRow): queryRow is MatchRow => {
Expand All @@ -58,7 +59,8 @@ export const isValidMatchRow = (queryRow: QueryResultRow): queryRow is MatchRow
queryRow.Date &&
queryRow.WinningTeamRatingChange !== undefined &&
queryRow.LosingTeamRatingChange !== undefined &&
queryRow.Team1Won !== undefined
queryRow.Team1Score !== undefined &&
queryRow.Team2Score !== undefined
}

export interface GameRow {
Expand Down
4 changes: 2 additions & 2 deletions backend/types/Match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Match', () => {
expect(() => new Match(
data.team1,
data.team2,
data.team1Won,
data.score,
data.date,
data.winningTeamRatingChange,
data.losingTeamRatingChange,
Expand All @@ -27,7 +27,7 @@ describe('Match', () => {
expect(new Match(
FOOSBALL_MATCH.team1,
FOOSBALL_MATCH.team2,
FOOSBALL_MATCH.team1Won,
FOOSBALL_MATCH.score,
FOOSBALL_MATCH.date,
FOOSBALL_MATCH.winningTeamRatingChange,
FOOSBALL_MATCH.losingTeamRatingChange,
Expand Down
17 changes: 15 additions & 2 deletions backend/types/Match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { UserInMatches } from './User'
import { Player } from './Player'
import { InputError } from '../errors/InputError'

interface Score {
team1Score: number;
team2Score: number;
}

export class Match {
constructor(
readonly team1: Array<Player>,
readonly team2: Array<Player>,
readonly team1Won: boolean,
readonly score: Score,
readonly date: Date,
readonly winningTeamRatingChange: number,
readonly losingTeamRatingChange: number,
Expand All @@ -19,16 +24,24 @@ export class Match {
throw new InputError('Inserting teams with unsupported number of players')
}
}

get team1Won(): boolean {
return this.score.team1Score > this.score.team2Score
}
}

export class MatchWithId {
constructor(
readonly id: number,
readonly team1: UserInMatches[],
readonly team2: UserInMatches[],
readonly team1Won: boolean,
readonly score: Score,
readonly date: Date,
readonly winningTeamRatingChange: number,
readonly losingTeamRatingChange: number,
) {}

get team1Won(): boolean {
return this.score.team1Score > this.score.team2Score
}
}

0 comments on commit e418ae0

Please sign in to comment.