Skip to content

Commit 5e54bd8

Browse files
committed
examples: simplify error handling for getMapMetadata and getMapRadar
1 parent 27bfbc0 commit 5e54bd8

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

examples/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Here you can find a overview of examples on how to use demoinfocs-golang.
44

5+
:information_source: Example code may not be production ready - specifically error handling and such is done in a simplified way and should not be used in critical systems as-is.
6+
57
|Example|Description
68
|-|-|
79
|[heatmap](heatmap)|Creating a heatmap from positions where players fired shots from|

examples/common.go

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,20 @@ func DemoPathFromArgs() string {
3232
return demPath
3333
}
3434

35+
func checkError(err error) {
36+
if err != nil {
37+
panic(err)
38+
}
39+
}
40+
3541
// RedirectStdout redirects standard output to dev null.
3642
// Panics if an error occurs.
3743
func RedirectStdout(f func()) {
3844
// Redirect stdout, the resulting image is written to this
3945
old := os.Stdout
46+
4047
r, w, err := os.Pipe()
41-
if err != nil {
42-
panic(err)
43-
}
48+
checkError(err)
4449

4550
os.Stdout = w
4651

@@ -55,42 +60,35 @@ func RedirectStdout(f func()) {
5560
os.Stdout = old
5661
}
5762

58-
func GetMapMetadata(name string, crc uint32) (metadata.Map, error) {
63+
// GetMapMetadata fetches metadata for a specific map version from
64+
// `https://radar-overviews.csgo.saiko.tech/<map>/<crc>/info.json`.
65+
// Panics if any error occurs.
66+
func GetMapMetadata(name string, crc uint32) metadata.Map {
5967
url := fmt.Sprintf("https://radar-overviews.csgo.saiko.tech/%s/%d/info.json", name, crc)
6068

6169
resp, err := http.Get(url)
62-
if err != nil {
63-
return metadata.Map{}, fmt.Errorf("failed to get map info.json from %q: %v", url, err)
64-
}
70+
checkError(err)
6571

6672
defer resp.Body.Close()
6773

6874
var data map[string]interface{}
6975

7076
err = json.NewDecoder(resp.Body).Decode(&data)
71-
if err != nil {
72-
return metadata.Map{}, fmt.Errorf("failed to decode as JSON: %v", err)
73-
}
77+
checkError(err)
7478

7579
mapInfo, ok := data[name].(map[string]interface{})
7680
if !ok {
77-
return metadata.Map{}, fmt.Errorf("failed to get map info.json entry for %q", name)
81+
panic(fmt.Sprintf("failed to get map info.json entry for %q", name))
7882
}
7983

8084
x, err := strconv.ParseFloat(mapInfo["pos_x"].(string), 64)
81-
if err != nil {
82-
return metadata.Map{}, fmt.Errorf("failed to get origin for X coordinate: %v", err)
83-
}
85+
checkError(err)
8486

8587
y, err := strconv.ParseFloat(mapInfo["pos_y"].(string), 64)
86-
if err != nil {
87-
return metadata.Map{}, fmt.Errorf("failed to get origin for Y coordinate: %v", err)
88-
}
88+
checkError(err)
8989

9090
scale, err := strconv.ParseFloat(mapInfo["scale"].(string), 64)
91-
if err != nil {
92-
return metadata.Map{}, fmt.Errorf("failed to get scale: %v", err)
93-
}
91+
checkError(err)
9492

9593
return metadata.Map{
9694
Name: name,
@@ -99,23 +97,22 @@ func GetMapMetadata(name string, crc uint32) (metadata.Map, error) {
9997
Y: y,
10098
},
10199
Scale: scale,
102-
}, nil
100+
}
103101
}
104102

105-
func GetMapRadar(name string, crc uint32) (image.Image, error) {
103+
// GetMapMetadata fetches metadata for a specific map version from
104+
// `https://radar-overviews.csgo.saiko.tech/<map>/<crc>/info.json`.
105+
// Panics if any error occurs.
106+
func GetMapRadar(name string, crc uint32) image.Image {
106107
url := fmt.Sprintf("https://radar-overviews.csgo.saiko.tech/%s/%d/radar.png", name, crc)
107108

108109
resp, err := http.Get(url)
109-
if err != nil {
110-
return nil, fmt.Errorf("failed to get map radar.png from %q: %v", url, err)
111-
}
110+
checkError(err)
112111

113112
defer resp.Body.Close()
114113

115114
img, _, err := image.Decode(resp.Body)
116-
if err != nil {
117-
return nil, fmt.Errorf("failed to decode as image: %v", err)
118-
}
115+
checkError(err)
119116

120-
return img, nil
117+
return img
121118
}

examples/heatmap/heatmap.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@ func main() {
4848

4949
p.RegisterNetMessageHandler(func(msg *msg.CSVCMsg_ServerInfo) {
5050
// Get metadata for the map that the game was played on for coordinate translations
51-
mapMetadata, err = ex.GetMapMetadata(header.MapName, msg.MapCrc)
52-
checkError(err)
51+
mapMetadata = ex.GetMapMetadata(header.MapName, msg.MapCrc)
5352

5453
// Load map overview image
55-
mapRadarImg, err = ex.GetMapRadar(header.MapName, msg.MapCrc)
56-
checkError(err)
54+
mapRadarImg = ex.GetMapRadar(header.MapName, msg.MapCrc)
5755
})
5856

5957
// Register handler for WeaponFire, triggered every time a shot is fired

examples/nade-trajectories/nade_trajectories.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,10 @@ func main() {
5858

5959
p.RegisterNetMessageHandler(func(msg *msg.CSVCMsg_ServerInfo) {
6060
// Get metadata for the map that the game was played on for coordinate translations
61-
curMap, err = ex.GetMapMetadata(header.MapName, msg.MapCrc)
62-
checkError(err)
61+
curMap = ex.GetMapMetadata(header.MapName, msg.MapCrc)
6362

6463
// Load map overview image
65-
mapRadarImg, err = ex.GetMapRadar(header.MapName, msg.MapCrc)
66-
checkError(err)
64+
mapRadarImg = ex.GetMapRadar(header.MapName, msg.MapCrc)
6765
})
6866

6967
nadeTrajectories := make(map[int64]*nadePath) // Trajectories of all destroyed nades

0 commit comments

Comments
 (0)