add import script for existing game logs
This commit is contained in:
parent
eabe338971
commit
08b332ac6f
5 changed files with 142 additions and 2 deletions
47
scripts/import_game_logs.ts
Normal file
47
scripts/import_game_logs.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import { DB_FILE, DB_PATCHES_DIR, DATA_DIR } from '../src/server/Dirs'
|
||||
import Db from '../src/server/Db'
|
||||
import fs from 'fs'
|
||||
import { logger } from '../src/common/Util'
|
||||
|
||||
const log = logger('import_game_logs.ts')
|
||||
const db = new Db(DB_FILE, DB_PATCHES_DIR)
|
||||
db.patch(true)
|
||||
|
||||
|
||||
for (const file of fs.readdirSync(DATA_DIR)) {
|
||||
const m = file.match(/^log_(.*)\.log$/)
|
||||
if (!m) {
|
||||
continue
|
||||
}
|
||||
|
||||
const gameId = m[1]
|
||||
log.info(`Importing log for game ${file}`)
|
||||
|
||||
const contents = fs.readFileSync(`${DATA_DIR}/${file}`, 'utf-8')
|
||||
let t = 0
|
||||
let datas = []
|
||||
for (const line of contents.split("\n")) {
|
||||
if (line === '') {
|
||||
continue
|
||||
}
|
||||
let parsed
|
||||
try {
|
||||
parsed = JSON.parse(line)
|
||||
} catch (e) {
|
||||
log.error('bad game', e)
|
||||
break
|
||||
}
|
||||
if (t === 0) {
|
||||
t = parsed[4]
|
||||
} else {
|
||||
t += parsed[parsed.length - 1]
|
||||
}
|
||||
datas.push({
|
||||
game_id: gameId,
|
||||
created: t / 1000,
|
||||
entry: line,
|
||||
})
|
||||
}
|
||||
db.insertMany('game_log', datas)
|
||||
log.info(`Done.`)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue