From 50d9ff891bbe4592c3eafde6d03995d4dcc32a95 Mon Sep 17 00:00:00 2001 From: Daria <44535712+daria305@users.noreply.github.com> Date: Sun, 7 Feb 2021 12:50:04 +0100 Subject: [PATCH] MNG-84 pokemon factory (#15) * MNG-84 Add pokemon data, import json and correct abstract class * MNG-84 Finish pokemon factory * MNG-84 Pokemon factory test and class corrections --- assets/poke_data.json | 434 +++++++++++++++++++++++++++ src/__tests__/pokemonFactory.spec.ts | 31 ++ src/fightClass.ts | 2 +- src/pokemonClass.ts | 34 ++- src/pokemonFactory.ts | 50 +++ 5 files changed, 540 insertions(+), 11 deletions(-) create mode 100644 assets/poke_data.json create mode 100644 src/__tests__/pokemonFactory.spec.ts create mode 100644 src/pokemonFactory.ts diff --git a/assets/poke_data.json b/assets/poke_data.json new file mode 100644 index 0000000..e01ed4a --- /dev/null +++ b/assets/poke_data.json @@ -0,0 +1,434 @@ +{ "pokemons": [{ + "name": "bulbasaur", + "types": ["grass", "poison"], + "stats": { + "hp": 45, + "attack": 49, + "defense": 49 + }, + "moves": [{ + "name": "razor-wind", + "accuracy": 100, + "power": 80, + "power_points": 10, + "type": "normal" + }, { + "name": "vine-whip", + "accuracy": 100, + "power": 45, + "power_points": 25, + "type": "grass" + }, { + "name": "headbutt", + "accuracy": 100, + "power": 70, + "power_points": 15, + "type": "normal" + }] +}, { + "name": "charmander", + "types": ["fire"], + "stats": { + "hp": 39, + "attack": 52, + "defense": 43 + }, + "moves": [{ + "name": "mega-punch", + "accuracy": 85, + "power": 80, + "power_points": 20, + "type": "normal" + }, { + "name": "fire-punch", + "accuracy": 100, + "power": 75, + "power_points": 15, + "type": "fire" + }, { + "name": "thunder-punch", + "accuracy": 100, + "power": 75, + "power_points": 15, + "type": "electric" + }, { + "name": "mega-kick", + "accuracy": 75, + "power": 120, + "power_points": 5, + "type": "normal" + }] +}, { + "name": "squirtle", + "types": ["water"], + "stats": { + "hp": 44, + "attack": 48, + "defense": 65 + }, + "moves": [{ + "name": "mega-punch", + "accuracy": 85, + "power": 80, + "power_points": 20, + "type": "normal" + }, { + "name": "ice-punch", + "accuracy": 100, + "power": 75, + "power_points": 15, + "type": "ice" + }, { + "name": "mega-kick", + "accuracy": 75, + "power": 120, + "power_points": 5, + "type": "normal" + }, { + "name": "bite", + "accuracy": 100, + "power": 60, + "power_points": 25, + "type": "dark" + }] +}, { + "name": "weedle", + "types": ["bug", "poison"], + "stats": { + "hp": 40, + "attack": 35, + "defense": 30 + }, + "moves": [{ + "name": "poison-sting", + "accuracy": 100, + "power": 15, + "power_points": 35, + "type": "poison" + }, { + "name": "bug-bite", + "accuracy": 100, + "power": 60, + "power_points": 20, + "type": "bug" + }, { + "name": "electroweb", + "accuracy": 95, + "power": 55, + "power_points": 15, + "type": "electric" + }] +}, { + "name": "pidgey", + "types": ["normal", "flying"], + "stats": { + "hp": 40, + "attack": 45, + "defense": 40 + }, + "moves": [{ + "name": "razor-wind", + "accuracy": 100, + "power": 80, + "power_points": 10, + "type": "normal" + }, { + "name": "gust", + "accuracy": 100, + "power": 40, + "power_points": 35, + "type": "flying" + }, { + "name": "wing-attack", + "accuracy": 100, + "power": 60, + "power_points": 35, + "type": "flying" + }, { + "name": "fly", + "accuracy": 95, + "power": 90, + "power_points": 15, + "type": "flying" + }, { + "name": "take-down", + "accuracy": 85, + "power": 90, + "power_points": 20, + "type": "normal" + }] +}, { + "name": "rattata", + "types": ["normal"], + "stats": { + "hp": 30, + "attack": 56, + "defense": 35 + }, + "moves": [{ + "name": "headbutt", + "accuracy": 100, + "power": 70, + "power_points": 15, + "type": "normal" + }, { + "name": "body-slam", + "accuracy": 100, + "power": 85, + "power_points": 15, + "type": "normal" + }, { + "name": "bite", + "accuracy": 100, + "power": 60, + "power_points": 25, + "type": "dark" + }, { + "name": "water-gun", + "accuracy": 100, + "power": 40, + "power_points": 25, + "type": "water" + }, { + "name": "ice-beam", + "accuracy": 100, + "power": 90, + "power_points": 10, + "type": "ice" + }] +}, { + "name": "pikachu", + "types": ["electric"], + "stats": { + "hp": 35, + "attack": 55, + "defense": 40 + }, + "moves": [{ + "name": "mega-punch", + "accuracy": 85, + "power": 80, + "power_points": 20, + "type": "normal" + }, { + "name": "thunder-punch", + "accuracy": 100, + "power": 75, + "power_points": 15, + "type": "electric" + }, { + "name": "slam", + "accuracy": 75, + "power": 80, + "power_points": 20, + "type": "normal" + }] +}, { + "name": "sandshrew", + "types": ["ground"], + "stats": { + "hp": 50, + "attack": 75, + "defense": 85 + }, + "moves": [{ + "name": "headbutt", + "accuracy": 100, + "power": 70, + "power_points": 15, + "type": "normal" + }, { + "name": "body-slam", + "accuracy": 100, + "power": 85, + "power_points": 15, + "type": "normal" + }, { + "name": "poison-sting", + "accuracy": 100, + "power": 15, + "power_points": 35, + "type": "poison" + }, { + "name": "submission", + "accuracy": 80, + "power": 80, + "power_points": 20, + "type": "fighting" + }] +}, { + "name": "clefairy", + "types": ["fairy"], + "stats": { + "hp": 70, + "attack": 45, + "defense": 48 + }, + "moves": [{ + "name": "mega-punch", + "accuracy": 85, + "power": 80, + "power_points": 20, + "type": "normal" + }, { + "name": "fire-punch", + "accuracy": 100, + "power": 75, + "power_points": 15, + "type": "fire" + }, { + "name": "ice-punch", + "accuracy": 100, + "power": 75, + "power_points": 15, + "type": "ice" + }, { + "name": "thunder-punch", + "accuracy": 100, + "power": 75, + "power_points": 15, + "type": "electric" + }, { + "name": "mega-kick", + "accuracy": 75, + "power": 120, + "power_points": 5, + "type": "normal" + }] +}, { + "name": "abra", + "types": ["psychic"], + "stats": { + "hp": 25, + "attack": 20, + "defense": 15 + }, + "moves": [{ + "name": "mega-punch", + "accuracy": 85, + "power": 80, + "power_points": 20, + "type": "normal" + }, { + "name": "fire-punch", + "accuracy": 100, + "power": 75, + "power_points": 15, + "type": "fire" + }, { + "name": "ice-punch", + "accuracy": 100, + "power": 75, + "power_points": 15, + "type": "ice" + }, { + "name": "thunder-punch", + "accuracy": 100, + "power": 75, + "power_points": 15, + "type": "electric" + }, { + "name": "mega-kick", + "accuracy": 75, + "power": 120, + "power_points": 5, + "type": "normal" + }, { + "name": "submission", + "accuracy": 80, + "power": 80, + "power_points": 20, + "type": "fighting" + }] +}, { + "name": "machop", + "types": ["fighting"], + "stats": { + "hp": 70, + "attack": 80, + "defense": 50 + }, + "moves": [{ + "name": "karate-chop", + "accuracy": 100, + "power": 50, + "power_points": 25, + "type": "fighting" + }, { + "name": "mega-punch", + "accuracy": 85, + "power": 80, + "power_points": 20, + "type": "normal" + }, { + "name": "fire-punch", + "accuracy": 100, + "power": 75, + "power_points": 15, + "type": "fire" + }, { + "name": "ice-punch", + "accuracy": 100, + "power": 75, + "power_points": 15, + "type": "ice" + }, { + "name": "thunder-punch", + "accuracy": 100, + "power": 75, + "power_points": 15, + "type": "electric" + }, { + "name": "mega-kick", + "accuracy": 75, + "power": 120, + "power_points": 5, + "type": "normal" + }, { + "name": "rolling-kick", + "accuracy": 85, + "power": 60, + "power_points": 15, + "type": "fighting" + }] +}, { + "name": "tentacruel", + "types": ["water", "poison"], + "stats": { + "hp": 80, + "attack": 70, + "defense": 65 + }, + "moves": [{ + "name": "take-down", + "accuracy": 85, + "power": 90, + "power_points": 20, + "type": "normal" + }, { + "name": "double-edge", + "accuracy": 100, + "power": 120, + "power_points": 15, + "type": "normal" + }, { + "name": "poison-sting", + "accuracy": 100, + "power": 15, + "power_points": 35, + "type": "poison" + }, { + "name": "acid", + "accuracy": 100, + "power": 40, + "power_points": 30, + "type": "poison" + }, { + "name": "water-gun", + "accuracy": 100, + "power": 40, + "power_points": 25, + "type": "water" + }] +}] +} \ No newline at end of file diff --git a/src/__tests__/pokemonFactory.spec.ts b/src/__tests__/pokemonFactory.spec.ts new file mode 100644 index 0000000..f455408 --- /dev/null +++ b/src/__tests__/pokemonFactory.spec.ts @@ -0,0 +1,31 @@ +import { PokemonFactory } from "../pokemonFactory" +import { PokemonType } from "../pokemonClass" +import * as data from "../../assets/poke_data.json" + + + +describe('Test factory for pokemon class', () => { + + it("Should return ", () => { + // Given data from poke_data JSON + const pokeData = data.pokemons; + const charmander = pokeData[1]; + const pikachu = pokeData[6]; + + // When + const factory = new PokemonFactory(pokeData); + const charmanderObj = factory.getPokemonByName("Charmander"); + const pikachuObj = factory.getPokemonByName("pikachu"); + + + // Then + expect(pikachuObj.name).toBe("Pikachu"); + expect(charmanderObj.name).toBe("Charmander"); + + expect(pikachuObj.currentHP).toBe(pikachu.stats.hp); + expect(charmanderObj.attackPoints).toBe(charmander.stats.attack); + + expect(pikachuObj.moves[0]).toEqual({moveName: pikachu.moves[0].name, moveType: pikachu.moves[0].type as PokemonType}); + expect(charmanderObj.moves[1]).toEqual({moveName: charmander.moves[1].name, moveType: charmander.moves[1].type as PokemonType}); + }) +}) \ No newline at end of file diff --git a/src/fightClass.ts b/src/fightClass.ts index a54aff3..3a350ef 100644 --- a/src/fightClass.ts +++ b/src/fightClass.ts @@ -26,7 +26,7 @@ export class Fight { if (!this._typesForChart.includes(attackType)) { throw new Error("Attack type does not exist") } - if (!this._typesForChart.includes(attackType)) { + if (!this._typesForChart.includes(pokemonDefenceType)) { throw new Error("Pokemon type does not exist") } return this._fightChart[this._typesForChart.indexOf(attackType)][this._typesForChart.indexOf(pokemonDefenceType)] diff --git a/src/pokemonClass.ts b/src/pokemonClass.ts index 3dd8e2c..a3ed3dc 100644 --- a/src/pokemonClass.ts +++ b/src/pokemonClass.ts @@ -13,16 +13,30 @@ export type PokemonType = | "fighting" | "rock"; -export abstract class Pokemon { - constructor() {} - abstract readonly _name: string; - abstract readonly _image: string; - abstract readonly _maxHP: number; - abstract _currentHP: number; - abstract readonly _attackPoints: number; - abstract readonly _defensePoints: number; - abstract readonly _types: PokemonType[]; - abstract readonly _moves: { moveName: string; moveType: PokemonType }[]; +export class Pokemon { + + readonly _name: string; + readonly _image: string; + readonly _maxHP: number; + private _currentHP: number; + readonly _attackPoints: number; + readonly _defensePoints: number; + readonly _types: PokemonType[]; + readonly _moves: { moveName: string; moveType: PokemonType }[]; + + constructor(name: string, image: string, maxHP: number, attackPoints: number, + defensePoints: number, types: PokemonType[], moves: { moveName: string; moveType: PokemonType }[]) { + + this._name = name; + this._image = image; + this._maxHP = maxHP; + this._currentHP = maxHP; + this._attackPoints = attackPoints; + this._defensePoints = defensePoints; + this._types = types; + this._moves = moves; + } + get name() { return this._name; diff --git a/src/pokemonFactory.ts b/src/pokemonFactory.ts new file mode 100644 index 0000000..6535df4 --- /dev/null +++ b/src/pokemonFactory.ts @@ -0,0 +1,50 @@ +import { Pokemon } from "./pokemonClass" +import { PokemonType } from "./pokemonClass" + + +export class PokemonFactory { + readonly _pokemons: Pokemon[]; + + constructor(pokeData: { + name: string; types: string[]; + stats: { hp: number; attack: number; defense: number; }; + moves: { name: string; accuracy: number; power: number; power_points: number; type: string; }[] + }[]) { + this._pokemons = []; + + for (let pokemon of pokeData) { + + const pokeImg = "../assets/" + pokemon.name + ".png" + + const pokeMoves = pokemon.moves.reduce( (tab, move) => { + tab.push({moveName: move.name, moveType: move.type as PokemonType}) + return tab + }, [] as {moveName: string, moveType: PokemonType}[]) + + const pokeTypes: PokemonType[] = pokemon.types.map(v => v as PokemonType) + + const pokemonObject = new Pokemon(this._capitalize(pokemon.name), pokeImg, + pokemon.stats.hp, pokemon.stats.attack, pokemon.stats.defense, + pokeTypes, pokeMoves) + + this._pokemons.push(pokemonObject) + } + } + + get pokemons(): Pokemon[] { + return this._pokemons + } + + getPokemonByName(name: string): Pokemon { + const pokemon = this._pokemons.find( pokemon => pokemon.name.toLowerCase() === name.toLowerCase()); + if (pokemon === undefined) { + throw new Error(`Pokemon ${name} is not found in the pokemon factory`) + } + return pokemon + } + + _capitalize(str: string): string { + return str.charAt(0).toUpperCase() + str.slice(1); + } +} +