-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
243 lines (204 loc) · 9.29 KB
/
Copy pathtest.html
File metadata and controls
243 lines (204 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<title>Hartenjagen Realtime Test</title>
<style>
body { font-family: sans-serif; margin: 20px; background: #f4f4f9; }
.card-btn { margin: 5px; padding: 10px; font-size: 16px; cursor: pointer; }
#log { background: #1e1e1e; color: #00ff66; padding: 15px; border-radius: 5px; font-family: monospace; max-height: 400px; overflow-y: auto; }
.trick { border: 2px solid #ccc; background: white; padding: 10px; margin-bottom: 10px; border-radius: 5px; }
</style>
</head>
<body>
<h1>Hartenjagen Realtime Test</h1>
<button onclick="startGame()">1. Start Nieuwe Game</button>
<div id="pass-action-container" style="display: none; margin: 10px 0;">
<button onclick="submitPassCards()" style="background-color: #4CAF50; color: white; padding: 10px;">
✉️ Bevestig & Verstuur 3 Gekozen Kaarten
</button>
</div>
<h3>Status: <span id="game-status">-</span> (Ronde: <span id="round-number">1</span>, Richting: <span id="pass-direction">-</span>)</h3>
<h3>Totaalscores: <span id="total-scores">-</span></h3>
<h4>Kaarten op Tafel:</h4>
<div id="current-trick">Geen kaarten op tafel</div>
<h4>Jouw Hand:</h4>
<div id="player-hand"></div>
<div id="log" style="margin-top:20px; font-family: monospace; background:#f4f4f4; padding:10px;"></div>
<script>
let currentGameId = null;
let selectedCardsToPass = []; // Houdt geselecteerde kaarten vast tijdens PASSING_CARDS
const API_URL = 'http://127.0.0.1:8000/api/game';
// 1. Start een nieuwe game
async function startGame() {
log("Nieuwe game starten...");
try {
const response = await fetch(`${API_URL}/start`, { method: 'POST' });
const data = await response.json();
if (data.status === 'success') {
currentGameId = data.game_id;
log(`Game gestart! ID: ${currentGameId}`);
updateUI(data.game_state);
}
} catch (err) {
log("❌ Fout bij starten: " + err);
}
}
// 2. Verstuur 3 gekozen kaarten tijdens de PASSING_CARDS fase
async function submitPassCards() {
if (selectedCardsToPass.length !== 3) {
alert("Selecteer exact 3 kaarten om door te geven.");
return;
}
log("3 kaarten doorgeven...");
try {
const response = await fetch(`${API_URL}/${currentGameId}/pass`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ cards: selectedCardsToPass })
});
const data = await response.json();
if (!response.ok) {
alert("Fout bij doorgeven: " + data.error);
log("❌ Fout: " + data.error);
return;
}
log("✅ Kaarten succesvol gewisseld!");
selectedCardsToPass = []; // Reset selectie
updateUI(data.game_state);
} catch (err) {
log("❌ Netwerkfout: " + err);
}
}
// 3. Speel een kaart tijdens de PLAYING_TRICKS fase
async function playCard(suit, rank) {
log(`Speel kaart: ${suit} ${rank}...`);
try {
const response = await fetch(`${API_URL}/${currentGameId}/play`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ suit: suit, rank: rank })
});
const data = await response.json();
if (!response.ok) {
alert("Ongeldige zet: " + data.error);
log("❌ Fout: " + data.error);
return;
}
log("✅ Zet geaccepteerd!");
updateUI(data.game_state);
} catch (err) {
log("❌ Netwerkfout: " + err);
}
}
// 4. Update de complete UI op basis van de speltoestand
function updateUI(gameState) {
// Status & Informatie
document.getElementById('game-status').innerText = gameState.state;
document.getElementById('round-number').innerText = gameState.round_number;
document.getElementById('pass-direction').innerText = gameState.pass_direction;
// Pas-knop tonen/verbergen
const passActionDiv = document.getElementById('pass-action-container');
if (gameState.state === 'PASSING_CARDS' && gameState.pass_direction !== 'NONE') {
passActionDiv.style.display = 'block';
} else {
passActionDiv.style.display = 'none';
}
// Toon huidige kaarten van de menselijke speler ('p1')
const humanPlayer = gameState.players.find(p => p.id === 'p1');
const handDiv = document.getElementById('player-hand');
handDiv.innerHTML = '';
console.log(humanPlayer)
if (humanPlayer && humanPlayer.hand) {
humanPlayer.hand.forEach(card => {
const cardBtn = document.createElement('button');
cardBtn.className = 'card-btn';
cardBtn.style.margin = '4px';
cardBtn.style.padding = '8px 12px';
const symbol = getSuitSymbol(card.suit);
cardBtn.innerHTML = `${symbol} ${getRankName(card.rank)}`;
// Is deze kaart geselecteerd voor de wisselfase?
const isSelected = selectedCardsToPass.some(
c => c.suit === card.suit && c.rank === card.rank
);
if (isSelected) {
cardBtn.style.border = '3px solid gold';
cardBtn.style.backgroundColor = '#e0f7fa';
}
// Klik-handler afhankelijk van de spelfase
cardBtn.onclick = () => {
if (gameState.state === 'PASSING_CARDS') {
toggleCardSelection(card, gameState);
} else if (gameState.state === 'PLAYING_TRICKS') {
playCard(card.suit, card.rank);
}
};
handDiv.appendChild(cardBtn);
});
}
// Toon kaarten op tafel
const trickDiv = document.getElementById('current-trick');
if (gameState.current_trick && gameState.current_trick.played_moves) {
const moves = Object.values(gameState.current_trick.played_moves);
if (moves.length === 0) {
trickDiv.innerHTML = "<i>Geen kaarten op tafel (nieuwe slag)</i>";
} else {
const cardsText = moves.map(move => {
const card = move.card;
const symbol = getSuitSymbol(card.suit);
return `<b>${move.player_name}:</b> ${symbol} ${getRankName(card.rank)}`;
}).join(' | ');
trickDiv.innerHTML = cardsText;
}
}
// Scores bijwerken
if (gameState.total_scores) {
const scoresText = Object.entries(gameState.total_scores)
.map(([pId, score]) => `${pId}: ${score} pt`)
.join(' | ');
document.getElementById('total-scores').innerText = scoresText;
}
}
// Hulpfunctie: Toggle selectie van kaarten voor de pas-fase
function toggleCardSelection(card, gameState) {
const index = selectedCardsToPass.findIndex(
c => c.suit === card.suit && c.rank === card.rank
);
if (index > -1) {
selectedCardsToPass.splice(index, 1); // Deselecteer
} else {
if (selectedCardsToPass.length >= 3) {
alert("Je hebt al 3 kaarten geselecteerd!");
return;
}
selectedCardsToPass.push(card); // Selecteer
}
// Ververst de hand-weergave om de gouden rand bij te werken
updateUI(gameState);
}
// Hulpfuncties voor opmaak
function getSuitSymbol(suit) {
switch (suit) {
case 'CLUBS': return '♣';
case 'DIAMONDS': return '<span style="color:red;">♦</span>';
case 'HEARTS': return '<span style="color:red;">♥</span>';
case 'SPADES': return '♠';
default: return suit;
}
}
function getRankName(rank) {
switch (rank) {
case 11: return 'J';
case 12: return 'Q';
case 13: return 'K';
case 14: return 'A';
default: return rank;
}
}
function log(msg) {
const logDiv = document.getElementById('log');
if (logDiv) logDiv.innerHTML += `<br>${msg}`;
}
</script>
</body>
</html>