Skip to content

Commit

Permalink
Merge pull request #140 from glinscott/match_support
Browse files Browse the repository at this point in the history
Match support
  • Loading branch information
glinscott authored Mar 20, 2018
2 parents 5be9e3d + c1a3a23 commit 5900fde
Show file tree
Hide file tree
Showing 9 changed files with 783 additions and 110 deletions.
5 changes: 5 additions & 0 deletions go/src/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export GOPATH=/Users/gary/go:/Users/gary/Development/leela-chess/go
```
Here, I've set my system install of go as the first entry, and then the leela-chess/go directory as the second.

Pre-reqs:
```
go get -u github.com/notnil/chess
```

Then you just need to `go build`, and it should produce a `client` executable.

# Running
Expand Down
46 changes: 35 additions & 11 deletions go/src/client/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,33 @@ import (
"log"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
)

func postJson(httpClient *http.Client, uri string, target interface{}) error {
r, err := httpClient.Post(uri, "application/json", bytes.NewBuffer([]byte{}))
func postParams(httpClient *http.Client, uri string, data map[string]string, target interface{}) error {
var encoded string
if data != nil {
values := url.Values{}
for key, val := range data {
values.Set(key, val)
}
encoded = values.Encode()
}
r, err := httpClient.Post(uri, "application/x-www-form-urlencoded", strings.NewReader(encoded))
if err != nil {
return err
}
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
err = json.Unmarshal(b, target)
if err != nil {
log.Printf("Bad JSON from %s -- %s\n", uri, string(b))
if target != nil {
err = json.Unmarshal(b, target)
if err != nil {
log.Printf("Bad JSON from %s -- %s\n", uri, string(b))
}
}
return err
}
Expand Down Expand Up @@ -62,15 +75,19 @@ func BuildUploadRequest(uri string, params map[string]string, paramName, path st
}

type NextGameResponse struct {
Type string
TrainingId uint
NetworkId uint
Sha string
Type string
TrainingId uint
NetworkId uint
Sha string
CandidateSha string
Params string
Flip bool
MatchGameId uint
}

func NextGame(httpClient *http.Client, hostname string) (NextGameResponse, error) {
func NextGame(httpClient *http.Client, hostname string, params map[string]string) (NextGameResponse, error) {
resp := NextGameResponse{}
err := postJson(httpClient, hostname+"/next_game", &resp)
err := postParams(httpClient, hostname+"/next_game", params, &resp)

if len(resp.Sha) == 0 {
return resp, errors.New("Server gave back empty SHA")
Expand All @@ -79,6 +96,13 @@ func NextGame(httpClient *http.Client, hostname string) (NextGameResponse, error
return resp, err
}

func UploadMatchResult(httpClient *http.Client, hostname string, match_game_id uint, result int, pgn string, params map[string]string) error {
params["match_game_id"] = strconv.Itoa(int(match_game_id))
params["result"] = strconv.Itoa(result)
params["pgn"] = pgn
return postParams(httpClient, hostname+"/match_result", params, nil)
}

func DownloadNetwork(httpClient *http.Client, hostname string, networkPath string, sha string) error {
uri := hostname + fmt.Sprintf("/get_network?sha=%s", sha)
r, err := httpClient.Get(uri)
Expand Down
Loading

0 comments on commit 5900fde

Please sign in to comment.