-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.js
108 lines (101 loc) · 4.47 KB
/
socket.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
function check_for_multiplayergame() {
// Receiver of the game runs this function
window.gid = window.location.hash;
var url = window.location.href;
window.socket = new EasyWebSocket(url);
window.socket.onopen = function(){
window.socket.send(window.uid+':LoggedIn');
}
window.socket.onmessage = function(event){
console.log("received "+ event.data);
var message = event.data.split(':');
var uid = message[0];
var status = message[1];
if (uid != window.uid && window.opponentUid == undefined) {
// If another user has send the message, check it out
if (status == 'LoggedIn') {
// add user as opponent, usually not needed, this function run only when being second player
window.opponentUid = uid;
} else if (status == 'Settings') {
console.log('receiving settings:', message);
window.opponentUid = uid;
var opponent_loc = message[2].split(',');
var own_loc = message[3].split(',');
window.startPos = new google.maps.LatLng(own_loc[0], own_loc[1]);
window.goalPosition = new google.maps.LatLng(opponent_loc[0], opponent_loc[1]);
window.opponentPos = new google.maps.LatLng(opponent_loc[0], opponent_loc[1]);
set_difficulty(parseInt(message[4]));
// setup game
// start game, and send 'Ready' if ready
startMultiPlayerGame();
}
} else if (uid == window.opponentUid) {
if (status == 'Location') {
var loc = message[2].split(',');
window.opponentPos = new google.maps.LatLng(loc[0], loc[1]);
window.opponentDist = loc[2];
opponentMoved();
} else if (status == 'Finish') {
//finish game, you've probably lost it
$('pano_canvas').grab(new Element('div.notice', {
id: 'gameover',
html: 'Aww!!<br/>You Lost! Your opponent has captured your flag!<br/>Refresh to play again!',
}));
$('gameover').highlight('#f88', '#fff');
}
}
}
}
function init_multiplayergame() {
// Owner of the game runs this function
// setup game: open Socket w/ listeners, and setup own game (not start yet..
window.gid = window.location.hash;
var url = window.location.href;
window.socket = new EasyWebSocket(url);
window.socket.onopen = function(){
window.socket.send(window.uid+':LoggedIn');
}
window.socket.onmessage = function(event){
console.log("received "+ event.data);
var message = event.data.split(':');
var uid = message[0];
var status = message[1];
if (uid != window.uid && window.opponentUid == undefined) {
// If another user has send the message, check it out
if (status == 'LoggedIn') {
// add user as opponent
window.opponentUid = uid;
// Send opponent Settings: both locations
window.socket.send(window.uid+':Settings:' +
window.startPos.lat()+','+window.startPos.lng()+':' +
window.goalPosition.lat()+','+window.goalPosition.lng()+':' +
window.difficulty);
}
} else if (uid == window.opponentUid) {
if (status == 'Ready') {
// start_game
$('waiting_msg').dispose();
startMultiPlayerGame();
} else if (status == 'Location') {
var loc = message[2].split(',');
window.opponentPos = new google.maps.LatLng(loc[0], loc[1]);
window.opponentDist = loc[2];
opponentMoved();
} else if (status == 'Finish') {
//finish game, you've probably lost it
$('pano_canvas').grab(new Element('div.notice', {
id: 'gameover',
html: 'Aww!!<br/>You Lost! Your opponent has captured your flag!<br/>Refresh to play again!',
}));
$('gameover').highlight('#f88', '#fff');
}
}
}
// Display Waiting message
$('text_box').set('opacity', 0);
$('disclaimer').set('opacity', 0);
$('pano_canvas').grab(new Element('div.notice', {
id: 'waiting_msg',
html: 'Waiting for opponent to connect...'
}));
}