You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
5.4 KiB
168 lines
5.4 KiB
import { NFC } from 'nfc-pcsc'
|
|
import fetch from 'node-fetch'
|
|
import fs from 'fs'
|
|
import { Feedback } from './statuslogger.js'
|
|
|
|
const CARDS_LOCATION = '/home/dietpi/YACards/'
|
|
const logger = Feedback()
|
|
logger.initLCD()
|
|
|
|
logger.info('[EABU]: Starting up (if this hangs check if pcscd is running)')
|
|
const nfc = new NFC()
|
|
const status = {
|
|
card: null,
|
|
confirmed: false,
|
|
newCardRegistration: false,
|
|
guestPlay: false,
|
|
readyCard: false,
|
|
countdown: 0,
|
|
newCard: false,
|
|
renewPossible: false,
|
|
}
|
|
|
|
logger.info('[EABU]: NFC Initialized')
|
|
nfc.on('error', err => {
|
|
logger.error('[PCSC]:', err);
|
|
});
|
|
nfc.on('reader', reader => {
|
|
logger.info(reader)
|
|
reader.on('card', card => {
|
|
logger.info(`[PCSC] Card ${card.uid} scanned`)
|
|
logger.lcd3('Card:' + card.uid.slice(0, 20 - 5))
|
|
if (status.card === null && !status.guestPlay) {
|
|
if (status.newCard) {
|
|
if (status.readyCard) {
|
|
status.card = card.uid
|
|
status.confirmed = false
|
|
status.countdown = 5
|
|
status.newCard = !fs.existsSync(CARDS_LOCATION + card.uid + '.track_0')
|
|
logger.info(`[YACE] Telling YACE to insert ${card.uid}`)
|
|
logger.info(`[EABU] New card!`)
|
|
logger.lcd2(' New card! ')
|
|
status.newCardRegistration = true
|
|
status.countdown = 600 // seconds
|
|
fetch('http://127.0.0.1:8082/api/v1/loadedCard?cardname=' + card.uid, { method: 'POST' })
|
|
} else {
|
|
logger.lcd2('Scan card only when ')
|
|
logger.lcd3('game asks for it !! ')
|
|
}
|
|
} else {
|
|
logger.info(`[EABU] Existing card!`)
|
|
logger.lcd2(' Welcome back! ')
|
|
if (status.readyCard) {
|
|
logger.info(`[EABU] Inserting...`)
|
|
fetch('http://127.0.0.1:8082/api/v1/insertedCard?cardname=' + card.uid, { method: 'POST' })
|
|
} else {
|
|
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 {
|
|
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 => {
|
|
logger.error('[PCSC] ', err);
|
|
});
|
|
})
|
|
|
|
|
|
const pollYACE = () => {
|
|
if (status.card !== null) {
|
|
fetch('http://127.0.0.1:8082/api/v1/hasCard')
|
|
.then(res => res.json())
|
|
.then(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
|
|
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://127.0.0.1:8082/api/v1/loadedCard?cardname=dummy', { method: 'POST' })
|
|
setTimeout(pollYACE, 1000)
|
|
} else {
|
|
// insertion confirmed! waiting for ejection
|
|
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
|
|
status.renewPossible = false
|
|
logger.info('[YACE] Telling YACE to load dummy')
|
|
fetch('http://127.0.0.1:8082/api/v1/loadedCard?cardname=dummy', { method: 'POST' })
|
|
} else {
|
|
logger.lcd1(' Card renewed! ')
|
|
status.newCard = false
|
|
status.renewPossible = false
|
|
}
|
|
setTimeout(pollYACE, 500)
|
|
}
|
|
})
|
|
.catch(err => {
|
|
logger.error('[EABU] Failure polling YACE', err)
|
|
setTimeout(pollYACE, 2000)
|
|
})
|
|
} else {
|
|
fetch('http://127.0.0.1:8082/api/v1/hasCard')
|
|
.then(res => res.json())
|
|
.then(bool => {
|
|
if (status.guestPlay) {
|
|
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', err)
|
|
setTimeout(pollYACE, 2000)
|
|
})
|
|
}
|
|
}
|
|
|
|
const pollYACE2 = () => {
|
|
fetch('http://127.0.0.1:8082/api/v1/readyCard')
|
|
.then(res => res.json())
|
|
.then(bool => {
|
|
status.readyCard = bool
|
|
if (status.readyCard) {
|
|
logger.lcd0('Wangan Maxi3DX+ ')
|
|
logger.lcd1('!SCAN YOUR CARD NOW!')
|
|
} else {
|
|
logger.lcd0('Wangan Maxi3DX+ ')
|
|
logger.lcd1('Waiting for game... ')
|
|
}
|
|
setTimeout(pollYACE2, 500)
|
|
})
|
|
.catch(err => {
|
|
logger.error('[EABU] Failure polling YACE (loop2)', err)
|
|
setTimeout(pollYACE2, 2000)
|
|
logger.lcd0('/!\ Emulator offline')
|
|
})
|
|
}
|
|
|
|
pollYACE()
|
|
pollYACE2()
|
|
|