UNPKG

@lovebowls/leaguejs

Version:

A framework-agnostic JavaScript library for managing leagues, teams, and matches

157 lines (134 loc) 4.05 kB
import { Team } from './Team'; import { Match } from './Match'; export interface LeagueSettings { pointsForWin?: number; pointsForDraw?: number; pointsForLoss?: number; promotionPositions?: number; relegationPositions?: number; timesTeamsPlayOther?: number; rinkPoints?: { pointsPerRinkWin?: number; pointsPerRinkDraw?: number; defaultRinks?: number; enabled?: boolean; }; } export interface LeagueData { _id?: string; name: string; settings?: LeagueSettings; teams?: Team[] | any[]; matches?: Match[] | any[]; createdAt?: Date; updatedAt?: Date; } export interface TeamStats { teamId: string; teamName: string; played: number; won: number; drawn: number; lost: number; shotsFor: number; shotsAgainst: number; points: number; inPromotionPosition: boolean; inRelegationPosition: boolean; /** Array of the last 5 match details */ matches?: Array<{ result: string; description: string; date: Date }>; shotDifference?: number; } export interface LeagueTableEntry extends TeamStats { teamId: string; teamName: string; } /** * League model representing a bowls league */ export class League { /** Unique identifier for the league */ _id: string; /** Name of the league */ name: string; /** League settings including points system and rules */ settings: LeagueSettings; /** List of teams in the league */ teams: Team[]; /** List of matches in the league */ matches: Match[]; /** Date when the league was created */ createdAt: Date; /** Date when the league was last updated */ updatedAt: Date; constructor(data: LeagueData); /** * Create a League instance from JSON data */ static fromJSON(jsonData: string | object): League; /** * Add a team to the league * @param team Team to add (either a Team instance or team data) * @returns True if team was added, false if already exists */ addTeam(team: Team | object): boolean; /** * Remove a team from the league * @param teamId ID of team to remove * @returns True if team was removed, false if not found */ removeTeam(teamId: string): boolean; /** * Add a match to the league * @param match Match instance or match data * @returns True if match was added, false otherwise */ addMatch(match: Match | object): boolean; /** * Get a team by its ID * @param teamId ID of the team * @returns The team if found, undefined otherwise */ getTeam(teamId: string): Team | undefined; /** * Get a match by its _id * @param matchId The _id of the match * @returns The match if found, undefined otherwise */ getMatch(matchId: string): Match | undefined; /** * Get all matches for a team * @param teamId ID of the team * @returns Array of matches involving the team */ getTeamMatches(teamId: string): Match[]; /** * Get statistics for a team * @param teamId ID of the team * @returns Team statistics */ getTeamStats(teamId: string): TeamStats; /** * Get the league table * @returns The league table */ getLeagueTable(): { leagueData: LeagueTableEntry[], metaData: { name: string } }; /** * Initialise fixtures for the league using a round-robin algorithm * @param startDate The start date for the first round of matches. If null, matches will have null dates * @param schedulingParams Advanced scheduling parameters * @returns True if fixtures were successfully created, false otherwise */ initialiseFixtures(startDate?: Date, schedulingParams?: { schedulingPattern?: 'interval' | 'dayOfWeek'; intervalNumber?: number; intervalUnit?: 'days' | 'weeks'; selectedDays?: number[]; maxMatchesPerDay?: number; }): boolean; /** * Convert league to JSON * @returns JSON representation of the league */ toJSON(): object; }