New NavRoutes detected, read, saved.

This commit is contained in:
punkfairie 2023-05-11 01:54:03 -07:00
parent 3d43590006
commit 6b0e1d8240
5 changed files with 61 additions and 23 deletions

View file

@ -68,7 +68,7 @@
<div class="container-fluid"> <div class="container-fluid">
<div class="row separator align-items-center"> <div class="row separator align-items-center">
<div class="col"><hr class="seperator"></div> <div class="col"><hr class="separator"></div>
</div> </div>
</div> </div>
</div> </div>

View file

@ -1,18 +1,20 @@
import { Tail as TailType } from 'tail' import type { Tail as TailType } from 'tail'
import type { autoScan, completeFsdJump, detailedScan, journalEntry, planetScan } from '../@types/journalLines' import type { autoScan, completeFsdJump, detailedScan, journalEntry, navRoute, planetScan } from '../@types/journalLines'
const chokidar = require('chokidar')
const EventEmitter = require('node:events')
const fs = require('node:fs')
const { globSync } = require('glob')
import { maxBy, findIndex, find } from 'lodash'
const os = require('node:os')
const path = require('node:path')
const { readFile } = require('node:fs/promises')
const reverseLineReader = require('reverse-line-reader')
const Tail = require('tail').Tail
import { Body } from '../models/Body' import { Body } from '../models/Body'
import { System } from '../models/System' import { System } from '../models/System'
const EventEmitter = require('events')
const fs = require('fs')
const path = require('path')
const { globSync } = require('glob')
const os = require('os')
const lineReader = require('reverse-line-reader')
const chokidar = require('chokidar')
const Tail = require('tail').Tail
import { maxBy, findIndex, find } from 'lodash'
// Set log() to console.log() so whenever I get around to setting up a log file, I don't have to // Set log() to console.log() so whenever I get around to setting up a log file, I don't have to
// search and replace all the console.log()'s. // search and replace all the console.log()'s.
const log = console.log.bind(console) const log = console.log.bind(console)
@ -22,6 +24,7 @@ export class JournalInterface extends EventEmitter {
journalPattern: string journalPattern: string
currentJournal: string|undefined currentJournal: string|undefined
location: System location: System
navRoute: System[]
constructor(isPackaged: boolean) { constructor(isPackaged: boolean) {
@ -43,11 +46,15 @@ export class JournalInterface extends EventEmitter {
this.currentJournal = this.getLatestJournal() this.currentJournal = this.getLatestJournal()
log(`New journal file found, now watching ${path.basename(this.currentJournal)}.`) log(`New journal file found, now watching ${path.basename(this.currentJournal)}.`)
this.navRoute = []
// LineReader seems to be async, so start async processes here. // LineReader seems to be async, so start async processes here.
this.location = new System('Unknown') this.location = new System('Unknown')
log('JournalInterface initialized. Attempting to find current location.') log('JournalInterface initialized. Attempting to find current location.')
this.getCurrentLocation() this.getCurrentLocation()
// -> getScannedBodies()
// --> getNavRoute()
} }
/* -------------------------------------------------------------------- getLatestJournal ---- */ /* -------------------------------------------------------------------- getLatestJournal ---- */
@ -56,9 +63,7 @@ export class JournalInterface extends EventEmitter {
getLatestJournal(): string|undefined { getLatestJournal(): string|undefined {
const journals = globSync(this.journalPattern) const journals = globSync(this.journalPattern)
return maxBy(journals, file => { return maxBy(journals, file => fs.statSync(file).mtime)
return fs.statSync(file).mtime
})
} }
/* ------------------------------------------------------------------ getCurrentLocation ---- */ /* ------------------------------------------------------------------ getCurrentLocation ---- */
@ -66,7 +71,7 @@ export class JournalInterface extends EventEmitter {
// Get current location on setup, so if app is restarted, user can pick up where they left off // Get current location on setup, so if app is restarted, user can pick up where they left off
// Rather than waiting til they jump to the next system to use the program again. // Rather than waiting til they jump to the next system to use the program again.
getCurrentLocation(): void { getCurrentLocation(): void {
lineReader.eachLine(this.currentJournal, (raw: string, last: boolean) => { reverseLineReader.eachLine(this.currentJournal, (raw: string, last: boolean) => {
if (raw) { // skip blank line at end of file if (raw) { // skip blank line at end of file
const line = JSON.parse(raw) const line = JSON.parse(raw)
@ -81,7 +86,7 @@ export class JournalInterface extends EventEmitter {
} }
} }
}).then(() => { }).then(() => {
lineReader.eachLine(this.currentJournal, (raw: string, last: boolean) => { reverseLineReader.eachLine(this.currentJournal, (raw: string, last: boolean) => {
if (raw) { if (raw) {
const line = JSON.parse(raw) const line = JSON.parse(raw)
@ -110,7 +115,7 @@ export class JournalInterface extends EventEmitter {
getScannedBodies(): void { getScannedBodies(): void {
let detailedScanLine: detailedScan|null = null let detailedScanLine: detailedScan|null = null
lineReader.eachLine(this.currentJournal, (raw: string, last: boolean) => { reverseLineReader.eachLine(this.currentJournal, (raw: string, last: boolean) => {
if (raw) { // Skip blank line at end of file. if (raw) { // Skip blank line at end of file.
const line: journalEntry = JSON.parse(raw) const line: journalEntry = JSON.parse(raw)
@ -168,7 +173,7 @@ export class JournalInterface extends EventEmitter {
} }
}).then(() => { }).then(() => {
log('Scanned bodies found.') log('Scanned bodies found.')
this.emit('INIT_COMPLETE') this.getNavRoute(true)
}) })
} }
@ -223,8 +228,30 @@ export class JournalInterface extends EventEmitter {
/* ------------------------------------------------------------------------- getNavRoute ---- */ /* ------------------------------------------------------------------------- getNavRoute ---- */
getNavRoute() { async getNavRoute(init: boolean = false) {
let routeFile: string|null = null
try {
routeFile = await readFile(this.journalDir + 'NavRoute.json', { encoding: 'utf8' })
} catch (err) {
log(`Error getting NavRoute: ${err.message}.`)
}
if (routeFile) {
const route: navRoute = JSON.parse(routeFile)
route.Route.forEach((system) => {
this.navRoute.push(new System(system.StarSystem, system.StarClass))
})
log('NavRoute set.')
if (init) {
this.emit('INIT_COMPLETE')
} else {
this.emit('SET_NAV_ROUTE')
}
}
} }
/* --------------------------------------------------------------------------- parseLine ---- */ /* --------------------------------------------------------------------------- parseLine ---- */

View file

@ -2,12 +2,14 @@ import { Body } from "./Body"
export class System { export class System {
name: string name: string
starClass: string|null
bodies: Body[] bodies: Body[]
constructor(StarSystem: string) { constructor(StarSystem: string, StarClass: string|null = null) {
// In future, this is where we preform EDSM lookup // In future, this is where we preform EDSM lookup
this.name = StarSystem this.name = StarSystem
this.starClass = StarClass
this.bodies = [] this.bodies = []
} }
} }

View file

@ -34,6 +34,7 @@ const { app } = require('electron')
import { JournalInterface } from './interfaces/JournalInterface' import { JournalInterface } from './interfaces/JournalInterface'
import { UI } from './models/UI' import { UI } from './models/UI'
import { Body } from './models/Body' import { Body } from './models/Body'
import { sep } from 'path'
// Grab app.isPackaged from main process // Grab app.isPackaged from main process
let isPackaged = false let isPackaged = false
@ -109,3 +110,9 @@ journal.on('BODY_SCANNED', (body, DSS) => {
$('#lowValueScans').appendChild(row) $('#lowValueScans').appendChild(row)
} }
}) })
/* --------------------------------------------------------------------------- nav route set ---- */
journal.on('SET_NAV_ROUTE', () => {
// clear previous nav route, if any
})

View file

@ -1,6 +1,8 @@
{ {
"extends": "@tsconfig/node20/tsconfig.json", "extends": "@tsconfig/node20/tsconfig.json",
"compilerOptions": {}, "compilerOptions": {
"useUnknownInCatchVariables": false,
},
"include": ["src"], "include": ["src"],
"exclude": ["node_modules"] "exclude": ["node_modules"]
} }