Commit e945c5c3 authored by nanahira's avatar nanahira

optimize single page

parent fe9fa2b0
Pipeline #41242 passed with stages
in 55 seconds
......@@ -86,7 +86,31 @@ export class AppService extends ConsoleLogger {
doc.addPage(newPage);
};
await copyFirstPageToBottom();
const filterType = (type: number) =>
ydk.main.filter((id) => {
if (!cardDataMap[id]) {
throw new BlankReturnMessageDto(
404,
`Card ID ${id} has invalid data`,
).toException();
}
return cardDataMap[id].type === type;
});
let needExtraPage = false;
if (
[1, 2, 4].some((type) => {
const filtered = filterType(type);
const grouped = Object.values(_.groupBy(filtered, (id) => id));
return grouped.length > 20;
})
) {
await copyFirstPageToBottom();
this.log(
`Added extra page to PDF for deck export due to card type overflow.`,
);
needExtraPage = true;
}
let pageCount = 0;
......@@ -130,10 +154,10 @@ export class AppService extends ConsoleLogger {
spaces = 20,
) => {
const groupedCards = Object.values(_.groupBy(cards, (id) => id));
const usePages = _.chunk(groupedCards, spaces);
await ensurePages(usePages.length - 1);
for (let i = 0; i < usePages.length; ++i) {
const currentPageCards = usePages[i];
const drawCardsOnPage = async (
currentPageCards: number[][],
pageIndex: number,
) => {
for (let j = 0; j < currentPageCards.length; ++j) {
const entry = currentPageCards[j];
const qty = entry.length;
......@@ -143,34 +167,32 @@ export class AppService extends ConsoleLogger {
doc,
qty.toString(),
moveDown(cord.qty, j),
i + 1,
pageIndex,
);
await this.drawTextService.drawTextInBox(
doc,
cardData[`${dto.lang || 'sc'}_name`] || '',
moveDown(cord.name, j),
i + 1,
pageIndex,
);
}
await this.drawTextService.drawTextInBox(
doc,
_.sumBy(currentPageCards, (s) => s.length).toString(),
moveDown(cord.qty, spaces),
i + 1,
pageIndex,
);
};
if (needExtraPage) {
const usePages = _.chunk(groupedCards, spaces);
await ensurePages(usePages.length - 1);
for (let i = 0; i < usePages.length; ++i) {
await drawCardsOnPage(usePages[i], i + 1);
}
} else {
await drawCardsOnPage(groupedCards, 0);
}
};
const filterType = (type: number) =>
ydk.main.filter((id) => {
if (!cardDataMap[id]) {
throw new BlankReturnMessageDto(
404,
`Card ID ${id} has invalid data`,
).toException();
}
return cardDataMap[id].type === type;
});
await drawCards(filterType(1), Coordinates.main.monsters);
this.log(`Filled monster cards.`);
await drawCards(filterType(2), Coordinates.main.spells);
......@@ -182,7 +204,9 @@ export class AppService extends ConsoleLogger {
await drawCards(ydk.side, Coordinates.side, 15);
this.log(`Filled side deck cards.`);
doc.removePage(0);
if (needExtraPage) {
doc.removePage(0);
}
const resBuf = Buffer.from(await doc.save());
this.log(`Generated PDF buffer for deck export.`);
......
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