-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
279 lines (215 loc) · 7.03 KB
/
app.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
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
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
colors = require('colors'),
mongoose = require('mongoose');
server.listen(3000);
// Database stuff
mongoose.connect('mongodb://foostable:republica@ds059908.mongolab.com:59908/foosball');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log('Yay connected to DB');
});
var gameSchema = mongoose.Schema({
gameDate: Date,
bluepoints: Number,
redpoints: Number,
bluePlayer1: String,
bluePlayer2: String,
redPlayer1: String,
redPlayer2: String,
});
var gameData = mongoose.model('gameData', gameSchema)
function saveGame() {
console.log("saving game!".yellow);
var newGameData = new gameData({
bluepoints: foosballGame.bluepoints,
redpoints: foosballGame.redpoints,
bluePlayer1: foosballGame.bluePlayer1,
bluePlayer2: foosballGame.bluePlayer2,
redPlayer1: foosballGame.redPlayer1,
redPlayer2: foosballGame.redPlayer2,
gameDate: foosballGame.currTime
});
newGameData.save(function (err) {
if (err) return console.error(err);
console.log('meow');
});
};
// var playerSchema = mongoose.Schema({
// name: String,
// instagramId: String,
// });
// var pointSchema = mongoose.Schema({
// pointScoredTime: String,
// point: Number,
// });
// end Database stuff
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
app.get('/games', function(req, res) {
res.sendfile(__dirname + '/games.html');
});
// Initial var values
var foosballGame = {
bluepoints: 0,
redpoints:0,
bluePlayer1:"",
bluePlayer2:"",
redPlayer1:"",
redPlayer2:"",
currTime: Date.now(),
gamePoints:{}
};
console.log(foosballGame.currTime);
var connectCounter = "0"
function millisecondsToStr (milliseconds) {
// TIP: to find current time in milliseconds, use:
// var current_time_milliseconds = new Date().getTime();
function numberEnding (number) {
return (number > 1) ? 's' : '';
}
var temp = Math.floor(milliseconds / 1000);
var minutes = Math.floor((temp %= 3600) / 60);
if (minutes) {
var minRemainder = (temp) - (minutes * 60);
return minutes + ' minute' + numberEnding(minutes) + ' ' + minRemainder + ' second' + numberEnding(minRemainder);
}
var seconds = temp % 60;
if (seconds) {
return seconds + ' second' + numberEnding(seconds);
}
return 'less than a second'; //'just now' //or other string you like;
}
function addpoint(data){
var pointTime = millisecondsToStr(Date.now() - foosballGame.currTime);
// goal();
console.log(pointTime);
switch (data)
{
case "blueplus":
console.log('blue team plus 1');
foosballGame.bluepoints = Number(foosballGame.bluepoints)+1;
console.log(foosballGame.bluepoints);
break;
case "redplus":
console.log('Red team plus 1');
foosballGame.redpoints = Number(foosballGame.redpoints)+1;
console.log(foosballGame.redpoints);
break;
case "bluemin":
console.log('blue team minus 1');
foosballGame.bluepoints = currentBP-1;
console.log(foosballGame.bluepoints);
break;
case "redmin":
console.log('Red team minus 1');
foosballGame.redpoints = currentRP-1;
console.log(foosballGame.redpoints);
break;
default:
console.log('unknown message received: '+data);
break;
};
// check for wins
if (foosballGame.bluepoints == "10") {
console.log("Blue team wins!".blue);
io.emit('status', 'Blue Team Wins!');
saveGame();
};
if (foosballGame.redpoints == "10") {
console.log("Red team wins!".blue);
io.emit('status', 'Red Team Wins!');
saveGame();
};
io.emit('score-update', {blue: foosballGame.bluepoints, red: foosballGame.redpoints, time : pointTime});
console.log('Score '+pointTime+'!'.blue);
};
console.log('Server listening on port 3000'.green);
// On first client connection start a new game
io.sockets.on('connection', function(socket){
gameData.find(function (err, gameData) {
if (err) return console.error(err);
console.log(gameData)
io.emit('gamescores',gameData);
});
connectCounter++;
console.log("connections: "+connectCounter);
console.log('New device connected'.green);
io.emit('status', 'New device connected!');
// Send score update to all devices on new connection
io.emit('score-update', {blue: foosballGame.bluepoints, red: foosballGame.redpoints});
// Receiving info from remote page
socket.on('score', function(data){
console.log('score received from Remote'.green)
// io.emit('score-update', data);
addpoint(data);
});
socket.on('status', function(data){
switch (data)
{ case "newGame":
newGame();
break;
};
});
socket.on('players', function(data){
console.log(data);
foosballGame.bluePlayer1 = data.bluePlayer1;
foosballGame.bluePlayer2 = data.bluePlayer2;
foosballGame.redPlayer1 = data.redPlayer1;
foosballGame.redPlayer2 = data.redPlayer2;
});
socket.on('pointData', function(data){
console.log(data);
});
function newGame() {
foosballGame.bluepoints = "00"
foosballGame.redpoints = "00"
foosballGame.currTime = Date.now();
io.emit('score-update', {blue: foosballGame.bluepoints, red: foosballGame.redpoints});
console.log('New Game Starting');
io.emit('status', "Starting new Game...");
};
socket.on('disconnect', function() {
connectCounter--; console.log("connections: "+connectCounter);
});
}); //end socket connection
// Disable all lines below if running without an arduino attached to server
var five = require("johnny-five"),
board,
button;
function goal() {
var piezo = new five.Piezo(3);
piezo.play({
song: [
["d4", 1/4],
[null, 1/8],
["c#4", 1/4],
[null, 1/8],
["g5", 1.5]
],
tempo: 150
});
};
board = new five.Board();
board.on("ready", function() {
var blueSensor = new five.Button(8);
var redSensor = new five.Button(10);
board.repl.inject({
blueSensor: button,
redSensor: button
});
blueSensor.on("up", function() {
console.log("up");
addpoint("blueplus");
});
redSensor.on("up", function() {
console.log("up");
addpoint("redplus");
});
goal();
});