Skip to content

Commit

Permalink
utils: Add function to map card to its image name.
Browse files Browse the repository at this point in the history
The function will be used during rendering the cards,
where we require the filenames of the images corresponding
to each variant of the UNO card.
  • Loading branch information
kuv2707 committed Jun 26, 2024
1 parent 2381134 commit edfab64
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions backend/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Response } from 'express';
import { AuthRequest } from './middlewares/authMiddleware';
import { CardColor, UNOCard } from './types';

export type ControllerFunction = (
req: AuthRequest,
Expand All @@ -15,3 +16,43 @@ export function catchError(fn: ControllerFunction): ControllerFunction {
}
};
}

export function getCardImageName(card: UNOCard): string {
function getColorAbbreviation(color: CardColor): string {
switch (color) {
case 'red':
return 'r';
case 'blue':
return 'b';
case 'green':
return 'g';
case 'yellow':
return 'o';
default:
return '';
}
}
if (card.type === 'wild') {
if (card.value === 'colchange') {
return 'CC';
} else {
return 'P4';
}
} else if (card.type === 'special') {
let value;
switch (card.value) {
case 'skip':
value = 'r';
break;
case 'reverse':
value = 'x';
break;
case 'draw2':
value = 'p2';
break;
}
return `${getColorAbbreviation(card.color)}${value}`;
} else {
return `${getColorAbbreviation(card.color)}${card.value}`;
}
}

0 comments on commit edfab64

Please sign in to comment.