Commit cd6fa34c authored by nanahira's avatar nanahira

add random and force pic

parent 2c347273
...@@ -178,17 +178,15 @@ export class YGOProCard implements YGOProCardLike { ...@@ -178,17 +178,15 @@ export class YGOProCard implements YGOProCardLike {
return result; return result;
} }
getDisplayString(config: YGOCardConfig) { getPic(config: YGOCardConfig, view = this.getView(config.getLang())) {
return segment('image', { url: config.renderUrl(view), cache: true }, []);
}
getDisplayString(config: YGOCardConfig, forcePic = false) {
const view = this.getView(config.getLang()); const view = this.getView(config.getLang());
const lines: string[] = []; const lines: string[] = [];
if (config.displayPic) { if (config.displayPic || forcePic) {
lines.push( lines.push(this.getPic(config, view).toString());
segment(
'image',
{ url: config.renderUrl(view), cache: true },
[],
).toString(),
);
} }
lines.push(`${view.name}[${view.id}]`); lines.push(`${view.name}[${view.id}]`);
const isMonster = this.isType('TYPE_MONSTER'); const isMonster = this.isType('TYPE_MONSTER');
......
...@@ -17,13 +17,16 @@ import { ...@@ -17,13 +17,16 @@ import {
PutSession, PutSession,
CommandLocale, CommandLocale,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import { Logger, 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';
export * from './config'; export * from './config';
const commonFilter =
'datas.type & 0x4000 = 0 and (datas.alias = 0 or datas.id - datas.alias > 10)';
@DefinePlugin({ name: 'ygocard', schema: YGOCardConfig }) @DefinePlugin({ name: 'ygocard', schema: YGOCardConfig })
export default class YGOCardPlugin export default class YGOCardPlugin
extends StarterPlugin(YGOCardConfig) extends StarterPlugin(YGOCardConfig)
...@@ -33,22 +36,43 @@ export default class YGOCardPlugin ...@@ -33,22 +36,43 @@ export default class YGOCardPlugin
private logger: Logger; private logger: Logger;
private dbs: SQL.Database[] = []; private dbs: SQL.Database[] = [];
private queryInDB(value: string, db: SQL.Database) { private querySQL(db: SQL.Database, sql: string, params: any = {}) {
const possibleValueNumber = parseInt(value) || 0; const statement = db.prepare(sql);
const statement = db.prepare( statement.bind(params);
"select datas.*,texts.name,texts.desc from datas,texts where datas.id = texts.id and datas.type & 0x4000 = 0 and (datas.alias = 0 or datas.id - datas.alias > 10) and (datas.id = $num or texts.name like ('%' || $query || '%') or texts.desc like ('%' || $query || '%'))",
);
statement.bind({
$num: possibleValueNumber,
$query: value,
});
const results: any[] = []; const results: any[] = [];
while (statement.step()) { while (statement.step()) {
results.push(statement.getAsObject()); results.push(statement.getAsObject());
} }
statement.free(); statement.free();
return results;
}
private queryInDB(value: string, db: SQL.Database) {
const possibleValueNumber = parseInt(value) || 0;
const results = this.querySQL(
db,
`select datas.*,texts.name,texts.desc from datas,texts where datas.id = texts.id and ${commonFilter} and (datas.id = $num or texts.name like ('%' || $query || '%') or texts.desc like ('%' || $query || '%'))`,
{
$num: possibleValueNumber,
$query: value,
},
);
return results.map((obj) => plainToInstance(YGOProCard, obj)); return results.map((obj) => plainToInstance(YGOProCard, obj));
} }
private randomCardInDB(db: SQL.Database) {
const allCardIds = this.querySQL(
db,
`select id from datas where ${commonFilter}`,
);
if (!allCardIds.length) return;
const id: number = Random.pick(allCardIds).id;
const [result] = this.queryInDB(id.toString(), db);
return result;
}
private randomCard() {
const db = Random.pick(this.dbs);
if (!db) return;
return this.randomCardInDB(db);
}
private queryInAllDBs(value: string, matchCount?: number) { private queryInAllDBs(value: string, matchCount?: number) {
return _.sortBy( return _.sortBy(
_.uniqBy( _.uniqBy(
...@@ -91,23 +115,27 @@ export default class YGOCardPlugin ...@@ -91,23 +115,27 @@ export default class YGOCardPlugin
@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('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,
) { ) {
if (!query) { if (!query && !random) {
await session.send(session.text('.prompt-input')); await session.send(session.text('.prompt-input'));
query = await session.prompt(); query = await session.prompt();
} }
if (!query) return; if (!query && !random) return;
if (!count) { if (!count) {
count = this.config.matchCount; count = this.config.matchCount;
} }
const cards = this.queryInAllDBs(query, count); const cards = random
? [this.randomCard()]
: this.queryInAllDBs(query, count);
if (!cards.length) { if (!cards.length) {
return session.text('.not-found'); return session.text('.not-found');
} }
if (cards.length === 1) { if (cards.length === 1) {
return cards[0].getDisplayString(this.config); return cards[0].getDisplayString(this.config, forcePic);
} }
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;
...@@ -138,6 +166,6 @@ export default class YGOCardPlugin ...@@ -138,6 +166,6 @@ export default class YGOCardPlugin
if (!card) { if (!card) {
return session.text('.not-found'); return session.text('.not-found');
} }
return card.getDisplayString(this.config); return card.getDisplayString(this.config, forcePic);
} }
} }
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