|
|
|
@ -1,10 +1,13 @@ |
|
|
|
|
import { NFC } from 'nfc-pcsc' |
|
|
|
|
import fetch from 'node-fetch' |
|
|
|
|
import fs from 'fs' |
|
|
|
|
import { Feedback } from './statuslogger.js' |
|
|
|
|
|
|
|
|
|
const CARDS_LOCATION = '/home/snufkin/Repos/apps/YACards/' |
|
|
|
|
const logger = Feedback() |
|
|
|
|
logger.initLCD() |
|
|
|
|
|
|
|
|
|
console.log('[ ][INF][EABU]: Starting up (if this hangs check if pcscd is running)') |
|
|
|
|
logger.info('[EABU]: Starting up (if this hangs check if pcscd is running)') |
|
|
|
|
const nfc = new NFC() |
|
|
|
|
const status = { |
|
|
|
|
card: null, |
|
|
|
@ -13,87 +16,101 @@ const status = { |
|
|
|
|
guestPlay: false, |
|
|
|
|
readyCard: false, |
|
|
|
|
countdown: 0, |
|
|
|
|
newCard: false |
|
|
|
|
newCard: false, |
|
|
|
|
renewPossible: false, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
console.log('[ ][INF][EABU]: NFC Initialized') |
|
|
|
|
logger.info('[EABU]: NFC Initialized') |
|
|
|
|
nfc.on('error', err => { |
|
|
|
|
console.error('[!][ERR][PCSC]:', err); |
|
|
|
|
logger.error('[PCSC]:', err); |
|
|
|
|
}); |
|
|
|
|
nfc.on('reader', reader => { |
|
|
|
|
// reader.aid = 'F222222222';
|
|
|
|
|
reader.on('card', card => { |
|
|
|
|
console.log(`[C][INF][PCSC] Card ${card.uid} scanned`) |
|
|
|
|
logger.info(`[PCSC] Card ${card.uid} scanned`) |
|
|
|
|
logger.lcd3('Card:' + card.uid.slice(0, 20 - 5)) |
|
|
|
|
if (status.card === null && !status.guestPlay) { |
|
|
|
|
status.card = card.uid |
|
|
|
|
status.confirmed = false |
|
|
|
|
status.countdown = 5 |
|
|
|
|
status.newCard = !fs.existsSync(CARDS_LOCATION + card.uid + '.track_0') |
|
|
|
|
console.log(`[C][INF][YACE] Telling YACE to insert ${card.uid}`) |
|
|
|
|
logger.info(`[YACE] Telling YACE to insert ${card.uid}`) |
|
|
|
|
if (status.newCard) { |
|
|
|
|
console.log(`[C][INF][EABU] New card!`) |
|
|
|
|
logger.info(`[EABU] New card!`) |
|
|
|
|
logger.lcd2(' New card! ') |
|
|
|
|
status.newCardRegistration = true |
|
|
|
|
status.countdown = 300 // seconds
|
|
|
|
|
status.countdown = 600 // seconds
|
|
|
|
|
fetch('http://localhost:8082/api/v1/loadedCard?cardname=' + card.uid, { method: 'POST' }) |
|
|
|
|
} else { |
|
|
|
|
console.log(`[C][INF][EABU] Existing card!`) |
|
|
|
|
logger.info(`[EABU] Existing card!`) |
|
|
|
|
logger.lcd2(' Welcome back! ') |
|
|
|
|
if (status.readyCard) { |
|
|
|
|
console.log(`[C][INF][EABU] Inserting...`) |
|
|
|
|
logger.info(`[EABU] Inserting...`) |
|
|
|
|
fetch('http://localhost:8082/api/v1/insertedCard?cardname=' + card.uid, { method: 'POST' }) |
|
|
|
|
} else { |
|
|
|
|
console.log(`[C][INF][EABU] Not inserting as game isn't ready for it...`) |
|
|
|
|
logger.info(`[EABU] Not inserting as game isn't ready for it...`) |
|
|
|
|
logger.lcd2('Scan card only when ') |
|
|
|
|
logger.lcd3('game asks for it !! ') |
|
|
|
|
status.card = null |
|
|
|
|
status.confirmed = false |
|
|
|
|
status.countdown = 0 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
console.log(`[C][INF][EABU] Ignoring as we already have a card or GUEST is playing...`) |
|
|
|
|
logger.info(`[EABU] Ignoring as we already have a card or GUEST is playing...`) |
|
|
|
|
logger.lcd2('Card ignored: ') |
|
|
|
|
logger.lcd3('Guest is playing rn!') |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
reader.on('error', err => { |
|
|
|
|
console.error('[!][ERR][PCSC] ', err); |
|
|
|
|
logger.error('[PCSC] ', err); |
|
|
|
|
}); |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const pollYACE = () => { |
|
|
|
|
// console.debug('[ ][DEB][EABU] STATUS ' + JSON.stringify(status))
|
|
|
|
|
if (status.card !== null) { |
|
|
|
|
// fetch('http://localhost:8082/api/v1/insertedCard')
|
|
|
|
|
// .then(res => res.json())
|
|
|
|
|
// .then(bool => console.log('[ ][INF][YACE] Emulator card inserted: ' + bool.inserted))
|
|
|
|
|
fetch('http://localhost:8082/api/v1/hasCard') |
|
|
|
|
.then(res => res.json()) |
|
|
|
|
.then(bool => { |
|
|
|
|
// console.debug('[ ][DEB][YACE] Emulator card present: ' + bool)
|
|
|
|
|
if (status.countdown > 0) { |
|
|
|
|
// counting down trying to confirm insertion
|
|
|
|
|
status.countdown-- |
|
|
|
|
status.confirmed = bool |
|
|
|
|
logger.lcd1('Countdown: ' + ('' + status.countdown).padStart(9, ' ')) |
|
|
|
|
if (bool) status.countdown = 0 |
|
|
|
|
setTimeout(pollYACE, 1000) |
|
|
|
|
} else if (!status.confirmed) { |
|
|
|
|
// couldn't confirm insertion
|
|
|
|
|
status.card = null |
|
|
|
|
status.newCard = false |
|
|
|
|
console.error('[!][ERR][YACE] Could not confirm card presence with emulator!') |
|
|
|
|
console.error('[!][INF][YACE] Telling YACE to load dummy just in case') |
|
|
|
|
logger.lcd1('FAILED??????????????') |
|
|
|
|
logger.error('[YACE] Could not confirm card presence with emulator!') |
|
|
|
|
logger.error('[YACE] Telling YACE to load dummy just in case') |
|
|
|
|
fetch('http://localhost:8082/api/v1/loadedCard?cardname=dummy', { method: 'POST' }) |
|
|
|
|
setTimeout(pollYACE, 1000) |
|
|
|
|
} else { |
|
|
|
|
// insertion confirmed! waiting for ejection
|
|
|
|
|
if (!bool) { |
|
|
|
|
logger.lcd1('Card inserted, glhf!') |
|
|
|
|
if (!bool && !status.renewPossible) { |
|
|
|
|
status.renewPossible = true |
|
|
|
|
status.countdown = 100 |
|
|
|
|
} else if (!bool && status.renewPossible) { |
|
|
|
|
logger.lcd1('Thanks for playing!!') |
|
|
|
|
status.card = null |
|
|
|
|
status.newCard = false |
|
|
|
|
console.error('[!][INF][YACE] Telling YACE to load dummy') |
|
|
|
|
status.renewPossible = false |
|
|
|
|
logger.info('[YACE] Telling YACE to load dummy') |
|
|
|
|
fetch('http://localhost:8082/api/v1/loadedCard?cardname=dummy', { method: 'POST' }) |
|
|
|
|
} else { |
|
|
|
|
logger.lcd1(' Card renewed! ') |
|
|
|
|
status.newCard = false |
|
|
|
|
status.renewPossible = false |
|
|
|
|
} |
|
|
|
|
setTimeout(pollYACE, 500) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
.catch(err => { |
|
|
|
|
console.error('[!][ERR][EABU] Failure polling YACE') |
|
|
|
|
logger.error('[EABU] Failure polling YACE', err) |
|
|
|
|
setTimeout(pollYACE, 2000) |
|
|
|
|
}) |
|
|
|
|
} else { |
|
|
|
@ -101,16 +118,22 @@ const pollYACE = () => { |
|
|
|
|
.then(res => res.json()) |
|
|
|
|
.then(bool => { |
|
|
|
|
if (status.guestPlay) { |
|
|
|
|
console.debug('[ ][DEB][YACE] Emulator GUEST card present: ' + bool) |
|
|
|
|
logger.info('[YACE] Emulator GUEST card present: ' + bool) |
|
|
|
|
} |
|
|
|
|
if (bool) { |
|
|
|
|
status.guestPlay = true |
|
|
|
|
logger.lcd1(' Guest play engaged ') |
|
|
|
|
} |
|
|
|
|
if (status.guestPlay && !bool) { |
|
|
|
|
status.guestPlay = false |
|
|
|
|
logger.lcd1(' Guest play ended ') |
|
|
|
|
} |
|
|
|
|
setTimeout(pollYACE, 200) |
|
|
|
|
}) |
|
|
|
|
.catch(err => { |
|
|
|
|
logger.error('[EABU] Failure polling YACE') |
|
|
|
|
setTimeout(pollYACE, 2000) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -119,13 +142,16 @@ const pollYACE2 = () => { |
|
|
|
|
.then(res => res.json()) |
|
|
|
|
.then(bool => { |
|
|
|
|
status.readyCard = bool |
|
|
|
|
if (status.readyCard) { |
|
|
|
|
logger.lcd1('!SCAN YOUR CARD NOW!') |
|
|
|
|
} else { |
|
|
|
|
logger.lcd1('Waiting for game... ') |
|
|
|
|
} |
|
|
|
|
setTimeout(pollYACE2, 500) |
|
|
|
|
}) |
|
|
|
|
.catch(err => { |
|
|
|
|
console.error('[!][ERR][EABU] Failure polling YACE (loop2)') |
|
|
|
|
setTimeout(pollYACE2, 2000) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
logger.error('[EABU] Failure polling YACE (loop2)') |
|
|
|
|
setTimeout(pollYACE2, 2000)})} |
|
|
|
|
|
|
|
|
|
pollYACE() |
|
|
|
|
pollYACE2() |
|
|
|
|