-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
155 lines (127 loc) · 4.84 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
<html>
<head>
<title>Whack a Fraudster</title>
<script src="elm.js"></script>
<script type="text/javascript" src="hack-cambridge-dist-webtag.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<script>
var app = Elm.Main.fullscreen();
var fingerprint;
var firstLocation;
// open connection
var connection = new WebSocket('ws://192.168.1.146:1337');
if(!window.WebSocket) {
// content.html($('<p>', { text:'Sorry, but your browser doesn\'t support WebSocket.'}));
// return;
console.log("websockets not supported");
}
connection.onopen = function () {
// connection is opened and ready to use
console.log("connection open");
FSJSC_profile('1234', null, function(data) {
if(Object.keys(data.eventData)[0] === "f") {
// fingerprint
fingerprint = data.eventData.f;
connection.send(JSON.stringify({type: "fingerprint", data: data.eventData.f}));
} else {
// behaviour
if(typeof firstLocation === "undefined") {
firstLocation = data.eventData.b;
}
connection.send(JSON.stringify({type: "mouseMove", data: data.eventData.b}));
}
});
app.ports.connectionOpenSignal.send(null);
};
connection.onmessage = function (message) {
// console.log("message received", message);
if(JSON.parse(message.data)) {
var parsedData = JSON.parse(message.data);
if(parsedData.type === "registration") {
// console.log("parsedData", parsedData.data);
app.ports.registeredAsLeadPlayer.send(parsedData.data);
}
if(parsedData.type === "clientNames") {
app.ports.clientNames.send(parsedData.data);
}
if(parsedData.type === "invalidName") {
app.ports.invalidName.send(null);
}
if(parsedData.type === "connections") {
// console.log("parsedData", parsedData.data);
app.ports.connections.send(parsedData.data);
}
if(parsedData.type === "updateReadyList") {
// console.log("parsedData", parsedData.data);
app.ports.updateReadyCount.send(parsedData.data);
}
if(parsedData.type === "startGame") {
// console.log("parsedData", parsedData.data);
app.ports.startGame.send(null);
}
if(parsedData.type === "sharingGridContents") {
// console.log("parsedData gridContents", parsedData.data);
app.ports.updateGridContents.send(parsedData.data);
}
if(parsedData.type === "sharingClickBox") {
app.ports.updateClickBox.send(parsedData.data);
}
if(parsedData.type === "sharingLevel") {
// console.log("parsedData sharingLevel", parsedData.data);
app.ports.updateLevel.send(parsedData.data);
}
if(parsedData.type === "sharingEndGame") {
app.ports.updateEndGame.send(null);
}
} else {
console.log("error parsing data");
}
};
connection.onerror = function (error) {
// an error occurred when sending/receiving data
};
app.ports.clickBox.subscribe(function(data) {
connection.send(JSON.stringify({type: "clickBox", data: data}));
});
app.ports.gameStartedByLead.subscribe(function() {
connection.send(JSON.stringify({type: "notifyAllStart"}));
});
app.ports.sendEndGameSignal.subscribe(function() {
connection.send(JSON.stringify({type: "endGameSignal"}));
});
app.ports.sendGridContents.subscribe(function(data) {
// send grid contents to the server
connection.send(JSON.stringify({type: "gridContents", data: data}));
});
app.ports.sendLevel.subscribe(function(data) {
connection.send(JSON.stringify({type: "notifyAllLevel", data: data}));
});
app.ports.sendPlayerName.subscribe(function(name) {
//send the player name for registration
connection.send(JSON.stringify({type: "name", name: name}));
});
app.ports.sendResetSignal.subscribe(function() {
connection.send(JSON.stringify({type: "resetServer"}));
});
app.ports.storeScore.subscribe(function(score) {
var scores = JSON.parse(localStorage.getItem("scores"));
if(!scores) {
scores = [];
}
scores.push({name: score[0], score: score[1]});
localStorage.setItem("scores", JSON.stringify(scores));
app.ports.sendScores.send(scores);
});
app.ports.requestForScores.subscribe(function() {
var scores = localStorage.getItem("scores");
app.ports.sendScores.send(JSON.parse(scores));
});
app.ports.sendPlayerIsReady.subscribe(function() {
connection.send(JSON.stringify({type: "playerReady"}));
});
</script>
</body>
</html>