Screen3Results.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import type { GameStats, SolverReplay } from '@wordle/shared';
  2. import { getMessages } from '../messages/index.js';
  3. import { LangSwitch } from '../components/LangSwitch.js';
  4. interface Screen3ResultsProps {
  5. won: boolean;
  6. attemptCount: number;
  7. revealedWord: string | null;
  8. stats: GameStats;
  9. replays?: SolverReplay[];
  10. onPlayAgain?: () => void;
  11. }
  12. const MINI_COLORS: Record<string, string> = { g: '#6aaa64', y: '#c9b458', x: '#787c7e' };
  13. function MiniBoard({ replay }: { replay: SolverReplay }) {
  14. return (
  15. <div style={{ marginBottom: '12px' }}>
  16. <div style={{ fontSize: '0.7rem', fontWeight: 'bold', color: '#555', marginBottom: '4px', textAlign: 'center' }}>
  17. {replay.solver}
  18. </div>
  19. {replay.steps.map((step, rowIdx) => (
  20. <div key={rowIdx} style={{ display: 'flex', gap: '2px', justifyContent: 'center', marginBottom: '2px' }}>
  21. {step.guess.split('').map((letter, colIdx) => {
  22. const c = step.colors[colIdx];
  23. return (
  24. <div key={colIdx} style={{
  25. width: '20px', height: '20px',
  26. backgroundColor: MINI_COLORS[c] ?? '#d3d6da',
  27. color: '#fff',
  28. display: 'flex', alignItems: 'center', justifyContent: 'center',
  29. fontSize: '0.55rem', fontWeight: 'bold',
  30. textTransform: 'uppercase', fontFamily: 'monospace',
  31. borderRadius: '2px',
  32. }}>
  33. {letter}
  34. </div>
  35. );
  36. })}
  37. </div>
  38. ))}
  39. </div>
  40. );
  41. }
  42. function Histogram({ stats }: { stats: GameStats }) {
  43. if (stats.gamesPlayed === 0) return <p style={{ color: '#787c7e' }}>{getMessages().results.noGames}</p>;
  44. const maxCount = Math.max(...stats.histogram, 1);
  45. return (
  46. <div style={{ maxWidth: '300px', margin: '0 auto' }}>
  47. {stats.histogram.map((count, idx) => {
  48. const label = idx < 6 ? `${idx + 1}` : 'X';
  49. const pct = (count / maxCount) * 100;
  50. return (
  51. <div key={idx} style={{ display: 'flex', alignItems: 'center', gap: '8px', marginBottom: '4px' }}>
  52. <span style={{ width: '20px', textAlign: 'right', fontWeight: 'bold', color: '#555' }}>{label}</span>
  53. <div style={{ flex: 1, height: '20px', backgroundColor: count > 0 ? '#6aaa64' : '#d3d6da', width: `${Math.max(pct, count > 0 ? 5 : 0)}%`, minWidth: count > 0 ? '12px' : '4px', borderRadius: '2px' }} />
  54. <span style={{ width: '24px', fontSize: '0.8rem', color: '#555' }}>{count}</span>
  55. </div>
  56. );
  57. })}
  58. </div>
  59. );
  60. }
  61. export function Screen3Results({ won, attemptCount, revealedWord, stats, replays, onPlayAgain }: Screen3ResultsProps) {
  62. const r = getMessages().results;
  63. return (
  64. <div style={{ maxWidth: '400px', margin: '24px auto', padding: '16px', fontFamily: 'sans-serif', textAlign: 'center', position: 'relative' }}>
  65. <LangSwitch />
  66. <h1 style={{ fontSize: '1.6rem', marginBottom: '12px' }}>
  67. {won ? r.congratulations : r.betterLuck}
  68. </h1>
  69. {won ? (
  70. <p style={{ fontSize: '1.1rem', marginBottom: '8px' }}>{r.solvedIn(attemptCount)}</p>
  71. ) : (
  72. revealedWord && <p style={{ fontSize: '1.1rem', marginBottom: '8px' }}>{r.theWordWas(revealedWord)}</p>
  73. )}
  74. <div style={{ margin: '24px 0' }}>
  75. <h2 style={{ fontSize: '1.1rem', marginBottom: '12px' }}>{r.statistics}</h2>
  76. <div style={{ display: 'flex', justifyContent: 'center', gap: '24px', marginBottom: '16px' }}>
  77. <div><div style={{ fontSize: '1.4rem', fontWeight: 'bold' }}>{stats.gamesPlayed}</div><div style={{ fontSize: '0.75rem', color: '#787c7e' }}>{r.played}</div></div>
  78. <div><div style={{ fontSize: '1.4rem', fontWeight: 'bold' }}>{stats.gamesPlayed > 0 ? Math.round((stats.wins / stats.gamesPlayed) * 100) : 0}%</div><div style={{ fontSize: '0.75rem', color: '#787c7e' }}>{r.winRate}</div></div>
  79. <div><div style={{ fontSize: '1.4rem', fontWeight: 'bold' }}>{stats.currentStreak}</div><div style={{ fontSize: '0.75rem', color: '#787c7e' }}>{r.streak}</div></div>
  80. <div><div style={{ fontSize: '1.4rem', fontWeight: 'bold' }}>{stats.maxStreak}</div><div style={{ fontSize: '0.75rem', color: '#787c7e' }}>{r.maxStreak}</div></div>
  81. </div>
  82. <Histogram stats={stats} />
  83. </div>
  84. {replays && replays.length > 0 && (
  85. <div style={{ margin: '24px 0' }}>
  86. <p style={{ fontSize: '0.85rem', color: '#787c7e', marginBottom: '12px' }}>
  87. {r.solverCaption}
  88. </p>
  89. <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '16px', maxWidth: '320px', margin: '0 auto' }}>
  90. {replays.map((rp) => (
  91. <MiniBoard key={rp.solver} replay={rp} />
  92. ))}
  93. </div>
  94. </div>
  95. )}
  96. {onPlayAgain && (
  97. <button
  98. onClick={onPlayAgain}
  99. style={{
  100. padding: '12px 48px',
  101. fontSize: '1.1rem',
  102. fontWeight: 'bold',
  103. backgroundColor: '#6aaa64',
  104. color: '#fff',
  105. border: 'none',
  106. borderRadius: '8px',
  107. cursor: 'pointer',
  108. }}
  109. >
  110. {r.playAgain}
  111. </button>
  112. )}
  113. </div>
  114. );
  115. }