-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
69 lines (56 loc) · 1.45 KB
/
main.go
File metadata and controls
69 lines (56 loc) · 1.45 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 main
import (
"flag"
"log"
"net/http"
"strconv"
"strings"
"github.com/socialgorithm/elon-server/domain"
"github.com/gorilla/websocket"
"github.com/socialgorithm/elon-server/render"
"github.com/socialgorithm/elon-server/simulator"
)
var upgrader = websocket.Upgrader{}
var simulation *simulator.Simulation
func main() {
var port = flag.String("port", "8080", "the port number to run on")
var test = flag.Bool("test", true, "whether the server should run in test mode")
simulation = simulator.CreateSimulation(1)
if *test {
go simulation.Start(*test)
}
log.Printf("Starting Elon Server on localhost:%s", *port)
http.HandleFunc("/", connectionHandler)
go http.ListenAndServe(":"+*port, nil)
render.Render(simulation)
}
func connectionHandler(w http.ResponseWriter, r *http.Request) {
connection, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
defer connection.Close()
for {
_, data, err := connection.ReadMessage()
if err != nil {
log.Println("Error:", err)
break
}
messageStr := string(data[:])
message := strings.Split(messageStr, " ")
switch message[0] {
case "start":
go simulation.Start(false)
break
case "input":
steering, _ := strconv.ParseFloat(message[1], 64)
throttle, _ := strconv.ParseFloat(message[2], 64)
carControlState := domain.CarControlState{
Steering: steering,
Throttle: throttle,
}
go simulation.Input(0, carControlState)
}
}
}