-
Notifications
You must be signed in to change notification settings - Fork 95
/
map_metadata.go
69 lines (53 loc) · 1.91 KB
/
map_metadata.go
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 examples
import (
"encoding/json"
"fmt"
"image"
"net/http"
)
// Map represents a CS:GO map. It contains information required to translate
// in-game world coordinates to coordinates relative to (0, 0) on the provided map-overviews (radar images).
type Map struct {
PosX float64 `json:"pos_x,string"`
PosY float64 `json:"pos_y,string"`
Scale float64 `json:"scale,string"`
}
// Translate translates in-game world-relative coordinates to (0, 0) relative coordinates.
func (m Map) Translate(x, y float64) (float64, float64) {
return x - m.PosX, m.PosY - y
}
// TranslateScale translates and scales in-game world-relative coordinates to (0, 0) relative coordinates.
// The outputs are pixel coordinates for the radar images found in the maps folder.
func (m Map) TranslateScale(x, y float64) (float64, float64) {
x, y = m.Translate(x, y)
return x / m.Scale, y / m.Scale
}
// GetMapMetadata fetches metadata for a specific map version from
// `https://radar-overviews.csgo.saiko.tech/<map>/<crc>/info.json`.
// Panics if any error occurs.
func GetMapMetadata(name string, crc uint32) Map {
url := fmt.Sprintf("https://radar-overviews.csgo.saiko.tech/%s/%d/info.json", name, crc)
resp, err := http.Get(url)
checkError(err)
defer resp.Body.Close()
var data map[string]Map
err = json.NewDecoder(resp.Body).Decode(&data)
checkError(err)
mapInfo, ok := data[name]
if !ok {
panic(fmt.Sprintf("failed to get map info.json entry for %q", name))
}
return mapInfo
}
// GetMapRadar fetches the radar image for a specific map version from
// `https://radar-overviews.csgo.saiko.tech/<map>/<crc>/radar.png`.
// Panics if any error occurs.
func GetMapRadar(name string, crc uint32) image.Image {
url := fmt.Sprintf("https://radar-overviews.csgo.saiko.tech/%s/%d/radar.png", name, crc)
resp, err := http.Get(url)
checkError(err)
defer resp.Body.Close()
img, _, err := image.Decode(resp.Body)
checkError(err)
return img
}