Skip to content

thanasispro/scoreboard-lib

Repository files navigation

World Cup Library

This project is a World Cup management library that allows you to track matches, update scores, and manage a scoreboard. It includes validation to ensure that scores and teams are handled properly, making it ideal for managing a set of football matches during a World Cup or similar event.

Features

  • Add a new match: Create a match between two teams with unique names.
  • Update the score: Safely update match scores with validation to ensure that scores can only change by +1 or -1.
  • Finish a match: Mark a match as finished by removing it from the scoreboard.
  • Retrieve match details: Get a summary of a match and its current score.
  • Error handling: Ensures that invalid match IDs, improper score updates, duplicate team names, and empty team names are handled correctly.

Installation and Setup

1. Clone the Repository

You can clone the repository using either HTTPS or SSH:

Using HTTPS:

git clone https://github.com/thanasispro/scoreboard-lib.git

Using SSH:

git git@github.com:thanasispro/scoreboard-lib.git

2. Install Dependencies

Navigate to the project directory and install the dependencies using pnpm, npm, or yarn:

Using pnpm:

pnpm install

Using npm:

npm install

Using yarn:

yarn install

3. Using the Library

After installing the dependencies, you can use the library in your project by importing the required classes. Here’s an example:

import { Scoreboard } from './path-to-library/src/services/Scoreboard';

const scoreboard = new Scoreboard();
const matchId = scoreboard.addMatch('Greece', 'Norway');

// Update the score
scoreboard.getMatch(matchId).updateScore(1, 0);

// Get a summary of the match
console.log(scoreboard.getMatch(matchId).print());  // Outputs: "Greece 1 - Norway 0"

// Finish the match
scoreboard.finishMatch(matchId);

4. Running Tests

To ensure the functionality is working as expected, you can run the tests included in the project.

Running Tests:

pnpm test

For npm:

npm test

For yarn:

yarn test

Checking Test Coverage:

pnpm run test:coverage

For npm:

npm run test:coverage

For yarn:

yarn run test:coverage

Key Functionalities

1. Add a Match

Create a new match between two teams:

const matchId = scoreboard.addMatch('Greece', 'Norway');

2. Update Score

Update the scores for a match. The score change is validated to only allow changes of +1 or -1:

scoreboard.getMatch(matchId).updateScore(1, 0);

3. Finish a Match

Mark a match as finished by removing it from the scoreboard:

scoreboard.finishMatch(matchId);

4. Get Match Summary

Retrieve the current score and summary of the match:

console.log(scoreboard.getMatch(matchId).print());  // Outputs: "Greece 1 - Norway 0"

5. Get Matches Sorted by Total Score

Retrieve the matches sorted by total score, from highest to lowest:

const sortedMatches = scoreboard.getByScore();
console.log(sortedMatches);  // Outputs an array of match summaries, sorted by total score.

Error Handling

  • Invalid Match ID: If you try to get or finish a match with an invalid matchId, an error will be thrown:
try {
  scoreboard.getMatch('invalid-id');
} catch (error) {
  console.error(error.message);  // Outputs: "Match with ID invalid-id does not exist."
}
  • Invalid Score Update: If you try to update the score by more than +1 or -1, an error will be thrown:
try {
  scoreboard.getMatch(matchId).updateScore(3, 0);  // Invalid score update
} catch (error) {
  console.error(error.message);  // Outputs: "Team's score can only change by +1 or -1."
}
  • Duplicate Team Names: If you try to add a match with the same team name for both teams, an error will be thrown:
try {
  scoreboard.addMatch('Greece', 'Greece');
} catch (error) {
  console.error(error.message);  // Outputs: "Home and away teams cannot have the same name."
}
  • Empty Team Names: If you try to add a match where one or both team names are empty, an error will be thrown:
try {
  scoreboard.addMatch('Greece', '');
} catch (error) {
  console.error(error.message);  // Outputs: "Team names cannot be empty."
}
  • Team Already in a Match: If you try to add a match where one or both teams are already involved in another ongoing match, an error will be thrown:
scoreboard.addMatch('Greece', 'Norway');
try {
  scoreboard.addMatch('Greece', 'Denmark');
} catch (error) {
  console.error(error.message);  // Outputs: "A match involving Greece is already active."
}
  • Team Not in Participating Countries: If one or both team names are not from the list of participating countries, an error will specify which team is invalid:

    try {
      scoreboard.addMatch('Atlantis', 'Norway');
    } catch (error) {
      console.error(error.message);  // Outputs: "Atlantis is not from participating countries."
    }
    
    try {
      scoreboard.addMatch('Norway', 'Ares');
    } catch (error) {
      console.error(error.message);  // Outputs: "Ares is not from participating countries."
    }

## License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published