-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.go
More file actions
69 lines (60 loc) · 1.53 KB
/
utils.go
File metadata and controls
69 lines (60 loc) · 1.53 KB
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
package gohbem
import (
"encoding/json"
"fmt"
"math"
"net/http"
)
// MasterFileURL is a remote address used to fetch MasterFile.
const MasterFileURL = "https://raw.githubusercontent.com/WatWowMap/Masterfile-Generator/master/master-latest-basics.json"
func roundFloat(val float64, precision uint) float64 {
ratio := math.Pow(10, float64(precision))
return math.Round(val*ratio) / ratio
}
func containsInt(slice []int, value int) bool {
for _, v := range slice {
if v == value {
return true
}
}
return false
}
func fetchMasterFile() (PokemonData, error) {
req, err := http.NewRequest("GET", MasterFileURL, nil)
if err != nil {
return PokemonData{}, ErrMasterFileFetch
}
req.Header.Set("User-Agent", fmt.Sprintf("Gohbem/%s", VERSION))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return PokemonData{}, ErrMasterFileFetch
}
//goland:noinspection GoUnhandledErrorResult
defer resp.Body.Close()
var data PokemonData
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return PokemonData{}, ErrMasterFileDecode
}
data.Initialized = true
return data, nil
}
func safetyCheck(o *Ohbem) error {
if !o.PokemonData.Initialized {
return ErrMasterFileUnloaded
}
if len(o.Leagues) == 0 {
return ErrLeaguesMissing
}
if len(o.LevelCaps) == 0 {
return ErrLevelCapsMissing
}
return nil
}
// log logs the given message using the provided logger, if available. If no logger is set, the message is ignored.
func (o *Ohbem) log(message string) {
if o.Logger != nil {
o.Logger.Print(message)
}
}