forked from dertseha/everoute-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
190 lines (146 loc) · 4.88 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"fmt"
"log"
"net/http"
"os"
"runtime"
"runtime/debug"
"strings"
"github.com/gorilla/rpc"
rpcJson "github.com/gorilla/rpc/json"
"github.com/dertseha/everoute"
"github.com/dertseha/everoute/travel/capabilities/jumpdrive"
"github.com/dertseha/everoute/travel/capabilities/jumpgate"
"github.com/dertseha/everoute/travel/rules/security"
"github.com/dertseha/everoute/travel/rules/transitcount"
"github.com/dertseha/everoute/universe"
"github.com/redteadev/everoute-web/data"
)
func reachableSystemPredicate() func(data.SolarSystemData) bool {
joveRegion := universe.Id(10000017)
specialSystems := make(map[universe.Id]interface{})
specialSystems[30000377] = nil
specialSystems[30000380] = nil
specialSystems[30000381] = nil
return func(system data.SolarSystemData) bool {
isJoveRegion := system.RegionId == joveRegion
_, isSpecialSystem := specialSystems[system.SolarSystemId]
return !isJoveRegion && !isSpecialSystem
}
}
func buildSolarSystems(builder *universe.UniverseBuilder) {
isSystemReachable := reachableSystemPredicate()
for _, system := range data.SolarSystems {
trueSec := universe.TrueSecurity(system.Security)
galaxyId := universe.NewEdenId
if system.RegionId >= 11000000 {
galaxyId = universe.WSpaceId
}
if isSystemReachable(system) {
builder.AddSolarSystem(system.SolarSystemId, system.ConstellationId, system.RegionId, galaxyId,
universe.NewSpecificLocation(system.X, system.Y, system.Z), trueSec)
}
}
}
func getSolarSystemIdsByName() map[string]universe.Id {
result := make(map[string]universe.Id)
for _, system := range data.SolarSystems {
result[system.Name] = system.SolarSystemId
}
return result
}
func getJumpGateDestinationName(gate data.JumpGateData) string {
destNameStart := strings.Index(gate.Name, "(") + 1
destNameEnd := strings.Index(gate.Name, ")")
return gate.Name[destNameStart:destNameEnd]
}
func getJumpGateKey(fromSolarSystemId, toSolarSystemId universe.Id) string {
return fmt.Sprintf("%d->%d", fromSolarSystemId, toSolarSystemId)
}
func getJumpGateLocations() map[string]universe.Location {
result := make(map[string]universe.Location)
solarSystemIdsByName := getSolarSystemIdsByName()
for _, gate := range data.JumpGates {
destName := getJumpGateDestinationName(gate)
key := getJumpGateKey(gate.SolarSystemId, solarSystemIdsByName[destName])
location := universe.NewSpecificLocation(gate.X, gate.Y, gate.Z)
result[key] = location
}
return result
}
func buildJumpGates(builder *universe.UniverseBuilder) {
jumpGateLocations := getJumpGateLocations()
ids := builder.SolarSystemIds()
isSystemReachable := func(id universe.Id) bool {
found := false
for _, temp := range ids {
if temp == id {
found = true
}
}
return found
}
for _, jumpData := range data.SolarSystemJumps {
if isSystemReachable(jumpData.FromSolarSystemId) && isSystemReachable(jumpData.ToSolarSystemId) {
extension := builder.ExtendSolarSystem(jumpData.FromSolarSystemId)
jumpBuilder := extension.BuildJump(jumpgate.JumpType, jumpData.ToSolarSystemId)
jumpBuilder.From(jumpGateLocations[getJumpGateKey(jumpData.FromSolarSystemId, jumpData.ToSolarSystemId)])
jumpBuilder.To(jumpGateLocations[getJumpGateKey(jumpData.ToSolarSystemId, jumpData.FromSolarSystemId)])
}
}
}
func dropUnusedData() {
data.SolarSystems = nil
data.SolarSystemJumps = nil
data.JumpGates = nil
}
func prepareUniverse() *universe.UniverseBuilder {
builder := universe.New().Extend()
buildSolarSystems(builder)
buildJumpGates(builder)
transitcount.ExtendUniverse(builder)
security.ExtendUniverse(builder)
jumpdrive.ExtendUniverse(builder, 10.0)
dropUnusedData()
return builder
}
func checkBaseUniverse(verse universe.Universe) {
ids := verse.SolarSystemIds()
for _, id := range ids {
solarSystem := verse.SolarSystem(id)
if solarSystem.GalaxyId() == universe.NewEdenId {
gateJumps := solarSystem.Jumps(jumpgate.JumpType)
if len(gateJumps) == 0 {
log.Printf("Solar System %v has no jump gates!", id)
}
}
}
}
func initRuntime() {
numCpu := runtime.NumCPU()
maxThreads := 250 // Heroku limit: 256
log.Printf("Initializing runtime to use %d CPUs and %d threads", numCpu, maxThreads)
runtime.GOMAXPROCS(numCpu)
debug.SetMaxThreads(maxThreads)
}
func main() {
log.Printf("everoute-web v%v using everoute v%v", Version, everoute.Version)
initRuntime()
log.Printf("Building universe...")
universeBuilder := prepareUniverse()
universe := universeBuilder.Build()
checkBaseUniverse(universe)
log.Printf("Initializing server...")
rpcServer := rpc.NewServer()
rpcServer.RegisterCodec(rpcJson.NewCodec(), "application/json")
service := NewRouteService(universe)
rpcServer.RegisterService(service, "Route")
http.Handle("/", rpcServer)
serverPort := os.Getenv("PORT")
if serverPort == "" {
serverPort = "3000"
}
log.Printf("Starting server on port <%s>...", serverPort)
http.ListenAndServe(":"+serverPort, nil)
}