|
| 1 | +import { getScrabbleDex } from '@/database/games'; |
| 2 | +import { Board } from '@/ps/commands/points'; |
1 | 3 | import { parseMod } from '@/ps/games/mods'; |
2 | 4 | import { checkWord } from '@/ps/games/scrabble/checker'; |
3 | 5 | import { ScrabbleMods } from '@/ps/games/scrabble/constants'; |
4 | 6 | import { ScrabbleModData } from '@/ps/games/scrabble/mods'; |
5 | 7 | import { toId } from '@/tools'; |
6 | 8 | import { ChatError } from '@/utils/chatError'; |
7 | 9 |
|
| 10 | +import type { ScrabbleDexEntry } from '@/database/games'; |
| 11 | +import type { ToTranslate, TranslationFn } from '@/i18n/types'; |
8 | 12 | import type { PSCommand } from '@/types/chat'; |
| 13 | +import type { ReactElement } from 'react'; |
| 14 | + |
| 15 | +export function renderScrabbleDexLeaderboard(entries: ScrabbleDexEntry[], $T: TranslationFn): ReactElement { |
| 16 | + const usersData = Object.values(entries.groupBy(entry => entry.by) as Record<string, ScrabbleDexEntry[]>).map(entries => { |
| 17 | + const name = entries.findLast(entry => entry.byName)?.byName ?? entries[0].by; |
| 18 | + const count = entries.length; |
| 19 | + const points = entries.map(entry => Math.max(1, entry.pokemon.length - 4)).sum(); |
| 20 | + return { name, count, points }; |
| 21 | + }); |
| 22 | + const sortedData = usersData |
| 23 | + .sortBy(({ count, points }) => [points, count], 'desc') |
| 24 | + .map(({ name, count, points }, index, data) => { |
| 25 | + let rank = index; |
| 26 | + |
| 27 | + const getPointsKey = (entry: { count: number; points: number }): string => [entry.count, entry.points].join(','); |
| 28 | + const userPointsKey = getPointsKey({ count, points }); |
| 29 | + |
| 30 | + while (rank > 0) { |
| 31 | + const prev = data[rank - 1]; |
| 32 | + if (getPointsKey(prev) !== userPointsKey) break; |
| 33 | + rank--; |
| 34 | + } |
| 35 | + |
| 36 | + return [rank + 1, name, count, points]; |
| 37 | + }); |
| 38 | + return <Board headers={['#', $T('COMMANDS.POINTS.HEADERS.USER'), 'Unique', 'Points']} data={sortedData} />; |
| 39 | +} |
9 | 40 |
|
10 | 41 | export const command: PSCommand[] = [ |
11 | 42 | { |
@@ -36,4 +67,33 @@ export const command: PSCommand[] = [ |
36 | 67 | broadcastHTML([['e6', 'f4'], ['e3', 'f6'], ['g5', 'd6'], ['e7', 'f5'], ['c5']].map(turns => turns.join(', ')).join('<br />')); |
37 | 68 | }, |
38 | 69 | }, |
| 70 | + { |
| 71 | + name: 'scrabbledex', |
| 72 | + help: 'Shows your current Scrabble Dex for UGO.', |
| 73 | + syntax: 'CMD', |
| 74 | + flags: { allowPMs: true }, |
| 75 | + categories: ['game'], |
| 76 | + async run({ message, broadcastHTML, $T }) { |
| 77 | + const allEntries = await getScrabbleDex(); |
| 78 | + const results = allEntries!.filter(entry => entry.by === message.author.id); |
| 79 | + const grouped = results.map(res => res.pokemon.toUpperCase()).groupBy(mon => mon.length); |
| 80 | + |
| 81 | + if (!results.length) throw new ChatError("You don't have any entries yet!" as ToTranslate); |
| 82 | + |
| 83 | + broadcastHTML( |
| 84 | + <details> |
| 85 | + <summary>ScrabbleDex ({results.length} entries)</summary> |
| 86 | + {Object.entries(grouped).filterMap(([length, mons]) => { |
| 87 | + if (mons) |
| 88 | + return ( |
| 89 | + <p> |
| 90 | + {length} ({mons.length}): {mons.list($T)} |
| 91 | + </p> |
| 92 | + ); |
| 93 | + })} |
| 94 | + </details>, |
| 95 | + { name: `scrabbledex-${message.author.id}` } |
| 96 | + ); |
| 97 | + }, |
| 98 | + }, |
39 | 99 | ]; |
0 commit comments