From d6c4fe3e11b580e1b27b8f56c40d3a634649b64d Mon Sep 17 00:00:00 2001 From: Kalane Date: Tue, 14 Sep 2021 13:49:24 +0200 Subject: [PATCH] feat: load more matches button --- client/src/store/modules/summoner.js | 11 +---- .../app/Controllers/Http/MatchesController.ts | 22 +++++++++ .../Controllers/Http/SummonersController.ts | 7 +-- server-v2/app/Services/MatchService.ts | 14 +++--- .../app/Validators/MatchesIndexValidator.ts | 45 +++++++++++++++++++ .../Validators/SummonerOverviewValidator.ts | 1 - server-v2/start/routes.ts | 2 +- 7 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 server-v2/app/Controllers/Http/MatchesController.ts create mode 100644 server-v2/app/Validators/MatchesIndexValidator.ts diff --git a/client/src/store/modules/summoner.js b/client/src/store/modules/summoner.js index 7c3e840..395833c 100644 --- a/client/src/store/modules/summoner.js +++ b/client/src/store/modules/summoner.js @@ -178,22 +178,15 @@ export const actions = { async moreMatches({ commit, getters, rootState }) { commit('MATCHES_LOADING') - const gameIds = getters.filteredMatchList + const matchIds = getters.filteredMatchList .slice(state.overview.matchIndex, state.overview.matchIndex + 10) - .map((gameId) => { - if(typeof gameId == 'string') { - return gameId - } - return gameId.gameId.toString() - }) const resp = await axios(({ url: 'match', data: { puuid: state.basic.account.puuid, - accountId: state.basic.account.accountId, region: rootState.regionsList[rootState.settings.region], - gameIds + matchIds }, method: 'POST' })).catch(() => { }) diff --git a/server-v2/app/Controllers/Http/MatchesController.ts b/server-v2/app/Controllers/Http/MatchesController.ts new file mode 100644 index 0000000..040e2dd --- /dev/null +++ b/server-v2/app/Controllers/Http/MatchesController.ts @@ -0,0 +1,22 @@ +import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' +import MatchService from 'App/Services/MatchService' +import MatchesIndexValidator from 'App/Validators/MatchesIndexValidator' + +export default class MatchesController { + /** + * POST - Return data from matches searched by gameIds + * @param ctx + */ + public async index({ request, response }: HttpContextContract) { + console.log('More Matches Request') + const { puuid, region, matchIds, season } = await request.validate(MatchesIndexValidator) + const matches = await MatchService.getMatches(region, matchIds, puuid) + + // TODO: add Stats here + // const stats = await StatsService.getSummonerStats(puuid, season) + return response.json({ + matches, + stats: 'TODO', + }) + } +} diff --git a/server-v2/app/Controllers/Http/SummonersController.ts b/server-v2/app/Controllers/Http/SummonersController.ts index a7199bc..4df1327 100644 --- a/server-v2/app/Controllers/Http/SummonersController.ts +++ b/server-v2/app/Controllers/Http/SummonersController.ts @@ -54,21 +54,22 @@ export default class SummonersController { public async overview({ request, response }: HttpContextContract) { console.time('OVERVIEW_REQUEST') - const { puuid, accountId, region, season } = await request.validate(SummonerOverviewValidator) + const { puuid, region, season } = await request.validate(SummonerOverviewValidator) const finalJSON: any = {} // Summoner in DB const summonerDB = await Summoner.firstOrCreate({ puuid: puuid }) // MATCHES BASIC - const matchIds = await summonerDB + const matchlist = await summonerDB .related('matchList') .query() .select('matchId') .orderBy('matchId', 'desc') .limit(10) + const matchIds = matchlist.map((m) => m.matchId) - finalJSON.matchesDetails = await MatchService.getMatches(region, matchIds, summonerDB) + finalJSON.matchesDetails = await MatchService.getMatches(region, matchIds, puuid) // TODO: STATS // console.time('STATS') diff --git a/server-v2/app/Services/MatchService.ts b/server-v2/app/Services/MatchService.ts index 8dda51e..2bf21ea 100644 --- a/server-v2/app/Services/MatchService.ts +++ b/server-v2/app/Services/MatchService.ts @@ -81,16 +81,16 @@ class MatchService { */ public async getMatches( region: string, - matchList: SummonerMatchlist[], - summonerDB: Summoner + matchIds: string[], + puuid: string ): Promise { console.time('getMatches') let matches: SerializedMatch[] = [] const matchesToGetFromRiot: MatchlistDto = [] - for (let i = 0; i < matchList.length; ++i) { + for (let i = 0; i < matchIds.length; ++i) { const matchSaved = await Match.query() - .where('id', matchList[i].matchId) + .where('id', matchIds[i]) .preload('blueTeam') .preload('redTeam') .preload('players') @@ -98,9 +98,9 @@ class MatchService { if (matchSaved) { // TODO: Serialize match from DB + put it in Redis + push it in "matches" - matches.push(BasicMatchSerializer.serializeOneMatch(matchSaved, summonerDB.puuid)) + matches.push(BasicMatchSerializer.serializeOneMatch(matchSaved, puuid)) } else { - matchesToGetFromRiot.push(matchList[i].matchId) + matchesToGetFromRiot.push(matchIds[i]) } } @@ -113,7 +113,7 @@ class MatchService { const parsedMatches: any = await MatchParser.parse(matchesFromApi) // TODO: Serialize match from DB + put it in Redis + push it in "matches" - const serializedMatches = BasicMatchSerializer.serialize(parsedMatches, summonerDB.puuid) + const serializedMatches = BasicMatchSerializer.serialize(parsedMatches, puuid) matches = [...matches, ...serializedMatches] } diff --git a/server-v2/app/Validators/MatchesIndexValidator.ts b/server-v2/app/Validators/MatchesIndexValidator.ts new file mode 100644 index 0000000..674865f --- /dev/null +++ b/server-v2/app/Validators/MatchesIndexValidator.ts @@ -0,0 +1,45 @@ +import { schema } from '@ioc:Adonis/Core/Validator' +import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' + +export default class MatchesIndexValidator { + constructor(protected ctx: HttpContextContract) {} + + /* + * Define schema to validate the "shape", "type", "formatting" and "integrity" of data. + * + * For example: + * 1. The username must be of data type string. But then also, it should + * not contain special characters or numbers. + * ``` + * schema.string({}, [ rules.alpha() ]) + * ``` + * + * 2. The email must be of data type string, formatted as a valid + * email. But also, not used by any other user. + * ``` + * schema.string({}, [ + * rules.email(), + * rules.unique({ table: 'users', column: 'email' }), + * ]) + * ``` + */ + public schema = schema.create({ + puuid: schema.string(), + region: schema.string(), + matchIds: schema.array().members(schema.string()), + season: schema.number.optional(), + }) + + /** + * Custom messages for validation failures. You can make use of dot notation `(.)` + * for targeting nested fields and array expressions `(*)` for targeting all + * children of an array. For example: + * + * { + * 'profile.username.required': 'Username is required', + * 'scores.*.number': 'Define scores as valid numbers' + * } + * + */ + public messages = {} +} diff --git a/server-v2/app/Validators/SummonerOverviewValidator.ts b/server-v2/app/Validators/SummonerOverviewValidator.ts index 3fd6c6b..e6700bc 100644 --- a/server-v2/app/Validators/SummonerOverviewValidator.ts +++ b/server-v2/app/Validators/SummonerOverviewValidator.ts @@ -25,7 +25,6 @@ export default class SummonerOverviewValidator { */ public schema = schema.create({ puuid: schema.string(), - accountId: schema.string(), region: schema.string(), season: schema.number.optional(), }) diff --git a/server-v2/start/routes.ts b/server-v2/start/routes.ts index bf6bcaa..e34e24c 100644 --- a/server-v2/start/routes.ts +++ b/server-v2/start/routes.ts @@ -37,7 +37,7 @@ Route.post('/summoner/overview', 'SummonersController.overview') // Route.post('/summoner/records', 'SummonersController.records') // Route.post('/summoner/live', 'SummonersController.liveMatchDetails') -// Route.post('/match', 'MatchesController.index') +Route.post('/match', 'MatchesController.index') // Route.post('/match/details', 'MatchesController.show') // Route.post('/match/details/ranks', 'MatchesController.showRanks')