33 lines
926 B
JavaScript
33 lines
926 B
JavaScript
document.addEventListener('DOMContentLoaded', () => {
|
|
// If user already has a hero, redirect to game page
|
|
api.get('/auth/me').then((data) => {
|
|
if (data.hasHero) {
|
|
window.location.href = '/game.html';
|
|
return;
|
|
}
|
|
}).catch(() => {
|
|
// Not logged in or error — stay on page (login form will show)
|
|
});
|
|
|
|
const form = document.getElementById('create-hero-form');
|
|
if (!form) return;
|
|
|
|
form.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
const name = document.getElementById('hero-name').value.trim();
|
|
const classKey = document.getElementById('hero-class').value;
|
|
|
|
if (!name || !classKey) {
|
|
showError('Please enter a hero name and choose a class.');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await api.post('/character', { name, class: classKey });
|
|
window.location.href = '/game.html';
|
|
} catch (_) {
|
|
// Error already shown via api()
|
|
}
|
|
});
|
|
});
|