diff --git a/libs/database/sqlite2mongo.js b/libs/database/sqlite2mongo.js index 03fd355..54c3005 100644 --- a/libs/database/sqlite2mongo.js +++ b/libs/database/sqlite2mongo.js @@ -80,41 +80,39 @@ async function checkForPlayers() { async function checkForFinishes() { - log("Checking for new players...") + log("Checking for new finishes...") - const latestFinish = await Finish.findOne({}).sort({ "firstFinish": "desc" }) - const date = latestFinish ? latestFinish.firstFinish.toISOString().replace(/[TZ]/g, " ").replace(/\.[0-9\s]+/, "") : undefined + const latestFinish = await Finish.findOne({}).sort({ "date": "desc" }) + const date = latestFinish ? latestFinish.date.toISOString().replace(/[TZ]/g, " ").replace(/\.[0-9\s]+/, "") : undefined const newFinishAmount = (await sqlite.get(`SELECT count(Name) FROM race ${date ? `WHERE Timestamp > datetime('${date}')` : ''}`))['count(Name)'] if (newFinishAmount === 0) return log("No new finishes found...") - log(`Found ${newFinishAmount} new players!`) - log("Importing new players...") - // await sqlite.exec("DROP TABLE IF EXISTS temp") - // await sqlite.exec("CREATE TABLE temp(Name varchar(128) NOT NULL, Timestamp timestamp NOT NULL)") - // await sqlite.exec(`INSERT INTO TEMP (Name, Timestamp) SELECT Name, Timestamp FROM (SELECT Name, Timestamp FROM (SELECT * FROM race ORDER BY Timestamp ASC, Name ASC) GROUP BY Name) ${date ? `WHERE Timestamp > datetime('${date}')` : ''}`) - // log("Imported new players into 'temp'!") + log(`Found ${newFinishAmount} new finishes!`) - // let addedPlayers = 0 - // let offset = -1 - // while (offset < newFinishAmount) { - // await sqlite.each( - // `SELECT Name, Timestamp FROM temp LIMIT 10000 OFFSET ${offset + 1}`, - // [], - // async (err, player) => { - // if (err) return log(err) - // if (addedPlayers >= newFinishAmount) return {} + let addedFinishes = 0 + let offset = -1 + while (offset < newFinishAmount) { + await sqlite.each( + `SELECT * FROM race ${date ? `WHERE Timestamp > datetime('${date}')` : ''} ORDER BY Timestamp LIMIT 5000 OFFSET ${offset + 1}`, + [], + async (err, finish) => { + if (err) return log(err) + if (addedFinishes >= newFinishAmount) return {} - // Player.create({ - // name: player.Name, - // firstFinish: player.Timestamp === '0000-00-00 00:00:00' ? new Date('January 1, 1970 00:00:00 UTC') : new Date(`${player.Timestamp}+00:00`) - // }).then(() => { - // ++addedPlayers - // log(`Added player ${addedPlayers}/${newFinishAmount} -> ${player.Name}`) - // }) - // } - // ) - // offset += 10000 - // } + Finish.create({ + map: finish.Map, + time: finish.Time, + date: finish.Timestamp === '0000-00-00 00:00:00' ? new Date('January 1, 1970 00:00:00 UTC') : new Date(`${finish.Timestamp}+00:00`), + serverLocation: finish.Server ?? '', + player: finish.Name + }).then(() => { + ++addedFinishes + log(`Added finish ${addedFinishes}/${newFinishAmount} -> At ${finish.Timestamp} «${finish.Name}» completed «${finish.Map}» in ${finish.Time} s`) + }) + } + ) + offset += 5000 + } } @@ -124,6 +122,7 @@ async function sqlite2mongo() { await checkForMaps() await checkForPlayers() + await checkForFinishes() } export default sqlite2mongo \ No newline at end of file