Commit 3e15baa8 authored by nanahira's avatar nanahira

bump to Koishi 4.8

parent 7d10ffe8
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-tabulate/issues" "url": "https://code.mycard.moe/3rdeye/koishi-plugin-tabulate/issues"
...@@ -32,22 +32,22 @@ ...@@ -32,22 +32,22 @@
"homepage": "https://code.mycard.moe/3rdeye/koishi-plugin-tabulate", "homepage": "https://code.mycard.moe/3rdeye/koishi-plugin-tabulate",
"dependencies": { "dependencies": {
"class-transformer": "^0.5.1", "class-transformer": "^0.5.1",
"koishi-thirdeye": "^10.3.2", "koishi-thirdeye": "^11.0.6",
"league-tabulator": "^1.0.0", "league-tabulator": "^1.0.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"moment": "^2.29.1", "moment": "^2.29.1",
"reflect-metadata": "^0.1.13" "reflect-metadata": "^0.1.13"
}, },
"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/lodash": "^4.14.176", "@types/lodash": "^4.14.176",
"@types/node": "^16.11.7", "@types/node": "^16.11.7",
"@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",
...@@ -63,7 +63,8 @@ ...@@ -63,7 +63,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';
import { Context, Cache, Session } from 'koishi'; import { Context, Session } from 'koishi';
import { TabulatePluginConfig, TabulatePluginConfigLike } from './config'; import { TabulatePluginConfig, TabulatePluginConfigLike } from './config';
import { import {
DefinePlugin, DefinePlugin,
InjectConfig, InjectConfig,
Inject, Inject,
OnApply,
UseCommand, UseCommand,
CommandUsage, CommandUsage,
CommandExample, CommandExample,
...@@ -15,53 +14,56 @@ import { ...@@ -15,53 +14,56 @@ import {
CommandDescription, CommandDescription,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import { Game, ReportScoreResult } from 'league-tabulator'; import { Game, ReportScoreResult } from 'league-tabulator';
import { classToPlain, plainToClass } from 'class-transformer'; import { plainToInstance, Transform } from 'class-transformer';
import _ from 'lodash'; import _ from 'lodash';
export * from './config'; export * from './config';
import AragamiPlugin, { CacheKey } from 'koishi-plugin-cache-aragami';
declare module 'koishi' { class TabulateMatches {
namespace Cache { @CacheKey()
interface Tables { location: string;
tabulateMatches: Record<string, Partial<Game>>;
} @Transform((params) =>
} _.mapValues(params.value, (v) => plainToInstance(Game, v)),
)
games: Record<string, Game>;
} }
@DefinePlugin({ name: 'tabulate', schema: TabulatePluginConfig }) @DefinePlugin({ name: 'tabulate', schema: TabulatePluginConfig })
export default class TabulatePlugin implements OnApply { export default class TabulatePlugin {
constructor(private ctx: Context, config: TabulatePluginConfigLike) {} constructor(private ctx: Context, config: TabulatePluginConfigLike) {}
async getGames(key: string) { async getGames(key: string) {
const dataFromCache = (await this.cache.get('tabulateMatches', key)) || {}; let data = await this.aragami.get(TabulateMatches, key);
return _.mapValues(dataFromCache, (d) => plainToClass(Game, d)); if (data) {
return data;
}
data = new TabulateMatches();
data.games = {};
data.location = key;
return data;
} }
async saveGame(game: Game) { async saveGame(game: Game) {
const dataFromCache = const matches = await this.getGames(game.location);
(await this.cache.get('tabulateMatches', game.location)) || {}; matches.games[game.getKey()] = game;
dataFromCache[game.getKey()] = classToPlain(game); return this.aragami.set(matches, { ttl: this.config.saveTime });
return this.cache.set('tabulateMatches', game.location, dataFromCache);
} }
async deleteGame(game: Game) { async deleteGame(game: Game) {
const dataFromCache = const matches = await this.getGames(game.location);
(await this.cache.get('tabulateMatches', game.location)) || {}; if (!matches.games[game.getKey()]) {
if (!dataFromCache[game.getKey()]) {
return; return;
} }
delete dataFromCache[game.getKey()]; delete matches.games[game.getKey()];
return this.cache.set('tabulateMatches', game.location, dataFromCache); return this.aragami.set(matches, { ttl: this.config.saveTime });
} }
@InjectConfig() @InjectConfig()
private config: TabulatePluginConfig; private config: TabulatePluginConfig;
@Inject('cache', true) @Inject(true)
private cache: Cache; private aragami: AragamiPlugin;
onApply() {
this.cache.table('tabulateMatches', { maxAge: this.config.saveTime });
}
@UseCommand('tabulate', '排表') @UseCommand('tabulate', '排表')
@CommandDescription({ en: 'Tabulate' }) @CommandDescription({ en: 'Tabulate' })
...@@ -175,7 +177,7 @@ export default class TabulatePlugin implements OnApply { ...@@ -175,7 +177,7 @@ export default class TabulatePlugin implements OnApply {
]), ]),
); );
const gamesMap = await this.getGames(exactLocation); const gamesMap = await this.getGames(exactLocation);
const game = Object.values(gamesMap).find((game) => const game = Object.values(gamesMap.games).find((game) =>
game.includesPendingPlayer(possibleNames), game.includesPendingPlayer(possibleNames),
); );
if (!game) { if (!game) {
......
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