feat: 看历史 7 天的手牌记录

This commit is contained in:
liutong.eric
2022-10-19 02:58:10 +08:00
parent b2acadda01
commit e073e556d2
14 changed files with 168 additions and 262 deletions
+46 -1
View File
@@ -35,7 +35,7 @@ export class GameRecordController extends BaseController {
try {
const { body } = this.getRequestBody();
const state = this.ctx.state;
const commandList = await this.commandService.findByRoomNumber(body.gameId);
const commandList = await this.commandService.findByGameID(body.gameId);
const gameList = await this.gameService.findByRoomNumber(body.roomNumber);
let result: IFindGameRecord;
console.log(state, 'user');
@@ -65,6 +65,51 @@ export class GameRecordController extends BaseController {
}
}
@post('/find/selfPast7DayGame')
async selfPast7DayGame() {
try {
const { body } = this.getRequestBody();
const gameIDList = await this.commandService.findPast7DayGameIDsByUserID(body.userID);
if (!gameIDList.length) {
this.success([]);
return;
}
const gameList = await this.gameService.findByIDs(gameIDList);
const commandList = await this.commandService.findByGameIDs(gameIDList);
const result: any = [];
gameList.forEach(g => {
if (g.status === EGameOverType.GAME_OVER) {
const winner = JSON.parse(g.winners || '')[0][0];
delete winner.handCard;
g.winners = JSON.stringify([[ winner ]]);
}
const gameCommandList = commandList.filter(c => {
return c.gameId === g.id;
});
// 过滤其他人手牌
gameCommandList.forEach(c => {
if (c.userId !== this.ctx.state.user.user.userId) {
c.handCard = '';
}
});
result.push({
gameCommandList,
winners: g.winners,
gameId: g.id,
});
});
this.success(result);
} catch (e) {
this.fail('find self command record error');
console.log(e);
}
}
@post('/find/gameRecord')
async index() {
try {
+1
View File
@@ -81,6 +81,7 @@ export default (appInfo: EggAppInfo) => {
redis: {
host: '127.0.0.1',
port: 6379,
password: '123456',
},
};
+3 -3
View File
@@ -1,4 +1,3 @@
export interface ICommandRecord {
roomNumber: string;
gameId: number;
@@ -13,7 +12,8 @@ export interface ICommandRecord {
}
export interface ICommandRecordService {
findById(gid: number): Promise<ICommandRecord>;
findByRoomNumber(gameId: number): Promise<ICommandRecord[]>;
findByGameID(gameID: number): Promise<ICommandRecord[]>;
findByGameIDs(gameIDs: number[]): Promise<ICommandRecord[]>;
add(commandRecord: ICommandRecord): Promise<any>;
findPast7DayGameIDsByUserID(userID: number): Promise<number []>;
}
+2 -1
View File
@@ -9,7 +9,8 @@ export interface IGame {
}
export interface IGameService {
findById(gid: number): Promise<IGame>;
findByID(gid: number): Promise<IGame>;
findByIDs(ids: number[]): Promise<IGame []>;
findByRoomNumber(roomNumber: number): Promise<IGame []>;
add(game: IGame): Promise<any>;
update(game: IGame): Promise<any>;
+1 -1
View File
@@ -2,5 +2,5 @@ export interface IUser {
nickName: string;
account: string;
password?: string;
id?: string;
id?: number;
}
+24 -4
View File
@@ -20,11 +20,31 @@ export class CommandRecord implements ICommandRecordService {
return { succeed: result.affectedRows === 1 };
}
async findById(gid: number): Promise<ICommandRecord> {
return await this.mysql.get('game_record', { id: gid });
async findPast7DayGameIDsByUserID(userID: number): Promise<number[]> {
const result = await this.mysql.query('SELECT\n' +
'DISTINCT gameId\n' +
'FROM command_record\n' +
'WHERE userId = ?\n' +
'AND create_time >= DATE_SUB(now(),interval 7 DAY)', [ userID ]);
const recordList = JSON.parse(JSON.stringify(result));
if (recordList) {
return recordList.map((item: ICommandRecord) => {
return item.gameId;
});
}
return [];
}
async findByRoomNumber(gameId: number): Promise<ICommandRecord []> {
async findByGameIDs(gameIDs: number[]): Promise<ICommandRecord []> {
const result = await this.mysql.select('command_record', {
where: {
gameId: gameIDs,
},
});
return JSON.parse(JSON.stringify(result));
}
async findByGameID(gameID: number): Promise<ICommandRecord []> {
const result = await this.mysql.query('SELECT\n' +
'\tcommand_record.counter,\n' +
'\tcommand_record.gameStatus,\n' +
@@ -39,7 +59,7 @@ export class CommandRecord implements ICommandRecordService {
'\tcommand_record\n' +
'INNER JOIN `user` ON `user`.id = command_record.userId\n' +
'INNER JOIN player ON player.userId = command_record.userId\n' +
'\twhere command_record.gameId = ? and player.gameId = ?', [ gameId, gameId ]);
'\twhere command_record.gameId = ? and player.gameId = ?', [ gameID, gameID ]);
console.log(result, '=============command');
return JSON.parse(JSON.stringify(result));
}
+7 -1
View File
@@ -27,10 +27,16 @@ export class GameService implements IGameService {
return { succeed: gameInfo.affectedRows === 1 };
}
async findById(gid: number): Promise<IGame> {
async findByID(gid: number): Promise<IGame> {
return await this.mysql.get('game', { id: gid });
}
async findByIDs(ids: number[]): Promise<IGame[]> {
return await this.mysql.select('game', {
where: { id: ids },
});
}
async findByRoomNumber(roomNumber: number): Promise<IGame []> {
const result = await this.mysql.select('game', {
where: { roomNumber },