import { Game } from './lib/game';
import { getTypeInfo, TypeName } from './lib/types';
import './style.css';
const app = document.getElementById('app')!;
const game = new Game(render);
game.nextQuestion();
function renderTypeBadge(name: TypeName, size: 'small' | 'large' = 'small'): string {
const info = getTypeInfo(name);
const sizeClass = size === 'large' ? 'type-badge-large' : '';
return `${info.name}`;
}
function render(): void {
const state = game.getState();
const { options, startKey } = game.getOptions();
const stats = game.getStats();
if (!state.currentQuestion) {
app.innerHTML = '
Loading...
';
return;
}
const q = state.currentQuestion;
const defenderHtml = q.defenderTypes.length === 1
? renderTypeBadge(q.defenderTypes[0], 'large')
: `${renderTypeBadge(q.defenderTypes[0], 'large')} / ${renderTypeBadge(q.defenderTypes[1], 'large')}`;
const optionsHtml = options.map((opt, i) => {
let cls = 'option-btn';
if (state.showResult) {
if (opt.value === q.correctAnswer) {
cls += ' correct';
} else if (i === state.selectedIndex && !state.isCorrect) {
cls += ' wrong';
}
} else if (i === state.selectedIndex) {
cls += ' selected';
}
const shortcut = `${startKey + i}`;
const disabled = state.showResult ? 'disabled' : '';
return `
`;
}).join('');
const resultHtml = state.showResult ? `
${state.isCorrect
? '
✓ Correct!
'
: `
✗ Wrong
${game.getExplanation()}
`
}
` : '';
app.innerHTML = `
Attacking Type
${renderTypeBadge(q.attackType, 'large')}
against
Defending Type${q.defenderTypes.length > 1 ? 's' : ''}
${defenderHtml}
${optionsHtml}
${resultHtml}
`;
attachEventListeners();
}
function attachEventListeners(): void {
document.querySelectorAll('.option-btn').forEach(btn => {
btn.addEventListener('click', () => {
const index = parseInt(btn.getAttribute('data-index')!);
game.selectAnswer(index);
});
});
const dualToggle = document.getElementById('dualToggle') as HTMLInputElement;
if (dualToggle) {
dualToggle.addEventListener('change', () => {
game.setAllowDualTypes(dualToggle.checked);
});
}
const nextBtn = document.querySelector('.next-btn');
if (nextBtn) {
nextBtn.addEventListener('click', () => {
game.nextQuestion();
});
}
}
document.addEventListener('keydown', (e) => {
const state = game.getState();
const { options, startKey } = game.getOptions();
if (state.showResult) {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
game.nextQuestion();
}
return;
}
const num = parseInt(e.key);
if (num >= startKey && num < startKey + options.length) {
e.preventDefault();
game.selectAnswer(num - startKey);
}
if (e.key === 'Enter') {
if (state.selectedAnswer !== null) {
e.preventDefault();
}
}
});
render();