This project implements a Pokémon Battle Simulator. It provides APIs for listing Pokémon, initiating battles, and retrieving battle results.
- CSV Data Handling
- Battle Simulation Logic
- API Endpoints
- Pokémon Name Validation
- Asynchronous Battle Processing
- Error Handling
- Testing
- UML Class Diagram
- The Pokémon dataset is loaded from a CSV file using Go's
encoding/csv
package. - We use the
load.LoadDataset()
function to read and parse the CSV file. - Each row in the CSV is converted into a
LoadPokemon
struct, which embeds theBasePokemon
struct. - The loaded data is stored in memory for quick access during battles.
- Normalizing input to lowercase
- Accepting one-word spelling mistakes
- Rejecting inputs with more significant differences
- Battles are simulated using the
battle.Battle()
function. - The damage calculation follows the formula provided in the assignment:
- Two rounds are simulated: Pokémon A attacking B, and B attacking A.
- The Pokémon dealing more damage is declared the winner.
- List Pokémon (API 1)
- Endpoint:
GET /pokemon
- Supports pagination with
page
andpageSize
query parameters.
- Initiate Battle (API 2)
- Endpoint:
POST /battle
- Accepts JSON body with
pokemonA
andpokemonB
names. - Returns a battle ID (UUID4 format) for asynchronous processing.
- Get Battle Status (API 3)
- Endpoint:
GET /battle/{battleId}
- Returns the current status and result (if available) of the specified battle.
- Pokémon names are normalized to lowercase for comparison.
- We use the Levenshtein distance algorithm to handle one-word spelling mistakes.
- The
GetPokemonByName()
function in theload
package implements this logic.
- Battles are processed asynchronously using goroutines.
- When a battle is initiated, it's added to a queue and processed in the background.
- The battle status can be checked using the battle ID returned by API 2.
- Custom error types are defined for various scenarios (e.g.,
ErrPokemonNotFound
). - API responses include appropriate HTTP status codes and error messages.
- The UML class diagram is included in the repository as
uml_class_diagram.png
. - It illustrates the relationships between key classes such as
BasePokemon
,LoadPokemon
,BattlePokemon
, and the API handlers.
- Clone the repository
- Install dependencies:
go mod tidy
- Run the server:
go run cmd/main.go
- Access the API at
http://localhost:8000
This project is licensed under the MIT License - see the LICENSE.md file for details.