This commit is contained in:
Kai Mosebach 2022-08-12 22:09:27 +01:00
parent 31b702136a
commit 646af978f9
1 changed files with 50 additions and 11 deletions

61
game.js
View File

@ -132,10 +132,10 @@ function setup() {
gpio.output(statusLED, true);
});
gpio.setup(childLED, gpio.OUT, () => {
gpio.output(childLED, true);
gpio.output(childLED, false);
});
gpio.setup(erwLED, gpio.OUT, () => {
gpio.output(erwLED, false);
gpio.output(erwLED, true);
});
gpio.setup(finishButton, gpio.DIR_IN, gpio.EDGE_FALLING, () => {
@ -185,6 +185,21 @@ function gameTimer() {
}
});
GAME.runTime++;
GAME.led = !GAME.led;
gpio.output(statusLED, GAME.led);
}
function failTimer() {
GAME.led = !GAME.led;
gpio.output(statusLED, GAME.led);
gpio.output(childLED, GAME.led);
gpio.output(erwLED, GAME.led);
}
function successTimer() {
GAME.led = !GAME.led;
gpio.output(statusLED, GAME.led);
gpio.output(childLED, !GAME.led);
gpio.output(erwLED, GAME.led);
}
function getRandomInt(max) {
@ -192,6 +207,13 @@ function getRandomInt(max) {
}
function start(value) {
if(GAME.failTimer) clearInterval(GAME.failTimer);
if(GAME.successTimer) clearInterval(GAME.successTimer);
if(MODE === GROWN) {
gpio.output(erwLED, true);
} else {
gpio.output(childLED, true);
}
GAME = {
events: [],
runTime: 0
@ -205,9 +227,9 @@ function start(value) {
} else {
GAME.events.push({ sound: backgroundGrown, start: 1});
}
GAME.events.push({ sound: erschreckSounds[getRandomInt(2)], start: 15+getRandomInt(10) });
GAME.events.push({ sound: erschreckSounds[getRandomInt(2)], start: 30+getRandomInt(10) });
GAME.events.push({ sound: erschreckSounds[getRandomInt(2)], start: 50+getRandomInt(10) });
GAME.events.push({ sound: erschreckSounds[getRandomInt(5)], start: 15+getRandomInt(10) });
GAME.events.push({ sound: erschreckSounds[getRandomInt(5)], start: 30+getRandomInt(10) });
GAME.events.push({ sound: erschreckSounds[getRandomInt(5)], start: 50+getRandomInt(10) });
GAME.mode = GROWN;
GAME.fail = failSound;
GAME.finish = finishSound;
@ -224,6 +246,7 @@ function start(value) {
GlobalLog.note('Starting new game', JSON.stringify(GAME, 2, null));
GAME.countTimer = setInterval(gameTimer, 1000);
sendBotMsg('new game startet');
} else {
GlobalLog.warn('START not in state', STATE);
}
@ -238,10 +261,11 @@ async function finish(value) {
playSound(GAME.finish);
STATE = STATES.FINISH;
clearInterval(GAME.countTimer)
GAME.successTimer = setInterval(failTimer, 600);
GlobalLog.note('Getting new image!')
exec(`raspistill -t 1000 -st -o /tmp/last.png`, (err) => {
exec(`fswebcam -r 1024x768 /tmp/last.png`, (err) => {
if(err) {
GloblLog.error('Could not get image', err);
GlobalLog.error('Could not get image', err);
}
GAME.events = [];
var msg;
@ -265,20 +289,24 @@ async function finish(value) {
}
}
sendBotGroupImg(groupid, '/tmp/last.png', msg);
GAME.led = true;
});
} else {
GlobalLog.warn('FINISH not in state', STATE);
}
// exec(`killall aplay`);
}
function fail(value) {
if(STATE === STATES.STARTED) {
GlobalLog.note('You failed after', GAME.runTime, 'seconds');
GAME.startplayer.stop();
player.stop();
playSound(GAME.fail);
STATE = STATES.FAIL;
clearInterval(GAME.countTimer)
GAME.failTimer = setInterval(failTimer, 100);
GAME.events = [];
player.stop();
} else {
GlobalLog.debug('FAIL not in state', STATE);
}
@ -286,13 +314,23 @@ function fail(value) {
function toggle(value) {
if(STATE === STATES.FINISH || STATE === STATES.FAIL) {
GlobalLog.info('Toggle');
if(GAME.failTimer) clearInterval(GAME.failTimer);
if(GAME.successTimer) clearInterval(GAME.successTimer);
if(MODE === GROWN) {
gpio.output(erwLED, true);
} else {
gpio.output(childLED, true);
}
GlobalLog.info('Toggle, old', MODE);
if(MODE === CHILD) {
MODE = GROWN;
gpio.output(childLED, false);
gpio.output(erwLED, true);
// set all values for GROWN mode
}
if(MODE === GROWN) {
} else {
MODE = CHILD;
gpio.output(childLED, true);
gpio.output(erwLED, false);
// set all values for CHILD mode
}
} else {
@ -310,6 +348,7 @@ function playSound(file, runTime){
// Do not stop background sound
if(GAME.runTime > 1) {
GAME.playing = file;
GAME.startplayer = player;
}
player.play({ path: `wav/${file}`, sync: true }).then(() => { delete GAME.playing })
}