Commit cd6fa34c authored by nanahira's avatar nanahira

add random and force pic

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