diff --git a/src/app.ts b/src/app.ts index 4ba328b..39546c6 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2,6 +2,7 @@ import { APP_TOKEN, PORT, SIGNING_SECRET, TOKEN, SCORES_API, DEBUG_MODE } from ' import { TParticipantsWords, TSessionScores } from './types'; import fetch from 'node-fetch'; import { + constructHallOfFame, constructResponse, constructScores, extractWordFromSentence, @@ -152,7 +153,7 @@ app.event('app_mention', async ({ event, say }) => { if (isGameRunning) { const allWords = constructResponse(participantsWords); - saySomething(say, `Il gioco è chiuso. Ecco le parole: \n ${allWords}`); + saySomething(say, `Il gioco è chiuso. Ecco le parole:\n${allWords}`); isGameRunning = false; isWaitingForWord = true; @@ -170,7 +171,7 @@ app.event('app_mention', async ({ event, say }) => { saySomething(say, 'Questa funzionalità non è supportata.'); } else { const readableScores = constructScores(sessionScores); - saySomething(say, `Ecco la classifica: \n ${readableScores}`); + saySomething(say, `Ecco la classifica:\n${readableScores}`); logger.log({ level: 'info', @@ -178,6 +179,54 @@ app.event('app_mention', async ({ event, say }) => { }); } + break; + + case "albo d'oro!": + if (!SCORES_API) { + saySomething(say, 'Questa funzionalità non è supportata.'); + } else { + logger.log({ + level: 'info', + message: `${event.user} asked for hall of fame. Fetching it from endpoint`, + }); + + let hallOfFame = null; + + axios + .get(`${SCORES_API}/hall-of-fame`) + .then((resp) => { + hallOfFame = resp.data; + + const readableHallOfFame = hallOfFame ? constructHallOfFame(hallOfFame) : ''; + + if (readableHallOfFame) { + saySomething(say, `Ecco l'albo d'oro:\n${readableHallOfFame}`); + + logger.log({ + level: 'info', + message: `Hall of fame was shown in the channel. Records: ${JSON.stringify( + hallOfFame, + )}`, + }); + + return; + } + + logger.log({ + level: 'info', + message: `Hall of fame was not shown in the channel because it was empty`, + }); + }) + .catch((error) => { + logger.log({ + level: 'info', + message: `Fetching of hall of fame failed. ${error}`, + }); + + return; + }); + } + break; } @@ -226,7 +275,7 @@ app.event('app_mention', async ({ event, say }) => { const readableChart = constructScores(sessionScores); saySomething( say, - `La parola era ${finalWord}. Ecco la classifica aggiornata: \n${readableChart}`, + `La parola era ${finalWord}. Ecco la classifica aggiornata:\n${readableChart}`, ); } } diff --git a/src/types.ts b/src/types.ts index 153f8b4..4767f4f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -13,3 +13,12 @@ export type TSingleScore = { user: string; score: number; }; + +export type THallOfFame = { + 'hall-of-fame': TSingleHallOfFameEntry[]; +}; + +export type TSingleHallOfFameEntry = { + season: string; + winner: string; +}; diff --git a/src/utilities.ts b/src/utilities.ts index 091df6d..f6a3e96 100644 --- a/src/utilities.ts +++ b/src/utilities.ts @@ -1,6 +1,6 @@ const _ = require('lodash'); -import { TParticipantsWords, TSessionScores, TSingleScore } from './types'; +import { THallOfFame, TParticipantsWords, TSessionScores, TSingleScore } from './types'; // I don't want to make an extra API call for this, so I will store the known // players nicknames here @@ -44,6 +44,20 @@ export const constructScores = (sessionScores: TSessionScores): string => { return finalResponse; }; +export const constructHallOfFame = (hallOfFame: THallOfFame): string => { + let finalResponse = ''; + + if (!hallOfFame) { + return ''; + } + + hallOfFame['hall-of-fame']?.forEach((singleRecord) => { + finalResponse = finalResponse + `🏆 *${singleRecord.season}*: ${singleRecord.winner} \n`; + }); + + return finalResponse; +}; + export const getWinners = (correctWord: string, participantsWords: TParticipantsWords): string[] => { const winnersArr = [] as string[];