-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
341 lines (289 loc) · 9.42 KB
/
Copy pathscript.js
File metadata and controls
341 lines (289 loc) · 9.42 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// State
let myUsername = '';
let currentMode = null;
let myScore = 0;
let opponentScore = 0;
// PeerJS State
let peer = null;
let conn = null; // Connection to opponent
let isHost = false;
let myMove = null;
let opponentMove = null;
// DOM Elements
const screens = {
login: document.getElementById('login-screen'),
waiting: document.getElementById('waiting-screen'),
game: document.getElementById('game-screen')
};
const dom = {
usernameInput: document.getElementById('username'),
roomIdDisplay: document.getElementById('room-id-display'),
shareLinkInput: document.getElementById('share-link'),
p1Name: document.getElementById('p1-name'),
p2Name: document.getElementById('p2-name'),
p1Score: document.getElementById('p1-score'),
p2Score: document.getElementById('p2-score'),
gameStatus: document.getElementById('game-status'),
resultOverlay: document.getElementById('result-overlay'),
resultMessage: document.getElementById('result-message')
};
// Check for room in URL (Peer ID of host)
const urlParams = new URLSearchParams(window.location.search);
const invitePeerId = urlParams.get('room');
if (invitePeerId) {
// If invited, adjust UI to focus on joining
const pvcBtn = document.getElementById('btn-pvc');
const pvpBtn = document.getElementById('btn-pvp');
if (pvcBtn) pvcBtn.style.display = 'none';
if (pvpBtn) {
pvpBtn.textContent = 'Entrar no Jogo';
pvpBtn.classList.add('btn-primary'); // Make it pop more
pvpBtn.classList.remove('btn-secondary');
}
}
function showScreen(screenName) {
Object.values(screens).forEach(s => s.classList.remove('active'));
screens[screenName].classList.add('active');
}
function joinGame(mode) {
const username = dom.usernameInput.value.trim() || 'Jogador';
myUsername = username;
currentMode = mode;
if (mode === 'pvc') {
startGamePvC();
} else {
// PvP Mode via PeerJS
initializePeer();
}
}
function initializePeer() {
showScreen('waiting');
dom.gameStatus.textContent = 'Inicializando conexão...';
// Create Peer
peer = new Peer(); // Auto-generate ID
peer.on('open', (id) => {
console.log('My Peer ID:', id);
if (invitePeerId) {
// We are JOINING a game
isHost = false;
connectToHost(invitePeerId);
} else {
// We are HOSTING a game
isHost = true;
setupHostUI(id);
}
});
peer.on('connection', (c) => {
// Incoming connection (only expected if Host)
if (conn && conn.open) {
c.close(); // Already connected
return;
}
conn = c;
setupConnectionHandlers();
});
peer.on('error', (err) => {
console.error(err);
alert('Erro na conexão P2P: ' + err.type);
leaveGame();
});
}
function setupHostUI(id) {
dom.roomIdDisplay.textContent = `ID da Sala: ${id}`;
const protocol = window.location.protocol;
const host = window.location.host;
const pathname = window.location.pathname; // Important for github pages usually /repo/
const shareUrl = `${protocol}//${host}${pathname}?room=${id}`;
dom.shareLinkInput.value = shareUrl;
dom.gameStatus.textContent = 'Aguardando oponente conectar...';
}
function connectToHost(hostId) {
dom.roomIdDisplay.textContent = 'Conectando ao host...';
conn = peer.connect(hostId, {
metadata: { username: myUsername }
});
setupConnectionHandlers();
}
function setupConnectionHandlers() {
conn.on('open', () => {
console.log('Connected!');
// Send my username
conn.send({ type: 'hello', username: myUsername });
});
conn.on('data', (data) => {
handleData(data);
});
conn.on('close', () => {
alert('Oponente desconectou!');
leaveGame();
});
}
function handleData(data) {
switch (data.type) {
case 'hello':
// Opponent sent their name
dom.p2Name.textContent = data.username;
if (isHost && conn) {
// Determine who is p1/p2 visually is local, but let's sync
// Send back my name just in case
conn.send({ type: 'hello_ack', username: myUsername });
startGamePvP();
}
break;
case 'hello_ack':
dom.p2Name.textContent = data.username;
startGamePvP();
break;
case 'move':
opponentMove = data.move;
checkRoundComplete();
break;
case 'result':
// Host sent result (for Joiner) or Sync
// Actually simpler: Host calculates, sends result. Joiner trusts Host.
// Or both calculate. Let's have Host calculate to allow P2P authoritative simple logic.
if (!isHost) {
handleResult(data);
}
break;
}
}
function startGamePvC() {
dom.p1Name.textContent = myUsername;
dom.p2Name.textContent = 'Computador';
showScreen('game');
dom.gameStatus.textContent = 'Escolha sua jogada!';
}
function startGamePvP() {
dom.p1Name.textContent = myUsername; // Me (always left)
// p2Name already set
showScreen('game');
dom.gameStatus.textContent = 'Batalha iniciada!';
}
function makeMove(move) {
if (currentMode === 'pvc') {
const computerMove = ['Pedra', 'Papel', 'Tesoura'][Math.floor(Math.random() * 3)];
const result = getWinner(move, computerMove);
showResult(move, computerMove, result);
} else {
// PvP
if (!conn) return;
myMove = move;
dom.gameStatus.textContent = `Você escolheu ${move}. Aguardando oponente...`;
// Send move to opponent
conn.send({ type: 'move', move: move });
checkRoundComplete();
}
}
function checkRoundComplete() {
if (myMove && opponentMove) {
// Both have moved
// Logic can be done locally by both if honest, or Host can authorize.
// Let's do local consistency for simplicity (Trust model).
const result = getWinner(myMove, opponentMove);
showResult(myMove, opponentMove, result);
// PeerJS generic handling - if Host, maybe send official result?
// Not strictly necessary if deterministic.
// Reset moves
myMove = null;
opponentMove = null;
}
}
function showResult(myM, opM, result) {
let message = '';
let color = '';
if (result === 'Vitória') {
message = 'VOCÊ VENCEU! 🎉';
color = 'var(--success)';
myScore++;
if (typeof Wowify !== 'undefined') {
Wowify.startParty({
confettiDuration: 3000,
colors: ['#bb00ff', '#00ffff', '#ffffff']
});
}
} else if (result === 'Derrota') {
message = 'VOCÊ PERDEU 💀';
color = 'var(--danger)';
opponentScore++;
} else {
message = 'EMPATE 🤝';
color = 'var(--secondary)';
}
const myEmoji = getEmoji(myM);
const opEmoji = getEmoji(opM);
// Update Score UI
dom.p1Score.textContent = myScore;
dom.p2Score.textContent = opponentScore;
// Update overlay
dom.resultMessage.textContent = message;
dom.resultMessage.style.color = color;
const existingDetails = dom.resultOverlay.querySelector('.result-details');
if (existingDetails) existingDetails.remove();
const detailsDiv = document.createElement('div');
detailsDiv.className = 'result-details';
detailsDiv.style.fontSize = '1.5rem';
detailsDiv.style.marginTop = '10px';
detailsDiv.style.marginBottom = '20px';
detailsDiv.innerHTML = `Você <span style="font-size: 2rem">${myEmoji}</span> VS <span style="font-size: 2rem">${opEmoji}</span> Oponente`;
dom.resultMessage.after(detailsDiv);
setTimeout(() => {
dom.resultOverlay.classList.remove('hidden');
}, 500);
}
function resetRound() {
dom.resultOverlay.classList.add('hidden');
dom.gameStatus.textContent = 'Escolha sua jogada!';
}
function leaveGame() {
if (peer) {
peer.destroy();
peer = null;
}
if (conn) {
conn.close();
conn = null;
}
currentMode = null;
isHost = false;
myMove = null;
opponentMove = null;
// Reset Scores
myScore = 0;
opponentScore = 0;
dom.p1Score.textContent = '0';
dom.p2Score.textContent = '0';
dom.resultOverlay.classList.add('hidden');
dom.gameStatus.textContent = 'Escolha sua jogada!';
showScreen('login');
}
function getWinner(p1, p2) {
if (p1 === p2) return 'Empate';
if (
(p1 === 'Pedra' && p2 === 'Tesoura') ||
(p1 === 'Papel' && p2 === 'Pedra') ||
(p1 === 'Tesoura' && p2 === 'Papel')
) {
return 'Vitória';
}
return 'Derrota';
}
function getEmoji(move) {
switch (move) {
case 'Pedra': return '✊';
case 'Papel': return '✋';
case 'Tesoura': return '✌️';
default: return move;
}
}
function copyLink() {
const copyText = dom.shareLinkInput;
copyText.select();
copyText.setSelectionRange(0, 99999);
navigator.clipboard.writeText(copyText.value);
const originalText = document.querySelector('.share-container button').textContent;
document.querySelector('.share-container button').textContent = 'Copiado!';
setTimeout(() => {
document.querySelector('.share-container button').textContent = originalText;
}, 2000);
}