Commit dcaaeb5c authored by nanahira's avatar nanahira

bump to Koishi 4.8

parent 5bbd0b89
import { App } from 'koishi'; import { Context } from 'koishi';
import TargetPlugin from '../src'; import TargetPlugin from '../src';
import ConsolePlugin from '@koishijs/plugin-console'; import ConsolePlugin from '@koishijs/plugin-console';
import SandboxPlugin from '@koishijs/plugin-sandbox'; import SandboxPlugin from '@koishijs/plugin-sandbox';
import * as DatabasePlugin from '@koishijs/plugin-database-memory'; import DatabasePlugin from '@koishijs/plugin-database-memory';
import CachePlugin from '@koishijs/plugin-cache-lru'; import AragamiPlugin from 'koishi-plugin-cache-aragami';
import ExtrasInDev from './extras'; import ExtrasInDev from './extras';
const app = new App({ const app = new Context({
port: 14514, port: 14514,
host: 'localhost', host: 'localhost',
prefix: '.', prefix: '.',
...@@ -19,7 +19,7 @@ app.plugin(ConsolePlugin, { ...@@ -19,7 +19,7 @@ app.plugin(ConsolePlugin, {
}); });
// Some services // Some services
app.plugin(CachePlugin); app.plugin(AragamiPlugin);
app.plugin(DatabasePlugin); app.plugin(DatabasePlugin);
// Some extras // Some extras
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
"ygo", "ygo",
"yugioh", "yugioh",
"ygopro", "ygopro",
"required:cache" "required:cache-aragami"
], ],
"bugs": { "bugs": {
"url": "https://code.mycard.moe/3rdeye/koishi-plugin-ygotournament/issues" "url": "https://code.mycard.moe/3rdeye/koishi-plugin-ygotournament/issues"
...@@ -38,19 +38,19 @@ ...@@ -38,19 +38,19 @@
"@aws-sdk/types": "3.38.0", "@aws-sdk/types": "3.38.0",
"@aws-sdk/util-format-url": "3.38.0", "@aws-sdk/util-format-url": "3.38.0",
"class-transformer": "^0.4.0", "class-transformer": "^0.4.0",
"koishi-thirdeye": "^10.3.2", "koishi-thirdeye": "^11.0.6",
"moment": "^2.29.1", "moment": "^2.29.1",
"schemastery-gen": "^3.1.14" "schemastery-gen": "^3.1.14"
}, },
"devDependencies": { "devDependencies": {
"@koishijs/plugin-cache-lru": "^1.0.0-rc.0", "@koishijs/plugin-console": "^4.1.1",
"@koishijs/plugin-console": "^3.3.2", "@koishijs/plugin-database-memory": "^1.4.1",
"@koishijs/plugin-database-memory": "^1.3.0", "@koishijs/plugin-sandbox": "^2.0.1",
"@koishijs/plugin-sandbox": "^1.1.3",
"@types/jest": "^27.4.0", "@types/jest": "^27.4.0",
"@types/node": "^16.11.6", "@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "^4.33.0", "@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0", "@typescript-eslint/parser": "^4.33.0",
"esbuild-loader": "^2.19.0",
"eslint": "^7.32.0", "eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1", "eslint-plugin-prettier": "^3.4.1",
...@@ -66,7 +66,8 @@ ...@@ -66,7 +66,8 @@
"ws": "^8.3.0" "ws": "^8.3.0"
}, },
"peerDependencies": { "peerDependencies": {
"koishi": "^4.7.5" "koishi": "^4.8.2",
"koishi-plugin-cache-aragami": "^1.0.4"
}, },
"jest": { "jest": {
"moduleFileExtensions": [ "moduleFileExtensions": [
......
// import 'source-map-support/register'; // import 'source-map-support/register';
export * from './config'; export * from './config';
import { YGOTournamentPluginConfig } from './config'; import { YGOTournamentPluginConfig } from './config';
import { classToPlain, plainToClass } from 'class-transformer'; import { plainToClass } from 'class-transformer';
import { MatchWrapper, TournamentWrapper } from './def/challonge'; import { MatchWrapper, TournamentWrapper } from './def/challonge';
import { S3Client } from '@aws-sdk/client-s3'; import { S3Client } from '@aws-sdk/client-s3';
import { getSignedUrl } from './presign'; import { getSignedUrl } from './presign';
import { SRVProRoomInfo } from './def/srvpro'; import { SRVProRoomInfo } from './def/srvpro';
import moment from 'moment'; import moment from 'moment';
import { DefinePlugin, Inject, selectContext, StarterPlugin } from 'koishi-thirdeye'; import {
import { Cache } from 'koishi'; DefinePlugin,
Inject,
selectContext,
StarterPlugin,
} from 'koishi-thirdeye';
import AragamiPlugin, { CacheKey } from 'koishi-plugin-cache-aragami';
export interface FromAndToTimeUnix { class LateDeclarationTime {
fromTime: number; fromTime: number;
toTime: number; toTime: number;
@CacheKey()
key() {
return 'current';
}
} }
declare module 'koishi' { class ChallongeData extends TournamentWrapper {
// eslint-disable-next-line @typescript-eslint/no-namespace @CacheKey()
namespace Cache { challongeId: string;
interface Tables {
challongeData: any;
lateDeclarationTime: FromAndToTimeUnix;
}
}
} }
@DefinePlugin() @DefinePlugin()
...@@ -30,32 +34,28 @@ export default class YGOTournamentPlugin extends StarterPlugin( ...@@ -30,32 +34,28 @@ export default class YGOTournamentPlugin extends StarterPlugin(
YGOTournamentPluginConfig, YGOTournamentPluginConfig,
) { ) {
@Inject(true) @Inject(true)
private cache: Cache; private aragami: AragamiPlugin;
private s3: S3Client; private s3: S3Client;
private async getChallongeCache() { private async getChallongeCache() {
const data = this.cache.get( return this.aragami.get(
'challongeData', ChallongeData,
this.config.tournament.challongeTournamentId, this.config.tournament.challongeTournamentId,
); );
if (data) {
return plainToClass(TournamentWrapper, data);
}
} }
private async deleteChallongeCache() { private async deleteChallongeCache() {
return this.cache.del( return this.aragami.get(
'challongeData', ChallongeData,
this.config.tournament.challongeTournamentId, this.config.tournament.challongeTournamentId,
); );
} }
private async setChallongeCache(data: TournamentWrapper) { private async setChallongeCache(data: TournamentWrapper) {
return this.cache.set( return this.aragami.set(ChallongeData, {
'challongeData', ...data,
this.config.tournament.challongeTournamentId, challongeId: this.config.tournament.challongeTournamentId,
classToPlain(data), });
);
} }
private async fetchChallongeData() { private async fetchChallongeData() {
...@@ -63,7 +63,7 @@ export default class YGOTournamentPlugin extends StarterPlugin( ...@@ -63,7 +63,7 @@ export default class YGOTournamentPlugin extends StarterPlugin(
if (cached) { if (cached) {
return cached; return cached;
} }
const plain = await this.ctx.http.get( const data = await this.ctx.http.get(
`${this.config.tournament.getChallongeUrl()}.json`, `${this.config.tournament.getChallongeUrl()}.json`,
{ {
params: { params: {
...@@ -73,9 +73,7 @@ export default class YGOTournamentPlugin extends StarterPlugin( ...@@ -73,9 +73,7 @@ export default class YGOTournamentPlugin extends StarterPlugin(
}, },
}, },
); );
const data = plainToClass(TournamentWrapper, plain); return this.setChallongeCache(data);
await this.setChallongeCache(data);
return data;
} }
private async onUserQuit(userId: string) { private async onUserQuit(userId: string) {
...@@ -158,7 +156,7 @@ export default class YGOTournamentPlugin extends StarterPlugin( ...@@ -158,7 +156,7 @@ export default class YGOTournamentPlugin extends StarterPlugin(
} }
private async onUserDeclareLate(userId: string) { private async onUserDeclareLate(userId: string) {
const timeUnix = await this.cache.get('lateDeclarationTime', 'current'); const timeUnix = await this.aragami.get(LateDeclarationTime, 'current');
// this.ctx.logger('test').warn(JSON.stringify(timeUnix)); // this.ctx.logger('test').warn(JSON.stringify(timeUnix));
if ( if (
!timeUnix || !timeUnix ||
...@@ -306,7 +304,6 @@ export default class YGOTournamentPlugin extends StarterPlugin( ...@@ -306,7 +304,6 @@ export default class YGOTournamentPlugin extends StarterPlugin(
if (!this.config.isTournamentEnabled()) { if (!this.config.isTournamentEnabled()) {
return; return;
} }
this.cache.table('lateDeclarationTime', { maxAge: 3600 * 1000 });
this.ctx this.ctx
.command('tournament/currentmatch', '获取当前对局') .command('tournament/currentmatch', '获取当前对局')
.shortcut('获取当前对局') .shortcut('获取当前对局')
...@@ -373,14 +370,13 @@ export default class YGOTournamentPlugin extends StarterPlugin( ...@@ -373,14 +370,13 @@ export default class YGOTournamentPlugin extends StarterPlugin(
.action(async ({ session, options }) => { .action(async ({ session, options }) => {
const fromTime = moment().add(options.delay, 'minutes'); const fromTime = moment().add(options.delay, 'minutes');
const toTime = fromTime.clone().add(options.duration, 'minutes'); const toTime = fromTime.clone().add(options.duration, 'minutes');
await this.cache.set( await this.aragami.set(
'lateDeclarationTime', LateDeclarationTime,
'current',
{ {
fromTime: fromTime.unix(), fromTime: fromTime.unix(),
toTime: toTime.unix(), toTime: toTime.unix(),
}, },
(options.delay + options.duration) * 60000, { ttl: (options.delay + options.duration) * 60000 },
); );
return `设置成功。将允许在 ${fromTime.format( return `设置成功。将允许在 ${fromTime.format(
'HH:mm:ss', 'HH:mm:ss',
...@@ -390,14 +386,14 @@ export default class YGOTournamentPlugin extends StarterPlugin( ...@@ -390,14 +386,14 @@ export default class YGOTournamentPlugin extends StarterPlugin(
.subcommand('.disablelate', '关闭迟到杀') .subcommand('.disablelate', '关闭迟到杀')
.usage('不再允许选手进行迟到杀操作。') .usage('不再允许选手进行迟到杀操作。')
.action(async () => { .action(async () => {
await this.cache.del('lateDeclarationTime', 'current'); await this.aragami.del(LateDeclarationTime, 'current');
return '设置成功。选手不再允许进行迟到杀操作。'; return '设置成功。选手不再允许进行迟到杀操作。';
}); });
judgeCommand judgeCommand
.subcommand('.refresh', '清理 Challonge 缓存') .subcommand('.refresh', '清理 Challonge 缓存')
.action(async () => { .action(async () => {
await this.cache.del( await this.aragami.del(
'challongeData', ChallongeData,
this.config.tournament.challongeTournamentId, this.config.tournament.challongeTournamentId,
); );
return '清理缓存成功。'; return '清理缓存成功。';
......
import { App } from 'koishi'; import { Context } from 'koishi';
import TargetPlugin from '../src'; import TargetPlugin from '../src';
describe('Test of plugin.', () => { describe('Test of plugin.', () => {
let app: App; let app: Context;
beforeEach(async () => { beforeEach(async () => {
app = new App(); app = new Context();
// app.plugin(TargetPlugin); // app.plugin(TargetPlugin);
await app.start(); await app.start();
}); });
......
const path = require('path'); const path = require('path');
const packgeInfo = require('./package.json'); const packgeInfo = require('./package.json');
const { ESBuildMinifyPlugin } = require('esbuild-loader');
function externalsFromDep() { function externalsFromDep() {
return Object.fromEntries( return Object.fromEntries(
...@@ -43,4 +44,11 @@ module.exports = { ...@@ -43,4 +44,11 @@ module.exports = {
koishi: 'koishi', koishi: 'koishi',
...(packAll ? {} : externalsFromDep()), ...(packAll ? {} : externalsFromDep()),
}, },
optimization: {
minimizer: [
new ESBuildMinifyPlugin({
keepNames: true,
}),
],
},
}; };
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