generated from BattlesnakeOfficial/starter-snake-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (115 loc) · 2.97 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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
)
type GameState struct {
Game Game `json:"game"`
Turn int `json:"turn"`
Board Board `json:"board"`
You Battlesnake `json:"you"`
}
type Game struct {
ID string `json:"id"`
Ruleset Ruleset `json:"ruleset"`
Timeout int32 `json:"timeout"`
}
type Ruleset struct {
Name string `json:"name"`
Version string `json:"version"`
}
type Board struct {
Height int `json:"height"`
Width int `json:"width"`
Food []Coord `json:"food"`
Snakes []Battlesnake `json:"snakes"`
// Used in non-standard game modes
Hazards []Coord `json:"hazards"`
}
type Battlesnake struct {
ID string `json:"id"`
Name string `json:"name"`
Health int32 `json:"health"`
Body []Coord `json:"body"`
Head Coord `json:"head"`
Length int32 `json:"length"`
Latency string `json:"latency"`
// Used in non-standard game modes
Shout string `json:"shout"`
Squad string `json:"squad"`
}
type Coord struct {
X int `json:"x"`
Y int `json:"y"`
}
// Response Structs
type BattlesnakeInfoResponse struct {
APIVersion string `json:"apiversion"`
Author string `json:"author"`
Color string `json:"color"`
Head string `json:"head"`
Tail string `json:"tail"`
}
type BattlesnakeMoveResponse struct {
Move string `json:"move"`
Shout string `json:"shout,omitempty"`
}
// HTTP Handlers
func HandleIndex(w http.ResponseWriter, r *http.Request) {
response := info()
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(response)
if err != nil {
log.Printf("ERROR: Failed to encode info response, %s", err)
}
}
func HandleStart(w http.ResponseWriter, r *http.Request) {
state := GameState{}
err := json.NewDecoder(r.Body).Decode(&state)
if err != nil {
log.Printf("ERROR: Failed to decode start json, %s", err)
return
}
start(state)
// Nothing to respond with here
}
func HandleMove(w http.ResponseWriter, r *http.Request) {
state := GameState{}
err := json.NewDecoder(r.Body).Decode(&state)
if err != nil {
log.Printf("ERROR: Failed to decode move json, %s", err)
return
}
response := move(state)
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(response)
if err != nil {
log.Printf("ERROR: Failed to encode move response, %s", err)
return
}
}
func HandleEnd(w http.ResponseWriter, r *http.Request) {
state := GameState{}
err := json.NewDecoder(r.Body).Decode(&state)
if err != nil {
log.Printf("ERROR: Failed to decode end json, %s", err)
return
}
end(state)
// Nothing to respond with here
}
// Main Entrypoint
func main() {
port := os.Getenv("PORT")
if len(port) == 0 {
port = "8080"
}
http.HandleFunc("/", HandleIndex)
http.HandleFunc("/start", HandleStart)
http.HandleFunc("/move", HandleMove)
http.HandleFunc("/end", HandleEnd)
log.Printf("Starting Battlesnake Server at http://0.0.0.0:%s...\n", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}