Commit 6962e146 authored by nanahira's avatar nanahira

first

parents
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# Next.js build output
.next
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
/build
/output
/config.yaml
.idea
/cards.cdb
/ocgcore
/info.json
/info.yaml
This diff is collapsed.
import { open, Database } from "sqlite";
import sqlite3 from "sqlite3";
import fs from "fs";
import _ from "underscore";
import YAML from "yaml";
interface Card {
id: number;
name: string;
}
interface QueryInfo {
upper: number;
lower: number;
where: string[];
}
let db: Database;
let constantDict = new Map<string, number>();
async function loadConstants() {
const commonContent = await fs.promises.readFile("./ocgcore/common.h", "utf-8");
for (let lineRaw of commonContent.split("\n")) {
const line = lineRaw.trim();
const match = line.match(/^#define ([_A-Z0-9]+)\t+(0x[0-9a-fA-F]+)/);
if (match) {
constantDict.set(match[1], parseInt(match[2]));
}
}
}
function replaceWhereClause(whereClause: string[]) {
const newClause = _.clone(whereClause);
if (!newClause.length) {
return "";
}
for (let i = 0; i < newClause.length; ++i) {
let clause = newClause[i];
for (let constantKey of Array.from(constantDict.keys())) {
const regex = new RegExp(`\{${constantKey}\}`, "g");
clause = clause.replace(regex, constantDict.get(constantKey).toString());
}
newClause[i] = `and ${clause}`;
}
return newClause.join(' ');
}
async function openDatabase() {
return await open({
filename: "./cards.cdb",
driver: sqlite3.Database
});
}
function getMedianCode(cards: Card[]) {
const sortedCards = cards.sort((a, b) => a.id - b.id);
const len = sortedCards.length;
if (len % 2 === 0) {
return Math.floor((sortedCards[len / 2].id + sortedCards[len / 2 + 1].id) / 2);
} else {
return sortedCards[(len + 1) / 2].id;
}
}
async function queryCards(queryInfo: QueryInfo) {
const replacedWhere = replaceWhereClause(queryInfo.where);
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 <= ? ${replacedWhere}`, [queryInfo.lower, queryInfo.upper]);
return cards;
}
async function loadYAML(path: string) {
const content = await fs.promises.readFile(path, "utf-8");
return YAML.parse(content);
}
async function main() {
db = await openDatabase();
const queryInfo: QueryInfo = await loadYAML("./info.yaml");
await loadConstants();
const cards = await queryCards(queryInfo);
console.log(`Cards:`);
for (let card of cards) {
console.log(JSON.stringify(card));
}
if (cards.length) {
const median = getMedianCode(cards);
console.log(`Median: ${median}`);
}
}
main()
upper: 100000000
lower: 0
where:
- type & {TYPE_TUNER} > 0
This diff is collapsed.
{
"name": "card-guesser",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/node": "^14.14.9",
"@types/underscore": "^1.10.24",
"sqlite": "^4.0.17",
"sqlite3": "^5.0.0",
"typescript": "^4.1.2",
"underscore": "^1.11.0",
"yaml": "^1.10.0"
}
}
{
"compilerOptions": {
"outDir": "build",
"module": "commonjs",
"target": "esnext",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
},
"compileOnSave": true,
"allowJs": true,
"include": [
"*.ts",
"src/*.ts"
]
}
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