Skip to content

Commit

Permalink
wip: teams, clubs, games
Browse files Browse the repository at this point in the history
  • Loading branch information
czosel committed Apr 22, 2024
1 parent 82a028f commit 747e7fd
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 18 deletions.
76 changes: 59 additions & 17 deletions server/lib/scraper.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ const asChunks = (games) => {
return chunks;
};

const getClubId = (clubHref) =>
clubHref && Number(parse(clubHref, true).query.club);
const getClubId = (href) => href && Number(parse(href, true).query.club);

const getTeamId = (href) => href && Number(parse(href, true).query.teamtable);

const getGameId = (href) => href && Number(parse(href, true).query.meeting);

const parsePromotion = (promotion) => {
return {
Expand Down Expand Up @@ -188,7 +191,7 @@ function league({ url }) {
}),
})
.error(error("scraping error in /league, continuing anyway"))
.data((data) => {
.data(async (data) => {
const titleParts = splitTitle(data.title);
const games = toArray(data.games)
.map(simplifyLinks)
Expand All @@ -215,24 +218,48 @@ function league({ url }) {
};
});

function findClubByName(clubs, search) {
return clubs.find(({ name }) => name === search);
}

const clubs = toArray(data.clubs)
.map((club) => ({
...club,
score: club.score.startsWith("zurückgezogen") ? "-:-" : club.score,
promotion: parsePromotion(club.promotion),
games: club.games || "",
balance: club.balance || "",
}))
.map(simplifyLinks);

res({
assoc: titleParts[0],
league: titleParts[1],
title: data.title,
breadcrumbs: extractBreadcrumbs(data),
chunks: asChunks(games),
clubs: toArray(data.clubs)
.map((club) => ({
...club,
score: club.score.startsWith("zurückgezogen")
? "-:-"
: club.score,
promotion: parsePromotion(club.promotion),
games: club.games || "",
balance: club.balance || "",
}))
.map(simplifyLinks),
clubs,
});

Promise.allSettled(
games.map(async (game) => {
const id = getGameId(game.href);
debugger;
const homeId = getTeamId(findClubByName(clubs, game.home).href);
const guestId = getTeamId(findClubByName(clubs, game.guest).href);
const data = {
homeId,
guestId,
result: game.result,
date: new Date(),
};
await prisma.game.upsert({
where: { id },
update: data,
create: { id, ...data },
});
})
);
});
});
}
Expand Down Expand Up @@ -383,7 +410,7 @@ function team({ url, format }, expressRes) {
})
)
.error(error("scraping error in /team, continuing anyway"))
.data((data) => {
.data(async (data) => {
if (format === "ics") {
const cal = ical({ domain: "tt-mobile.ch", name: data.club });
const league = `(${extractBreadcrumbs(data)[1]?.name || ""})`;
Expand Down Expand Up @@ -419,14 +446,29 @@ function team({ url, format }, expressRes) {
opponent: game.guest.includes(data.club) ? game.home : game.guest,
isHome: !game.guest.includes(data.club),
}));

const id = getTeamId(url);
const name = splitTitle(data.title)[2];
const clubId = getClubId(data.clubHref);

try {
await prisma.team.upsert({
where: { id },
update: { name, clubId },
create: { id, name, clubId },
});
} catch (e) {
console.log("club", clubId, "doesn't exist yet...");
}

res({
...data,
games,
locations: extractLocations(data.locations),
league: splitTitle(data.title)[1],
breadcrumbs: extractBreadcrumbs(data),
name: splitTitle(data.title)[2],
clubId: getClubId(data.clubHref),
name,
clubId,
players: toArray(data.players).map(simplifyLinks),
});
});
Expand Down
11 changes: 11 additions & 0 deletions server/prisma/migrations/20240422174440_team/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- CreateTable
CREATE TABLE "team" (
"id" INTEGER NOT NULL,
"name" TEXT NOT NULL,
"clubId" INTEGER NOT NULL,

CONSTRAINT "team_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "team" ADD CONSTRAINT "team_clubId_fkey" FOREIGN KEY ("clubId") REFERENCES "club"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
16 changes: 16 additions & 0 deletions server/prisma/migrations/20240422181012_game/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- CreateTable
CREATE TABLE "game" (
"id" INTEGER NOT NULL,
"homeId" INTEGER NOT NULL,
"guestId" INTEGER NOT NULL,
"result" TEXT NOT NULL,
"date" DATE NOT NULL,

CONSTRAINT "game_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "game" ADD CONSTRAINT "game_homeId_fkey" FOREIGN KEY ("homeId") REFERENCES "club"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "game" ADD CONSTRAINT "game_guestId_fkey" FOREIGN KEY ("guestId") REFERENCES "club"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
11 changes: 11 additions & 0 deletions server/prisma/migrations/20240422185303_game_rel/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- DropForeignKey
ALTER TABLE "game" DROP CONSTRAINT "game_guestId_fkey";

-- DropForeignKey
ALTER TABLE "game" DROP CONSTRAINT "game_homeId_fkey";

-- AddForeignKey
ALTER TABLE "game" ADD CONSTRAINT "game_homeId_fkey" FOREIGN KEY ("homeId") REFERENCES "team"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "game" ADD CONSTRAINT "game_guestId_fkey" FOREIGN KEY ("guestId") REFERENCES "team"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
28 changes: 27 additions & 1 deletion server/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
generator client {
provider = "prisma-client-js"
provider = "prisma-client-js"
previewFeatures = ["relationJoins"]
}

Expand All @@ -13,14 +13,40 @@ model Club {
name String
logo String?
players Player[]
teams Team[]
@@map("club")
}

model Team {
id Int @id
name String
clubId Int
club Club @relation(fields: [clubId], references: [id])
homeGames Game[] @relation("HomeGames")
guestGames Game[] @relation("GuestGames")
@@map("team")
}

model Player {
id Int @id
firstName String
lastName String
clubId Int?
club Club? @relation(fields: [clubId], references: [id])
@@map("player")
}

model Game {
id Int @id
homeId Int
guestId Int
result String
date DateTime @db.Date
home Team @relation("HomeGames", fields: [homeId], references: [id])
guest Team @relation("GuestGames", fields: [guestId], references: [id])
@@map("game")
}

0 comments on commit 747e7fd

Please sign in to comment.