Certainly! Below is a Twig template for a Darts scoring website, along with a TypeScript script to handle the functionality.
Twig Template
{# darts_scoring.twig #}
<div class="max-w-md mx-auto mt-10 p-6 bg-white rounded-md shadow-md">
<h2 class="text-2xl font-bold mb-6">Darts Scoring</h2>
<div class="grid grid-cols-3 gap-4 mb-4">
{% for score in [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] %}
<button class="score-button bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" data-score="{{ score }}">{{ score }}</button>
{% endfor %}
</div>
<div class="grid grid-cols-3 gap-4 mb-4">
<button class="multiplier-button bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded" data-multiplier="3">Triple</button>
<button class="multiplier-button bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded" data-multiplier="2">Double</button>
</div>
<input type="hidden" id="current-score" value="0">
<button id="submit-score" class="w-full bg-purple-500 hover:bg-purple-700 text-white font-bold py-2 px-4 rounded" disabled>Submit Score</button>
</div>
<script src="{{ asset('js/darts_scoring.js') }}"></script>
TypeScript Script
Create a file named darts_scoring.ts and compile it to darts_scoring.js.
// darts_scoring.ts
document.addEventListener('DOMContentLoaded', () => {
const scoreButtons = document.querySelectorAll('.score-button');
const multiplierButtons = document.querySelectorAll('.multiplier-button');
const currentScoreInput = document.getElementById('current-score') as HTMLInputElement;
const submitScoreButton = document.getElementById('submit-score') as HTMLButtonElement;
let currentScore = 0;
scoreButtons.forEach(button => {
button.addEventListener('click', () => {
const score = parseInt(button.getAttribute('data-score') || '0', 10);
currentScore = score;
updateScoreDisplay();
});
});
multiplierButtons.forEach(button => {
button.addEventListener('click', () => {
const multiplier = parseInt(button.getAttribute('data-multiplier') || '1', 10);
currentScore *= multiplier;
updateScoreDisplay();
});
});
submitScoreButton.addEventListener('click', async () => {
const userId = getCookie('user_id');
const gameId = getCookie('game_id');
if (userId && gameId) {
const scoreData = {
user_id: userId,
game_id: gameId,
score: currentScore
};
try {
const response = await fetch('/api/v1/score', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(scoreData)
});
if (response.ok) {
alert('Score submitted successfully!');
currentScore = 0;
updateScoreDisplay();
} else {
alert('Failed to submit score.');
}
} catch (error) {
console.error('Error:', error);
alert('An error occurred while submitting the score.');
}
} else {
alert('User ID or Game ID not found.');
}
});
function updateScoreDisplay() {
currentScoreInput.value = currentScore.toString();
submitScoreButton.disabled = currentScore === 0;
}
function getCookie(name: string): string | null {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop()?.split(';').shift();
return null;
}
});
Compilation
Ensure you have TypeScript installed and then compile the TypeScript file:
tsc darts_scoring.ts
This will generate darts_scoring.js, which you can include in your Twig template as shown above.