From 78fec963f1995b4234a22de89db70faec7d53564 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Wed, 29 Sep 2021 20:56:00 +0200 Subject: [PATCH] Separeted Finishes from TeamFinishes. --- libs/database/sqlite2mongo.js | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/libs/database/sqlite2mongo.js b/libs/database/sqlite2mongo.js index 5ee2dfd..03fd355 100644 --- a/libs/database/sqlite2mongo.js +++ b/libs/database/sqlite2mongo.js @@ -2,6 +2,7 @@ import initLog from "../utills/log.js" import { sqlite } from "./init.js" import Level from '../../schemas/Level.js' import Player from '../../schemas/Player.js' +import Finish from '../../schemas/Finish.js' const log = initLog("sqlite2mongo") @@ -78,6 +79,45 @@ async function checkForPlayers() { } +async function checkForFinishes() { + log("Checking for new players...") + + const latestFinish = await Finish.findOne({}).sort({ "firstFinish": "desc" }) + const date = latestFinish ? latestFinish.firstFinish.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'!") + + // 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 {} + + // 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 + // } +} + + async function sqlite2mongo() { log("Checking for additions to 'ddnet.sqlite'...")