forked from software-mansion-labs/GoL2
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
inlcude gamestate and timestamp in whitelist table && update scripts
- Loading branch information
1 parent
0409797
commit f6c9836
Showing
5 changed files
with
88 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class migration1707646272528 implements MigrationInterface { | ||
name = 'migration1707646272528' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "whitelist" ADD "timestamp" integer NOT NULL`); | ||
await queryRunner.query(`ALTER TABLE "whitelist" ADD "gameState" character varying NOT NULL`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "whitelist" DROP COLUMN "gameState"`); | ||
await queryRunner.query(`ALTER TABLE "whitelist" DROP COLUMN "timestamp"`); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,68 @@ | ||
import fs from 'fs/promises' | ||
import { saveWhitelistProofsFromFileToDB } from "./saveWhitelistProofsFromFileToDB" | ||
import fs from "fs/promises"; | ||
import { Whitelist } from "../entity/whitelist"; | ||
import { AppDataSource } from "./db"; | ||
|
||
type DetailsObject = { | ||
[generation: string]: { | ||
user_id: string; | ||
game_state: string; | ||
timestamp: number; | ||
}; | ||
} | ||
|
||
export const checkWhitelistProofs = async () => { | ||
try { | ||
const res = await AppDataSource.query(`SELECT EXISTS(SELECT 1 FROM whitelist) AS isNotEmpty;`); | ||
const isNotEmpty = res[0].isnotempty; | ||
if (isNotEmpty) { | ||
console.log("Whitelist is not empty. Skipping."); | ||
return; | ||
} | ||
const whitelistFiles = (await fs.readdir("whitelist")).filter((filename) => filename.endsWith(".json") && filename !== "details.json"); | ||
const detailsString = await fs.readFile(`whitelist/details.json`, "utf-8") | ||
const details: DetailsObject = JSON.parse(detailsString); | ||
whitelistFiles.forEach(async (filename) => { | ||
console.log(`Saving ${filename} ...`); | ||
await saveWhitelistProofsFromFileToDB(`whitelist/${filename}`, details); | ||
console.log(`${filename} saved to DB.`); | ||
}); | ||
} catch (err) { | ||
console.error("checkWhitelistProofs", err); | ||
} | ||
|
||
}; | ||
|
||
|
||
|
||
const saveWhitelistProofsFromFileToDB = async (filename: string, details: DetailsObject) => { | ||
console.log(`Retrieving whitelist proofs from "${filename}" file.`); | ||
try { | ||
const dataRaw = await fs.readFile(filename, "utf8"); | ||
const data = JSON.parse(dataRaw); | ||
|
||
const whitelist = []; | ||
try { | ||
const whitelistFiles = await fs.readdir('whitelist') | ||
whitelistFiles.forEach(async (filename) => { | ||
console.log(`Saving ${filename} ...`) | ||
await saveWhitelistProofsFromFileToDB(`whitelist/${filename}`) | ||
console.log(`${filename} saved to DB.`) | ||
}) | ||
}catch(err) { | ||
console.error('Error saving whitelist proofs to DB', err) | ||
} | ||
} | ||
// @ts-ignore | ||
for (const generation in data) { | ||
const proofs = data[generation]; | ||
if (!proofs.length) continue; | ||
const proofText = proofs.join(","); | ||
const whitelistRow = new Whitelist(); | ||
whitelistRow.generation = Number(generation); | ||
whitelistRow.proof = proofText; | ||
if(!details[generation]) { | ||
console.error(`No details found for generation ${generation}`); | ||
} | ||
whitelistRow.timestamp = details[generation].timestamp; | ||
whitelistRow.gameState = details[generation].game_state; | ||
whitelist.push(whitelistRow); | ||
} | ||
await AppDataSource.manager.save(whitelist); | ||
console.log("Data inserted successfully."); | ||
} catch (err) { | ||
console.error("Error inserting data", err); | ||
} | ||
} catch (err) { | ||
console.error("Error reading file", err); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.