Commit abe143da authored by nanahira's avatar nanahira

/history

parent 092fadcd
Pipeline #2467 passed with stages
in 37 seconds
...@@ -221,4 +221,8 @@ export class AppController { ...@@ -221,4 +221,8 @@ export class AppController {
const code = await this.appService.submitDeckInfo(body); const code = await this.appService.submitDeckInfo(body);
res.status(code).json({ code }); res.status(code).json({ code });
} }
@Get('history')
async getBattleHistory(@Query() query: any) {
return await this.appService.getBattleHistory(query);
}
} }
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { InjectConnection, InjectEntityManager } from '@nestjs/typeorm'; import { InjectConnection, InjectEntityManager } from '@nestjs/typeorm';
import { import {
Brackets,
Connection, Connection,
EntityManager, EntityManager,
LessThan, LessThan,
LessThanOrEqual, LessThanOrEqual,
Like, Like,
MoreThanOrEqual, MoreThanOrEqual,
SelectQueryBuilder,
} from 'typeorm'; } from 'typeorm';
import { UserInfo } from './entities/mycard/UserInfo'; import { UserInfo } from './entities/mycard/UserInfo';
import Filter from 'bad-words-chinese'; import Filter from 'bad-words-chinese';
...@@ -622,7 +624,6 @@ export class AppService { ...@@ -622,7 +624,6 @@ export class AppService {
if (!tryFirstWin) { if (!tryFirstWin) {
firstWin = true; firstWin = true;
} }
console.log(tryFirstWin);
} }
const ptResult = EloUtility.getEloScore(userA.pt, userB.pt, sa, sb); const ptResult = EloUtility.getEloScore(userA.pt, userB.pt, sa, sb);
...@@ -1050,6 +1051,20 @@ export class AppService { ...@@ -1050,6 +1051,20 @@ export class AppService {
voteCountMap[voteId] = voteCountResult.voteCount; voteCountMap[voteId] = voteCountResult.voteCount;
} }
private async queryPageAndTotal<T>(
page_no: number,
page_num: number,
queryFunction: () => SelectQueryBuilder<T>,
) {
// page_no 当前页数 page_num 每页展示数
// offset = (page_no - 1) * page_num
// select * from battle_history limit 5 offset 15;
const offset = (page_no - 1) * page_num;
const total = await queryFunction().getCount();
const data = await queryFunction().limit(page_num).offset(offset).getMany();
return { total, data };
}
async getVotes(query: any) { async getVotes(query: any) {
const username = query.username; const username = query.username;
const type = query.type; const type = query.type;
...@@ -1064,21 +1079,22 @@ export class AppService { ...@@ -1064,21 +1079,22 @@ export class AppService {
const from_date = query.from_date; const from_date = query.from_date;
const to_date = query.to_date; const to_date = query.to_date;
// page_no 当前页数 page_num 每页展示数
// offset = (page_no - 1) * page_num
// select * from battle_history limit 5 offset 15;
const page_no: number = query.page || 1; const page_no: number = query.page || 1;
const page_num: number = query.page_num || 15; const page_num: number = query.page_num || 15;
const offset = (page_no - 1) * page_num; const offset = (page_no - 1) * page_num;
const repo = this.mcdb.getRepository(Votes); const repo = this.mcdb.getRepository(Votes);
const { total, data: votes } = await this.queryPageAndTotal(
page_no,
page_num,
() => {
const voteQuery = repo.createQueryBuilder(); const voteQuery = repo.createQueryBuilder();
if (status !== undefined) { if (status !== undefined) {
voteQuery.where('status = :status', { status }); voteQuery.where('status = :status', { status });
} }
const total = await voteQuery.getCount();
voteQuery.orderBy('create_time', 'DESC'); voteQuery.orderBy('create_time', 'DESC');
voteQuery.limit(page_num).offset(offset); return voteQuery;
const votes = await voteQuery.getMany(); },
);
const optionCountMap: any = {}; const optionCountMap: any = {};
const voteCountMap: any = {}; const voteCountMap: any = {};
await Promise.all( await Promise.all(
...@@ -1373,4 +1389,49 @@ export class AppService { ...@@ -1373,4 +1389,49 @@ export class AppService {
}); });
return code; return code;
} }
async getBattleHistory(query: any) {
const username: string = query.username;
const type = query.type;
let arena: string = null; //1 athletic 2 entertain
if (type === '1') {
arena = 'athletic';
}
if (type === '2') {
arena = 'entertain';
}
const from_date = query.from_date;
const to_date = query.to_date;
// page_no 当前页数 page_num 每页展示数
// offset = (page_no - 1) * page_num
// select * from battle_history limit 5 offset 15;
const page_no: number = query.page || 1;
const page_num: number = query.page_num || 15;
const ret = await this.queryPageAndTotal(page_no, page_num, () => {
const query = this.mcdb
.getRepository(BattleHistory)
.createQueryBuilder()
.where('1 = 1');
if (username) {
query.andWhere(
new Brackets((qb) => {
qb.where('usernamea = :usernamea', {
usernamea: username,
}).orWhere('usernameb = :usernameb', {
usernameb: username,
});
}),
);
}
if (arena) {
query.andWhere('type = :arena', { arena });
}
query.orderBy('start_time', 'DESC');
return query;
});
return ret;
}
} }
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