-
Notifications
You must be signed in to change notification settings - Fork 0
/
universe.go
136 lines (116 loc) · 3.71 KB
/
universe.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package esi
import (
"fmt"
"net/http"
"sync"
)
type Position struct {
X float32 `json:"x"`
Y float32 `json:"y"`
Z float32 `json:"z"`
}
type Planet struct {
PlanetID int `json:"planet_id"`
MoonsIDs []int `json:"moons"`
}
type System struct {
StarID int `json:"star_id"`
SystemID int `json:"system_id"`
Name string `json:"name"`
Position Position `json:"position"`
ConstellationID int `json:"constellation_id"`
Planets []Planet `json:"planets"`
SecurityClass string `json:"security_class"`
StargatesIDs []int `json:"stargates"`
StationsIDs []int `json:"stations"`
}
func (c *Client) GetSystem(systemID int) (system System) {
req := c.CreateRequest(http.MethodGet,
fmt.Sprintf("/universe/systems/%d/", systemID), nil, false)
resp := PerformRequest(req)
UnmarshalResponse(resp, &system)
return system
}
type Destination struct {
SystemID int `json:"system_id"`
StargateID int `json:"stargate_id"`
}
type Stargate struct {
StargateID int `json:"stargate_id"`
Name string `json:"name"`
TypeID int `json:"type_id"`
Position Position `json:"position"`
SystemID int `json:"system_id"`
Destination Destination `json:"destination"`
}
func (c *Client) GetStargate(stargateID int) (stargate Stargate) {
req := c.CreateRequest(http.MethodGet,
fmt.Sprintf("/universe/stargates/%d/", stargateID), nil, false)
resp := PerformRequest(req)
UnmarshalResponse(resp, &stargate)
return stargate
}
func (c *Client) GetConnectingSystems(systemID int, depth int) (map[int]System,
map[int]Stargate) {
systems := make(map[int]System)
stargates := make(map[int]Stargate)
var waitGroup sync.WaitGroup
c.getConnectingSystemsMaps(systemID, depth, systems, stargates, &waitGroup)
waitGroup.Wait()
return systems, stargates
}
func (c *Client) getConnectingSystemsMaps(systemID int, depth int,
systems map[int]System, stargates map[int]Stargate,
waitGroup *sync.WaitGroup) {
currSystem := c.GetSystem(systemID)
systems[currSystem.SystemID] = currSystem
if depth >= 0 {
waitGroup.Add(len(currSystem.StargatesIDs))
for _, stargateID := range currSystem.StargatesIDs {
go func(stargateID int) {
defer waitGroup.Done()
currStargate := c.GetStargate(stargateID)
stargates[currStargate.StargateID] = currStargate
c.getConnectingSystemsMaps(currStargate.Destination.SystemID, depth-1, systems, stargates, waitGroup)
}(stargateID)
}
}
}
type SystemJumps struct {
ShipJumps int `json:"ship_jumps"`
SystemID int `json:"system_id"`
}
func (c *Client) GetSystemJumps() map[int]SystemJumps {
systemJumps := make(map[int]SystemJumps)
systemJumpsResponse := getSystemJumpsResponse(c)
for _, systemJump := range systemJumpsResponse {
systemJumps[systemJump.SystemID] = systemJump
}
return systemJumps
}
func getSystemJumpsResponse(c *Client) (systemJumps []SystemJumps) {
req := c.CreateRequest(http.MethodGet, "/universe/system_jumps/", nil, false)
resp := PerformRequest(req)
UnmarshalResponse(resp, &systemJumps)
return systemJumps
}
type SystemKills struct {
NpcKills int `json:"npc_kills"`
PodKills int `json:"pod_kills"`
ShipKills int `json:"ship_kills"`
SystemID int `json:"system_id"`
}
func (c *Client) GetSystemKills() map[int]SystemKills {
systemKills := make(map[int]SystemKills)
systemKillsResponse := getSystemKillsResponse(c)
for _, systemKill := range systemKillsResponse {
systemKills[systemKill.SystemID] = systemKill
}
return systemKills
}
func getSystemKillsResponse(c *Client) (systemKills []SystemKills) {
req := c.CreateRequest(http.MethodGet, "/universe/system_kills/", nil, false)
resp := PerformRequest(req)
UnmarshalResponse(resp, &systemKills)
return systemKills
}