Skip to content

Commit 7f6547a

Browse files
authored
Merge pull request #4 from wltu/library
Finished First Version of Library
2 parents 8c44146 + d41bf05 commit 7f6547a

14 files changed

+843
-354
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ client
1919
makefile
2020

2121
main/
22+
23+
.vscode/

README.md

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,76 @@ Unofficial library of accessing Hearthstone game data in Go
55
![Mac Go](https://github.com/wltu/HearthstoneGo/workflows/Mac%20Go/badge.svg?branch=master)
66
![Linux Go](https://github.com/wltu/HearthstoneGo/workflows/Linux%20Go/badge.svg?branch=master)
77

8-
### Project Structure
8+
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.
9+
10+
## Get Getting Started
11+
### Installing
12+
Given that you have working Golang environment. If not refer to [here](https://golang.org/doc/install).
13+
```
14+
go get github.com/wltu/HearthstoneGo
15+
```
16+
17+
### Simple Example
18+
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.
19+
```
20+
package main
21+
22+
import (
23+
"fmt"
24+
"os"
25+
26+
"github.com/wltu/HearthstoneGo/cmd/api"
27+
)
28+
29+
func main() {
30+
fmt.Println("Hello World!")
31+
32+
clientID := os.Getenv("CLIENT_ID")
33+
clientSecret := os.Getenv("CLIENT_SECRET")
34+
35+
if client, ok := api.NewAPI("USA", clientID, clientSecret); ok {
36+
fmt.Println(client.ClientToken)
37+
38+
// Search for single card.
39+
client.SearchCard("52119-arch-villain-rafaam")
40+
41+
// Search for single card back.
42+
client.SearchCardBack("155-pizza-stone")
43+
44+
// Search for a set of cards
45+
client.BeginCardCollectionSearch()
46+
47+
// Set optional parameters.
48+
// Visit card_collection.go for more info.
49+
client.SetCardTextFilter("lookout")
50+
51+
client.EndCardCollectionSearch()
52+
53+
// Search for a set of card backs
54+
client.BeginCardBackCollectionSearch()
55+
56+
// Set optional parameters.
57+
// Visit card_back_collection.go for more info.
58+
client.SetCardBackCategory("esports")
59+
60+
client.SetCardTextFilter("lookout")
61+
62+
client.EndCardBackCollectionSearch()
63+
64+
// Search for deck
65+
id := "AAECAQcG+wyd8AKS+AKggAOblAPanQMMS6IE/web8wLR9QKD+wKe+wKz/AL1gAOXlAOalAOSnwMA"
66+
client.SearchDeck(id)
67+
68+
} else {
69+
fmt.Println("Error in setting up HearthstoneAPI Client!")
70+
}
71+
}
72+
73+
```
74+
75+
76+
## Project Structure
977
This project follows loosely the proejct structure [here](https://github.com/golang-standards/project-layout).
1078

11-
### Design Document
79+
## Design Document
1280
The rough design document for the project can be found [here](https://docs.google.com/document/d/1hwWPqrOF7vG7u6qqmdCPqRR4Js99LyKEcchpjR17Z3E/edit?usp=sharing).

cmd/api/card.go

Lines changed: 71 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,49 @@ package api
33
import (
44
"fmt"
55
"net/http"
6-
"strconv"
76
)
87

8+
// CardError is present if the card is not found
9+
type CardError struct {
10+
Status int `json:"status"`
11+
StatusMessage string `json:"statusMessage"`
12+
Message string `json:"message"`
13+
}
14+
15+
// Battlegrounds provide information of a card in battlegrounds game mode
16+
type Battlegrounds struct {
17+
Tier int `json:"tier"`
18+
Hero bool `json:"hero"`
19+
UpgradeID int `json:"upgradeId"`
20+
Image string `json:"image"`
21+
ImageGold string `json:"imageGold"`
22+
}
23+
924
// Card provide information of a Hearthstone card
1025
type Card struct {
11-
ID int `json:"id"`
12-
Collectible int `json:"collectible"`
13-
Slug string `json:"slug"`
14-
ClassID int `json:"classId"`
15-
MultiClassIds []int `json:"multiClassIds"`
16-
MinionTypeID int `json:"minionTypeId"`
17-
CardTypeID int `json:"cardTypeId"`
18-
CardSetID int `json:"cardSetId"`
19-
RarityID int `json:"rarityId"`
20-
ArtistName string `json:"artistName"`
21-
Health int `json:"health"`
22-
Attack int `json:"attack"`
23-
ManaCost int `json:"manaCost"`
24-
Name string `json:"name"`
25-
Text string `json:"text"`
26-
Image string `json:"image"`
27-
ImageGold string `json:"imageGold"`
28-
FlavorText string `json:"flavorText"`
29-
CropImage string `json:"cropImage"`
30-
ChildIds []int `json:"childIds"`
31-
KeywordIds []int `json:"keywordIds"`
26+
ID int `json:"id"`
27+
Collectible int `json:"collectible"`
28+
Slug string `json:"slug"`
29+
ClassID int `json:"classId"`
30+
MultiClassIds []int `json:"multiClassIds"`
31+
MinionTypeID int `json:"minionTypeId"`
32+
CardTypeID int `json:"cardTypeId"`
33+
CardSetID int `json:"cardSetId"`
34+
RarityID int `json:"rarityId"`
35+
ArtistName string `json:"artistName"`
36+
Health int `json:"health"`
37+
Attack int `json:"attack"`
38+
ManaCost int `json:"manaCost"`
39+
Name string `json:"name"`
40+
Text string `json:"text"`
41+
Image string `json:"image"`
42+
ImageGold string `json:"imageGold"`
43+
FlavorText string `json:"flavorText"`
44+
CropImage string `json:"cropImage"`
45+
ChildIds []int `json:"childIds"`
46+
KeywordIds []int `json:"keywordIds"`
47+
Battlegrounds Battlegrounds `json:"battlegrounds"`
48+
Error CardError `json:"error"`
3249
}
3350

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

5370
// String function for Card
5471
func (card Card) String() string {
72+
if card.Battlegrounds != (Battlegrounds{}) {
73+
return fmt.Sprintf("%s: tier %d %d/%d",
74+
card.Name, card.Battlegrounds.Tier,
75+
card.Attack, card.Health)
76+
}
77+
5578
return fmt.Sprintf("%s: %d mana %d/%d",
5679
card.Name, card.ManaCost,
5780
card.Attack, card.Health)
5881
}
5982

60-
// SetID update the current id value for cardSearch
61-
func (search *cardSearch) SetID(id string) {
62-
search.id = id
63-
}
64-
65-
// SetLocale set the optional parameter of locale for cardSearch
66-
func (search *cardSearch) SetLocale(locale string) {
67-
search.locale = locale
68-
}
69-
70-
// SetGameMode set the optional parameter of game mode for cardSearch
71-
func (search *cardSearch) SetGameMode(gameMode string) {
72-
search.optional["gameMode"] = gameMode
73-
}
74-
7583
func (search *cardSearch) execute(client *http.Client, token string) interface{} {
7684
url := search.url +
7785
"hearthstone/cards/" +
7886
search.id + "?locale=" +
79-
search.locale + "&" +
80-
"access_token=" + token
87+
search.locale + "&"
8188

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

93+
url += "access_token=" + token
94+
8695
card := Card{}
8796
err := get(client, url, &card)
8897

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

98-
// CardCollection provide information of a Hearthstone Card Collection
99-
type CardCollection struct {
100-
Cards []Card `json:"cards"`
101-
CardCount int `json:"cardCount"`
102-
PageCount int `json:"pageCount"`
103-
Page int `json:"page"`
104-
}
105-
106-
// cardCollectionSearch provides parameters for a card collection search
107-
type cardCollectionSearch struct {
108-
// Required Parameters
109-
url string
110-
locale string
111-
112-
// Optional Parameters
113-
optionalString map[string]string
114-
optionalInt map[string]int
115-
}
116-
117-
// NewCardCollectionSearch acts as a constructor for cardCollectionSearch
118-
func (client *HearthstoneAPI) newCardCollectionSearch() cardCollectionSearch {
119-
return cardCollectionSearch{
120-
url: client.apiURL,
121-
locale: client.locale,
122-
optionalString: make(map[string]string),
123-
optionalInt: make(map[string]int),
124-
}
125-
}
126-
127-
// SetGameMode set the optional parameter of game mode for cardCollectionSearch
128-
func (search *cardCollectionSearch) SetGameMode(gameMode string) {
129-
search.optionalString["gameMode"] = gameMode
130-
}
131-
132-
// SetCardSet set the optional parameter of card set for cardCollectionSearch
133-
func (search *cardCollectionSearch) SetCardSet(set string) {
134-
search.optionalString["set"] = set
135-
}
107+
// SearchCard request a specific card by id
108+
func (client *HearthstoneAPI) SearchCard(id string) *Card {
109+
search := client.newCardSearch(id)
136110

137-
// SetClass set the optional parameter of hero class for cardCollectionSearch
138-
func (search *cardCollectionSearch) SetClass(class string) {
139-
search.optionalString["class"] = class
140-
}
141-
142-
// SetManaCost set the optional parameter of card mana cost for cardCollectionSearch
143-
func (search *cardCollectionSearch) SetManaCost(manaCost int) {
144-
search.optionalInt["manaCost"] = manaCost
145-
}
111+
if output, ok := client.execute(&search).(Card); ok {
112+
if output.Error.Status != 0 {
113+
return nil
114+
}
146115

147-
// SetTiers set the optional parameter of minion tiers (Battleground Only) for cardCollectionSearch
148-
func (search *cardCollectionSearch) SetTiers(tiers []int) {
149-
output := string(tiers[0])
150-
for i := 1; i < len(tiers); i++ {
151-
output += "%2C" + string(tiers[i])
116+
if output.Collectible == 1 {
117+
return &output
118+
}
152119
}
153-
search.optionalString["tier"] = output
154-
}
155-
156-
// SetAttack set the optional parameter of minion attack for cardCollectionSearch
157-
func (search *cardCollectionSearch) SetAttack(attack int) {
158-
search.optionalInt["SetAttack"] = attack
159-
}
160-
161-
// SetHealth set the optional parameter of minion health for cardCollectionSearch
162-
func (search *cardCollectionSearch) SetHealth(health int) {
163-
search.optionalInt["health"] = health
164-
}
165-
166-
// SetCollectible set the optional parameter of collectible for cardCollectionSearch
167-
func (search *cardCollectionSearch) SetCollectible(collectible int) {
168-
search.optionalInt["collectible"] = collectible
169-
}
170120

171-
// SetPage set the optional parameter of page number for cardCollectionSearch
172-
// Not all the requle for the request return all at once
173-
func (search *cardCollectionSearch) SetPage(page int) {
174-
search.optionalInt["page"] = page
121+
return nil
175122
}
176123

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

182-
for key, element := range search.optionalString {
183-
url += key + "=" + element + "&"
184-
}
129+
if output, ok := client.execute(&search).(Card); ok {
130+
if output.Error.Status != 0 {
131+
return nil
132+
}
185133

186-
for key, element := range search.optionalInt {
187-
url += key + "=" + strconv.Itoa(element) + "&"
134+
if output.Battlegrounds != (Battlegrounds{}) {
135+
return &output
136+
}
188137
}
189138

190-
url += "access_token=" + token
191-
192-
cardCollection := CardCollection{}
193-
err := get(client, url, &cardCollection)
194-
195-
if err != nil {
196-
panic(err)
197-
}
198-
199-
// print(cardCollection)
200-
201-
return cardCollection
139+
return nil
202140
}

0 commit comments

Comments
 (0)