From e073e556d2d19ff470d7fa27c71806da612445d4 Mon Sep 17 00:00:00 2001 From: "liutong.eric" Date: Wed, 19 Oct 2022 02:58:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=9C=8B=E5=8E=86=E5=8F=B2=207=20?= =?UTF-8?q?=E5=A4=A9=E7=9A=84=E6=89=8B=E7=89=8C=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 205 ------------------------ client/src/interface/IGameRecord.ts | 19 ++- client/src/router/index.ts | 3 +- client/src/service/index.ts | 76 ++++----- client/src/views/game.vue | 6 +- client/src/views/home.vue | 24 ++- client/src/views/login.vue | 2 +- server/src/app/controller/gameRecord.ts | 47 +++++- server/src/config/config.default.ts | 1 + server/src/interface/ICommandRecord.ts | 6 +- server/src/interface/IGame.ts | 3 +- server/src/interface/IUser.ts | 2 +- server/src/service/commandRecord.ts | 28 +++- server/src/service/game.ts | 8 +- 14 files changed, 168 insertions(+), 262 deletions(-) delete mode 100755 .idea/workspace.xml diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100755 index 20090c9..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,205 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - true - - true - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1586870120922 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/client/src/interface/IGameRecord.ts b/client/src/interface/IGameRecord.ts index 7c69b15..90683e4 100644 --- a/client/src/interface/IGameRecord.ts +++ b/client/src/interface/IGameRecord.ts @@ -1,3 +1,16 @@ -export interface IGameRecord { - gameId: number; -} +export interface IGameRecord { + gameId: number; +} + +export interface ICommandRecord { + roomNumber: string; + gameId: number; + userId: string; + type: string; + pot: number; + commonCard: string; + handCard?: string; + gameStatus: number; + command: string; + counter: number; +} diff --git a/client/src/router/index.ts b/client/src/router/index.ts index 8216006..d381278 100644 --- a/client/src/router/index.ts +++ b/client/src/router/index.ts @@ -5,7 +5,7 @@ import Login from '../views/login.vue'; import Register from '../views/register.vue'; import Game from '../views/game.vue'; import service from '../service'; -import origin from '../utils/origin'; +import cookie from 'js-cookie'; Vue.use(VueRouter); @@ -58,6 +58,7 @@ router.beforeEach(async (to, from, next) => { try { const result = await service.checkLogin(); console.log(result); + cookie.set('user_id', result.data.userId); next(); } catch (e) { await router.replace({ name: 'login' }); diff --git a/client/src/service/index.ts b/client/src/service/index.ts index 6ece508..16b0d12 100644 --- a/client/src/service/index.ts +++ b/client/src/service/index.ts @@ -1,36 +1,40 @@ -import request from '../utils/request'; - -export default { - register: ({ userAccount = '', password = '', nickName = '' }) => request({ - url: '/user/register', - body: { userAccount, password, nickName }, - }), - login: (userAccount: string, password: string ) => request({ - url: '/user/login', - body: { userAccount, password }, - }), - checkLogin: () => request({ - url: '/user', - body: {}, - }), - createRoom: (isShort: boolean, smallBlind: number, time: number) => request({ - url: '/game/room', - body: { isShort, smallBlind, time }, - }), - findRoom: (roomNumber: string) => request({ - url: '/game/room/find', - body: { roomNumber }, - }), - buyIn: (buyInSize: number) => request({ - url: '/game/buyIn', - body: { buyInSize }, - }), - commandRecordList: (roomNumber: string, gameId: number) => request({ - url: '/game/record/find/commandRecord', - body: { roomNumber, gameId }, - }), - gameRecordList: (roomNumber: string) => request({ - url: '/game/record/find/gameRecord', - body: { roomNumber }, - }), -}; +import request from '../utils/request'; + +export default { + register: ({ userAccount = '', password = '', nickName = '' }) => request({ + url: '/user/register', + body: { userAccount, password, nickName }, + }), + login: (userAccount: string, password: string ) => request({ + url: '/user/login', + body: { userAccount, password }, + }), + checkLogin: () => request({ + url: '/user', + body: {}, + }), + createRoom: (isShort: boolean, smallBlind: number, time: number) => request({ + url: '/game/room', + body: { isShort, smallBlind, time }, + }), + findRoom: (roomNumber: string) => request({ + url: '/game/room/find', + body: { roomNumber }, + }), + buyIn: (buyInSize: number) => request({ + url: '/game/buyIn', + body: { buyInSize }, + }), + commandRecordList: (roomNumber: string, gameId: number) => request({ + url: '/game/record/find/commandRecord', + body: { roomNumber, gameId }, + }), + gameRecordList: (roomNumber: string) => request({ + url: '/game/record/find/gameRecord', + body: { roomNumber }, + }), + selfPast7DayGame: (userID: number) => request({ + url: '/game/record/find/selfPast7DayGame', + body: { userID }, + }), +}; diff --git a/client/src/views/game.vue b/client/src/views/game.vue index 048e2bd..802bee1 100644 --- a/client/src/views/game.vue +++ b/client/src/views/game.vue @@ -306,8 +306,7 @@ const sit = []; for (let i = 0; i < 9; i++) { sit.push(node.node); - const next = node.next; - node = next; + node = node.next; } return sit; } @@ -526,7 +525,7 @@ } private closeAudio() { - this.audioStatus = !this.audioStatus + this.audioStatus = !this.audioStatus; } private play() { @@ -594,7 +593,6 @@ this.$plugin.toast('can\'t find the room'); } } - private created() { try { this.socketInit(); diff --git a/client/src/views/home.vue b/client/src/views/home.vue index 68516af..a53051b 100644 --- a/client/src/views/home.vue +++ b/client/src/views/home.vue @@ -32,6 +32,9 @@
test record
+
+ 7 day game history +
@@ -65,7 +68,7 @@ import gameRecord from '@/components/GameRecord.vue'; import service from '../service'; import cookie from 'js-cookie'; - import {IGameRecord} from '@/interface/IGameRecord'; + import { IGameRecord } from '@/interface/IGameRecord'; @Component({ components: { @@ -128,6 +131,25 @@ } } + private async selfPast7DayGame() { + try { + const userIDStr = cookie.get('user_id'); + if (userIDStr) { + const userID = Number(userIDStr); + const {data} = await service.selfPast7DayGame(userID); + data.forEach((v: IGameRecord) => { + this.gameList.push({ gameId: v.gameId}); + }); + this.currGameIndex = data.length; + this.commandList = data[data.length - 1].gameCommandList; + this.showRecord = true; + } + } catch (e) { + console.log(e); + this.$plugin.toast('can\'t find the user command record list'); + } + } + private async getRecord(index: number) { try { console.log('ccc'); diff --git a/client/src/views/login.vue b/client/src/views/login.vue index 552546e..c3ab9e3 100644 --- a/client/src/views/login.vue +++ b/client/src/views/login.vue @@ -43,7 +43,7 @@ try { const result = await service.login(this.userAccount, this.password); const { token } = result.data; - cookie.set('token', token, {expires: 1}); + cookie.set('token', token, { expires: 1 }); localStorage.setItem('token', token); await this.$router.push({name: 'home'}); } catch (e) { diff --git a/server/src/app/controller/gameRecord.ts b/server/src/app/controller/gameRecord.ts index 1f60ed2..761fceb 100644 --- a/server/src/app/controller/gameRecord.ts +++ b/server/src/app/controller/gameRecord.ts @@ -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 { diff --git a/server/src/config/config.default.ts b/server/src/config/config.default.ts index 279da0c..9eae547 100644 --- a/server/src/config/config.default.ts +++ b/server/src/config/config.default.ts @@ -81,6 +81,7 @@ export default (appInfo: EggAppInfo) => { redis: { host: '127.0.0.1', port: 6379, + password: '123456', }, }; diff --git a/server/src/interface/ICommandRecord.ts b/server/src/interface/ICommandRecord.ts index 2a35543..a712616 100644 --- a/server/src/interface/ICommandRecord.ts +++ b/server/src/interface/ICommandRecord.ts @@ -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; - findByRoomNumber(gameId: number): Promise; + findByGameID(gameID: number): Promise; + findByGameIDs(gameIDs: number[]): Promise; add(commandRecord: ICommandRecord): Promise; + findPast7DayGameIDsByUserID(userID: number): Promise; } diff --git a/server/src/interface/IGame.ts b/server/src/interface/IGame.ts index 6c40b5a..8785eb5 100644 --- a/server/src/interface/IGame.ts +++ b/server/src/interface/IGame.ts @@ -9,7 +9,8 @@ export interface IGame { } export interface IGameService { - findById(gid: number): Promise; + findByID(gid: number): Promise; + findByIDs(ids: number[]): Promise; findByRoomNumber(roomNumber: number): Promise; add(game: IGame): Promise; update(game: IGame): Promise; diff --git a/server/src/interface/IUser.ts b/server/src/interface/IUser.ts index ee8e13e..41bb198 100644 --- a/server/src/interface/IUser.ts +++ b/server/src/interface/IUser.ts @@ -2,5 +2,5 @@ export interface IUser { nickName: string; account: string; password?: string; - id?: string; + id?: number; } diff --git a/server/src/service/commandRecord.ts b/server/src/service/commandRecord.ts index 9ea7757..2e1353e 100644 --- a/server/src/service/commandRecord.ts +++ b/server/src/service/commandRecord.ts @@ -20,11 +20,31 @@ export class CommandRecord implements ICommandRecordService { return { succeed: result.affectedRows === 1 }; } - async findById(gid: number): Promise { - return await this.mysql.get('game_record', { id: gid }); + async findPast7DayGameIDsByUserID(userID: number): Promise { + 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 { + async findByGameIDs(gameIDs: number[]): Promise { + const result = await this.mysql.select('command_record', { + where: { + gameId: gameIDs, + }, + }); + return JSON.parse(JSON.stringify(result)); + } + + async findByGameID(gameID: number): Promise { 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)); } diff --git a/server/src/service/game.ts b/server/src/service/game.ts index 5066376..09b10df 100644 --- a/server/src/service/game.ts +++ b/server/src/service/game.ts @@ -27,10 +27,16 @@ export class GameService implements IGameService { return { succeed: gameInfo.affectedRows === 1 }; } - async findById(gid: number): Promise { + async findByID(gid: number): Promise { return await this.mysql.get('game', { id: gid }); } + async findByIDs(ids: number[]): Promise { + return await this.mysql.select('game', { + where: { id: ids }, + }); + } + async findByRoomNumber(roomNumber: number): Promise { const result = await this.mysql.select('game', { where: { roomNumber },