-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
327 lines (271 loc) · 9.04 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/notnil/chess"
"github.com/notnil/chess/uci"
"github.com/spf13/viper"
)
// Config struct to hold configurable parameters
type Config struct {
EnginePath string
ServerAddr string
MoveTime time.Duration
}
var (
uciEngine *uci.Engine
game *chess.Game
config Config
)
func init() {
// Initialize viper for configuration
// viper.SetDefault("engine_path", "stockfish")
viper.SetDefault("server_addr", ":8080")
viper.SetDefault("move_time", 10)
viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("yaml") // or viper.SetConfigType("YAML")
viper.AddConfigPath(".") // optionally look for config in the working directory
if err := viper.ReadInConfig(); err != nil {
log.Printf("Error reading config file, using defaults: %v", err)
}
// Load configuration into the config struct
config = Config{
EnginePath: viper.GetString("engine_path"),
ServerAddr: viper.GetString("server_addr"),
MoveTime: time.Duration(viper.GetInt("move_time")) * time.Millisecond,
}
// Start the UCI engine in a background process
engine, err := uci.New(config.EnginePath)
if err != nil {
log.Fatalf("Failed to create UCI engine: %v", err)
}
// Set UCI options as per config
options := viper.GetStringMap("uci_options")
for key, value := range options {
if err := engine.Run(uci.CmdSetOption{Name: key, Value: fmt.Sprintf("%v", value)}); err != nil {
log.Fatalf("Failed to set UCI option %s: %v", key, err)
}
}
if err := engine.Run(uci.CmdUCI, uci.CmdIsReady, uci.CmdUCINewGame); err != nil {
log.Fatalf("Failed to run UCI engine: %v", err)
}
uciEngine = engine
game = chess.NewGame() // Initialize the game globally
}
func handleMove(w http.ResponseWriter, r *http.Request) {
uciMove := r.URL.Query().Get("uci")
sanMove := r.URL.Query().Get("san")
var userMove *chess.Move
var err error
if uciMove != "" {
// Try to decode the move as UCI
userMove, err = chess.UCINotation{}.Decode(game.Position(), uciMove)
if err != nil {
http.Error(w, "Invalid UCI move", http.StatusBadRequest)
return
}
} else if sanMove != "" {
// Try to decode the move as SAN
userMove, err = chess.AlgebraicNotation{}.Decode(game.Position(), sanMove)
if err != nil {
http.Error(w, "Invalid SAN move", http.StatusBadRequest)
return
}
} else {
http.Error(w, "Move not specified", http.StatusBadRequest)
return
}
if err := game.Move(userMove); err != nil {
http.Error(w, "Failed to apply user's move", http.StatusInternalServerError)
return
}
// Set the position after user's move
cmdPos := uci.CmdPosition{Position: game.Position()}
if err := uciEngine.Run(cmdPos); err != nil {
http.Error(w, "Failed to set position", http.StatusInternalServerError)
return
}
// Start the engine to calculate the best move
cmdGo := uci.CmdGo{MoveTime: config.MoveTime}
if err := uciEngine.Run(cmdGo); err != nil {
http.Error(w, "Failed to start engine search", http.StatusInternalServerError)
return
}
// Get the best move
bestMove := uciEngine.SearchResults().BestMove
if err := game.Move(bestMove); err != nil {
http.Error(w, "Failed to apply best move", http.StatusInternalServerError)
return
}
// Respond with the best move as JSON
w.Header().Set("Content-Type", "application/json")
legalMoves := game.Position().ValidMoves()
legalMovesStr := make([]string, len(legalMoves))
for i, move := range legalMoves {
legalMovesStr[i] = move.String()
}
// Get the current game moves in SAN format
moveHistory := game.MoveHistory()
sanMoves := make([]string, len(moveHistory))
for i, move := range moveHistory {
sanMoves[i] = chess.AlgebraicNotation{}.Encode(move.PrePosition, move.Move)
}
response := map[string]interface{}{
"best_move": bestMove.String(),
"fen": game.Position().String(),
"legal_moves": legalMovesStr,
"info": uciEngine.SearchResults().Info,
"pgn": sanMoves,
}
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, "Failed to encode JSON response", http.StatusInternalServerError)
return
}
}
func handleShow(w http.ResponseWriter, r *http.Request) {
// Get the current position in FEN format
ascii := game.Position().Board().Draw()
// Respond with the current board position in ascii
fmt.Fprintf(w, "Current position: %s", ascii)
}
func handleFen(w http.ResponseWriter, r *http.Request) {
// Get the current position in FEN format
fen := game.Position().String()
// Get the legal moves
legalMoves := game.Position().ValidMoves()
legalMovesStr := make([]string, len(legalMoves))
for i, move := range legalMoves {
legalMovesStr[i] = move.String()
}
// Get the current game moves in SAN format
moveHistory := game.MoveHistory()
sanMoves := make([]string, len(moveHistory))
for i, move := range moveHistory {
sanMoves[i] = chess.AlgebraicNotation{}.Encode(move.PrePosition, move.Move)
}
// Set the content type to application/json
w.Header().Set("Content-Type", "application/json")
// Create a response map including legal moves and SAN moves
response := map[string]interface{}{
"fen": fen,
"legal_moves": legalMovesStr,
"pgn": sanMoves,
}
// Encode the response as JSON and write it to the response writer
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, "Failed to encode JSON response", http.StatusInternalServerError)
return
}
}
func handleNewGame(w http.ResponseWriter, r *http.Request) {
// Initialize a new game
game = chess.NewGame()
// Set the content type to application/json
w.Header().Set("Content-Type", "application/json")
// Create a response map
response := map[string]string{"message": "New game started"}
// Encode the response as JSON and write it to the response writer
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, "Failed to encode JSON response", http.StatusInternalServerError)
return
}
}
func handleUCIInfo(w http.ResponseWriter, r *http.Request) {
// Get the UCI engine information
info := uciEngine.ID()
options := uciEngine.Options()
// Create a JSON response with the UCI engine information and options
response := map[string]interface{}{
"info": info,
"options": options,
}
// Set the content type to application/json
w.Header().Set("Content-Type", "application/json")
// Encode the response as JSON and write it to the response writer
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, "Failed to encode JSON response", http.StatusInternalServerError)
return
}
}
func handlePlay(w http.ResponseWriter, r *http.Request) {
// Set the content type to text/html
w.Header().Set("Content-Type", "text/html")
// Open the play.html file
file, err := os.Open("play.html")
if err != nil {
http.Error(w, "Failed to open play.html", http.StatusInternalServerError)
return
}
defer file.Close()
// Copy the contents of the file to the response writer
if _, err := io.Copy(w, file); err != nil {
http.Error(w, "Failed to serve play.html", http.StatusInternalServerError)
return
}
}
func handleImg(w http.ResponseWriter, r *http.Request) {
// Set the content type to image/png
w.Header().Set("Content-Type", "image/png")
// Extract the piece parameter from the query
piece := r.URL.Query().Get("piece")
if piece == "" {
http.Error(w, "Piece not specified", http.StatusBadRequest)
return
}
// Construct the file path for the requested piece image
filePath := fmt.Sprintf("img/%s", piece)
// Open the image file
file, err := os.Open(filePath)
if err != nil {
http.Error(w, "Failed to open image file", http.StatusInternalServerError)
return
}
defer file.Close()
// Copy the contents of the image file to the response writer
if _, err := io.Copy(w, file); err != nil {
http.Error(w, "Failed to serve image", http.StatusInternalServerError)
return
}
}
func main() {
http.HandleFunc("/", handlePlay)
http.HandleFunc("/move", handleMove)
http.HandleFunc("/show", handleShow)
http.HandleFunc("/fen", handleFen)
http.HandleFunc("/new", handleNewGame)
http.HandleFunc("/info", handleUCIInfo)
http.HandleFunc("/img", handleImg)
server := &http.Server{Addr: config.ServerAddr}
// Channel to listen for interrupt or terminate signals
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
// Run the server in a goroutine
go func() {
log.Printf("Starting server on %s", config.ServerAddr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Could not listen on %s: %v\n", config.ServerAddr, err)
}
}()
// Block until a signal is received
<-stop
log.Println("Shutting down server...")
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Attempt a graceful shutdown
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Server forced to shutdown: %v", err)
}
if err := uciEngine.Close(); err != nil {
log.Printf("Error quitting UCI engine: %v", err)
}
log.Println("Server exiting")
}