Skip to content

Commit

Permalink
game time limit [clp195urc0001uj0kopn9j2sm]
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloendoh committed Nov 18, 2023
1 parent bb99524 commit b393b02
Show file tree
Hide file tree
Showing 6 changed files with 487 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/domains/lolrates/lolRates/LolRatesController.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
import { Get, JsonController } from "routing-controllers"
import {
CurrentUser,
Get,
JsonController,
QueryParam,
} from "routing-controllers"
import { User } from "../../../entities/User"
import LolRateRepository from "../../../repositories/lolrates/LolRateRepository"
import { LolRatesService } from "./LolRatesService"

@JsonController()
export class LolRatesController {
constructor(private lolRateRepo = LolRateRepository) {}
constructor(
private lolRateRepo = LolRateRepository,
private lolratesService = new LolRatesService()
) {}

@Get("/lolRates")
async getDocs() {
const allWinrates = await this.lolRateRepo.findWinrates()
const allWinrates = (await this.lolRateRepo.findWinrates()).map((item) => {
let count = 0
if (item.uggWin) count++
if (item.opggWin) count++
if (item.lolgraphsWin) count++

return {
...item,
avgWin: (item.uggWin + item.opggWin + item.lolgraphsWin) / count,
avgPick: (item.uggPick + item.uggPick + item.lolgraphsPick) / count,
}
})

const updatedAt = await this.lolRateRepo.getUpdatedAt()

return { rates: allWinrates, updatedAt: updatedAt[0] }
}

@Get("/playtime")
async getPlaytime(
@CurrentUser({ required: true }) user: User,
@QueryParam("offsetHours")
offsetHours: number,

@QueryParam("summonerName")
summonerName: string
) {
const data = await this.lolratesService.getPlaytime(
offsetHours,
summonerName
)

return {
playedMinutes: data.playtimeInMinutes,
playedHours: data.playtimeInHours,
}
}
}
87 changes: 87 additions & 0 deletions src/domains/lolrates/lolRates/LolRatesService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import actualAxios from "axios"
import { myEnvs } from "../../../utils/myEnvs"
import myRedis from "../../../utils/redis/myRedis"
import { redisKeys } from "../../../utils/redis/redisKeys"
import { LeagueMatchDetails } from "./types/LeagueMatchDetails"

export class LolRatesService {
constructor(private axios = actualAxios.create(), private redis = myRedis) {}

async getPlaytime(offsetHours: number, summonerName: string) {
const apiKey = myEnvs.RIOT_API_KEY

const data = await this.axios
.get<{ puuid: string }>(
`https://br1.api.riotgames.com/lol/summoner/v4/summoners/by-name/${summonerName}?api_key=${apiKey}`
)
.then((res) => res.data)

const lastMonday = new Date()
lastMonday.setDate(lastMonday.getDate() - lastMonday.getDay() + 1)
lastMonday.setHours(0, 0, 0, 0)
lastMonday.setHours(lastMonday.getHours() - offsetHours)

const epochLastMonday = lastMonday.getTime() / 1000

const matchesIds = await this.axios
.get<string[]>(
`https://americas.api.riotgames.com/lol/match/v5/matches/by-puuid/${data.puuid}/ids?startTime=${epochLastMonday}&count=100&api_key=${apiKey}`
)
.then((res) => res.data)

console.log(matchesIds)

const matches = await this.#getMatchesDetails(matchesIds)
console.log({ matches })

const totalPlaytimeInSeconds = matches.reduce(
(acc, match) => acc + match.gameDuration,
0
)

const playtimeInMinutes = totalPlaytimeInSeconds / 60
const playtimeInHours = playtimeInMinutes / 60

return {
playtimeInMinutes,
playtimeInHours,
}
}

async #getMatchesDetails(matchesIds: string[]) {
const matches: {
gameDuration: number
gameMode: string
}[] = []
for (const id of matchesIds) {
const cachedInfo = await this.redis.get(redisKeys.leagueMatch(id))

if (cachedInfo) {
matches.push(JSON.parse(cachedInfo))
continue
}

const matchDetails = await this.axios
.get<LeagueMatchDetails>(
`https://americas.api.riotgames.com/lol/match/v5/matches/${id}?api_key=${myEnvs.RIOT_API_KEY}`
)
.then((res) => res.data)

const relevantInfo = {
gameDuration: matchDetails.info.gameDuration,
gameMode: matchDetails.info.gameMode,
}

await this.redis.set(
redisKeys.leagueMatch(id),
JSON.stringify(relevantInfo),
"EX",
60 * 60 * 24 * 14
)

matches.push(relevantInfo)
}

return matches
}
}
Loading

0 comments on commit b393b02

Please sign in to comment.