Commit d3ed7e14 authored by nanahira's avatar nanahira

checkers

parent c4242b1b
......@@ -111,3 +111,4 @@ dist
/ocgcore
/info.json
/info.yaml
/lflist.conf
......@@ -3,15 +3,11 @@ import sqlite3 from "sqlite3";
import fs from "fs";
import _, { where } from "underscore";
import YAML from "yaml";
import {Card, CheckerInfo, loadCheckers } from "./src/checker";
import { AbstractChecker } from "./src/checkers/AbstractChecker";
type Where = string | any;
interface Card {
id: number;
name: string;
desc: string;
}
interface RegexCondition {
pattern: string;
field: 'name' | 'desc';
......@@ -22,7 +18,7 @@ interface QueryInfo {
lower: number;
noFilterAlias: boolean;
where?: Where[];
regexConditions?: RegexCondition[];
conditions?: CheckerInfo[];
mayIncorrectStatements?: MayIncorrectStatement[];
}
......@@ -32,9 +28,11 @@ interface MayIncorrectStatement {
where: Where[];
}
let checkersList: Map<string, AbstractChecker>;
let db: Database;
let constantDict = new Map<string, number>();
async function loadConstants() {
try {
const commonContent = await fs.promises.readFile("./ocgcore/common.h", "utf-8");
......@@ -165,10 +163,16 @@ async function queryCards(queryInfo: QueryInfo) {
const sql = `select datas.id,texts.name,texts.desc from datas,texts where datas.id = texts.id and datas.type & 0x4000 = 0 and datas.id > ? and datas.id <= ? ${queryInfo.noFilterAlias ? "" : "and (datas.alias = 0 or datas.id - datas.alias > 10)"} ${whereClauseString} ${mayIncorrectClauseString} order by datas.id asc`;
console.error(`SQL: ${sql}`);
let cards: Card[] = await db.all(sql, [queryInfo.lower, queryInfo.upper]);
if (queryInfo.regexConditions) {
for (let regexCondition of queryInfo.regexConditions) {
const regexObj = new RegExp(regexCondition.pattern);
cards = cards.filter(card => (card[regexCondition.field] as string).match(regexObj));
if (queryInfo.conditions) {
for (let checkerInfo of queryInfo.conditions) {
const checker = checkersList.get(checkerInfo.type);
if (!checker) {
console.error(`Checker ${checkerInfo.type} not found.`);
continue;
}
const wantedStatement = checkerInfo.statement != null ? checkerInfo.statement : true;
await checker.setInfo(checkerInfo.data);
cards = cards.filter(card => checker.check(card));
}
}
return cards;
......@@ -180,6 +184,7 @@ async function loadYAML(path: string) {
}
async function main() {
checkersList = await loadCheckers();
db = await openDatabase();
const queryInfo: QueryInfo = await loadYAML("./info.yaml");
await loadConstants();
......
upper: 100000000
lower: 0
noFilterAlias: false
checkers:
- type: lflist
statement: true
data:
match: '^2021.4$'
limit: 3
- type: regex
statement: true
data:
field: name
pattern: '^.*$'
where:
- "type & {TYPE_MONSTER} > 0"
mayIncorrectStatements:
......
import { AbstractChecker } from './checkers/AbstractChecker';
import { RegexChecker } from './checkers/RegexChecker';
export interface Card {
id: number;
name: string;
desc: string;
}
export interface CheckerInfo {
type: string;
statement?: boolean;
data: any;
}
export async function loadCheckers() {
const checkersList = new Map<string, AbstractChecker>();
// add checkers here
const regexChecker = new RegexChecker();
checkersList.set('regex', regexChecker);
for (let checkerName of checkersList.keys()) {
const checker = checkersList.get(checkerName);
console.log(`Loading checker ${checkerName}`);
await checker.initialize();
}
return checkersList;
}
export interface Card {
id: number;
name: string;
desc: string;
}
export class AbstractChecker {
info: any;
async initialize() {
}
async setInfo(info: any) {
this.info = info;
}
check(card: Card) {
return true;
}
}
import { AbstractChecker, Card } from "./AbstractChecker";
export class RegexChecker extends AbstractChecker {
info: any;
regexObj: RegExp;
async initialize() {
}
async setInfo(info: any) {
await super.setInfo(info);
this.regexObj = new RegExp(info.pattern);
}
check(card: Card) {
return !!(card[this.info.field] as string).match(this.regexObj);
}
}
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