Skip to content

Commit

Permalink
feat: search for extra pwhl player details
Browse files Browse the repository at this point in the history
  • Loading branch information
shayypy committed Feb 2, 2024
1 parent 1f25bb4 commit 0193777
Show file tree
Hide file tree
Showing 7 changed files with 436 additions and 7 deletions.
13 changes: 13 additions & 0 deletions packages/bot/migrations/0005_solid_eternals.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE `players` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`league` text NOT NULL,
`nativeId` text NOT NULL,
`epId` text,
`epSlug` text,
`fullName` text NOT NULL,
`country` text,
`height` integer,
`weight` integer
);
--> statement-breakpoint
CREATE UNIQUE INDEX `players_league_nativeId_unique` ON `players` (`league`,`nativeId`);
259 changes: 259 additions & 0 deletions packages/bot/migrations/meta/0005_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
{
"version": "5",
"dialect": "sqlite",
"id": "588e47bc-0b1f-46f9-b87f-812ae7b3a51a",
"prevId": "a1f008b6-47d5-4f55-8d68-ecff9171c93e",
"tables": {
"games": {
"name": "games",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"league": {
"name": "league",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"nativeId": {
"name": "nativeId",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"lastKnownAwayGoals": {
"name": "lastKnownAwayGoals",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 0
},
"lastKnownHomeGoals": {
"name": "lastKnownHomeGoals",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 0
},
"lastKnownPenaltyCount": {
"name": "lastKnownPenaltyCount",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 0
},
"lastKnownPeriodId": {
"name": "lastKnownPeriodId",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"lastPostedHypeMinutes": {
"name": "lastPostedHypeMinutes",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"postedPreview": {
"name": "postedPreview",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": false
},
"postedUnofficialFinal": {
"name": "postedUnofficialFinal",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": false
},
"postedFinal": {
"name": "postedFinal",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": false
}
},
"indexes": {
"games_league_nativeId_unique": {
"name": "games_league_nativeId_unique",
"columns": [
"league",
"nativeId"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"notifications": {
"name": "notifications",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"league": {
"name": "league",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"channelId": {
"name": "channelId",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"teamIds": {
"name": "teamIds",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": []
},
"send": {
"name": "send",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": {}
},
"active": {
"name": "active",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": true
}
},
"indexes": {
"notifications_league_channelId_unique": {
"name": "notifications_league_channelId_unique",
"columns": [
"league",
"channelId"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"players": {
"name": "players",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"league": {
"name": "league",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"nativeId": {
"name": "nativeId",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"epId": {
"name": "epId",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"epSlug": {
"name": "epSlug",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"fullName": {
"name": "fullName",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"country": {
"name": "country",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"height": {
"name": "height",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"weight": {
"name": "weight",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {
"players_league_nativeId_unique": {
"name": "players_league_nativeId_unique",
"columns": [
"league",
"nativeId"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
9 changes: 8 additions & 1 deletion packages/bot/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
"when": 1705777293932,
"tag": "0004_futuristic_pepper_potts",
"breakpoints": true
},
{
"idx": 5,
"version": "5",
"when": 1706834767044,
"tag": "0005_solid_eternals",
"breakpoints": true
}
]
}
}
32 changes: 26 additions & 6 deletions packages/bot/src/commands/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
getKhlLocale,
transformLocalizations,
} from "../util/l10n";
import { getEpHtPlayer } from "../ep/rest";
import { DBWithSchema, getDb } from "../db";

type KhlPartialPlayer = Pick<APILightPlayer, "id" | "name" | "shirt_number"> & {
team: { name: string } | null;
Expand Down Expand Up @@ -239,6 +241,7 @@ export const playerSearchSelectCallback: SelectMenuCallback = async (ctx) => {
const getPwhlPlayerEmbed = async (
ctx: InteractionContext<APIInteraction>,
playerInput: number | RosterPlayer,
db?: DBWithSchema,
) => {
const locale = getHtLocale(ctx);
const client = getPwhlClient(locale);
Expand All @@ -261,6 +264,17 @@ const getPwhlPlayerEmbed = async (
const number =
"jersey_number" in player ? player.jersey_number : player.tp_jersey_number;

const playerDetails = await getEpHtPlayer(
playerId,
player,
"pwhl",
db ?? getDb(ctx.env.DB),
teamName,
);
const epUrl = playerDetails?.epId
? `https://www.eliteprospects.com/player/${playerDetails.epId}/${playerDetails.epSlug}`
: undefined;

const currentSeason = allSeasons[0];
const allStats = (
await client.getPlayerProfileStatsBySeason(Number(playerId))
Expand Down Expand Up @@ -299,30 +313,36 @@ const getPwhlPlayerEmbed = async (
birthdayToday ? " 🎉" : ""
})\n`;
}
if (player.birthtown || player.birthprov || player.birthcntry) {
description += `${
player.birthtown || player.birthprov || player.birthcntry
}\n`;
const hometown =
player.birthtown ||
player.birthprov ||
player.birthcntry ||
playerDetails?.country;
if (hometown) {
description += `${hometown}\n`;
}
if (player.position) {
description += `${s(ctx, "position")} ${player.position} ${
player.shoots ? `(${player.shoots})` : ""
}\n`;
}
const height = Number(player.height);
const height = playerDetails?.height ?? Number(player.height);
if (height && !Number.isNaN(height)) {
const inches = height * 0.39;
const feet = Math.floor(inches / 12);
const imperial = `${feet}' ${Math.floor(inches - feet * 12)}"`;

description += `${s(ctx, "height")} ${height} cm / ${imperial}\n`;
}
const weight = Number(player.weight);
const weight = playerDetails?.weight ?? Number(player.weight);
if (weight && !Number.isNaN(weight)) {
const pounds = Math.floor(weight * 2.2);
// const stones = Math.floor(player.weight * 0.15747);
description += `${s(ctx, "weight")} ${weight} kg / ${pounds} lb\n`;
}
if (epUrl) {
description += `[Elite Prospects](${epUrl})`;
}

if (currentStats && totalStats) {
const dashes = (length: number) => Array(length).fill("-").join("");
Expand Down
2 changes: 2 additions & 0 deletions packages/bot/src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ import { drizzle } from "drizzle-orm/d1";
import * as schema from "./schema";

export const getDb = (db: D1Database) => drizzle(db, { schema });

export type DBWithSchema = ReturnType<typeof getDb>;
20 changes: 20 additions & 0 deletions packages/bot/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,23 @@ export const games = sqliteTable(
unq: unique().on(table.league, table.nativeId),
}),
);

export const players = sqliteTable(
"players",
{
id: integer("id").primaryKey({ autoIncrement: true }),
league: text("league").$type<League>().notNull(),
nativeId: text("nativeId").notNull(),
epId: text("epId"),
epSlug: text("epSlug"),
fullName: text("fullName").notNull(),
country: text("country"),
/** Centimeters */
height: integer("height"),
/** Kilograms */
weight: integer("weight"),
},
(table) => ({
unq: unique().on(table.league, table.nativeId),
}),
);
Loading

0 comments on commit 0193777

Please sign in to comment.