Skip to content

Commit

Permalink
Add interfaces for Faceit API responses (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
niekcandaele authored Aug 8, 2020
1 parent 1eb283a commit 1217ee7
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 15 deletions.
79 changes: 79 additions & 0 deletions src/faceit/apiResponses/faceitSearchSteam.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
export interface IInfractions {
last_infraction_date: string;
afk: number;
leaver: number;
qm_not_checkedin: number;
qm_not_voted: number;
}

export interface IPlatforms {
steam: string;
}

export interface IEU {
selected_ladder_id: string;
}

export interface IRegions {
EU: IEU;
}

export interface ICsgo {
game_profile_id: string;
region: string;
regions: IRegions;
skill_level_label: string;
game_player_id: string;
skill_level: number;
faceit_elo: number;
game_player_name: string;
}

export interface IEU2 {
selected_ladder_id: string;
}

export interface IRegions2 {
EU: IEU2;
}

export interface ICsco {
game_profile_id: string;
region: string;
regions: IRegions2;
skill_level_label: string;
game_player_id: string;
skill_level: number;
faceit_elo: number;
game_player_name: string;
}

export interface IGames {
csgo: ICsgo;
csco: ICsco;
}

export interface ISettings {
language: string;
}

export interface IFaceitSearchForSteamId {
player_id: string;
nickname: string;
avatar: string;
country: string;
cover_image: string;
cover_featured_image: string;
infractions: IInfractions;
platforms: IPlatforms;
games: IGames;
settings: ISettings;
friends_ids: string[];
bans: any[];
new_steam_id: string;
steam_id_64: string;
steam_nickname: string;
membership_type: string;
memberships: string[];
faceit_url: string;
}
98 changes: 98 additions & 0 deletions src/faceit/apiResponses/matchDetails.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
export interface IRoster {
player_id: string;
nickname: string;
avatar: string;
membership: string;
game_player_id: string;
game_player_name: string;
game_skill_level: number;
anticheat_required: boolean;
}

export interface IFaction2 {
faction_id: string;
leader: string;
avatar: string;
roster: IRoster[];
substituted: boolean;
name: string;
type: string;
}

export interface IRoster2 {
player_id: string;
nickname: string;
avatar: string;
membership: string;
game_player_id: string;
game_player_name: string;
game_skill_level: number;
anticheat_required: boolean;
}

export interface IFaction1 {
faction_id: string;
leader: string;
avatar: string;
roster: IRoster2[];
substituted: boolean;
name: string;
type: string;
}

export interface ITeams {
faction2: IFaction2;
faction1: IFaction1;
}

export interface IEntity {
name: string;
class_name: string;
game_map_id: string;
guid: string;
image_lg: string;
image_sm: string;
}

export interface IMap {
entities: IEntity[];
pick: string[];
}

export interface IVoting {
voted_entity_types: string[];
map: IMap;
}

export interface IScore {
faction1: number;
faction2: number;
}

export interface IResults {
winner: string;
score: IScore;
}

export interface IMatchDetails {
match_id: string;
version: number;
game: string;
region: string;
competition_id: string;
competition_type: string;
competition_name: string;
organizer_id: string;
teams: ITeams;
voting: IVoting;
calculate_elo: boolean;
configured_at: number;
started_at: number;
finished_at: number;
demo_url: string[];
chat_room_id: string;
best_of: number;
results: IResults;
status: string;
faceit_url: string;
}
79 changes: 79 additions & 0 deletions src/faceit/apiResponses/playerHistory.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
export interface IPlayer {
player_id: string;
nickname: string;
avatar: string;
skill_level: number;
game_player_id: string;
game_player_name: string;
faceit_url: string;
}

export interface IFaction1 {
team_id: string;
nickname: string;
avatar: string;
type: string;
players: IPlayer[];
}

export interface IPlayer2 {
player_id: string;
nickname: string;
avatar: string;
skill_level: number;
game_player_id: string;
game_player_name: string;
faceit_url: string;
}

export interface IFaction2 {
team_id: string;
nickname: string;
avatar: string;
type: string;
players: IPlayer2[];
}

export interface ITeams {
faction1: IFaction1;
faction2: IFaction2;
}

export interface IScore {
faction1: number;
faction2: number;
}

export interface IResults {
winner: string;
score: IScore;
}

export interface IItem {
match_id: string;
game_id: string;
region: string;
match_type: string;
game_mode: string;
max_players: number;
teams_size: number;
teams: ITeams;
playing_players: string[];
competition_id: string;
competition_name: string;
competition_type: string;
organizer_id: string;
status: string;
started_at: number;
finished_at: number;
results: IResults;
faceit_url: string;
}

export interface IPlayerHistory {
items: IItem[];
start: number;
end: number;
from: number;
to: number;
}
37 changes: 22 additions & 15 deletions src/faceit/faceit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import { CsgoMatchDto } from '../match/dto/csgoMatch.dto';
import { MatchService } from '../match/match.service';
import { QueueService } from '../queue/queue.service';
import { LastCheckedType, UserRepository } from '../user/user.repository';
import { IFaceitSearchForSteamId } from './apiResponses/faceitSearchSteam.interface';
import { IHubMatches } from './apiResponses/hubMatches.interface';

// TODO create interfaces for response data from Faceit API (Perhaps these API calls belong in @bantr/lib ...)
import { IMatchDetails } from './apiResponses/matchDetails.interface';
import { IPlayerHistory } from './apiResponses/playerHistory.interface';

/**
* Service for Faceit
Expand Down Expand Up @@ -75,9 +76,9 @@ export class FaceitService {
const history = await this.getPlayerHistory(user.faceitId);

this.logger.debug(
`Found ${history.data.items.length} matches for user "${user.username}" with FaceIt ID "${user.faceitId}"`
`Found ${history.items.length} matches for user "${user.username}" with FaceIt ID "${user.faceitId}"`
);
for (const match of history.data.items) {
for (const match of history.items) {
if (match.status !== 'finished') {
// Match has not finished yet, don't parse data
continue;
Expand Down Expand Up @@ -156,7 +157,7 @@ export class FaceitService {
private async updateUserFaceit(user: User) {
try {
const response = await this.getFaceitProfileForSteamId(user.steamId);
const { player_id: faceitId, nickname } = response.data;
const { player_id: faceitId, nickname } = response;
user.faceitId = faceitId;
user.faceitName = nickname;
return await this.userRepository.saveUser(user);
Expand All @@ -170,8 +171,13 @@ export class FaceitService {
* Tries to find a Faceit profile based on steam ID
* @param steamId STEAM64 ID
*/
private async getFaceitProfileForSteamId(steamId: string): Promise<any> {
return await this.doRequest(`/players?game=csgo&game_player_id=${steamId}`);
private async getFaceitProfileForSteamId(
steamId: string
): Promise<IFaceitSearchForSteamId> {
const response = await this.doRequest(
`/players?game=csgo&game_player_id=${steamId}`
);
return response.data;
}

private async transformAPIResponseToMatch(
Expand Down Expand Up @@ -209,8 +215,9 @@ export class FaceitService {
* Get the matches for a player in the last month
* @param id
*/
private async getPlayerHistory(id: string) {
return await this.doRequest(`/players/${id}/history`);
private async getPlayerHistory(id: string): Promise<IPlayerHistory> {
const response = await this.doRequest(`/players/${id}/history`);
return response.data;
}

private determineFaceitMatchType(faceitMatch) {
Expand Down Expand Up @@ -262,7 +269,6 @@ export class FaceitService {
return 'https://open.faceit.com/data/v4';
}

// TODO: Create a proper interface for this API response
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private async doRequest(endpoint: string): Promise<any> {
try {
Expand All @@ -284,8 +290,9 @@ export class FaceitService {
}
}

private async getMatchDetails(matchId: string) {
return await this.doRequest(`/matches/${matchId}`);
private async getMatchDetails(matchId: string): Promise<IMatchDetails> {
const response = await this.doRequest(`/matches/${matchId}`);
return response.data;
}

private async getHubMatches(hubId: string, limit = 50): Promise<IHubMatches> {
Expand All @@ -297,9 +304,9 @@ export class FaceitService {

private async getDemo(matchId: string): Promise<string> | null {
const matchDetails = await this.getMatchDetails(matchId);
if (matchDetails.data) {
if (matchDetails.data.demo_url) {
return matchDetails.data.demo_url[0];
if (matchDetails) {
if (matchDetails.demo_url) {
return matchDetails.demo_url[0];
}
}
return null;
Expand Down

0 comments on commit 1217ee7

Please sign in to comment.