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