Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const net = require ('net');
const ip = "192.168.1.52";
const port = 5000;
const client = new net.Socket();
let currentUser = null;
const Gpio = require('onoff').Gpio;
const led_1 = new Gpio(17, 'out');
const led_2 = new Gpio(27, 'out');
const led_3 = new Gpio(22, 'out');
const led_4 = new Gpio(23, 'out');
const switch_1 = new Gpio(10, 'in', 'rising');
const switch_2 = new Gpio(9, 'in', 'rising');
const switch_3 = new Gpio(11, 'in', 'rising');
const switch_4 = new Gpio(26, 'in', 'rising');
let sequence = [];
let showingSequence = [];
let guessingSequence = [];
let randomStart = ['red', 'yellow', 'white', 'green'];
let guessingMode = false;
let playing = false;
let score = 0;
const turnOffAll = () => {
led_1.writeSync(0);
led_2.writeSync(0);
led_3.writeSync(0);
led_4.writeSync(0);
};
const turnOnAll = () => {
led_1.writeSync(1);
led_2.writeSync(1);
led_3.writeSync(1);
led_4.writeSync(1);
};
switch_1.watch(() => {
if (guessingMode) {
guessColor('green');
}
});
switch_2.watch(() => {
if (guessingMode) {
guessColor('white');
}
});
switch_3.watch(() => {
if (guessingMode) {
guessColor('yellow');
}
});
switch_4.watch(() => {
if (guessingMode) {
guessColor('red');
}
});
const showColor = (color, cleanup) => {
if (cleanup) {
turnOffAll();
}
console.log('Showing color', color);
setTimeout(() => {
switch (color) {
case 'green':
led_1.writeSync(1);
led_2.writeSync(0);
led_3.writeSync(0);
led_4.writeSync(0);
break;
case 'white':
led_1.writeSync(0);
led_2.writeSync(1);
led_3.writeSync(0);
led_4.writeSync(0);
break;
case 'yellow':
led_1.writeSync(0);
led_2.writeSync(0);
led_3.writeSync(1);
led_4.writeSync(0);
break;
case 'red':
led_1.writeSync(0);
led_2.writeSync(0);
led_3.writeSync(0);
led_4.writeSync(1);
break;
}
}, cleanup ? 200 : 0);
}
turnOffAll();
getRandomInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const newRandomColor = () => {
const colors = ['red', 'yellow', 'white', 'green'];
return colors[getRandomInt(0, 3)];
};
const nextRound = () => {
const gameData = JSON.stringify({"username": currentUser, "score": score, "playing": playing});
console.log(gameData);
client.write(gameData);
[0, 1, 2, 3, 4, 5].forEach((index) => {
setTimeout(() => {
if (index % 2 ) {
turnOffAll();
} else {
turnOnAll();
}
}, 200 * index);
});
setTimeout(() => {
turnOffAll();
sequence.push(newRandomColor());
showingSequence = showingSequence.concat(sequence);
showSequence();
}, 200 * 7);
}
function showSequence() {
if (playing) {
console.log('showSequence');
if (showingSequence.length > 0) {
showColor(showingSequence.shift(), true);
setTimeout(showSequence, 1000);
} else {
setTimeout(guess, 1000);
}
}
}
function guess () {
turnOffAll();
console.log('Guessing');
guessingMode = true;
guessingSequence = guessingSequence.concat(sequence);
console.log(guessingSequence);
}
function guessColor (color) {
showColor(color, true);
setTimeout(turnOffAll, 500);
if (color !== guessingSequence.shift()) {
looser();
} else {
score += 50;
if (guessingSequence.length < 1) {
guessingMode = false;
nextRound();
}
}
}
function looser () {
guessingMode = false;
playing = false;
const gameData = JSON.stringify({"username": currentUser, "score": score, "playing": playing});
console.log(gameData);
client.write(gameData);
turnOffAll();
randomStart = ['red', 'yellow', 'white', 'green'];
randomStart = randomStart
.concat(randomStart)
.concat(randomStart)
.concat(randomStart)
.concat(randomStart)
.concat(randomStart)
.concat(randomStart)
.concat(randomStart)
.concat(randomStart)
.concat(randomStart)
.concat(randomStart);
(function move() {
if (randomStart.length > 0) {
showColor(randomStart.pop(), false);
setTimeout(move, 200);
} else {
turnOffAll();
}
})();
}
function play () {
sequence = [];
showingSequence = [];
guessingSequence = [];
playing = true;
score = 0;
console.log('Play');
nextRound();
}
client.on('data', (data) => {
const receivedData = JSON.parse(data.toString());
console.log(receivedData);
if ( receivedData.action === 'newGame' ) {
currentUser = receivedData.data.username;
play();
}
if ( receivedData.action === 'clear' ) {
guessingMode = false;
playing = false;
client.end();
}
});
client.on('close', (data) => {
console.log("Conexion perdida... Intentando reconectar");
connect();
});
const connect = () => {
client.connect(port, ip, () => {
console.log("Conectado con " + ip);
});
}
connect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.