-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
game time limit [clp195urc0001uj0kopn9j2sm]
- Loading branch information
1 parent
bb99524
commit b393b02
Showing
6 changed files
with
487 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.