Commit d37c1be2 authored by nanahira's avatar nanahira

arena system

parent 542ddefd
Pipeline #19592 passed with stages
in 8 minutes and 18 seconds
......@@ -20,6 +20,7 @@ RUN npm ci && npm cache clean --force
COPY --from=builder /app/dist ./dist
COPY --chown=mycard:mycard ./config.example.yaml ./config.yaml
COPY --chown=mycard:mycard ./views ./views
ENV NODE_PG_FORCE_NATIVE=true
EXPOSE 3000
......
......@@ -103,6 +103,11 @@ import { ArenaScoreService } from './arena-score/arena-score.service';
type: 'parent',
name: '用户服务',
children: [
{
type: 'click',
name: 'MyCard 战绩',
command: 'arena-score',
},
{
type: 'click',
name: '服务器列表',
......
import { Injectable } from '@nestjs/common';
import { PutUserId, UseCommand } from 'koishi-nestjs';
import { TemplateService } from '../template/template.service';
import { HttpService } from '@nestjs/axios';
import { MycardAuthService } from 'nestjs-mycard';
import { MycardAccountService } from '../mycard-account/mycard-account.service';
import { lastValueFrom } from 'rxjs';
@Injectable()
export class ArenaScoreService {}
export class ArenaScoreService {
constructor(
private template: TemplateService,
private http: HttpService,
private mycard: MycardAuthService,
private mycardAccount: MycardAccountService,
) {}
@UseCommand('arena-score', '查询 MyCard 匹配战绩')
async arenaScoreCommand(@PutUserId() id: string) {
const user = await this.mycardAccount.findOrCreateUser(id);
if (!user.mycardId) {
return (
'您还未绑定 MyCard 帐号。' + (await this.mycardAccount.bind(id, true))
);
}
const mycardUser = await this.mycard.getUser(user.mycardId);
const result = await lastValueFrom(
this.http.get('https://sapi.moecube.com:444/ygopro/arena/user', {
params: {
username: mycardUser.username,
},
}),
);
return this.template.render('arena-score', {
...result.data,
name: mycardUser.username,
});
}
}
......@@ -9,8 +9,9 @@ import cryptoRandomString from 'crypto-random-string';
import { MycardAuthService, MycardUser } from 'nestjs-mycard';
import { ConfigService } from '@nestjs/config';
import { BindQueryDto } from './dto/bind-query.dto';
import { TemplateService } from '../template/template.service';
@CacheTTL(1800)
@CacheTTL(1800 * 1000)
class BindSession {
@CacheKey()
sessionId: string;
......@@ -42,6 +43,7 @@ export class MycardAccountService extends CrudService(User) {
@InjectAragami() private aragami: Aragami,
private mycard: MycardAuthService,
private config: ConfigService,
private template: TemplateService,
) {
super(repo);
}
......@@ -67,19 +69,21 @@ export class MycardAccountService extends CrudService(User) {
}
@UseCommand('bind', '绑定 MyCard 帐号')
async bind(@PutUserId() id: string) {
async bind(@PutUserId() id: string, needHint = false) {
const session = BindSession.create(id);
console.log(session.sessionId);
await this.aragami.set(session);
const url = loginUrl(
`${this.rootUrl}/api/mycard-account/bind?session=${session.sessionId}`,
);
return `请点击下面的链接完成 MyCard 帐号绑定:\n${url}`;
return this.template.render('bind-account', { url, needHint });
}
async bindCallback(
dto: BindQueryDto,
): Promise<{ error?: string; user?: MycardUser }> {
const session = await this.aragami.get(BindSession, dto.session);
console.log(dto.session, session);
if (!session) {
return { error: '会话不存在。' };
}
......
{{name}},您的的 DP 是 {{pt}},排名第 {{arena_rank}}。您的 EXP 是 {{exp}},排名第 {{exp_rank}}
您的竞技匹配总场数是 {{athletic_all}},胜场数是 {{athletic_win}},胜率是 {{athletic_wl_ratio}}%。
您的娱乐匹配总场数是 {{entertain_all}},胜场数是 {{entertain_win}},胜率是 {{entertain_wl_ratio}}%。
\ No newline at end of file
请点击下面的链接完成 MyCard 帐号绑定。
{{url}}
{{#needHint}}日后可以通过指令 bind 绑定或换绑 MyCard 帐号,通过指令 unbind 解绑 MyCard 帐号。{{/needHint}}
\ No newline at end of file
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