-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandombot.js
More file actions
31 lines (26 loc) · 965 Bytes
/
Copy pathrandombot.js
File metadata and controls
31 lines (26 loc) · 965 Bytes
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
const U = 'http://127.0.0.1:8000/api/game';
const R = r => r.json();
async function run() {
let g = await fetch(`${U}/start`, { method: 'POST' }).then(R);
const id = g.game_id;
while (!g.game_state.is_finished) {
const s = g.game_state;
const p1 = s.players.find(p => p.id === 'p1');
console.log(s.total_scores);
if (s.state === 'PASSING_CARDS') {
g = await fetch(`${U}/${id}/pass`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ cards: p1.hand.slice(0, 3) })
}).then(R);
} else if (s.state === 'PLAYING_TRICKS' && s.current_player === 'p1') {
const card = s.legal_moves[Math.floor(Math.random() * s.legal_moves.length)];
g = await fetch(`${U}/${id}/play`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ suit: card.suit, rank: card.rank })
}).then(R);
}
}
}
run();