-
Notifications
You must be signed in to change notification settings - Fork 0
/
nextMove.go
71 lines (51 loc) · 1.44 KB
/
nextMove.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
package web
import (
"fmt"
"net/http"
"github.com/thijzert/chesseract/chesseract"
)
var NextMoveHandler nextMoveHandler
type nextMoveHandler struct{}
type nextMoveRequest struct {
NextIndex int
}
// The NextMoveResponse wraps a NextMoveHandler API response
type NextMoveResponse struct {
Move *chesseract.Move `json:"move,omitempty"`
}
func (nextMoveHandler) handleNextMove(p Provider, r nextMoveRequest) (NextMoveResponse, error) {
var rv NextMoveResponse
g, err := p.Game()
if err != nil {
return rv, err
}
if len(g.Match.Moves) > r.NextIndex {
rv.Move = &g.Match.Moves[r.NextIndex]
}
// TODO: Hang around for a bit, and see if a move gets added.
// We could probably use some sort of observer-with-goroutines-and-chans
// But until then, let's not get too fancy and just span the server.
return rv, nil
}
func (nextMoveHandler) DecodeRequest(r *http.Request) (Request, error) {
var rv nextMoveRequest
var err error
_, err = fmt.Sscanf(r.FormValue("nextindex"), "%d", &rv.NextIndex)
if err != nil {
return rv, err
}
if rv.NextIndex < 0 {
return rv, errBadRequest("", "")
}
return rv, err
}
// Below: boilerplate code
func (h nextMoveHandler) HandleRequest(p Provider, r Request) (Response, error) {
req, ok := r.(nextMoveRequest)
if !ok {
return withError(errWrongRequestType{})
}
return h.handleNextMove(p, req)
}
func (nextMoveRequest) FlaggedAsRequest() {}
func (NextMoveResponse) FlaggedAsResponse() {}