File size: 3,624 Bytes
aeb9637
 
 
 
 
 
 
 
 
a64b653
 
aeb9637
 
 
 
 
 
 
 
a64b653
 
 
 
aeb9637
 
 
 
 
8ec33d8
a64b653
 
aeb9637
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a64b653
 
 
 
 
 
 
 
 
 
 
 
2d83648
 
a64b653
 
 
 
 
 
 
 
 
 
 
 
aeb9637
 
 
 
 
 
 
 
 
 
 
a64b653
 
 
 
8ec33d8
aeb9637
 
 
 
 
 
a64b653
aeb9637
 
a64b653
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ec33d8
aeb9637
 
 
 
 
a64b653
aeb9637
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";
import { useTranslation } from "@/hooks/useTranslation";
import { Button } from "@/components/ui/button";
import { Play } from "lucide-react";

interface HighScore {
  id: string;
  player_name: string;
  score: number;
  avg_words_per_round: number;
  created_at: string;
  session_id: string;
  game?: {
    language: string;
  };
  game_id?: string;
}

interface ScoresTableProps {
  scores: HighScore[];
  startIndex: number;
  showThemeColumn?: boolean;
  onPlayGame?: (gameId: string) => void;
  selectedMode?: 'daily' | 'all-time';
}

const getRankMedal = (rank: number) => {
  switch (rank) {
    case 1:
      return "๐Ÿฅ‡";
    case 2:
      return "๐Ÿฅˆ";
    case 3:
      return "๐Ÿฅ‰";
    default:
      return rank;
  }
};

const getLanguageEmoji = (language: string) => {
  switch (language) {
    case 'en':
      return '๐Ÿ‡ฌ๐Ÿ‡ง';
    case 'de':
      return '๐Ÿ‡ฉ๐Ÿ‡ช';
    case 'fr':
      return '๐Ÿ‡ซ๐Ÿ‡ท';
    case 'it':
      return '๐Ÿ‡ฎ๐Ÿ‡น';
    case 'es':
      return '๐Ÿ‡ช๐Ÿ‡ธ';
    case 'pt':
      return '๐Ÿ‡ต๐Ÿ‡น';
    default:
      return '๐ŸŒ';
  }
};

export const ScoresTable = ({
  scores,
  startIndex,
  showThemeColumn = false,
  onPlayGame,
  selectedMode = 'daily'
}: ScoresTableProps) => {
  const t = useTranslation();

  return (
    <div className="rounded-md border">
      <Table>
        <TableHeader>
          <TableRow>
            <TableHead>{t.leaderboard.rank}</TableHead>
            <TableHead>{t.leaderboard.player}</TableHead>
            <TableHead>{t.leaderboard.roundsColumn}</TableHead>
            <TableHead>{t.leaderboard.avgWords}</TableHead>
            {selectedMode === 'all-time' && (
              <TableHead className="text-center">
                {t.leaderboard.playSameWords}
              </TableHead>
            )}
          </TableRow>
        </TableHeader>
        <TableBody>
          {scores?.map((score, index) => {
            const absoluteRank = startIndex + index + 1;
            const medal = getRankMedal(absoluteRank);
            const language = score.game?.language || 'en';
            return (
              <TableRow key={score.id}>
                <TableCell className="align-middle">{medal}</TableCell>
                <TableCell className="flex items-center gap-2 h-full align-middle">
                  {score.player_name}
                  <span>{getLanguageEmoji(language)}</span>
                </TableCell>
                <TableCell className="align-middle">{score.score}</TableCell>
                <TableCell className="align-middle">{score.avg_words_per_round.toFixed(1)}</TableCell>
                {selectedMode === 'all-time' && (
                  <TableCell className="text-center align-middle">
                    {score.game_id && onPlayGame && (
                      <Button
                        variant="ghost"
                        size="sm"
                        onClick={() => onPlayGame(score.game_id!)}
                        className="gap-2 mx-auto"
                      >
                        <Play className="h-4 w-4" />
                      </Button>
                    )}
                  </TableCell>
                )}
              </TableRow>
            );
          })}
          {!scores?.length && (
            <TableRow>
              <TableCell colSpan={5} className="text-center">
                {t.leaderboard.noScores}
              </TableCell>
            </TableRow>
          )}
        </TableBody>
      </Table>
    </div>
  );
};