Untitled

 avatar
unknown
plain_text
a year ago
2.2 kB
6
Indexable
import Matches from '../database/models/Matches.model';
import Teams from '../database/models/Teams.model';

const getAllMatches = async () => {
  const matches = await Matches.findAll({
    where: { inProgress: false },
  });
  return matches;
};

const calculateTotalPoints = (homeTeamGoals: number, awayTeamGoals: number) => {
  if (homeTeamGoals > awayTeamGoals) return 3;
  if (homeTeamGoals === awayTeamGoals) return 1;
  return 0;
};

const calculateTotalGames = (homeTeamGoals: number, awayTeamGoals: number) => {
  if (homeTeamGoals > awayTeamGoals) return 1;
  if (homeTeamGoals === awayTeamGoals) return 1;
  return 0;
};

const calculateTotalVictories = (homeTeamGoals: number, awayTeamGoals: number) => {
  if (homeTeamGoals > awayTeamGoals) return 1;
  return 0;
};

const calculateTotalDraws = (homeTeamGoals: number, awayTeamGoals: number) => {
  if (homeTeamGoals === awayTeamGoals) return 1;
  return 0;
};

const calculateTotalLosses = (homeTeamGoals: number, awayTeamGoals: number) => {
  if (homeTeamGoals < awayTeamGoals) return 1;
  return 0;
};

const calculateGoalsFavor = (homeTeamGoals: number, _awayTeamGoals: number) => homeTeamGoals;

const calculateGoalsOwn = (_homeTeamGoals: number, awayTeamGoals: number) => awayTeamGoals;

const getHomeLeaderboard = async () => {
  const matches = await getAllMatches();
  const teams = await Teams.findAll();

  const newMatches = matches.map((match) => {
    const homeTeam = teams.find((team) => team.id === match.homeTeamId);
    return {
      name: homeTeam?.teamName,
      totalPoints: calculateTotalPoints(match.homeTeamGoals, match.awayTeamGoals),
      totalGames: calculateTotalGames(match.homeTeamGoals, match.awayTeamGoals),
      totalVictories: calculateTotalVictories(match.homeTeamGoals, match.awayTeamGoals),
      totalDraws: calculateTotalDraws(match.homeTeamGoals, match.awayTeamGoals),
      totalLosses: calculateTotalLosses(match.homeTeamGoals, match.awayTeamGoals),
      goalsFavor: calculateGoalsFavor(match.homeTeamGoals, match.awayTeamGoals),
      goalsOwn: calculateGoalsOwn(match.homeTeamGoals, match.awayTeamGoals),
    };
  });
  return newMatches;
};

export default { getHomeLeaderboard };
Editor is loading...
Leave a Comment