-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
executable file
·609 lines (522 loc) · 23.6 KB
/
index.html
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Real-time Tic Tac Toe with Super Chat!</title>
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Indie+Flower" />
<style>
body {
background-image: url("bg.jpg");
}
h1 {
text-align: center;
color: #005;
font: 100 35px "Indie Flower", Helvetica, sans-serif;
text-shadow: 3px 3px 0px rgba(0,0,0,0.1), 3px 3px 0px rgba(0,0,0,0.05);
}
.left-game-zone {
transform-style: preserver-3d;
position: absolute;
margin-top: -1%;
}
.canvas-wrapper {
width: 100%;
}
#tic-tac-toe-board {
display: block;
margin-left: 10%;
margin-bottom: 2%;
}
#resetGameButton {
margin-left: 45%;
margin-top: 2%;
padding-left: 10%;
padding-right: 10%;
padding-top: 1%;
padding-bottom: 1%;
}
#playerToMove {
margin-left: 10%;
position: left;
color: red;
font: 100 19px "Indie Flower", Helvetica, sans-serif;
text-shadow: 1px 1px 0px rgba(0,0,0,0.1), 1px 1px 0px rgba(0,0,0,0.05);
}
@media (min-width:1151px) {
.right-chat-zone {
color: #005;
font: 100 19px "Indie Flower", Helvetica, sans-serif;
text-shadow: 1px 1px 0px rgba(0,0,0,0.1), 1px 1px 0px rgba(0,0,0,0.05);
border-style: groove;
display: block;
margin-left: 50%;
margin-right: 15%;
width: 35%;
}
.right-video-zone {
margin-top: -2%;
margin-left: 50%;
padding: 0;
}
#chatZone strong {
color: white;
background-color: black;
padding: 1.5px;
}
#chat_form #message {
margin-left: 2%;
width: 68%;
}
.myBox {
width: 500px;
height: 280px;
overflow: auto;
}
#sendButton {
width: 11%;
margin-left: 0%;
}
#callButton {
width: 5%;
margin-top: 4px;
position: absolute;
margin-left: 29%;
}
}
@media (max-width:1150px) {
.right-chat-zone {
color: #005;
font: 100 19px "Indie Flower", Helvetica, sans-serif;
text-shadow: 1px 1px 0px rgba(0,0,0,0.1), 1px 1px 0px rgba(0,0,0,0.05);
border-style: groove;
display: block;
margin-top: 15%;
margin-left: 15%;
margin-right: 15%;
width: 70%;
}
#sendButton {
width: 11%;
}
#callButton {
width: 10%;
margin-top: 5px;
position: absolute;
margin-left: 58%;
}
#chat_form #message {
width: 70%;
}
.myBox {
width: 500px;
height: 280px;
overflow: auto;
}
.right-video-zone {
margin-top: 3%;
margin-left: 60%;
padding: 0;
}
}
</style>
</head>
<body>
<h1>Real-time Tic Tac Toe with Super Chat!</h1>
<div class="left-game-zone">
<div class="canvas-wrapper">
<canvas id="tic-tac-toe-board" class="center-v"></canvas>
<span id="playerToMove">Waiting for players...</span>
<button type="submit" id="resetGameButton">Reset Game</button>
</div>
</div>
<div class="right-video-zone">
<video id="localVideo" width="260" height="250" autoplay> </video>
<video id="remoteVideo" width="260" height="250" autoplay> </video>
</div>
<div class="right-chat-zone">
<input type="submit" id="callButton" value="Call" />
<form action="/" method="post" id="chat_form">
<input type="text" name="message" id="message" placeholder="Your message..." size="45" autofocus />
<input type="submit" id="sendButton" value="Send" />
</form>
<div class="myBox">
<section id="chatZone"></section>
</div>
</div>
<script src="jquery-1.10.1.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
/* ################## CHAT CODE ################## */
// Connecting to socket.io
var socket = io.connect("https://192.168.1.2:443"); // 212.251.17.119
// The username is requested, sent to the server and displayed in the title.
var user_name = "";
var user_id = 0;
var opponent_name = "";
while(true) { // username has to be in correct format
user_name = prompt("What\'s your username? (at least 3 characters)");
if (user_name && user_name.length >= 3)
break;
}
socket.emit("new_client", user_name);
document.title = "[" + user_name + "] " + document.title;
// When a message is received it's inserted in the page.
socket.on("message", function (data) {
insertMessage(data.username, data.message)
})
// When a new client connects, the information is displayed.
// Also, if there is no opponent, it means that only 1 user is connected.
// If another one will connect, it means that this will be the opponent.
// His name will be saved.
socket.on("new_client", function (username) {
$("#chatZone").prepend("<p><em>" + username + " has joined the chat!</em></p>");
if (opponent_name === "")
opponent_name = username;
})
// When a new client connects, the information is displayed in common chat board.
socket.on("client_left", function (username) {
$("#chatZone").prepend("<p><em>" + username + " has left!</em></p>");
})
// When the form is submitted, the message is sent and displayed on the page.
$("#chat_form").submit(function () {
var message = $("#message").val();
if (message.length > 0) {
socket.emit("message", message); // Sends the message to the others
insertMessage(user_name, message); // Also displays the message on our page
}
$("#message").val('').focus(); // Empties the chat form and puts the focus back on it
return false; // Blocks 'classic' sending of the form
});
// Adds a message to the common chat board.
function insertMessage (username, message) {
$("#chatZone").prepend("<p><strong>" + username + "</strong> " + message + "</p>");
}
/* ################## VIDEO CODE ################## */
var localStream;
var localVideo = document.getElementById("localVideo");
var remoteVideo = document.getElementById("remoteVideo");
var callButton = document.getElementById("callButton");
// we are waiting for loading streams - if they are not available, we
// cannot call anyway.
callButton.disabled = true;
callButton.onclick = call;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
navigator.getUserMedia({audio:true, video:true}, gotStream, // both audio and video
function (error) { console.log(error); });
var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var SessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription;
var pc = new RTCPeerConnection({"iceServers": []});
function gotStream (stream) {
localVideo.src = window.URL.createObjectURL(stream);
localStream = stream;
callButton.disabled = false; // we got stream, we can call now
pc.addStream(stream);
}
pc.onicecandidate = function (event) {
console.log(event);
if (!event || !event.candidate)
return;
else
socket.emit("video call", {type: "iceCandidate", "candidate": event.candidate});
};
var remoteStream;
pc.onaddstream = function (event) {
remoteStream = event.stream;
var remoteVideo = document.getElementById("remoteVideo");
remoteVideo.src = window.URL.createObjectURL(event.stream);
remoteVideo.play();
};
socket.on("video call", function (data) {
switch(data.type) {
case "iceCandidate":
console.log("case : iceCandidate");
pc.addIceCandidate(new RTCIceCandidate(data.candidate));
break;
case "offer":
console.log("case : offer");
pc.setRemoteDescription(new SessionDescription(data.description), function() { console.log("failure callback"); });
pc.createAnswer(function(description) {
pc.setLocalDescription(new SessionDescription(description));
socket.emit("video call", {type: "answer", "description": description});
}, function(){ console.log("failure callback"); });
break;
case "answer":
console.log("case : answer");
pc.setRemoteDescription(new SessionDescription(data.description),function(){ console.log("failure callback"); });
break;
}
});
function call() {
console.log("Calling another peer");
console.log(pc.iceConnectionState);
console.log(pc);
if(pc.iceConnectionState == "closed"){
pc = new RTCPeerConnection({"iceServers": []});
pc.addStream(localStream);
console.log(pc);
}
pc.createOffer( function (description) {
console.log("Creating offer for the other peer");
pc.setLocalDescription(new SessionDescription(description), function(){ console.log("failure callback"); });
socket.emit("video call", {type: "offer", "description": description});
}, function() { console.log("failure callback"); });
};
</script>
<script type="text/javascript">
/* ################## GAME CODE ################## */
"use strict";
var FIELDS_TO_WIN = 4,
NR_FIELDS = 14,
CANVAS_SIZE = 500,
SECTION_SIZE = CANVAS_SIZE / NR_FIELDS,
LINE_COLOR = "#005",
LINE_SIZE = 2,
canvas,
context,
board = [],
player_can_move = false;
var beep = new Audio("beep.mp3");
beep.volume = 0.2;
function proceedWinning (player) {
var winner_name = "";
if (player === user_id)
winner_name = user_name;
else
winner_name = opponent_name;
alert("Player '" + winner_name + "' won!");
socket.emit("reset_game");
resetGame();
}
/* https://stackoverflow.com/questions/35917734/how-do-i-traverse-an-array-diagonally-in-javascript */
function diagonal (array, bottomToTop) {
var returnArray = [];
for (var k = 0; k <= 2 * (NR_FIELDS - 1); ++k)
{
var temp = [];
for (var y = NR_FIELDS - 1; y >= 0; --y)
{
var x = k - (bottomToTop ? NR_FIELDS - y : y);
if (x >= 0 && x < NR_FIELDS)
temp.push(array[y][x]);
}
if(temp.length >= FIELDS_TO_WIN)
returnArray.push(temp);
}
return returnArray;
}
function helperWinArrChecker (arr_to_check) {
var last_player = 0;
var player_points = 0;
player_can_move = false;
for (var x = 0; x < arr_to_check.length; x++)
{
if (arr_to_check[x] !== last_player)
player_points = 1;
else
player_points++;
last_player = arr_to_check[x];
if (last_player !== 0 && player_points >= FIELDS_TO_WIN)
{
setTimeout(function() { proceedWinning(last_player); }, 200);
return true;
}
}
return false;
}
function checkWin () {
console.log(board);
/* Check columns and rows */
for (var x = 0; x < NR_FIELDS; x++)
{
var tmp_arr_columns = [];
var tmp_arr_rows = [];
for (var y = 0; y < NR_FIELDS; y++)
{
tmp_arr_columns.push(board[y][x]);
tmp_arr_rows.push(board[x][y]);
}
if (helperWinArrChecker(tmp_arr_columns))
return;
if (helperWinArrChecker(tmp_arr_rows))
return;
}
/* Check both diagonal ways: / and \ directions */
var diagonal_arrays = diagonal(board, true);
for (var x = 0; x <= diagonal_arrays.length-1; x++) {
if (helperWinArrChecker(diagonal_arrays[x]))
return;
}
var diagonal_arrays = diagonal(board, false);
for (var x = 0; x <= diagonal_arrays.length-1; x++) {
if (helperWinArrChecker(diagonal_arrays[x]))
return;
}
return;
}
function drawO (xCordinate, yCordinate) {
var halfSectionSize = (0.5 * SECTION_SIZE);
var centerX = xCordinate + halfSectionSize;
var centerY = yCordinate + halfSectionSize;
var radius = (SECTION_SIZE - 25 + (NR_FIELDS / 2)) / 2;
var startAngle = 0 * Math.PI;
var endAngle = 2 * Math.PI;
context.lineWidth = 4;
context.strokeStyle = "red";
context.beginPath();
context.arc(centerX, centerY, radius, startAngle, endAngle);
context.stroke();
}
function drawX (xCordinate, yCordinate) {
context.lineWidth = 4;
context.strokeStyle = "orange";
context.beginPath();
var offset = (SECTION_SIZE - 25 + (NR_FIELDS / 2)) / 2;
context.moveTo(xCordinate + offset, yCordinate + offset);
context.lineTo(xCordinate + SECTION_SIZE - offset, yCordinate + SECTION_SIZE - offset);
context.moveTo(xCordinate + offset, yCordinate + SECTION_SIZE - offset);
context.lineTo(xCordinate + SECTION_SIZE - offset, yCordinate + offset);
context.stroke();
}
function getCanvasMousePosition (event) {
var rect = canvas.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top
}
}
function addOpponentMove (opponentName, opponentId, move) {
opponent_name = opponentName; // allocate always - it can be changed if 1 user left and other comes...
var done_move = move.split(",");
if (done_move.length !== 2) {
console.log("There is some error in coordinations sent from Player '" + opponent_name + "'.");
return;
}
var yCordinate = done_move[0] * SECTION_SIZE;
var xCordinate = done_move[1] * SECTION_SIZE;
if (opponentId === 1)
drawO(xCordinate, yCordinate);
else
drawX(xCordinate, yCordinate);
board[done_move[0]][done_move[1]] = opponentId;
}
function addPlayerMove (event) {
var mouse = getCanvasMousePosition(event);
var xCordinate;
var yCordinate;
for (var x = 0; x < NR_FIELDS; x++)
{
for (var y = 0; y < NR_FIELDS; y++)
{
if (board[y][x] != 0) // no move, field is already occupied
continue;
xCordinate = x * SECTION_SIZE;
yCordinate = y * SECTION_SIZE;
if (
mouse.x >= xCordinate && mouse.x <= xCordinate + SECTION_SIZE &&
mouse.y >= yCordinate && mouse.y <= yCordinate + SECTION_SIZE
) {
if (user_id === 1)
drawO(xCordinate, yCordinate);
else
drawX(xCordinate, yCordinate);
board[y][x] = user_id;
var given_move = y + ',' + x;
socket.emit("given_move", {username: user_name, user_id: user_id, given_move: given_move});
return true; // there was a move
}
}
}
return false; // there was no move
}
function drawLines (lineWidth, strokeStyle) {
var lineStart = 4;
var lineLenght = CANVAS_SIZE - 5;
context.lineWidth = lineWidth;
context.lineCap = "round";
context.strokeStyle = strokeStyle;
context.beginPath();
/* Horizontal lines */
for (var y = 1; y < NR_FIELDS; y++)
{
context.moveTo(lineStart, y * SECTION_SIZE);
context.lineTo(lineLenght, y * SECTION_SIZE);
}
/* Vertical lines */
for (var x = 1; x < NR_FIELDS; x++)
{
context.moveTo(x * SECTION_SIZE, lineStart);
context.lineTo(x * SECTION_SIZE, lineLenght);
}
context.stroke();
}
function getInitialBoard () {
board = [];
for (var x = 0; x < NR_FIELDS; x++)
{
board.push([]);
for (var y = 0; y < NR_FIELDS; y++)
board[x].push(0);
}
return board;
}
function resetGame () {
context.clearRect(0, 0, canvas.width, canvas.height);
drawLines(LINE_SIZE, LINE_COLOR);
board = getInitialBoard();
}
function initGame () {
canvas = document.getElementById("tic-tac-toe-board");
context = canvas.getContext("2d");
canvas.width = CANVAS_SIZE;
canvas.height = CANVAS_SIZE;
context.translate(0.5, 0.5);
drawLines(LINE_SIZE, LINE_COLOR);
board = getInitialBoard();
var reset_button = document.getElementById("resetGameButton");
reset_button.addEventListener("click", function() {
resetGame();
socket.emit("reset_game");
});
socket.on("game_ready_get_uids", function(ready_players) {
console.log(ready_players);
// Game starts now. Time to save player ID and opponent name.
// Opponent ID is not needed.
for (var key in ready_players) {
if (key === user_name)
user_id = ready_players[key];
else
opponent_name = key;
}
if (user_id === 1) { // there will be only 2 players with ID 1 and 2
document.getElementById("playerToMove").textContent = "Player to move - " + user_name;
player_can_move = true; // player with ID 1 starts the game
}
else {
document.getElementById("playerToMove").textContent = "Player to move - " + opponent_name;
player_can_move = false;
}
});
canvas.addEventListener("click", function(event) {
if (player_can_move) {
// if he made a move or not. There could be another field already occupied.
if (addPlayerMove(event)) {
player_can_move = false;
document.getElementById("playerToMove").textContent = "Player to move - " + opponent_name;
beep.play();
checkWin();
}
}
});
socket.on("given_move", function(data) {
addOpponentMove(data.username, data.user_id, data.given_move);
checkWin();
document.getElementById("playerToMove").textContent = "Player to move - " + user_name;
player_can_move = true;
});
socket.on("reset_game", function() { resetGame(); });
}
// Game starts right now, right there!
initGame();
</script>
</body>
</html>