Record incomplete games

Resolves #20.
This commit is contained in:
Trevor Slocum 2024-07-01 12:35:46 -07:00
parent 28e2b6ccfd
commit 51c66bd680
5 changed files with 38 additions and 36 deletions

View file

@ -63,7 +63,7 @@ When no moves are possible, only the roll is specified.
##### Terminate
When a player resigns voluntarily or abandons an unfinished game, the player terminating the game early is indicated.
When a player resigns voluntarily or abandons an incomplete game, the player terminating the game early is indicated.
`1 t`

View file

@ -508,7 +508,7 @@ func recordGameResult(g *serverGame, winType int8, replay [][]byte) error {
dbLock.Lock()
defer dbLock.Unlock()
if db == nil || g.Started.IsZero() || g.Winner == 0 {
if db == nil || g.Started.IsZero() || g.Winner == 0 || len(g.replay) == 0 {
return nil
}
@ -523,7 +523,7 @@ func recordGameResult(g *serverGame, winType int8, replay [][]byte) error {
}
defer tx.Commit(context.Background())
_, err = tx.Exec(context.Background(), "INSERT INTO game (variant, started, ended, player1, account1, player2, account2, points, winner, wintype, replay) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)", g.Variant, g.Started.Unix(), ended.Unix(), g.Player1.Name, g.account1, g.Player2.Name, g.account2, g.Points, g.Winner, winType, bytes.Join(replay, []byte("\n")))
_, err = tx.Exec(context.Background(), "INSERT INTO game (variant, started, ended, player1, account1, player2, account2, points, winner, wintype, replay) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)", g.Variant, g.Started.Unix(), ended.Unix(), g.allowed1, g.account1, g.allowed2, g.account2, g.Points, g.Winner, winType, bytes.Join(replay, []byte("\n")))
if err != nil {
return err
}

View file

@ -587,6 +587,10 @@ func (g *serverGame) nextTurn(reroll bool) {
})
}
func (g *serverGame) addReplayHeader() {
g.replay = append([][]byte{[]byte(fmt.Sprintf("i %d %s %s %d %d %d %d %d %d", g.Started.Unix(), g.allowed1, g.allowed2, g.Points, g.Player1.Points, g.Player2.Points, g.Winner, g.DoubleValue, g.Variant))}, g.replay...)
}
func (g *serverGame) handleWin() bool {
if g.Winner == 0 {
return false
@ -633,7 +637,7 @@ func (g *serverGame) handleWin() bool {
}
}
g.replay = append([][]byte{[]byte(fmt.Sprintf("i %d %s %s %d %d %d %d %d %d", g.Started.Unix(), g.Player1.Name, g.Player2.Name, g.Points, g.Player1.Points, g.Player2.Points, g.Winner, winPoints, g.Variant))}, g.replay...)
g.addReplayHeader()
r1, r2, r3 := g.Roll1, g.Roll2, g.Roll3
if r2 > r1 {

View file

@ -351,7 +351,19 @@ func (s *server) handleGames() {
g.Winner = 2
}
}
err := recordMatchResult(g, matchTypeCasual)
g.addReplayHeader()
opponent := 1
if g.Winner == 1 {
opponent = 2
}
g.replay = append(g.replay, []byte(fmt.Sprintf("%d t", opponent)))
err := recordGameResult(g, 4, g.replay)
if err != nil {
log.Fatalf("failed to record game result: %s", err)
}
err = recordMatchResult(g, matchTypeCasual)
if err != nil {
log.Fatalf("failed to record match result: %s", err)
}

View file

@ -570,63 +570,40 @@ COMMANDS:
PlayerNumber: cmd.client.playerNumber,
Available: clientGame.LegalMoves(false),
}
var winner int8
addReplayHeader := func() {
clientGame.replay = append([][]byte{[]byte(fmt.Sprintf("i %d %s %s %d %d %d %d %d %d", clientGame.Started.Unix(), clientGame.Player1.Name, clientGame.Player2.Name, clientGame.Points, clientGame.Player1.Points, clientGame.Player2.Points, winner, clientGame.DoubleValue, clientGame.Variant))}, clientGame.replay...)
}
if gameState.MayDecline() {
winner = opponent.playerNumber
clientGame.Winner = opponent.playerNumber
clientGame.NextPartialTurn(opponent.playerNumber)
cmd.client.sendNotice(gotext.GetD(cmd.client.language, "Declined double offer."))
clientGame.opponent(cmd.client).sendNotice(fmt.Sprintf(gotext.GetD(clientGame.opponent(cmd.client).language, "%s declined double offer."), cmd.client.name))
addReplayHeader()
clientGame.replay = append(clientGame.replay, []byte(fmt.Sprintf("%d d %d 0", clientGame.Turn, clientGame.DoubleValue*2)))
} else if gameState.Turn == 0 || gameState.Turn != cmd.client.playerNumber {
cmd.client.sendNotice(gotext.GetD(cmd.client.language, "You may not resign until it is your turn."))
continue
} else {
winner = cmd.client.playerNumber
clientGame.NextPartialTurn(cmd.client.playerNumber)
clientGame.Winner = opponent.playerNumber
clientGame.NextPartialTurn(opponent.playerNumber)
cmd.client.sendNotice(gotext.GetD(cmd.client.language, "Resigned."))
clientGame.opponent(cmd.client).sendNotice(fmt.Sprintf(gotext.GetD(clientGame.opponent(cmd.client).language, "%s resigned."), cmd.client.name))
clientGame.replay = append(clientGame.replay, []byte(fmt.Sprintf("%d t", cmd.client.playerNumber)))
}
clientGame.Ended = time.Now()
var reset bool
if winner == 1 {
if clientGame.Winner == 1 {
clientGame.Player1.Points = clientGame.Player1.Points + clientGame.DoubleValue
if clientGame.Player1.Points >= clientGame.Points {
clientGame.Winner = 1
clientGame.Ended = time.Now()
} else {
reset = true
}
reset = clientGame.Player1.Points < clientGame.Points
} else {
clientGame.Player2.Points = clientGame.Player2.Points + clientGame.DoubleValue
if clientGame.Player2.Points >= clientGame.Points {
clientGame.Winner = 2
clientGame.Ended = time.Now()
} else {
reset = true
}
reset = clientGame.Player2.Points < clientGame.Points
}
addReplayHeader()
clientGame.addReplayHeader()
var winEvent *bgammon.EventWin
if clientGame.Winner != 0 {
winEvent = &bgammon.EventWin{
Points: clientGame.DoubleValue,
}
if clientGame.Winner == 1 {
winEvent.Player = clientGame.Player1.Name
} else {
winEvent.Player = clientGame.Player2.Name
}
err := recordGameResult(clientGame, 4, clientGame.replay)
if err != nil {
log.Fatalf("failed to record game result: %s", err)
@ -637,6 +614,15 @@ COMMANDS:
if err != nil {
log.Fatalf("failed to record match result: %s", err)
}
winEvent = &bgammon.EventWin{
Points: clientGame.DoubleValue,
}
if clientGame.Winner == 1 {
winEvent.Player = clientGame.Player1.Name
} else {
winEvent.Player = clientGame.Player2.Name
}
}
}