All files CardDealer.ts

100% Statements 45/45
100% Branches 22/22
100% Functions 18/18
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73    1x   1x 20x     35x   34x   33x   41x   31x   30x       2x   2x   1x 1x       2x   2x   1x       3x       2x   2x       3x       2x       1x       2x       3x 3x   4x 2x 1x    
import { PokemonCard, PokemonCardTipo } from "./types";
 
const limiteDeck = 5;
 
export class CardDealer {
    private deck: PokemonCard[] = [];
 
    addCard(card: PokemonCard): void {
        if(card.hp <= 0) throw new Error("HP da carta deve ser maior que 0.");
 
        if(card.ataque < 0) throw new Error("Ataque da carta não pode ser negativo.");
 
        if(card.nome.trim() === "") throw new Error("Nome da carta não pode ser vazio.");
 
        if(this.deck.some((c) => c.id === card.id)) throw new Error(`Carta com id ${card.id} já está no deck.`);
 
        if(this.deck.length >= limiteDeck) throw new Error("Deck atingiu o limite de cartas. (Máximo: 5)");
    
        this.deck.push(card);
    }
 
    removeCard(id: string): PokemonCard {
        const index = this.deck.findIndex((c) => c.id === id);
    
        if(index === -1) throw new Error(`Carta com id ${id} não encontrada no deck. Não foi possível remover.`);
    
        const [removedCard] = this.deck.splice(index, 1);
        return removedCard;
    }
 
    getCard(id: string): PokemonCard {
        const card = this.deck.find((c) => c.id === id);
    
        if(!card) throw new Error(`Carta com id ${id} não encontrada no deck. Não foi possível obter.`);
    
        return card;
    }
 
    getCardsByTipo(tipo: PokemonCardTipo): PokemonCard[] {
        return this.deck.filter((c) => c.tipo === tipo);
    }
 
    getCardMaisForte(): PokemonCard {
    if(this.deck.length === 0) throw new Error("Deck está vazio. Não é possível obter a carta mais forte.");
    
    return this.deck.reduce((maisForte, card) => card.ataque > maisForte.ataque ? card : maisForte);
    }
 
    getTotalHp(): number {
        return this.deck.reduce((total, card) => total + card.hp, 0);
    }
 
    deckFull(): boolean {
        return this.deck.length >= limiteDeck;
    }
 
    clearDeck(): void {
        this.deck = [];
    }
 
    getTamanhoDeck(): number {
        return this.deck.length;
    }
 
    getCardsByRaridade(raridade: string): PokemonCard[] {
        const raridadesValidas = ["Comum", "Incomum", "Rara", "Lendária"];
        if (!raridadesValidas.includes(raridade)) throw new Error(`Raridade '${raridade}' é inválida.`);
 
        const resultado = this.deck.filter((c) => c.raridade === raridade);
        if (resultado.length === 0) throw new Error(`Nenhuma carta com raridade '${raridade}' encontrada no deck.`);
        return resultado;
    }
}