overfast_api/lib/players/player.dart
2024-05-07 00:14:45 +02:00

315 lines
7.3 KiB
Dart

import 'package:overfast_api/client.dart';
import 'package:overfast_api/utils/types.dart';
class PlayerSummary {
final String username;
final String? avatar;
final String? namecard;
final String? title;
final PlayerEndorsement endorsement;
final PlatformCompetitiveRank? competitiveRank;
const PlayerSummary({
required this.username,
this.avatar,
this.namecard,
this.title,
required this.endorsement,
required this.competitiveRank,
});
factory PlayerSummary.fromJson(Map<String, dynamic> json) {
return PlayerSummary(
username: json['username'],
avatar: json['avatar'],
namecard: json['namecard'],
title: json['title'],
endorsement: PlayerEndorsement.fromJson(json['endorsement']),
competitiveRank: PlatformCompetitiveRank.fromJson(json['competitive']),
);
}
Map<String, dynamic> toJson() {
return {
'username': username,
'avatar': avatar,
'namecard': namecard,
'title': title,
'endorsement': endorsement.toJson(),
'competitiveRank': competitiveRank?.toJson(),
};
}
}
class PlayerEndorsement {
final int level;
final String frame;
const PlayerEndorsement({
required this.level,
required this.frame,
});
factory PlayerEndorsement.fromJson(Map<String, dynamic> json) {
return PlayerEndorsement(
level: json['level'],
frame: json['frame'],
);
}
Map<String, dynamic> toJson() {
return {
'level': level,
'frame': frame,
};
}
}
class PlatformCompetitiveRank {
final CompetitiveRank? pc;
final CompetitiveRank? console;
const PlatformCompetitiveRank({
this.pc,
this.console,
});
factory PlatformCompetitiveRank.fromJson(Map<String, dynamic> json) {
return PlatformCompetitiveRank(
pc: json['pc'] != null ? CompetitiveRank.fromJson(json['pc']) : null,
console: json['console'] != null
? CompetitiveRank.fromJson(json['console'])
: null,
);
}
Map<String, dynamic> toJson() {
return {
'pc': pc?.toJson(),
'console': console?.toJson(),
};
}
}
class CompetitiveRank {
final int season;
final RoleDetails? tank;
final RoleDetails? damage;
final RoleDetails? support;
final RoleDetails? openQueue;
const CompetitiveRank({
required this.season,
required this.tank,
required this.damage,
required this.support,
required this.openQueue,
});
factory CompetitiveRank.fromJson(Map<String, dynamic> json) {
return CompetitiveRank(
season: json['season'],
tank: json['tank'] == null ? null : RoleDetails.fromJson(json['tank']),
damage:
json['damage'] == null ? null : RoleDetails.fromJson(json['damage']),
support: json['support'] == null
? null
: RoleDetails.fromJson(json['support']),
openQueue:
json['open'] == null ? null : RoleDetails.fromJson(json['open']),
);
}
Map<String, dynamic> toJson() {
return {
'season': season,
'tank': tank?.toJson(),
'damage': damage?.toJson(),
'support': support?.toJson(),
};
}
}
class RoleDetails {
final String division;
final int tier;
final String roleIcon;
final String rankIcon;
final String tierIcon;
const RoleDetails({
required this.division,
required this.tier,
required this.roleIcon,
required this.rankIcon,
required this.tierIcon,
});
factory RoleDetails.fromJson(Map<String, dynamic> json) {
return RoleDetails(
division: json['division'],
tier: json['tier'],
roleIcon: json['role_icon'],
rankIcon: json['rank_icon'],
tierIcon: json['tier_icon'],
);
}
Map<String, dynamic> toJson() {
return {
'division': division,
'tier': tier,
'roleIcon': roleIcon,
'rankIcon': rankIcon,
'tierIcon': tierIcon,
};
}
}
class StatsSummary {
final StatsRecap general;
final Map<String, StatsRecap> heroes;
final Map<String, StatsRecap> roles;
const StatsSummary({
required this.general,
required this.heroes,
required this.roles,
});
factory StatsSummary.fromJson(Map<String, dynamic> json) {
return StatsSummary(
general: StatsRecap.fromJson(json['general']),
heroes: Map.from(json['heroes']).map(
(key, value) => MapEntry(key, StatsRecap.fromJson(value)),
),
roles: Map.from(json['roles']).map(
(key, value) => MapEntry(key, StatsRecap.fromJson(value)),
),
);
}
Map<String, dynamic> toJson() {
return {
'general': general.toJson(),
'heroes': heroes.map((key, value) => MapEntry(key, value.toJson())),
'roles': roles.map((key, value) => MapEntry(key, value.toJson())),
};
}
}
class StatsRecap {
final int gamesPlayed;
final int gamesWon;
final int gamesLost;
final Duration timePlayed;
final double winrate;
final double kda;
final SubStatsRecap total;
final SubStatsRecap average;
const StatsRecap({
required this.gamesPlayed,
required this.gamesWon,
required this.gamesLost,
required this.timePlayed,
required this.winrate,
required this.kda,
required this.total,
required this.average,
});
factory StatsRecap.fromJson(Map<String, dynamic> json) {
return StatsRecap(
gamesPlayed: json['games_played'],
gamesWon: json['games_won'],
gamesLost: json['games_lost'],
timePlayed: Duration(seconds: json['time_played']),
winrate: json['winrate'],
kda: json['kda'],
total: SubStatsRecap.fromJson(json['total']),
average: SubStatsRecap.fromJson(json['average']),
);
}
Map<String, dynamic> toJson() {
return {
'games_played': gamesPlayed,
'games_won': gamesWon,
'games_lost': gamesLost,
'time_played': timePlayed.inSeconds,
'winrate': winrate,
'kda': kda,
'total': total.toJson(),
'average': average.toJson(),
};
}
}
class SubStatsRecap {
final num eliminations;
final num assists;
final num deaths;
final num damage;
final num healing;
const SubStatsRecap({
required this.eliminations,
required this.assists,
required this.deaths,
required this.damage,
required this.healing,
});
factory SubStatsRecap.fromJson(Map<String, dynamic> json) {
return SubStatsRecap(
eliminations: json['eliminations'],
assists: json['assists'],
deaths: json['deaths'],
damage: json['damage'],
healing: json['healing'],
);
}
Map<String, dynamic> toJson() {
return {
'eliminations': eliminations,
'assists': assists,
'deaths': deaths,
'damage': damage,
'healing': healing,
};
}
}
class Player {
final String playerId;
final Overfast _client;
const Player(this._client, {required this.playerId});
// I.. don't know how to properly type this.
Future<Map> career({
Platform? platform,
Gamemode gamemode = Gamemode.competitive,
HeroKey? hero = HeroKey.allHeroes,
}) {
return _client.get<Map>(
'/players/$playerId/stats/career',
{
if (platform != null) 'platform': platform.toString(),
'gamemode': gamemode.name,
if (hero != null) 'hero': hero.toString(),
},
);
}
Future<PlayerSummary> summary() =>
_client.get<PlayerSummary>('/players/$playerId/summary');
Future<StatsSummary> statsSummary() =>
_client.get<StatsSummary>('/players/$playerId/stats/summary');
}