-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
146 lines (130 loc) · 3.79 KB
/
Copy pathserver.js
File metadata and controls
146 lines (130 loc) · 3.79 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
// server.js
const http = require('http');
const WebSocket = require('ws');
const server = http.createServer();
const wss = new WebSocket.Server({ server });
const rooms = {}; // roomCode -> { players: [ws,...], state }
function broadcast(roomCode) {
const room = rooms[roomCode];
if (!room) return;
const payload = JSON.stringify({ type: 'state', state: room.state });
room.players.forEach(ws => {
if (ws.readyState === WebSocket.OPEN) ws.send(payload);
});
}
function createDeck() {
const suits = ['♠','♥','♦','♣'];
const ranks = ['A','2','3','4','5','6','7','8','9','10','J','Q','K'];
const d = [];
for (let s of suits) for (let r of ranks) d.push({ rank: r, suit: s });
for (let i = d.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[d[i], d[j]] = [d[j], d[i]];
}
return d;
}
function cardValue(c) {
if (c.rank === 'A') return 11;
if (['J','Q','K'].includes(c.rank)) return 10;
return parseInt(c.rank, 10);
}
function handValue(hand) {
let total = 0, aces = 0;
for (let c of hand) {
total += cardValue(c);
if (c.rank === 'A') aces++;
}
while (total > 21 && aces > 0) {
total -= 10;
aces--;
}
return total;
}
wss.on('connection', ws => {
let joinedRoom = null;
let playerId = null;
ws.on('message', msg => {
let data;
try { data = JSON.parse(msg); } catch { return; }
if (data.type === 'join') {
const roomCode = data.room || 'default';
if (!rooms[roomCode]) {
rooms[roomCode] = {
players: [],
state: {
deck: [],
dealer: [],
players: [],
currentPlayer: 0,
inRound: false,
message: ''
}
};
}
const room = rooms[roomCode];
room.players.push(ws);
playerId = room.state.players.length;
room.state.players.push({ hand: [], total: 0 });
joinedRoom = roomCode;
broadcast(joinedRoom);
}
if (!joinedRoom) return;
const room = rooms[joinedRoom];
const state = room.state;
if (data.type === 'deal') {
state.deck = createDeck();
state.dealer = [];
state.players.forEach(p => p.hand = []);
state.inRound = true;
state.currentPlayer = 0;
state.message = '';
state.players.forEach(p => {
p.hand.push(state.deck.pop());
});
state.dealer.push(state.deck.pop());
state.players.forEach(p => {
p.hand.push(state.deck.pop());
});
state.dealer.push(state.deck.pop());
state.players.forEach(p => p.total = handValue(p.hand));
broadcast(joinedRoom);
}
if (data.type === 'hit' && state.inRound) {
const p = state.players[state.currentPlayer];
p.hand.push(state.deck.pop());
p.total = handValue(p.hand);
if (p.total > 21) {
state.message = `Player ${state.currentPlayer + 1} busts.`;
state.currentPlayer++;
if (state.currentPlayer >= state.players.length) {
// dealer plays
while (handValue(state.dealer) < 17) {
state.dealer.push(state.deck.pop());
}
state.inRound = false;
}
}
broadcast(joinedRoom);
}
if (data.type === 'stand' && state.inRound) {
state.currentPlayer++;
if (state.currentPlayer >= state.players.length) {
while (handValue(state.dealer) < 17) {
state.dealer.push(state.deck.pop());
}
state.inRound = false;
}
broadcast(joinedRoom);
}
});
ws.on('close', () => {
if (!joinedRoom) return;
const room = rooms[joinedRoom];
if (!room) return;
room.players = room.players.filter(p => p !== ws);
if (room.players.length === 0) delete rooms[joinedRoom];
});
});
server.listen(8080, () => {
console.log('Server on http://localhost:8080');
});