Skip to content

Commit 6a786a9

Browse files
author
Roman Vaivod
committed
Add score in matches
1 parent 9e56d77 commit 6a786a9

13 files changed

Lines changed: 274 additions & 228 deletions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* eslint-disable @typescript-eslint/camelcase */
2+
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate'
3+
4+
export const shorthands: ColumnDefinitions | undefined = undefined
5+
6+
export async function up(pgm: MigrationBuilder): Promise<void> {
7+
pgm.addColumns('Matches', {
8+
Team1Score: { type: 'integer' },
9+
Team2Score: { type: 'integer' },
10+
})
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
UPDATE "Matches"
2+
SET "Team1Score" = '1', "Team2Score" = '0'
3+
WHERE "Team1Won" = 'true';
4+
5+
UPDATE "Matches"
6+
SET "Team1Score" = '0', "Team2Score" = '1'
7+
WHERE "Team1Won" = 'false';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-disable @typescript-eslint/camelcase */
2+
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate'
3+
4+
export const shorthands: ColumnDefinitions | undefined = undefined
5+
6+
export async function up(pgm: MigrationBuilder): Promise<void> {
7+
pgm.dropColumns('Matches', ['Team1Won'])
8+
pgm.alterColumn('Matches', 'Team1Score', {
9+
notNull: true,
10+
})
11+
pgm.alterColumn('Matches', 'Team2Score', {
12+
notNull: true,
13+
})
14+
}

backend/db_test/test_data.sql

Lines changed: 176 additions & 176 deletions
Large diffs are not rendered by default.

backend/repositories/MatchRepository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Promise<Match> => {
4141
return new Match(
4242
team1,
4343
team2,
44-
matchDescription.team1Won,
44+
matchDescription.team1Won? { team1Score: 1, team2Score: 0 } : { team1Score: 0, team2Score: 1 },
4545
date,
4646
winningTeamRatingChange,
4747
losingTeamRatingChange,

backend/storage/StorageContext.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ describe('StorageContext', () => {
272272
FOOSBALL_MATCH.date,
273273
FOOSBALL_MATCH.winningTeamRatingChange,
274274
FOOSBALL_MATCH.losingTeamRatingChange,
275-
FOOSBALL_MATCH.team1Won,
276275
FOOSBALL_MATCH.gameId,
276+
FOOSBALL_MATCH.score.team1Score,
277+
FOOSBALL_MATCH.score.team2Score,
277278
])
278279
})
279280
it('returns match with id', () => {

backend/storage/StorageContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ export class StorageContext {
180180
const query = dbQueries.insertMatch
181181
const values = [team1Player1.id, team1Player1.rating, team1Player2.id, team1Player2.rating,
182182
team2Player1.id, team2Player1.rating, team2Player2.id, team2Player2.rating,
183-
match.date, match.winningTeamRatingChange, match.losingTeamRatingChange, match.team1Won,
184-
match.gameId]
183+
match.date, match.winningTeamRatingChange, match.losingTeamRatingChange,
184+
match.gameId, match.score.team1Score, match.score.team2Score]
185185

186186
let row
187187
try {

backend/storage/db/db-queries.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ export const insertMatch = oneLine`
1717
INSERT INTO "Matches"(
1818
"Team1Player1Id", "Team1Player1Rating", "Team1Player2Id", "Team1Player2Rating",
1919
"Team2Player1Id", "Team2Player1Rating", "Team2Player2Id", "Team2Player2Rating",
20-
"Date", "WinningTeamRatingChange", "LosingTeamRatingChange", "Team1Won", "GameId"
20+
"Date", "WinningTeamRatingChange", "LosingTeamRatingChange", "GameId",
21+
"Team1Score", "Team2Score"
2122
)
22-
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) RETURNING *
23+
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) RETURNING *
2324
`
2425

2526
export const selectAllMatches = 'SELECT * FROM "Matches"'

backend/storage/db/db-transformations.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,27 @@ export const createMatchFromDbRow = (matchRow: QueryResultRow): MatchWithId => {
4848
matchRating: Number(matchRow.Team2Player2Rating),
4949
}] : []
5050

51-
return {
52-
id: Number(matchRow.Id),
53-
team1: [
51+
return new MatchWithId(
52+
Number(matchRow.Id),
53+
[
5454
{
5555
id: Number(matchRow.Team1Player1Id),
5656
matchRating: Number(matchRow.Team1Player1Rating),
5757
},
5858
...team1Player2Array,
5959
],
60-
team2: [
60+
[
6161
{
6262
id: Number(matchRow.Team2Player1Id),
6363
matchRating: Number(matchRow.Team2Player1Rating),
6464
},
6565
...team2Player2Array,
6666
],
67-
date: matchRow.Date,
68-
winningTeamRatingChange: Number(matchRow.WinningTeamRatingChange),
69-
losingTeamRatingChange: Number(matchRow.LosingTeamRatingChange),
70-
team1Won: Boolean(matchRow.Team1Won),
71-
}
67+
matchRow.Team1Score > matchRow.Team2Score,
68+
matchRow.Date,
69+
Number(matchRow.WinningTeamRatingChange),
70+
Number(matchRow.LosingTeamRatingChange),
71+
)
7272
}
7373

7474
export const createGameFromDbRow = (gameRow: QueryResultRow): Game => {

backend/tests/TestData.ts

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { MatchWithId, Match } from '../types/Match'
22
import { Player } from '../types/Player'
33
import { MatchDescription } from '../types/MatchDescription'
44
import * as moment from 'moment'
5-
import { UserRow } from '../types/Database'
5+
import { UserRow, MatchRow } from '../types/Database'
66

77
const NOW_MOMENT = moment('2020-03-25 10:00:00')
88
export const NOW = NOW_MOMENT.toDate()
@@ -25,21 +25,22 @@ export const FOOSBALL_GAME = {
2525
...FOOSBALL_DATA,
2626
}
2727

28-
export const FOOSBALL_MATCH_ROW = {
28+
export const FOOSBALL_MATCH_ROW: MatchRow = {
2929
Id: '2',
30-
Team1Player1Id: '1',
31-
Team1Player1Rating: '1001',
32-
Team1Player2Id: '2',
33-
Team1Player2Rating: '1002',
34-
Team2Player1Id: '3',
35-
Team2Player1Rating: '1003',
36-
Team2Player2Id: '4',
37-
Team2Player2Rating: '1004',
30+
Team1Player1Id: 1,
31+
Team1Player1Rating: 1001,
32+
Team1Player2Id: 2,
33+
Team1Player2Rating: 1002,
34+
Team2Player1Id: 3,
35+
Team2Player1Rating: 1003,
36+
Team2Player2Id: 4,
37+
Team2Player2Rating: 1004,
3838
Date: NOW,
39-
WinningTeamRatingChange: '16',
40-
LosingTeamRatingChange: '-16',
41-
Team1Won: 'true',
42-
GameId: '1',
39+
WinningTeamRatingChange: 16,
40+
LosingTeamRatingChange: -16,
41+
Team1Score: 1,
42+
Team2Score: 0,
43+
GameId: 1,
4344
}
4445

4546
export const FOOSBALL_MATCH_DESCRIPTION: MatchDescription = {
@@ -48,15 +49,15 @@ export const FOOSBALL_MATCH_DESCRIPTION: MatchDescription = {
4849
team1Won: true,
4950
}
5051

51-
export const FOOSBALL_MATCH_WITH_ID: MatchWithId = {
52-
id: 2,
53-
team1:[ { id: 1, matchRating: 1001 }, { id: 2, matchRating: 1002 }],
54-
team2:[ { id: 3, matchRating: 1003 }, { id: 4, matchRating: 1004 }],
55-
team1Won: true,
56-
date: NOW,
57-
winningTeamRatingChange: 16,
58-
losingTeamRatingChange: -16,
59-
}
52+
export const FOOSBALL_MATCH_WITH_ID = new MatchWithId(
53+
2,
54+
[ { id: 1, matchRating: 1001 }, { id: 2, matchRating: 1002 }],
55+
[ { id: 3, matchRating: 1003 }, { id: 4, matchRating: 1004 }],
56+
true,
57+
NOW,
58+
16,
59+
-16,
60+
)
6061

6162
export const TONDA_PLAYER_ROW = {
6263
Id: '3',
@@ -106,15 +107,15 @@ export const PETR_PLAYER: Player = {
106107
initialRating: 1200,
107108
}
108109

109-
export const FOOSBALL_MATCH: Match = {
110-
team1: [ TONDA_PLAYER ],
111-
team2: [ RADEK_PLAYER, PETR_PLAYER],
112-
team1Won: true,
113-
date: FOOSBALL_MATCH_ROW.Date,
114-
winningTeamRatingChange: 16,
115-
losingTeamRatingChange: -16,
116-
gameId: 1,
117-
}
110+
export const FOOSBALL_MATCH = new Match(
111+
[ TONDA_PLAYER ],
112+
[ RADEK_PLAYER, PETR_PLAYER],
113+
{ team1Score: 1, team2Score: 0 },
114+
FOOSBALL_MATCH_ROW.Date,
115+
16,
116+
-16,
117+
1,
118+
)
118119

119120
export const TONDA_USER_ROW: UserRow = {
120121
Id: 4,

0 commit comments

Comments
 (0)