diff --git a/src/dto/getDataByName.dto.ts b/src/dto/getDataByName.dto.ts new file mode 100644 index 0000000..2ef128e --- /dev/null +++ b/src/dto/getDataByName.dto.ts @@ -0,0 +1,6 @@ +import { IsDefined } from "class-validator"; + +export class GetDataByNameDTO { + @IsDefined() + name: string; +} diff --git a/src/libs/BHAPI.ts b/src/libs/BHAPI.ts index 88def7f..c020184 100644 --- a/src/libs/BHAPI.ts +++ b/src/libs/BHAPI.ts @@ -1,6 +1,7 @@ import { BadRequestException, Injectable, + NotFoundException, RequestTimeoutException, } from "@nestjs/common"; import { @@ -129,6 +130,15 @@ export class BHAPIService { return elo; } + public async getBHIDFromName(name: string): Promise { + const playerArray = (await this.makeAPIRequest("/rankings/1v1/all/1", { + name, + })) as IPlayerStats[]; + + if (playerArray[0]) return playerArray[0].brawlhalla_id; + else throw new NotFoundException("Player not found"); + } + public async getSteamDataByURL(profileUrl: string): Promise { const test = /(steamcommunity\.com\/(id|profiles)\/([^\s]+))/i; const match = test.exec(profileUrl); diff --git a/src/routers/glory/glory.controller.ts b/src/routers/glory/glory.controller.ts index bda444b..61fe91a 100644 --- a/src/routers/glory/glory.controller.ts +++ b/src/routers/glory/glory.controller.ts @@ -3,6 +3,7 @@ import { APIRes } from "api-types"; import { RateLimit } from "nestjs-rate-limit"; import CONFIG from "src/config"; import { GetDataByBHIDDTO } from "src/dto/getDataByBHID.dto"; +import { GetDataByNameDTO } from "src/dto/getDataByName.dto"; import { GetDataBySteamIDDTO } from "src/dto/getDataBySteamID.dto"; import { GetDataBySteamURLDTO } from "src/dto/getDataBySteamURL.dto"; import { GloryEntity } from "./glory.entity"; @@ -44,4 +45,11 @@ export class GloryController { ): Promise> { return this.gloryService.getGloryBySteamURL(getDataBySteamURLDTO); } + + @Get("name") + public async getGloryByName( + @Query() getDataByNameDTO: GetDataByNameDTO, + ): Promise> { + return this.gloryService.getGloryByName(getDataByNameDTO); + } } diff --git a/src/routers/glory/glory.module.ts b/src/routers/glory/glory.module.ts index 0872835..79bd1a6 100644 --- a/src/routers/glory/glory.module.ts +++ b/src/routers/glory/glory.module.ts @@ -3,12 +3,15 @@ import { TypeOrmModule } from "@nestjs/typeorm"; import { BHAPIService } from "src/libs/BHAPI"; import { SteamDataEntity } from "src/routers/steamdata/steamdata.entity"; import { SteamDataService } from "src/routers/steamdata/steamdata.service"; +import { BHIDEntity } from "../utils/bhid.entity"; import { GloryController } from "./glory.controller"; import { GloryEntity } from "./glory.entity"; import { GloryService } from "./glory.service"; @Module({ - imports: [TypeOrmModule.forFeature([GloryEntity, SteamDataEntity])], + imports: [ + TypeOrmModule.forFeature([GloryEntity, SteamDataEntity, BHIDEntity]), + ], controllers: [GloryController], providers: [GloryService, BHAPIService, SteamDataService], exports: [TypeOrmModule, GloryService], diff --git a/src/routers/glory/glory.service.ts b/src/routers/glory/glory.service.ts index c6fb7f1..88a2428 100644 --- a/src/routers/glory/glory.service.ts +++ b/src/routers/glory/glory.service.ts @@ -3,11 +3,13 @@ import { InjectRepository } from "@nestjs/typeorm"; import { APIRes } from "api-types"; import CONFIG from "src/config"; import { GetDataByBHIDDTO } from "src/dto/getDataByBHID.dto"; +import { GetDataByNameDTO } from "src/dto/getDataByName.dto"; import { GetDataBySteamIDDTO } from "src/dto/getDataBySteamID.dto"; import { GetDataBySteamURLDTO } from "src/dto/getDataBySteamURL.dto"; import { BHAPIService } from "src/libs/BHAPI"; import { SteamDataService } from "src/routers/steamdata/steamdata.service"; import { MongoRepository } from "typeorm"; +import { BHIDEntity } from "../utils/bhid.entity"; import { GloryEntity } from "./glory.entity"; @Injectable() @@ -15,6 +17,8 @@ export class GloryService { constructor( @InjectRepository(GloryEntity) private readonly gloryRepository: MongoRepository, + @InjectRepository(BHIDEntity) + private readonly bhidRepository: MongoRepository, private readonly bhAPIService: BHAPIService, private readonly steamDataService: SteamDataService, ) {} @@ -92,6 +96,47 @@ export class GloryService { } } + public async getIDByName(name: string): Promise { + const bhidData = await this.bhidRepository.findOne({ + where: { + name, + }, + }); + + if (!bhidData) { + const bhid = await this.bhAPIService.getBHIDFromName(name); + const data = new BHIDEntity({ bhid, name, lastSynced: Date.now() }); + + const repository = this.bhidRepository.create(data); + await this.bhidRepository.save(repository); + + return bhid; + } else { + if (Date.now() - bhidData.lastSynced > CONFIG.SYNC_PERIOD) { + const bhid = await this.bhAPIService.getBHIDFromName(name); + const data = new BHIDEntity({ + bhid, + name, + lastSynced: Date.now(), + }); + + await this.bhidRepository.updateOne({ name }, { $set: data }); + + return bhid; + } else { + return bhidData.bhid; + } + } + } + + public async getGloryByName({ + name, + }: GetDataByNameDTO): Promise> { + const brawlhalla_id = await this.getIDByName(name); + + return this.getGloryByID({ brawlhalla_id }); + } + public async getGloryBySteamID({ steam_id, }: GetDataBySteamIDDTO): Promise> { diff --git a/src/routers/ranked/ranked.controller.ts b/src/routers/ranked/ranked.controller.ts index e17cad5..81fa6d1 100644 --- a/src/routers/ranked/ranked.controller.ts +++ b/src/routers/ranked/ranked.controller.ts @@ -3,6 +3,7 @@ import { APIRes } from "api-types"; import { RateLimit } from "nestjs-rate-limit"; import CONFIG from "src/config"; import { GetDataByBHIDDTO } from "src/dto/getDataByBHID.dto"; +import { GetDataByNameDTO } from "src/dto/getDataByName.dto"; import { GetDataBySteamIDDTO } from "src/dto/getDataBySteamID.dto"; import { GetDataBySteamURLDTO } from "src/dto/getDataBySteamURL.dto"; import { RankedEntity } from "./ranked.entity"; @@ -45,4 +46,11 @@ export class RankedController { ): Promise> { return this.rankedService.getRankedBySteamURL(getDataBySteamURLDTO); } + + @Get("name") + public async getRankedByName( + @Query() getDataByNameDTO: GetDataByNameDTO, + ): Promise> { + return this.rankedService.getRankedByName(getDataByNameDTO); + } } diff --git a/src/routers/ranked/ranked.module.ts b/src/routers/ranked/ranked.module.ts index ec8f67b..4abae86 100644 --- a/src/routers/ranked/ranked.module.ts +++ b/src/routers/ranked/ranked.module.ts @@ -3,12 +3,16 @@ import { TypeOrmModule } from "@nestjs/typeorm"; import { BHAPIService } from "src/libs/BHAPI"; import { SteamDataEntity } from "src/routers/steamdata/steamdata.entity"; import { SteamDataService } from "src/routers/steamdata/steamdata.service"; +import { GloryModule } from "../glory/glory.module"; import { RankedController } from "./ranked.controller"; import { RankedEntity } from "./ranked.entity"; import { RankedService } from "./ranked.service"; @Module({ - imports: [TypeOrmModule.forFeature([RankedEntity, SteamDataEntity])], + imports: [ + TypeOrmModule.forFeature([RankedEntity, SteamDataEntity]), + GloryModule, + ], controllers: [RankedController], providers: [RankedService, BHAPIService, SteamDataService], exports: [TypeOrmModule, RankedService], diff --git a/src/routers/ranked/ranked.service.ts b/src/routers/ranked/ranked.service.ts index b0da79e..04fc233 100644 --- a/src/routers/ranked/ranked.service.ts +++ b/src/routers/ranked/ranked.service.ts @@ -3,11 +3,13 @@ import { InjectRepository } from "@nestjs/typeorm"; import { APIRes } from "api-types"; import CONFIG from "src/config"; import { GetDataByBHIDDTO } from "src/dto/getDataByBHID.dto"; +import { GetDataByNameDTO } from "src/dto/getDataByName.dto"; import { GetDataBySteamIDDTO } from "src/dto/getDataBySteamID.dto"; import { GetDataBySteamURLDTO } from "src/dto/getDataBySteamURL.dto"; import { BHAPIService } from "src/libs/BHAPI"; import { SteamDataService } from "src/routers/steamdata/steamdata.service"; import { MongoRepository } from "typeorm"; +import { GloryService } from "../glory/glory.service"; import { RankedEntity } from "./ranked.entity"; @Injectable() @@ -17,6 +19,7 @@ export class RankedService { private readonly rankedRepository: MongoRepository, private readonly bhAPIService: BHAPIService, private readonly steamDataService: SteamDataService, + private readonly gloryService: GloryService, ) {} public returnPing(): APIRes { @@ -95,6 +98,14 @@ export class RankedService { } } + public async getRankedByName({ + name, + }: GetDataByNameDTO): Promise> { + const brawlhalla_id = await this.gloryService.getIDByName(name); + + return this.getRankedByID({ brawlhalla_id }); + } + public async getRankedBySteamID({ steam_id, }: GetDataBySteamIDDTO): Promise> { diff --git a/src/routers/stats/stats.controller.ts b/src/routers/stats/stats.controller.ts index d1fcf08..53bb883 100644 --- a/src/routers/stats/stats.controller.ts +++ b/src/routers/stats/stats.controller.ts @@ -3,6 +3,7 @@ import { APIRes } from "api-types"; import { RateLimit } from "nestjs-rate-limit"; import CONFIG from "src/config"; import { GetDataByBHIDDTO } from "src/dto/getDataByBHID.dto"; +import { GetDataByNameDTO } from "src/dto/getDataByName.dto"; import { GetDataBySteamIDDTO } from "src/dto/getDataBySteamID.dto"; import { GetDataBySteamURLDTO } from "src/dto/getDataBySteamURL.dto"; import { StatsEntity } from "./stats.entity"; @@ -45,4 +46,11 @@ export class StatsController { ): Promise> { return this.statsService.getStatsBySteamURL(getDataBySteamURLDTO); } + + @Get("name") + public async getStatsByName( + @Query() getDataByNameDTO: GetDataByNameDTO, + ): Promise> { + return this.statsService.getStatsByName(getDataByNameDTO); + } } diff --git a/src/routers/stats/stats.module.ts b/src/routers/stats/stats.module.ts index 96f1f33..dda54d3 100644 --- a/src/routers/stats/stats.module.ts +++ b/src/routers/stats/stats.module.ts @@ -3,12 +3,16 @@ import { TypeOrmModule } from "@nestjs/typeorm"; import { BHAPIService } from "src/libs/BHAPI"; import { SteamDataEntity } from "src/routers/steamdata/steamdata.entity"; import { SteamDataService } from "src/routers/steamdata/steamdata.service"; +import { GloryModule } from "../glory/glory.module"; import { StatsController } from "./stats.controller"; import { StatsEntity } from "./stats.entity"; import { StatsService } from "./stats.service"; @Module({ - imports: [TypeOrmModule.forFeature([StatsEntity, SteamDataEntity])], + imports: [ + TypeOrmModule.forFeature([StatsEntity, SteamDataEntity]), + GloryModule, + ], controllers: [StatsController], providers: [StatsService, BHAPIService, SteamDataService], exports: [TypeOrmModule, StatsService], diff --git a/src/routers/stats/stats.service.ts b/src/routers/stats/stats.service.ts index f665260..dbd254f 100644 --- a/src/routers/stats/stats.service.ts +++ b/src/routers/stats/stats.service.ts @@ -3,11 +3,13 @@ import { InjectRepository } from "@nestjs/typeorm"; import { APIRes } from "api-types"; import CONFIG from "src/config"; import { GetDataByBHIDDTO } from "src/dto/getDataByBHID.dto"; +import { GetDataByNameDTO } from "src/dto/getDataByName.dto"; import { GetDataBySteamIDDTO } from "src/dto/getDataBySteamID.dto"; import { GetDataBySteamURLDTO } from "src/dto/getDataBySteamURL.dto"; import { BHAPIService } from "src/libs/BHAPI"; import { SteamDataService } from "src/routers/steamdata/steamdata.service"; import { MongoRepository } from "typeorm"; +import { GloryService } from "../glory/glory.service"; import { StatsEntity } from "./stats.entity"; @Injectable() @@ -17,6 +19,7 @@ export class StatsService { private readonly statsRepository: MongoRepository, private readonly bhAPIService: BHAPIService, private readonly steamDataService: SteamDataService, + private readonly gloryService: GloryService, ) {} public returnPing(): APIRes { @@ -92,6 +95,14 @@ export class StatsService { } } + public async getStatsByName({ + name, + }: GetDataByNameDTO): Promise> { + const brawlhalla_id = await this.gloryService.getIDByName(name); + + return this.getStatsByID({ brawlhalla_id }); + } + public async getStatsBySteamID({ steam_id, }: GetDataBySteamIDDTO): Promise> { diff --git a/src/routers/utils/bhid.entity.ts b/src/routers/utils/bhid.entity.ts new file mode 100644 index 0000000..8afeeea --- /dev/null +++ b/src/routers/utils/bhid.entity.ts @@ -0,0 +1,20 @@ +import { Column, Entity, ObjectId, ObjectIdColumn } from "typeorm"; + +@Entity({ name: "BHIDEntity" }) +export class BHIDEntity { + @ObjectIdColumn() + _id: ObjectId; + + @Column({ nullable: false }) + bhid: number; + + @Column({ nullable: false }) + name: string; + + @Column({ nullable: false }) + lastSynced: number; + + constructor(partial: Partial) { + Object.assign(this, partial); + } +}