Skip to content

Commit

Permalink
Merge pull request #4 from wltu/library
Browse files Browse the repository at this point in the history
Finished First Version of Library
  • Loading branch information
wltu authored Jul 1, 2020
2 parents 8c44146 + d41bf05 commit 7f6547a
Show file tree
Hide file tree
Showing 14 changed files with 843 additions and 354 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ client
makefile

main/

.vscode/
72 changes: 70 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,76 @@ Unofficial library of accessing Hearthstone game data in Go
![Mac Go](https://github.com/wltu/HearthstoneGo/workflows/Mac%20Go/badge.svg?branch=master)
![Linux Go](https://github.com/wltu/HearthstoneGo/workflows/Linux%20Go/badge.svg?branch=master)

### Project Structure
The HearthstoneGo library provides the developer easy access to the Blizzard Hearthstone API. It handels all of the official API calls and supply the developer all the information they need.

## Get Getting Started
### Installing
Given that you have working Golang environment. If not refer to [here](https://golang.org/doc/install).
```
go get github.com/wltu/HearthstoneGo
```

### Simple Example
The `CLIENT_ID` and `CLIENT_SECRET` is created for the Blizzard API. Please follow the instruction [here](https://develop.battle.net/documentation/guides/getting-started) to create your own to use the library.
```
package main
import (
"fmt"
"os"
"github.com/wltu/HearthstoneGo/cmd/api"
)
func main() {
fmt.Println("Hello World!")
clientID := os.Getenv("CLIENT_ID")
clientSecret := os.Getenv("CLIENT_SECRET")
if client, ok := api.NewAPI("USA", clientID, clientSecret); ok {
fmt.Println(client.ClientToken)
// Search for single card.
client.SearchCard("52119-arch-villain-rafaam")
// Search for single card back.
client.SearchCardBack("155-pizza-stone")
// Search for a set of cards
client.BeginCardCollectionSearch()
// Set optional parameters.
// Visit card_collection.go for more info.
client.SetCardTextFilter("lookout")
client.EndCardCollectionSearch()
// Search for a set of card backs
client.BeginCardBackCollectionSearch()
// Set optional parameters.
// Visit card_back_collection.go for more info.
client.SetCardBackCategory("esports")
client.SetCardTextFilter("lookout")
client.EndCardBackCollectionSearch()
// Search for deck
id := "AAECAQcG+wyd8AKS+AKggAOblAPanQMMS6IE/web8wLR9QKD+wKe+wKz/AL1gAOXlAOalAOSnwMA"
client.SearchDeck(id)
} else {
fmt.Println("Error in setting up HearthstoneAPI Client!")
}
}
```


## Project Structure
This project follows loosely the proejct structure [here](https://github.com/golang-standards/project-layout).

### Design Document
## Design Document
The rough design document for the project can be found [here](https://docs.google.com/document/d/1hwWPqrOF7vG7u6qqmdCPqRR4Js99LyKEcchpjR17Z3E/edit?usp=sharing).
204 changes: 71 additions & 133 deletions cmd/api/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,49 @@ package api
import (
"fmt"
"net/http"
"strconv"
)

// CardError is present if the card is not found
type CardError struct {
Status int `json:"status"`
StatusMessage string `json:"statusMessage"`
Message string `json:"message"`
}

// Battlegrounds provide information of a card in battlegrounds game mode
type Battlegrounds struct {
Tier int `json:"tier"`
Hero bool `json:"hero"`
UpgradeID int `json:"upgradeId"`
Image string `json:"image"`
ImageGold string `json:"imageGold"`
}

// Card provide information of a Hearthstone card
type Card struct {
ID int `json:"id"`
Collectible int `json:"collectible"`
Slug string `json:"slug"`
ClassID int `json:"classId"`
MultiClassIds []int `json:"multiClassIds"`
MinionTypeID int `json:"minionTypeId"`
CardTypeID int `json:"cardTypeId"`
CardSetID int `json:"cardSetId"`
RarityID int `json:"rarityId"`
ArtistName string `json:"artistName"`
Health int `json:"health"`
Attack int `json:"attack"`
ManaCost int `json:"manaCost"`
Name string `json:"name"`
Text string `json:"text"`
Image string `json:"image"`
ImageGold string `json:"imageGold"`
FlavorText string `json:"flavorText"`
CropImage string `json:"cropImage"`
ChildIds []int `json:"childIds"`
KeywordIds []int `json:"keywordIds"`
ID int `json:"id"`
Collectible int `json:"collectible"`
Slug string `json:"slug"`
ClassID int `json:"classId"`
MultiClassIds []int `json:"multiClassIds"`
MinionTypeID int `json:"minionTypeId"`
CardTypeID int `json:"cardTypeId"`
CardSetID int `json:"cardSetId"`
RarityID int `json:"rarityId"`
ArtistName string `json:"artistName"`
Health int `json:"health"`
Attack int `json:"attack"`
ManaCost int `json:"manaCost"`
Name string `json:"name"`
Text string `json:"text"`
Image string `json:"image"`
ImageGold string `json:"imageGold"`
FlavorText string `json:"flavorText"`
CropImage string `json:"cropImage"`
ChildIds []int `json:"childIds"`
KeywordIds []int `json:"keywordIds"`
Battlegrounds Battlegrounds `json:"battlegrounds"`
Error CardError `json:"error"`
}

type cardSearch struct {
Expand All @@ -52,37 +69,29 @@ func (client *HearthstoneAPI) newCardSearch(id string) cardSearch {

// String function for Card
func (card Card) String() string {
if card.Battlegrounds != (Battlegrounds{}) {
return fmt.Sprintf("%s: tier %d %d/%d",
card.Name, card.Battlegrounds.Tier,
card.Attack, card.Health)
}

return fmt.Sprintf("%s: %d mana %d/%d",
card.Name, card.ManaCost,
card.Attack, card.Health)
}

// SetID update the current id value for cardSearch
func (search *cardSearch) SetID(id string) {
search.id = id
}

// SetLocale set the optional parameter of locale for cardSearch
func (search *cardSearch) SetLocale(locale string) {
search.locale = locale
}

// SetGameMode set the optional parameter of game mode for cardSearch
func (search *cardSearch) SetGameMode(gameMode string) {
search.optional["gameMode"] = gameMode
}

func (search *cardSearch) execute(client *http.Client, token string) interface{} {
url := search.url +
"hearthstone/cards/" +
search.id + "?locale=" +
search.locale + "&" +
"access_token=" + token
search.locale + "&"

for key, element := range search.optional {
url += key + "=" + element + "&"
}

url += "access_token=" + token

card := Card{}
err := get(client, url, &card)

Expand All @@ -95,108 +104,37 @@ func (search *cardSearch) execute(client *http.Client, token string) interface{}
return card
}

// CardCollection provide information of a Hearthstone Card Collection
type CardCollection struct {
Cards []Card `json:"cards"`
CardCount int `json:"cardCount"`
PageCount int `json:"pageCount"`
Page int `json:"page"`
}

// cardCollectionSearch provides parameters for a card collection search
type cardCollectionSearch struct {
// Required Parameters
url string
locale string

// Optional Parameters
optionalString map[string]string
optionalInt map[string]int
}

// NewCardCollectionSearch acts as a constructor for cardCollectionSearch
func (client *HearthstoneAPI) newCardCollectionSearch() cardCollectionSearch {
return cardCollectionSearch{
url: client.apiURL,
locale: client.locale,
optionalString: make(map[string]string),
optionalInt: make(map[string]int),
}
}

// SetGameMode set the optional parameter of game mode for cardCollectionSearch
func (search *cardCollectionSearch) SetGameMode(gameMode string) {
search.optionalString["gameMode"] = gameMode
}

// SetCardSet set the optional parameter of card set for cardCollectionSearch
func (search *cardCollectionSearch) SetCardSet(set string) {
search.optionalString["set"] = set
}
// SearchCard request a specific card by id
func (client *HearthstoneAPI) SearchCard(id string) *Card {
search := client.newCardSearch(id)

// SetClass set the optional parameter of hero class for cardCollectionSearch
func (search *cardCollectionSearch) SetClass(class string) {
search.optionalString["class"] = class
}

// SetManaCost set the optional parameter of card mana cost for cardCollectionSearch
func (search *cardCollectionSearch) SetManaCost(manaCost int) {
search.optionalInt["manaCost"] = manaCost
}
if output, ok := client.execute(&search).(Card); ok {
if output.Error.Status != 0 {
return nil
}

// SetTiers set the optional parameter of minion tiers (Battleground Only) for cardCollectionSearch
func (search *cardCollectionSearch) SetTiers(tiers []int) {
output := string(tiers[0])
for i := 1; i < len(tiers); i++ {
output += "%2C" + string(tiers[i])
if output.Collectible == 1 {
return &output
}
}
search.optionalString["tier"] = output
}

// SetAttack set the optional parameter of minion attack for cardCollectionSearch
func (search *cardCollectionSearch) SetAttack(attack int) {
search.optionalInt["SetAttack"] = attack
}

// SetHealth set the optional parameter of minion health for cardCollectionSearch
func (search *cardCollectionSearch) SetHealth(health int) {
search.optionalInt["health"] = health
}

// SetCollectible set the optional parameter of collectible for cardCollectionSearch
func (search *cardCollectionSearch) SetCollectible(collectible int) {
search.optionalInt["collectible"] = collectible
}

// SetPage set the optional parameter of page number for cardCollectionSearch
// Not all the requle for the request return all at once
func (search *cardCollectionSearch) SetPage(page int) {
search.optionalInt["page"] = page
return nil
}

func (search *cardCollectionSearch) execute(client *http.Client, token string) interface{} {
url := search.url +
"hearthstone/cards/?locale=" +
search.locale + "&"
// SearchBattlegroundsCard request a specific battlegrounds card by id
func (client *HearthstoneAPI) SearchBattlegroundsCard(id string) *Card {
search := client.newCardSearch(id)
search.optional["gameMode"] = "battlegrounds"

for key, element := range search.optionalString {
url += key + "=" + element + "&"
}
if output, ok := client.execute(&search).(Card); ok {
if output.Error.Status != 0 {
return nil
}

for key, element := range search.optionalInt {
url += key + "=" + strconv.Itoa(element) + "&"
if output.Battlegrounds != (Battlegrounds{}) {
return &output
}
}

url += "access_token=" + token

cardCollection := CardCollection{}
err := get(client, url, &cardCollection)

if err != nil {
panic(err)
}

// print(cardCollection)

return cardCollection
return nil
}
Loading

0 comments on commit 7f6547a

Please sign in to comment.