Skip to content

Commit

Permalink
introducing hall of fame
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniocosentino committed Mar 23, 2024
1 parent c688bd7 commit 2cedc78
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
55 changes: 52 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand All @@ -170,14 +171,62 @@ 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',
message: `${event.user} asked for scores. Current scores: ${JSON.stringify(sessionScores)}`,
});
}

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;
}

Expand Down Expand Up @@ -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}`,
);
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
16 changes: 15 additions & 1 deletion src/utilities.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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[];

Expand Down

0 comments on commit 2cedc78

Please sign in to comment.