-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
143 lines (107 loc) · 3.35 KB
/
main.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
// *INFO*
// SERVERSEITIGES JS FILE
// DER SERVER SCHICKT SENSORDATEN AN DEN CLIENT
// Mit Server verbinden: ssh [email protected]
// Passwort: 123456
// DIESE DATEI AUSFÜHREN MIT DEM COMMAND sudo node main.js
// index.html kann anschliessend über 172.20.10.X:3000 aufgerufen werden.
// Server libraries
var express = require('express');
var app = express()
var server = require('http').Server(app);
var io = require('socket.io')(server);
// Raspberry libraries
var five = require('johnny-five');
var Raspi = require('raspi-io');
var board = new five.Board({
io: new Raspi()
});
// Start the express server
server.listen(3000);
console.log('Access client screen on <IP>:3000');
app.get('/', function(req, res) {
// Join all arguments together and normalize the resulting path.
//res.sendFile(__dirname + '/client/index.html');
res.sendFile(__dirname + '/client/index.html');
});
// Add static files files in client folder
app.use(express.static(__dirname + '/client'));
// Variables
var sensor1;
var sensor2;
var button;
var brightness;
var score = 0;
var gamerunning = false;
var countdownstarted = false;
var singleplayer = false; // sets if singleplayermode has been started yet
thegame();
function thegame(){
// RASPBERRY BOARD
board.on('ready', function () {
sensor1 = new five.Sensor.Digital({
pin: 'P1-11',
freq: 1,
threshold: 0.05,
});
sensor2 = new five.Sensor.Digital({
pin: 'P1-7',
freq: 1,
threshold: 0.05,
});
button = new five.Button('P1-40')
button.on("press", function() {
console.log( "Button pressed" );
if(!countdownstarted && singleplayer){
io.emit('button-status', true);
countdownstarted = true;
}
}); // Button endet Hier
// Sensoren aktivieren
sensor1.on("change", function() {
brightness = this.value;
console.log("Sensor 1: Die Helligkeit ist: " + this.value);
console.log('\u0007');
//if (brightness == 0 && gamerunning){
if (brightness == 0 && gamerunning){
console.log('SCORE IN PIPE A + 10 Points');
score = score + 10;
console.log(score);
io.emit('score', score);
io.emit('lastgoal', 10);
};
});
sensor2.on("change", function() {
brightness = this.value;
console.log("Sensor 2: Die Helligkeit ist: " + this.value);
console.log('\u0007');
if (brightness == 0 && gamerunning){
console.log('SCORE IN PIPE B + 25 Points');
score = score + 25;
console.log(score);
io.emit('score', score);
io.emit('lastgoal', 25);
};
});
io.on('connection', function(socket) {
socket.on('singleplayer', function(newgame){
console.log("Singleplayer");
singleplayer = true;
}); // ENDE Function singleplayer
socket.on('gamestart', function(gamestart){
//console.log("Das funktioniert so toll!");
gamerunning = gamestart;
console.log("das game läuft:" + gamerunning);
}); // ENDE Function gamestart
socket.on('newgame', function(newgame){
score = 0;
gamerunning = false;
countdownstarted = false;
singleplayer = false;
thegame(); // start thegame() new
console.log("NEW GAME");
}); // ENDE Function gamestart
}); //ENDE IO CONNECTION
}); // ENDE BOARD
}; // ENDE thegame()
// SOCKET