Commit ff610fe5 authored by nanahira's avatar nanahira

improve deck gen

parent 78238e21
Pipeline #3872 failed with stages
in 2 minutes and 16 seconds
......@@ -12,10 +12,15 @@ export interface Deck extends CardPool {
export class DeckGenerator {
cards: CardPool;
decks: Deck[];
constructor(cards: CardPool) {
this.cards = cards;
}
private getExtraCardCountInSide() {
private getDeckString(deck: Deck) {
const deckText = '#generated by ygopro-cn-database-generator\n#main\n' + deck.main.join('\n') + '\n#extra\n' + deck.extra.join('\n') + '\n!side\n' + deck.side.join('\n') + '\n';
return deckText;
}
/*private getExtraCardCountInSide() {
if (!this.cards.main.length) {
return 15;
}
......@@ -34,16 +39,6 @@ export class DeckGenerator {
const simplifiedRatio = (mainExtraRatio - minRatio) / (maxRatio - minRatio);
return Math.ceil(15 * simplifiedRatio);
}
private splitCards(codes: number[], unit: number) {
const count = Math.ceil(codes.length / unit);
return _.range(count).map(i => {
return codes.slice(i * unit, (i + 1) * unit);
});
}
private getDeckString(deck: Deck) {
const deckText = '#generated by ygopro-cn-database-generator\n#main\n' + deck.main.join('\n') + '\n#extra\n' + deck.extra.join('\n') + '\n!side\n' + deck.side.join('\n') + '\n';
return deckText;
}
private getDeckFromPart(main: number[], extra: number[]) {
const targetMain = main.slice(0, 60);
const targetExtra = extra.slice(0, 15);
......@@ -53,18 +48,43 @@ export class DeckGenerator {
extra: targetExtra,
side: targetSide
}
}*/
private splitCards(codes: number[], unit: number) {
const count = Math.ceil(codes.length / unit);
return _.range(count).map(i => {
return codes.slice(i * unit, (i + 1) * unit);
});
}
private isCanFillSide() {
return this.cards.main.length + this.cards.extra.length <= this.decks.length * 15;
}
private fillSide() {
const restCards = this.cards.main.concat(this.cards.extra);
const units = this.splitCards(restCards, 15);
if (units.length > this.decks.length) {
throw `Not enough deck. ${units.length} expected, got ${this.decks.length}.`;
}
private getDecks() {
const extraInSide = this.getExtraCardCountInSide();
for (let i = 0; i < units.length; ++i){
this.decks[i].side = units[i];
}
return true;
}
private getDecks() {
/* const extraInSide = this.getExtraCardCountInSide();
const mainInSide = 15 - extraInSide;
const extraCount = 15 + extraInSide;
const mainCount = 60 + mainInSide;
const mainParts = this.splitCards(this.cards.main, mainCount);
const extraParts = this.splitCards(this.cards.extra, extraCount);
return _.range(Math.max(mainParts.length, extraParts.length)).map(i => this.getDeckFromPart(mainParts[i] || [], extraParts[i] || []));
return _.range(Math.max(mainParts.length, extraParts.length)).map(i => this.getDeckFromPart(mainParts[i] || [], extraParts[i] || []));*/
this.decks = [];
while(!this.isCanFillSide()) {
this.decks.push({ main: this.cards.main.splice(0, 60), extra: this.cards.extra.splice(0, 15), side: [] });
}
this.fillSide();
}
getDeckTexts() {
const decks = this.getDecks();
return decks.map(d => this.getDeckString(d));
this.getDecks();
return this.decks.map(d => this.getDeckString(d));
}
}
import { open, Database } from "sqlite";
import sqlite3 from "sqlite3";
import { promises as fs } from "fs";
import { DeckGenerator } from "../src/deck";
import _ from "underscore";
async function openDatabase(path: string) {
return await open({
filename: path,
driver: sqlite3.Database
});
}
const targetDirectory = process.argv[3];
async function createDirectory() {
const createDirectoryPaths = [
targetDirectory
];
for (let createDirectoryPath of createDirectoryPaths) {
try {
await fs.access(createDirectoryPath);
} catch (e) {
await fs.mkdir(createDirectoryPath, { recursive: true });
}
}
}
async function generateDecks(main: number[], extra: number[]) {
const deckGenerator = new DeckGenerator({
main, extra
});
const deckTexts = deckGenerator.getDeckTexts();
await Promise.all(_.range(deckTexts.length).map(i => fs.writeFile(`${targetDirectory}/${i}.ydk`, deckTexts[i])));
}
async function main() {
console.log(`Creating directory ${targetDirectory}.`);
await createDirectory();
console.log(`Opening database ${process.argv[2]}.`);
const db = await openDatabase(process.argv[2]);
console.log(`Reading database ${process.argv[2]}.`);
const main = (await db.all('select id from datas where (type & 0x4000) = 0 and (type & (0x4000000 | 0x800000 | 0x4000 | 0x2000 | 0x40)) = 0')).map(m => m.id);
const extra = (await db.all('select id from datas where (type & 0x4000) = 0 and (type & (0x4000000 | 0x800000 | 0x4000 | 0x2000 | 0x40)) > 0')).map(m => m.id);
console.log(`${main.length} mains and ${extra.length} extras.`);
await db.close();
console.log(`Generating decks to ${targetDirectory}.`);
await generateDecks(main, extra);
}
main();
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