Skip to content

Commit

Permalink
✨ Implement getting data by name again (deploy)
Browse files Browse the repository at this point in the history
  • Loading branch information
barbarbar338 committed Nov 27, 2023
1 parent 2634b9b commit 242a3ed
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/dto/getDataByName.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IsDefined } from "class-validator";

export class GetDataByNameDTO {
@IsDefined()
name: string;
}
10 changes: 10 additions & 0 deletions src/libs/BHAPI.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
BadRequestException,
Injectable,
NotFoundException,
RequestTimeoutException,
} from "@nestjs/common";
import {
Expand Down Expand Up @@ -129,6 +130,15 @@ export class BHAPIService {
return elo;
}

public async getBHIDFromName(name: string): Promise<number> {
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<ISteamData> {
const test = /(steamcommunity\.com\/(id|profiles)\/([^\s]+))/i;
const match = test.exec(profileUrl);
Expand Down
8 changes: 8 additions & 0 deletions src/routers/glory/glory.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -44,4 +45,11 @@ export class GloryController {
): Promise<APIRes<GloryEntity>> {
return this.gloryService.getGloryBySteamURL(getDataBySteamURLDTO);
}

@Get("name")
public async getGloryByName(
@Query() getDataByNameDTO: GetDataByNameDTO,
): Promise<APIRes<GloryEntity>> {
return this.gloryService.getGloryByName(getDataByNameDTO);
}
}
5 changes: 4 additions & 1 deletion src/routers/glory/glory.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
45 changes: 45 additions & 0 deletions src/routers/glory/glory.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ 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()
export class GloryService {
constructor(
@InjectRepository(GloryEntity)
private readonly gloryRepository: MongoRepository<GloryEntity>,
@InjectRepository(BHIDEntity)
private readonly bhidRepository: MongoRepository<BHIDEntity>,
private readonly bhAPIService: BHAPIService,
private readonly steamDataService: SteamDataService,
) {}
Expand Down Expand Up @@ -92,6 +96,47 @@ export class GloryService {
}
}

public async getIDByName(name: string): Promise<number> {
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<APIRes<GloryEntity>> {
const brawlhalla_id = await this.getIDByName(name);

return this.getGloryByID({ brawlhalla_id });
}

public async getGloryBySteamID({
steam_id,
}: GetDataBySteamIDDTO): Promise<APIRes<GloryEntity>> {
Expand Down
8 changes: 8 additions & 0 deletions src/routers/ranked/ranked.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -45,4 +46,11 @@ export class RankedController {
): Promise<APIRes<RankedEntity>> {
return this.rankedService.getRankedBySteamURL(getDataBySteamURLDTO);
}

@Get("name")
public async getRankedByName(
@Query() getDataByNameDTO: GetDataByNameDTO,
): Promise<APIRes<RankedEntity>> {
return this.rankedService.getRankedByName(getDataByNameDTO);
}
}
6 changes: 5 additions & 1 deletion src/routers/ranked/ranked.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
11 changes: 11 additions & 0 deletions src/routers/ranked/ranked.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -17,6 +19,7 @@ export class RankedService {
private readonly rankedRepository: MongoRepository<RankedEntity>,
private readonly bhAPIService: BHAPIService,
private readonly steamDataService: SteamDataService,
private readonly gloryService: GloryService,
) {}

public returnPing(): APIRes<null> {
Expand Down Expand Up @@ -95,6 +98,14 @@ export class RankedService {
}
}

public async getRankedByName({
name,
}: GetDataByNameDTO): Promise<APIRes<RankedEntity>> {
const brawlhalla_id = await this.gloryService.getIDByName(name);

return this.getRankedByID({ brawlhalla_id });
}

public async getRankedBySteamID({
steam_id,
}: GetDataBySteamIDDTO): Promise<APIRes<RankedEntity>> {
Expand Down
8 changes: 8 additions & 0 deletions src/routers/stats/stats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -45,4 +46,11 @@ export class StatsController {
): Promise<APIRes<StatsEntity>> {
return this.statsService.getStatsBySteamURL(getDataBySteamURLDTO);
}

@Get("name")
public async getStatsByName(
@Query() getDataByNameDTO: GetDataByNameDTO,
): Promise<APIRes<StatsEntity>> {
return this.statsService.getStatsByName(getDataByNameDTO);
}
}
6 changes: 5 additions & 1 deletion src/routers/stats/stats.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
11 changes: 11 additions & 0 deletions src/routers/stats/stats.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -17,6 +19,7 @@ export class StatsService {
private readonly statsRepository: MongoRepository<StatsEntity>,
private readonly bhAPIService: BHAPIService,
private readonly steamDataService: SteamDataService,
private readonly gloryService: GloryService,
) {}

public returnPing(): APIRes<null> {
Expand Down Expand Up @@ -92,6 +95,14 @@ export class StatsService {
}
}

public async getStatsByName({
name,
}: GetDataByNameDTO): Promise<APIRes<StatsEntity>> {
const brawlhalla_id = await this.gloryService.getIDByName(name);

return this.getStatsByID({ brawlhalla_id });
}

public async getStatsBySteamID({
steam_id,
}: GetDataBySteamIDDTO): Promise<APIRes<StatsEntity>> {
Expand Down
20 changes: 20 additions & 0 deletions src/routers/utils/bhid.entity.ts
Original file line number Diff line number Diff line change
@@ -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<BHIDEntity>) {
Object.assign(this, partial);
}
}

0 comments on commit 242a3ed

Please sign in to comment.