From ae5696152bb8beb8f7873ddf846bf3b5b68cce00 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Tue, 19 Dec 2023 18:14:10 -0800 Subject: [PATCH] Send replay of current match when no match is specified --- pkg/server/server.go | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/pkg/server/server.go b/pkg/server/server.go index c26b62d..65e315e 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -677,7 +677,7 @@ COMMANDS: clientGame := s.gameByClient(cmd.client) if clientGame != nil && clientGame.client1 != cmd.client && clientGame.client2 != cmd.client { switch keyword { - case bgammon.CommandHelp, "h", bgammon.CommandJSON, bgammon.CommandList, "ls", bgammon.CommandBoard, "b", bgammon.CommandLeave, "l", bgammon.CommandDisconnect, bgammon.CommandPong: + case bgammon.CommandHelp, "h", bgammon.CommandJSON, bgammon.CommandList, "ls", bgammon.CommandBoard, "b", bgammon.CommandLeave, "l", bgammon.CommandReplay, bgammon.CommandDisconnect, bgammon.CommandPong: // These commands are allowed to be used by spectators. default: cmd.client.sendNotice("Command ignored: You are spectating this match.") @@ -1662,22 +1662,31 @@ COMMANDS: } _ = setAccountSetting(cmd.client.account, name, value) case bgammon.CommandReplay: + var ( + id int + replay []byte + err error + ) if len(params) == 0 { - cmd.client.sendNotice("Please specify the game as follows: replay ") - continue + if clientGame == nil || clientGame.Winner == 0 { + cmd.client.sendNotice("Please specify the game as follows: replay ") + continue + } + id = -1 + replay = bytes.Join(clientGame.replay, []byte("\n")) + } else { + id, err = strconv.Atoi(string(params[0])) + if err != nil || id < 0 { + cmd.client.sendNotice("Invalid replay ID provided.") + continue + } + replay, err = replayByID(id) + if err != nil { + cmd.client.sendNotice("Invalid replay ID provided.") + continue + } } - - id, err := strconv.Atoi(string(params[0])) - if err != nil || id < 0 { - cmd.client.sendNotice("Invalid replay ID provided.") - continue - } - - replay, err := replayByID(id) - if err != nil { - cmd.client.sendNotice("Invalid replay ID provided.") - continue - } else if len(replay) == 0 { + if len(replay) == 0 { cmd.client.sendNotice("No replay was recorded for that game.") continue }