Commit 980c4376 authored by nanahira's avatar nanahira

change payment interface to pay-exp

parent 0593163d
Pipeline #17562 passed with stages
in 2 minutes and 28 seconds
......@@ -14,6 +14,7 @@ import {
Query,
UploadedFile,
UseInterceptors,
ValidationPipe,
} from '@nestjs/common';
import { AppService } from './app.service';
import { config } from './config';
......@@ -33,6 +34,7 @@ import {
} from '@nestjs/swagger';
import { FileUploadDto } from './dto/FileUploadDto';
import { HomePageMatchCountDto } from './dto/HomePageMatchCount.dto';
import { PayExpDto } from './dto/PayExp.dto';
@Controller('api')
@ApiTags('arena')
......@@ -274,11 +276,20 @@ export class AppController {
return this.appService.getLastMonthBattleCount();
}
@Get('novelai-auth')
@ApiOperation({ summary: 'novelai 用认证' })
@Get('pay-exp')
@ApiOperation({ summary: '支付 exp' })
@ApiHeader({ name: 'Authorization' })
@ApiOkResponse({ type: CodeResponseDto })
async novelaiAuth(@Headers('Authorization') token: string) {
return this.appService.novelaiAuth(token);
async payExp(
@Headers('Authorization') token: string,
@Query(
new ValidationPipe({
transform: true,
transformOptions: { enableImplicitConversion: true },
}),
)
query: PayExpDto,
) {
return this.appService.payExp(token, query);
}
}
......@@ -40,6 +40,7 @@ import { AthleticCheckerService } from './athletic-checker/athletic-checker.serv
import { HomePageMatchCountDto } from './dto/HomePageMatchCount.dto';
import { AccountService } from './account/account.service';
import { CodeResponseDto } from './dto/CodeResponse.dto';
import { PayExpDto } from './dto/PayExp.dto';
const attrOffset = 1010;
const raceOffset = 1020;
......@@ -1781,7 +1782,7 @@ export class AppService {
return new HomePageMatchCountDto(count);
}
async novelaiAuth(token: string) {
async payExp(token: string, dto: PayExpDto) {
const user = await this.accountService.checkToken(token);
if (user.admin) {
return new CodeResponseDto(200);
......@@ -1793,22 +1794,22 @@ export class AppService {
});
if (!userInfo) {
this.log.log(
`${user.username} has no duel records, cannot use novelai.`,
`${user.username} has no duel records, cannot use ${dto.getName()}.`,
);
throw new HttpException(new CodeResponseDto(402), 402);
}
if (userInfo.exp < config.novelaiCost) {
if (userInfo.exp < dto.cost) {
this.log.log(
`${user.username} has no enough exp to use novelai: ${userInfo.exp}`,
);
throw new HttpException(new CodeResponseDto(402), 402);
}
this.log.log(
`${user.username} paid ${config.novelaiCost} exp to use novelai.`,
`${user.username} paid ${dto.cost} exp to use ${dto.getName()}.`,
);
await edb
.getRepository(UserInfo)
.decrement({ username: user.username }, 'exp', config.novelaiCost);
.decrement({ username: user.username }, 'exp', dto.cost);
return new CodeResponseDto(200);
});
}
......
......@@ -9,7 +9,6 @@ export interface Config {
analyzerHost: string;
enableSchedule: boolean;
accountsUrl: string;
novelaiCost: number;
}
export const athleticCheckConfig = {
......@@ -35,7 +34,4 @@ export const config: Config = {
enableSchedule: !process.env.NO_SCHEDULE,
accountsUrl:
process.env.ACCOUNTS_URL || 'https://sapi.moecube.com:444/accounts',
novelaiCost: process.env.NOVELAI_COST
? parseInt(process.env.NOVELAI_COST)
: 3,
};
......@@ -5,8 +5,11 @@ export class CodeResponseDto {
code: number;
@ApiProperty({ description: '是否成功' })
success: boolean;
constructor(code: number) {
@ApiProperty({ description: '错误信息' })
message?: string;
constructor(code: number, message?: string) {
this.success = code < 400;
this.code = code;
this.message = message;
}
}
import { ApiProperty } from "@nestjs/swagger";
import { IsInt, IsOptional, IsPositive, IsString } from "class-validator";
export class PayExpDto {
@IsInt()
@IsPositive()
@ApiProperty({ description: '支付数量' })
cost: number;
@IsString()
@IsOptional()
@ApiProperty({ description: '支付名称' })
name?: string;
getName() {
return this.name || 'service';
}
}
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