-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (112 loc) · 3.17 KB
/
index.js
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
/**
* Game register
*/
var team;
var id;
var isInGame = false;
function lostConnection(disconnectedText) {
console.log("disconnected");
$("#warning-info").text(disconnectedText);
$("#warning-info").show();
}
function pingServer() {
$.post(path + "/input", {
id: id,
direction: 0
}, function() {
$("#warning-info").hide();
}).fail(function() {
lostConnection("Disconnected, reconnecting...");
});
}
function register() {
$("#team-info").text("Registering into a team...");
$("#team-info").show();
$.get(path + "/register", function(data) {
console.log(data);
team = data.team;
id = data.id;
$("#team-info").text("You are in team " + data.team);
/* Set leave page warning */
$(window).bind('beforeunload', function(){
return "Are you sure you want to leave?";
});
/* Set ping timer */
pingTimer = setInterval(pingServer, heartTime);
/* Start game */
isInGame = true;
/* Enable buttons */
$(".paddle").prop("disabled", false);
$(".paddle").removeClass("not-enabled btn-outline-secondary");
/* Set buttons colour */
if (data.team === "zucc") {
$(".paddle").addClass("paddle-zucc");
} else if (data.team === "user") {
$(".paddle").addClass("paddle-user");
}
}).fail(function() {
$("#team-info").hide();
lostConnection("Disconnected, please reload the page");
});
}
function sendMotion(direction) {
var audio = new Audio("http://soundbible.com/grab.php?id=1705&type=mp3");
audio.play();
try {
var vibrate = window.navigator.vibrate(100);
} catch (err) {
console.log("vibrate not supported");
}
$.post(path + "/input", {
id: id,
direction: direction
}, function() {
console.log(direction + " sent");
}).fail(function() {
lostConnection("Disconnected, reconnecting...");
});
}
/* Hide JavaScript warning */
$("#warning-info").hide();
register();
$(".paddle").click(function() {
if (!isInGame) {
return;
}
direction = parseInt($(this).data("direction"));
console.log(direction);
sendMotion(direction);
})
$(document).keydown(function(e) {
console.log(e.keyCode + " pressed");
switch(e.keyCode) {
case 38:
// move up
$(".paddle").first().addClass("paddle-" + team +"-active");
sendMotion(-1);
break;
case 40:
// move down
$(".paddle").last().addClass("paddle-" + team +"-active");
sendMotion(1);
break;
default:
break;
}
});
$(document).keyup(function(e) {
switch(e.keyCode) {
case 38:
// move up
$(".paddle").first().removeClass("paddle-zucc-active");
$(".paddle").first().removeClass("paddle-user-active");
break;
case 40:
// move down
$(".paddle").last().removeClass("paddle-zucc-active");
$(".paddle").last().removeClass("paddle-user-active");
break;
default:
break;
}
});