Commit 13ef911c authored by nanahira's avatar nanahira

add may incorrect statement

parent d0df4db5
...@@ -4,6 +4,8 @@ import fs from "fs"; ...@@ -4,6 +4,8 @@ import fs from "fs";
import _, { where } from "underscore"; import _, { where } from "underscore";
import YAML from "yaml"; import YAML from "yaml";
type Where = string | any;
interface Card { interface Card {
id: number; id: number;
name: string; name: string;
...@@ -12,7 +14,14 @@ interface Card { ...@@ -12,7 +14,14 @@ interface Card {
interface QueryInfo { interface QueryInfo {
upper: number; upper: number;
lower: number; lower: number;
where: (string | number)[]; where?: Where[];
mayIncorrectStatements?: MayIncorrectStatement[];
}
interface MayIncorrectStatement {
incorrectCount: number;
lessThanOrEqual: boolean;
where: Where[];
} }
let db: Database; let db: Database;
...@@ -28,35 +37,86 @@ async function loadConstants() { ...@@ -28,35 +37,86 @@ async function loadConstants() {
} }
} }
} }
/*
function replaceClause(clause: string | any, falseStatement: boolean) { function replaceClause(clause: string | any, statement: boolean) {
if (typeof (clause) !== "string") { if (typeof (clause) !== "string") {
return falseStatement ? ; return (statement === (clause === 0)) ? "0" : "1";
} }
let newClause = clause; let newClause = clause;
for (let constantKey of Array.from(constantDict.keys())) { for (let constantKey of Array.from(constantDict.keys())) {
const regex = new RegExp(`\{${constantKey}\}`, "g"); const regex = new RegExp(`\{${constantKey}\}`, "g");
newClause = newClause.replace(regex, constantDict.get(constantKey).toString()); newClause = newClause.replace(regex, constantDict.get(constantKey).toString());
} }
if() if (statement) {
return `and (${newClause})`;
} else {
return `and not (${newClause})`;
}
} }
*/
function replaceWhereClause(whereClause: (string | number)[]) { function parseWhereClause(whereClause: (string | any)[]) {
const newClauses: string[] = []; if (!whereClause || !whereClause.length) {
if (!whereClause.length) {
return ""; return "";
} }
for (let clause of whereClause) { const newClauses = whereClause.map(w => replaceClause(w, true));
if (typeof (clause) !== "string") { return newClauses.join(' ');
continue; }
function getCombination(arr: number[], count: number): number[][] {
const len = arr.length;
if (count === len || count === 0) {
return [arr.slice(0, count)]
} else if (count > len / 2) {
const reversedCombination = getCombination(arr, len - count);
return reversedCombination.map(m => _.without(arr, ...m))
} else {
const [first, ...other] = arr;
let res: number[][] = [];
const [combinationsWithFirst, combinationsOthers]: [number[][], number[][]] = [
getCombination(other, count - 1),
getCombination(other, count)
];
res = res.concat(combinationsWithFirst.map(comb => [first, ...comb]))
res = res.concat(combinationsOthers);
return res;
}
}
function generateBoolSequence(totalCount: number, falseCount: number): boolean[][] {
const allTrueSequence = _.range(totalCount).map(m => true);
const falsePositions = getCombination(_.range(totalCount), falseCount);
return falsePositions.map(falsePosition => {
const currentSequence = _.clone(allTrueSequence);
for (let pos of falsePosition) {
currentSequence[pos] = false;
} }
for (let constantKey of Array.from(constantDict.keys())) { return currentSequence;
const regex = new RegExp(`\{${constantKey}\}`, "g"); })
clause = clause.replace(regex, constantDict.get(constantKey).toString()); }
function parseMayIncorrectStatement(mayIncorrectStatement: MayIncorrectStatement) {
if (!mayIncorrectStatement.where.length) {
return "";
}
const startCount = mayIncorrectStatement.lessThanOrEqual ? 0 : mayIncorrectStatement.incorrectCount;
let outerClauses: string[] = [];
for (let i = startCount; i <= mayIncorrectStatement.incorrectCount; ++i) {
const correctSequences = generateBoolSequence(mayIncorrectStatement.where.length, i);
for (let correctSequence of correctSequences) {
const clauses = mayIncorrectStatement.where.map((clause, index) => replaceClause(clause, correctSequence[index]));
const combinedClause = `(1 ${clauses.join(' ')})`;
outerClauses.push(combinedClause);
} }
newClauses.push(`and (${clause})`);
} }
return newClauses.join(' '); return `and (${outerClauses.join(' or ')})`;
}
function parseMayIncorrectStatements(mayIncorrectStatements: MayIncorrectStatement[]) {
if (!mayIncorrectStatements || !mayIncorrectStatements.length) {
return "";
}
return mayIncorrectStatements.map(m => parseMayIncorrectStatement(m)).join(" ");
} }
async function openDatabase() { async function openDatabase() {
...@@ -77,9 +137,11 @@ function getMedianCode(cards: Card[]) { ...@@ -77,9 +137,11 @@ function getMedianCode(cards: Card[]) {
} }
async function queryCards(queryInfo: QueryInfo) { async function queryCards(queryInfo: QueryInfo) {
const replacedWhere = replaceWhereClause(queryInfo.where); const whereClauseString = parseWhereClause(queryInfo.where);
console.log(replacedWhere); const mayIncorrectClauseString = parseMayIncorrectStatements(queryInfo.mayIncorrectStatements);
const cards: Card[] = await db.all(`select datas.id,texts.name from datas,texts where datas.id = texts.id and datas.type & 0x4000 = 0 and datas.id > ? and datas.id <= ? and (datas.alias = 0 or datas.alias - datas.id > 10) ${replacedWhere} order by datas.id asc`, [queryInfo.lower, queryInfo.upper]); const sql = `select datas.id,texts.name from datas,texts where datas.id = texts.id and datas.type & 0x4000 = 0 and datas.id > ? and datas.id <= ? and (datas.alias = 0 or datas.alias - datas.id > 10) ${whereClauseString} ${mayIncorrectClauseString} order by datas.id asc`;
console.log(`SQL: ${sql}`);
const cards: Card[] = await db.all(sql, [queryInfo.lower, queryInfo.upper]);
return cards; return cards;
} }
......
upper: 100000000 upper: 100000000
lower: 0 lower: 0
where: where:
- "not (type & {TYPE_MONSTER} > 0)" - "1"
mayIncorrectStatements:
- incorrectCount: 4
lessThanOrEqual: true
where:
- "type & {TYPE_FUSION} = 0"
- "type & {TYPE_SYNCHRO} > 0"
- "type & {TYPE_XYZ} > 0"
- "type & {TYPE_LINK} > 0"
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