v0.13.1 concession management and record, clients update, version report
test / test (push) Canceled after 0s
test / test (push) Canceled after 0s
This commit is contained in:
1 parent
f1ac4cbbe7
commit
6e8d80c788
30 files changed
+704
-81
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"chess/internal/server/chess"
|
||||
"chess/internal/server/core"
|
||||
@@ -69,6 +70,9 @@ func BuildPGN(record *storage.GameRecord, moves []storage.MoveRecord, ply int) (
|
||||
if complete {
|
||||
result = chess.ResultToken(record.Result)
|
||||
comment = Describe(record.Result, record.Termination)
|
||||
if note := continuedAfter(record, len(moves)); note != "" {
|
||||
comment = strings.TrimSpace(note + " " + comment)
|
||||
}
|
||||
}
|
||||
start := record.StartTimeUTC.UTC()
|
||||
tags := []chess.Tag{
|
||||
@@ -105,6 +109,29 @@ func BuildPGN(record *storage.GameRecord, moves []storage.MoveRecord, ply int) (
|
||||
}, nil
|
||||
}
|
||||
|
||||
// continuedAfter notes a resignation or agreed draw that an undo took back,
|
||||
// such as "White resigned at ply 24; play continued."; empty when there was
|
||||
// none or it is still the game's result.
|
||||
func continuedAfter(record *storage.GameRecord, plies int) string {
|
||||
if record.ConcessionResult == "" {
|
||||
return ""
|
||||
}
|
||||
if record.ConcessionResult == record.Result &&
|
||||
record.ConcessionTermination == record.Termination && record.ConcessionPly == plies {
|
||||
return ""
|
||||
}
|
||||
var event string
|
||||
switch record.ConcessionResult {
|
||||
case "white_wins":
|
||||
event = "Black resigned"
|
||||
case "black_wins":
|
||||
event = "White resigned"
|
||||
default:
|
||||
event = "A draw was agreed"
|
||||
}
|
||||
return fmt.Sprintf("%s at ply %d; play continued.", event, record.ConcessionPly)
|
||||
}
|
||||
|
||||
// Describe returns a sentence for a stored result and termination, such as
|
||||
// "Black wins by resignation." or "Draw by threefold repetition."; empty for
|
||||
// an unfinished game.
|
||||
|
||||
@@ -154,3 +154,24 @@ func TestBuildPGN(t *testing.T) {
|
||||
t.Error("ply beyond the line accepted")
|
||||
}
|
||||
}
|
||||
|
||||
// A resignation taken back by an undo is noted before the live result.
|
||||
func TestBuildPGNNotesContinuedConcession(t *testing.T) {
|
||||
moves := stored(t, chess.StartFEN, "e2e4", "e7e5", "d2d4")
|
||||
rec := record(chess.StartFEN, "", "")
|
||||
rec.ConcessionResult, rec.ConcessionTermination, rec.ConcessionPly = "black_wins", "resignation", 2
|
||||
|
||||
pgn, err := BuildPGN(rec, moves, -1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasSuffix(pgn.Text, "\n\n1. e4 e5 2. d4 { White resigned at ply 2; play continued. } *\n") {
|
||||
t.Errorf("continued game:\n%s", pgn.Text)
|
||||
}
|
||||
|
||||
// The concession that is still the result needs no note.
|
||||
rec.Result, rec.Termination, rec.ConcessionPly = "black_wins", "resignation", 3
|
||||
if pgn, _ = BuildPGN(rec, moves, -1); strings.Contains(pgn.Text, "continued") {
|
||||
t.Errorf("standing resignation noted as continued:\n%s", pgn.Text)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user