Skip to content

Commit

Permalink
Merge pull request #4 from marianogappa/parse_notation_wip
Browse files Browse the repository at this point in the history
Fixes build flag. Implements basic ParseNotation API method.
  • Loading branch information
marianogappa authored Mar 18, 2020
2 parents 2d761db + f3b7a5f commit 4035886
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
11 changes: 11 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ func (a API) DoAction(game InputGame, action InputAction) (OutputGame, OutputAct
}
return mapGameToOutputGame(parsedGame.doAction(parsedAction)), mapInternalActionToAction(parsedAction), nil
}

func (a API) ParseNotation(game InputGame, notationString string) (OutputGame, []OutputGameStep, error) {
parsedGame, err := a.parseGame(game)
if err != nil {
return OutputGame{}, []OutputGameStep{}, err
}

// TODO at the moment there only exists an algebraic notation parser
gameSteps, err := newNotationParserAlgebraic(characteristics{}).parse(parsedGame, notationString)
return mapGameToOutputGame(parsedGame), mapGameStepsToOutputGameSteps(gameSteps), err
}
18 changes: 18 additions & 0 deletions api/api_entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ type OutputAction struct {
CapturedPieceType string `json:"capturedPieceType"`
}

type OutputGameStep struct {
Game OutputGame `json:"game"`
Action OutputAction `json:"action"`
ActionString string `json:"actionString"`
}

func mapGameToOutputGame(g game) OutputGame {
var o OutputGame

Expand Down Expand Up @@ -167,3 +173,15 @@ func mapInternalActionToAction(a action) OutputAction {
CapturedPieceType: a.capturedPiece.pieceType.String(),
}
}

func mapGameStepsToOutputGameSteps(gss []gameStep) []OutputGameStep {
ogs := make([]OutputGameStep, len(gss))
for _, gs := range gss {
ogs = append(ogs, OutputGameStep{
Game: mapGameToOutputGame(gs.g),
Action: mapInternalActionToAction(gs.a),
ActionString: gs.s,
})
}
return ogs
}
44 changes: 44 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,50 @@ func handleCliDoAction(flagDoAction *string) {
fmt.Println(string(byts))
}

func handleServerParseNotation(w http.ResponseWriter, r *http.Request) {
type args struct {
Game api.InputGame `json:"game"`
NotationString string `json:"notationString"`
}
var input args
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
fmt.Fprintln(w, formatError(err))
return
}
defer r.Body.Close()
outputGame, outputGameSteps, err := a.ParseNotation(input.Game, input.NotationString)
if err != nil {
fmt.Fprintln(w, formatError(err))
return
}
type out struct {
Game api.OutputGame `json:"game"`
OutputGameSteps []api.OutputGameStep `json:"outputGameSteps"`
}
json.NewEncoder(w).Encode(out{outputGame, outputGameSteps})
}

func handleCliParseNotation(flagParseNotation *string) {
type args struct {
Game api.InputGame `json:"game"`
NotationString string `json:"notationString"`
}
var input args
if err := json.Unmarshal([]byte(*flagParseNotation), &input); err != nil {
mustCliFatal(err)
}
outputGame, outputGameSteps, err := a.ParseNotation(input.Game, input.NotationString)
if err != nil {
mustCliFatal(err)
}
type out struct {
Game api.OutputGame `json:"game"`
OutputGameSteps []api.OutputGameStep `json:"outputGameSteps"`
}
byts, _ := json.Marshal(out{outputGame, outputGameSteps})
fmt.Println(string(byts))
}

func mustCliFatal(err error) {
fmt.Println(formatError(err))
os.Exit(1)
Expand Down
16 changes: 9 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//+build !js,wasm
// +build !js

package main

Expand All @@ -9,20 +9,20 @@ import (
)

var (
flagServe = flag.Int("serve", 0, "Start a server on the specified port.")
flagDefaultGame = flag.Bool("defaultGame", false, "Default API call. Returns a default game.")
flagParseGame = flag.String("parseGame", "", "ParseGame API call. Requires a JSON string with arguments. Please review spec.")
flagDoAction = flag.String("doAction", "", "DoAction API call. Requires a JSON string with arguments. Please review spec.")
flagServe = flag.Int("serve", 0, "Start a server on the specified port.")
flagDefaultGame = flag.Bool("defaultGame", false, "Default API call. Returns a default game.")
flagParseGame = flag.String("parseGame", "", "ParseGame API call. Requires a JSON string with arguments. Please review spec.")
flagDoAction = flag.String("doAction", "", "DoAction API call. Requires a JSON string with arguments. Please review spec.")
flagParseNotation = flag.String("parseNotation", "", "ParseNotation API call. Requires a JSON string with arguments. Please review spec.")
)

// api.InputGame{FENString: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"},

func main() {
flag.Parse()

http.HandleFunc("/parseGame", handleServerParseGame)
http.HandleFunc("/defaultGame", handleServerDefaultGame)
http.HandleFunc("/doAction", handleServerDoAction)
http.HandleFunc("/parseNotation", handleServerParseNotation)

switch {
case *flagServe != 0:
Expand All @@ -33,5 +33,7 @@ func main() {
handleCliParseGame(flagParseGame)
case *flagDoAction != "":
handleCliDoAction(flagDoAction)
case *flagParseNotation != "":
handleCliParseNotation(flagParseNotation)
}
}

0 comments on commit 4035886

Please sign in to comment.