sprcl
Version:
Simple API client for Clash of Clans, Clash Royale, and Brawl Stars
204 lines (193 loc) • 6.02 kB
JavaScript
class Badge {
constructor(name, level, maxLevel, progress, target, pic) {
this.name = name;
this.level = level;
this.maxLevel = maxLevel;
this.progress = progress;
this.target = target;
this.pic = pic;
}
static fromJSON(jsonObject) {
return new Badge(
jsonObject.name,
jsonObject.level,
jsonObject.maxLevel,
jsonObject.progress,
jsonObject.target,
jsonObject.iconUrls?.large || ""
);
}
toString() {
return this.name;
}
}
class Achievement {
constructor(name, stars, value, target, info) {
this.name = name;
this.stars = stars;
this.value = value;
this.target = target;
this.info = info;
}
static fromJSON(jsonObject) {
return new Achievement(
jsonObject.name,
jsonObject.stars,
jsonObject.value,
jsonObject.target,
jsonObject.info
);
}
toString() {
return this.name;
}
}
class Card {
constructor(name, id, level, maxLevel, rarity, count, elixirCost, iconUrl) {
this.name = name;
this.id = id;
this.level = level;
this.maxLevel = maxLevel;
this.rarity = rarity;
this.count = count;
this.elixirCost = elixirCost;
this.iconUrl = iconUrl;
}
static fromJSON(jsonObject) {
return new Card(
jsonObject.name,
jsonObject.id,
jsonObject.level,
jsonObject.maxLevel,
jsonObject.rarity,
jsonObject.count,
jsonObject.elixirCost,
jsonObject.iconUrls?.medium || ""
);
}
toString() {
return this.name;
}
}
class Player {
constructor(
tag,
name,
expLevel,
trophies,
bestTrophies,
wins,
losses,
battleCount,
threeCrownWins,
challengeCardsWon,
challengeMaxWins,
tournamentCardsWon,
tournamentBattleCount,
role,
donations,
donationsReceived,
totalDonations,
warDayWins,
clanCardsCollected,
clan,
arena,
leagueStatistics,
badges,
achievements,
cards,
currentDeck,
currentFavouriteCard,
starPoints,
expPoints,
totalExpPoints,
legacyTrophyRoadHighScore,
currentPathOfLegendSeasonResult,
lastPathOfLegendSeasonResult,
bestPathOfLegendSeasonResult
) {
this.tag = tag;
this.name = name;
this.expLevel = expLevel;
this.trophies = trophies;
this.bestTrophies = bestTrophies;
this.wins = wins;
this.losses = losses;
this.battleCount = battleCount;
this.threeCrownWins = threeCrownWins;
this.challengeCardsWon = challengeCardsWon;
this.challengeMaxWins = challengeMaxWins;
this.tournamentCardsWon = tournamentCardsWon;
this.tournamentBattleCount = tournamentBattleCount;
this.role = role;
this.donations = donations;
this.donationsReceived = donationsReceived;
this.totalDonations = totalDonations;
this.warDayWins = warDayWins;
this.clanCardsCollected = clanCardsCollected;
this.clan = clan;
this.arena = arena;
this.leagueStatistics = leagueStatistics;
this.badges = badges;
this.achievements = achievements;
this.cards = cards;
this.currentDeck = currentDeck;
this.currentFavouriteCard = currentFavouriteCard;
this.starPoints = starPoints;
this.expPoints = expPoints;
this.totalExpPoints = totalExpPoints;
this.legacyTrophyRoadHighScore = legacyTrophyRoadHighScore;
this.currentPathOfLegendSeasonResult = currentPathOfLegendSeasonResult;
this.lastPathOfLegendSeasonResult = lastPathOfLegendSeasonResult;
this.bestPathOfLegendSeasonResult = bestPathOfLegendSeasonResult;
}
static fromJSON(jsonObject) {
return new Player(
jsonObject.tag,
jsonObject.name,
jsonObject.expLevel,
jsonObject.trophies,
jsonObject.bestTrophies,
jsonObject.wins,
jsonObject.losses,
jsonObject.battleCount,
jsonObject.threeCrownWins,
jsonObject.challengeCardsWon,
jsonObject.challengeMaxWins,
jsonObject.tournamentCardsWon,
jsonObject.tournamentBattleCount,
jsonObject.role,
jsonObject.donations,
jsonObject.donationsReceived,
jsonObject.totalDonations,
jsonObject.warDayWins,
jsonObject.clanCardsCollected,
jsonObject.clan ? {
tag: jsonObject.clan.tag,
name: jsonObject.clan.name,
badgeId: jsonObject.clan.badgeId
} : null,
jsonObject.arena ? {
id: jsonObject.arena.id,
name: jsonObject.arena.name
} : null,
jsonObject.leagueStatistics,
jsonObject.badges?.map(Badge.fromJSON) || [],
jsonObject.achievements?.map(Achievement.fromJSON) || [],
jsonObject.cards?.map(Card.fromJSON) || [],
jsonObject.currentDeck?.map(Card.fromJSON) || [],
jsonObject.currentFavouriteCard ? Card.fromJSON(jsonObject.currentFavouriteCard) : null,
jsonObject.starPoints,
jsonObject.expPoints,
jsonObject.totalExpPoints,
jsonObject.legacyTrophyRoadHighScore,
jsonObject.currentPathOfLegendSeasonResult,
jsonObject.lastPathOfLegendSeasonResult,
jsonObject.bestPathOfLegendSeasonResult
);
}
toString() {
return this.name;
}
}
module.exports = { Player };