Commit 643e7174 authored by nanahira's avatar nanahira

demo

parent 5ceeea9e
Pipeline #19395 passed with stage
in 31 seconds
......@@ -5,6 +5,7 @@ import * as SandboxPlugin from '@koishijs/plugin-sandbox';
import DatabasePlugin from '@koishijs/plugin-database-memory';
import * as Help from '@koishijs/plugin-help';
import ExtrasInDev from './extras';
import PuppeteerPlugin from 'koishi-plugin-puppeteer';
const app = new Context({
port: 14514,
......@@ -29,6 +30,9 @@ app.plugin(ExtrasInDev);
// Target plugin
app.plugin(TargetPlugin, {
databasePaths: ['./dev/ygopro-database/locales/zh-CN'],
usePuppeteer: true,
});
app.plugin(PuppeteerPlugin);
app.start();
This diff is collapsed.
......@@ -179,16 +179,11 @@ export class YGOProCard implements YGOProCardLike {
}
getPic(config: YGOCardConfig, view = this.getView(config.getLang())) {
return segment('image', { url: config.renderUrl(view), cache: true }, []);
return config.renderUrl(view);
}
getDisplayString(config: YGOCardConfig, forcePic = false) {
const view = this.getView(config.getLang());
getDescription(config: YGOCardConfig, view = this.getView(config.getLang())) {
const lines: string[] = [];
if (config.displayPic || forcePic) {
lines.push(this.getPic(config, view).toString());
}
lines.push(`${view.name}[${view.id}]`);
const isMonster = this.isType('TYPE_MONSTER');
let typeString = `[${view.displayType}]`;
if (isMonster) {
......@@ -215,6 +210,17 @@ export class YGOProCard implements YGOProCardLike {
return lines.join('\n');
}
getDisplayString(config: YGOCardConfig) {
const view = this.getView(config.getLang());
const lines: string[] = [];
if (config.displayPic) {
lines.push(segment.image(this.getPic(config, view)).toString());
}
lines.push(`${view.name}[${view.id}]`);
lines.push(this.getDescription(config, view));
return lines.join('\n');
}
getDistanceFrom(value: string) {
if (parseInt(value) === this.id) {
return 1;
......
......@@ -46,6 +46,9 @@ export class YGOCardConfig {
})
picUrl: string;
@DefineSchema({ description: '使用 Puppeteer 渲染一张图。', default: false })
usePuppeteer: boolean;
renderUrl(obj: Partial<YGOProCardLike>) {
return renderTemplate(
this.picUrl,
......
......@@ -16,11 +16,13 @@ import {
PutArg,
PutSession,
CommandLocale,
Inject,
} from 'koishi-thirdeye';
import { Logger, Random, Session } from 'koishi';
import initSqlJs from 'sql.js';
import * as localeZh from './locales/zh';
import * as localeEn from './locales/en';
import PuppeteerPlugin from 'koishi-plugin-puppeteer';
export * from './config';
......@@ -35,6 +37,9 @@ export default class YGOCardPlugin
@InjectLogger()
private logger: Logger;
@Inject()
private puppeteer: PuppeteerPlugin;
private dbs: SQL.Database[] = [];
private querySQL(db: SQL.Database, sql: string, params: any = {}) {
const statement = db.prepare(sql);
......@@ -110,13 +115,33 @@ export default class YGOCardPlugin
this.dbs.forEach((db) => db.close());
}
async renderCard(card: YGOProCard) {
if (!this.config.usePuppeteer || !this.puppeteer) {
return card.getDisplayString(this.config);
}
return (
<html>
<body>
<div>
<image src={card.getPic(this.config)} />
</div>
<div>
{card.name} {card.id}
</div>
<div>
<p>{card.getDescription(this.config)}</p>
</div>
</body>
</html>
);
}
@UseCommand('{{commandName}} [query]')
@CommandLocale('zh', localeZh.cardCommand)
@CommandLocale('en', localeEn.cardCommand)
async onCardCommand(
@PutArg(0) query: string,
@PutOption('random', '--random') random: boolean,
@PutOption('forcePic', '--force-pic') forcePic: boolean,
@PutOption('count', '-c <count:posint>') count: number,
@PutSession() session: Session,
) {
......@@ -135,7 +160,7 @@ export default class YGOCardPlugin
return session.text('.not-found');
}
if (cards.length === 1) {
return cards[0].getDisplayString(this.config, forcePic);
return this.renderCard(cards[0]);
}
const itemLines = cards.map((c, i) => `${i + 1}. ${c.getIdAndName()}`);
const borderLength = Math.max(...itemLines.map((l) => l.length)) + 1;
......@@ -166,6 +191,6 @@ export default class YGOCardPlugin
if (!card) {
return session.text('.not-found');
}
return card.getDisplayString(this.config, forcePic);
return this.renderCard(card);
}
}
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