Commit e899ad8e authored by nanahira's avatar nanahira

ydk

parent 200d016d
......@@ -5,7 +5,7 @@
"main": "run.ts",
"scripts": {
"build": "tsc",
"start": "node build/run.js"
"start": "node build/run.js | bunyan"
},
"repository": {
"type": "git",
......
......@@ -3,13 +3,17 @@ import { NWFetcher } from "./src/nwfetcher";
import _ from "underscore";
import { CNOCGFetcher } from "./src/cnocg";
import { ExtraCards } from "./src/ExtraCards";
import { DirectFetcher } from "./src/direct";
async function main() {
const dbreader = new DBReader({ name: "Database", level: "debug" });
const dbreader = new DBReader({ name: "Database" });
await dbreader.init();
const cards = await CNOCGFetcher.fetchOnce();
const missingExtraCards = ExtraCards.filter(c => !cards.some(cc => c.code === cc.code));
await dbreader.run(cards.concat(missingExtraCards));
//const cards = await CNOCGFetcher.fetchOnce();
//const missingExtraCards = ExtraCards.filter(c => !cards.some(cc => c.code === cc.code));
//await dbreader.run(cards.concat(missingExtraCards));
const cards = await DirectFetcher.fetchOnce();
//console.log(cards);
await dbreader.run(cards);
process.exit();
}
main();
import Base from "./base";
import { promises as fs } from "fs";
import { Card } from "./dbreader";
export class DirectFetcher extends Base {
static async fetchOnce() {
const fetcher = new DirectFetcher({ name: "Temp Direct Fetcher", level: 'debug' });
await fetcher.init();
return await fetcher.fetch();
}
cardCodes: number[] = [];
private addCode(code: number) {
const posToRemove: number[] = [];
for (let i = 0; i < this.cardCodes.length; ++i) {
const c = this.cardCodes[i];
// 完全相同的不加入
if (c === code) {
this.log.debug(`Skipped full dup: ${code} ${c}`);
return;
}
const codeDifference = code - c;
// 新加入的卡号比某一卡号大,并且 10 以内,则新的卡是那张卡的关联卡,不入。
if (codeDifference > 0 && codeDifference <= 10) {
this.log.debug(`Skipped forward-related dup: ${code} ${c}`);
return;
}
// 新加入的卡号比某一卡号小,并且 10 以内,则把旧的卡删掉。-
if (codeDifference < 0 && codeDifference >= -10) {
this.log.debug(`Would remove backword-related dup: ${code} ${c}`);
posToRemove.push(i);
}
}
posToRemove.sort((a, b) => b - a);
for (const pos of posToRemove) {
const codeRemoved = this.cardCodes.splice(pos, 1);
this.log.debug(`Removed backword-related dup: ${codeRemoved}`);
}
this.cardCodes.push(code);
}
private async workWithYdk(ydkFile: string) {
this.log.info(`Reading ${ydkFile}.`);
const content = await fs.readFile(`./packs/${ydkFile}`, 'utf-8');
for (let line of content.split('\n')) {
const match = line.trim().match(/^(\d{5,9})$/);
if (!match) {
continue;
}
const code = parseInt(match[1]);
this.addCode(code);
}
}
async fetch(): Promise<Card[]> {
this.log.info(`Started reading from ydk cards.`);
const ydkFiles = await fs.readdir('./packs');
this.log.info(`${ydkFiles.length} packs found.`);
for (let file of ydkFiles) {
await this.workWithYdk(file);
}
const finalCodes = this.cardCodes;
this.log.info(`${finalCodes.length} cards in total.`);
return finalCodes.map(code => new Card(code));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment