refactor: create a Repository to store mongo requests

This commit is contained in:
Valentin Kaelin 2019-11-12 19:47:23 +01:00
parent 1e3462ebcd
commit b4a2edfd32
5 changed files with 233 additions and 248 deletions

View file

@ -3,8 +3,8 @@
const Jax = use('Jax') const Jax = use('Jax')
const DetailedMatch = use('App/Models/DetailedMatch') const DetailedMatch = use('App/Models/DetailedMatch')
const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer') const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer')
const Match = use('App/Models/Match')
const MatchHelper = use('App/Helpers/MatchHelper') const MatchHelper = use('App/Helpers/MatchHelper')
const StatsHelper = use('App/Helpers/StatsHelper')
const Summoner = use('App/Models/Summoner') const Summoner = use('App/Models/Summoner')
class MatchController { class MatchController {
@ -21,32 +21,7 @@ class MatchController {
await summonerDB.save() await summonerDB.save()
// Stats const stats = await StatsHelper.getSummonerStats(account)
const globalStats = await Match.globalStats(account.puuid)
const gamemodeStats = await Match.gamemodeStats(account.puuid)
const roleStats = await Match.roleStats(account.puuid)
// Check if all roles are in the array
const roles = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'SUPPORT']
for (const role of roles) {
if (!roleStats.find(r => r.role === role)) {
roleStats.push({
count: 0,
losses: 0,
role,
wins: 0
})
}
}
const championClassStats = await Match.championClassStats(account.puuid)
const mates = await Match.mates(account.puuid, account.name)
const stats = {
global: globalStats[0],
league: gamemodeStats,
role: roleStats.sort(MatchHelper.sortTeamByRole),
class: championClassStats,
mates,
}
return response.json({ return response.json({
matches, matches,

View file

@ -2,8 +2,8 @@
const Jax = use('Jax') const Jax = use('Jax')
const MatchHelper = use('App/Helpers/MatchHelper') const MatchHelper = use('App/Helpers/MatchHelper')
const StatsHelper = use('App/Helpers/StatsHelper')
const Summoner = use('App/Models/Summoner') const Summoner = use('App/Models/Summoner')
const Match = use('App/Models/Match')
class SummonerController { class SummonerController {
/** /**
@ -62,31 +62,7 @@ class SummonerController {
// STATS // STATS
console.time('STATS') console.time('STATS')
const globalStats = await Match.globalStats(account.puuid) finalJSON.stats = await StatsHelper.getSummonerStats(account)
const gamemodeStats = await Match.gamemodeStats(account.puuid)
const roleStats = await Match.roleStats(account.puuid)
// Check if all roles are in the array
const roles = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'SUPPORT']
for (const role of roles) {
if (!roleStats.find(r => r.role === role)) {
roleStats.push({
count: 0,
losses: 0,
role,
wins: 0
})
}
}
const championClassStats = await Match.championClassStats(account.puuid)
const mates = await Match.mates(account.puuid, account.name)
finalJSON.stats = {
global: globalStats[0],
league: gamemodeStats,
role: roleStats.sort(MatchHelper.sortTeamByRole),
class: championClassStats,
mates,
}
console.timeEnd('STATS') console.timeEnd('STATS')
// SAVE IN DB // SAVE IN DB

View file

@ -0,0 +1,40 @@
'use strict'
const MatchHelper = use('App/Helpers/MatchHelper')
const MatchRepository = make('App/Repositories/MatchRepository')
class StatsHelper {
constructor() {
this.matchRepository = MatchRepository
}
async getSummonerStats(account) {
const globalStats = await this.matchRepository.globalStats(account.puuid)
const gamemodeStats = await this.matchRepository.gamemodeStats(account.puuid)
const roleStats = await this.matchRepository.roleStats(account.puuid)
// Check if all roles are in the array
const roles = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'SUPPORT']
for (const role of roles) {
if (!roleStats.find(r => r.role === role)) {
roleStats.push({
count: 0,
losses: 0,
role,
wins: 0
})
}
}
const championClassStats = await this.matchRepository.championClassStats(account.puuid)
const mates = await this.matchRepository.mates(account.puuid, account.name)
return {
global: globalStats[0],
league: gamemodeStats,
role: roleStats.sort(MatchHelper.sortTeamByRole),
class: championClassStats,
mates,
}
}
}
module.exports = new StatsHelper()

View file

@ -4,201 +4,6 @@
const Model = use('Model') const Model = use('Model')
class Match extends Model { class Match extends Model {
/**
* Get Summoner's statistics for all played champion classes
* @param puuid of the summoner
*/
static championClassStats(puuid) {
return Match.query().aggregate([
{
$match: {
summoner_puuid: puuid
}
},
{
$group: {
_id: { "$arrayElemAt": ["$champion.tags", 0] },
count: { $sum: 1 },
wins: {
$sum: {
$cond: [
{ $eq: ["$result", "Win"] }, 1, 0
]
}
},
losses: {
$sum: {
$cond: [
{ $eq: ["$result", "Fail"] }, 1, 0
]
}
}
}
}
])
}
/**
* Get Summoner's statistics for all played modes
* @param puuid of the summoner
*/
static gamemodeStats(puuid) {
return Match.query().aggregate([
{
$match: {
summoner_puuid: puuid
}
},
{
$group: {
_id: "$gamemode",
count: { $sum: 1 },
wins: {
$sum: {
$cond: [
{ $eq: ["$result", "Win"] }, 1, 0
]
}
},
losses: {
$sum: {
$cond: [
{ $eq: ["$result", "Fail"] }, 1, 0
]
}
}
}
}
])
}
/**
* Get global Summoner's statistics
* @param puuid of the summoner
*/
static globalStats(puuid) {
return Match.query().aggregate([
{
$match: {
summoner_puuid: puuid
}
},
{
$group: {
_id: null,
count: { $sum: 1 },
time: { $sum: "$time" },
wins: {
$sum: {
$cond: [
{ $eq: ["$result", "Win"] }, 1, 0
]
}
},
losses: {
$sum: {
$cond: [
{ $eq: ["$result", "Fail"] }, 1, 0
]
}
},
kills: { $sum: "$stats.kills" },
deaths: { $sum: "$stats.deaths" },
assists: { $sum: "$stats.assists" },
minions: { $sum: "$stats.minions" },
vision: { $sum: "$stats.vision" },
kp: { $avg: "$stats.kp" },
}
}
])
}
/**
* Get Summoner's statistics for the 5 differnt roles
* @param puuid of the summoner
*/
static roleStats(puuid) {
return Match.query().aggregate([
{
$match: {
summoner_puuid: puuid,
role: { $not: { $eq: 'NONE' } }
}
},
{
$group: {
_id: "$role",
count: { $sum: 1 },
wins: {
$sum: {
$cond: [
{ $eq: ["$result", "Win"] }, 1, 0
]
}
},
losses: {
$sum: {
$cond: [
{ $eq: ["$result", "Fail"] }, 1, 0
]
}
}
},
},
{
$project: {
role: "$_id",
count: "$count",
wins: "$wins",
losses: "$losses",
}
}
])
}
/**
* Get Summoner's mates list
* @param puuid of the summoner
* @param summonerName of the summoner
*/
static mates(puuid, summonerName) {
return Match.query().aggregate([
{
$match: {
summoner_puuid: puuid
}
},
{ $unwind: "$allyTeam" },
{
$group: {
_id: "$allyTeam.name",
count: { $sum: 1 },
wins: {
$sum: {
$cond: [
{ $eq: ["$result", "Win"] }, 1, 0
]
}
},
losses: {
$sum: {
$cond: [
{ $eq: ["$result", "Fail"] }, 1, 0
]
}
}
},
},
{
$match: {
_id: { $not: { $eq: summonerName } },
'count': { $gte: 2 }
}
},
{ $sort: { 'count': -1 } },
{ $limit: 15 },
])
}
} }
module.exports = Match module.exports = Match

View file

@ -0,0 +1,189 @@
'use strict'
class MatchRepository {
static get inject() {
return ['App/Models/Match']
}
constructor(Match) {
this.Match = Match
}
/**
* Get Summoner's statistics for all played champion classes
* @param puuid of the summoner
*/
championClassStats(puuid) {
return this.Match.query().aggregate([
{
$match: {
summoner_puuid: puuid
}
},
{
$group: {
_id: { "$arrayElemAt": ["$champion.tags", 0] },
count: { $sum: 1 },
wins: {
$sum: {
$cond: [{ $eq: ["$result", "Win"] }, 1, 0]
}
},
losses: {
$sum: {
$cond: [{ $eq: ["$result", "Fail"] }, 1, 0]
}
}
}
}
])
}
/**
* Get Summoner's statistics for all played modes
* @param puuid of the summoner
*/
gamemodeStats(puuid) {
return this.Match.query().aggregate([
{
$match: {
summoner_puuid: puuid
}
},
{
$group: {
_id: "$gamemode",
count: { $sum: 1 },
wins: {
$sum: {
$cond: [{ $eq: ["$result", "Win"] }, 1, 0]
}
},
losses: {
$sum: {
$cond: [{ $eq: ["$result", "Fail"] }, 1, 0]
}
}
}
}
])
}
/**
* Get global Summoner's statistics
* @param puuid of the summoner
*/
globalStats(puuid) {
return this.Match.query().aggregate([
{
$match: {
summoner_puuid: puuid
}
},
{
$group: {
_id: null,
count: { $sum: 1 },
time: { $sum: "$time" },
wins: {
$sum: {
$cond: [{ $eq: ["$result", "Win"] }, 1, 0]
}
},
losses: {
$sum: {
$cond: [{ $eq: ["$result", "Fail"] }, 1, 0]
}
},
kills: { $sum: "$stats.kills" },
deaths: { $sum: "$stats.deaths" },
assists: { $sum: "$stats.assists" },
minions: { $sum: "$stats.minions" },
vision: { $sum: "$stats.vision" },
kp: { $avg: "$stats.kp" },
}
}
])
}
/**
* Get Summoner's statistics for the 5 differnt roles
* @param puuid of the summoner
*/
roleStats(puuid) {
return this.Match.query().aggregate([
{
$match: {
summoner_puuid: puuid,
role: { $not: { $eq: 'NONE' } }
}
},
{
$group: {
_id: "$role",
count: { $sum: 1 },
wins: {
$sum: {
$cond: [{ $eq: ["$result", "Win"] }, 1, 0]
}
},
losses: {
$sum: {
$cond: [{ $eq: ["$result", "Fail"] }, 1, 0]
}
}
},
},
{
$project: {
role: "$_id",
count: "$count",
wins: "$wins",
losses: "$losses",
}
}
])
}
/**
* Get Summoner's mates list
* @param puuid of the summoner
* @param summonerName of the summoner
*/
mates(puuid, summonerName) {
return this.Match.query().aggregate([
{
$match: {
summoner_puuid: puuid
}
},
{ $unwind: "$allyTeam" },
{
$group: {
_id: "$allyTeam.name",
count: { $sum: 1 },
wins: {
$sum: {
$cond: [{ $eq: ["$result", "Win"] }, 1, 0]
}
},
losses: {
$sum: {
$cond: [{ $eq: ["$result", "Fail"] }, 1, 0]
}
}
},
},
{
$match: {
_id: { $not: { $eq: summonerName } },
'count': { $gte: 2 }
}
},
{ $sort: { 'count': -1 } },
{ $limit: 15 },
])
}
}
module.exports = MatchRepository