commit 73b729f210c91c7dc57aec05e8ffc7d836295819
parent 2b0bced5bb7cb662f6691cb6dbac3011ffc11549
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Wed, 4 Oct 2023 03:29:59 -0400
simplify code
Diffstat:
3 files changed, 9 insertions(+), 16 deletions(-)
diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go
@@ -109,14 +109,7 @@ func ChessHandler(c echo.Context) error {
data.Error = "invalid username"
return c.Render(http.StatusOK, "chess", data)
}
- color := data.Color
- if color == "r" {
- color = utils.RandChoice([]string{"w", "b"})
- }
- if color == "b" {
- player1, player2 = player2, player1
- }
- if _, err := interceptors.ChessInstance.NewGame1("", config.GeneralRoomID, player1, player2); err != nil {
+ if _, err := interceptors.ChessInstance.NewGame1("", config.GeneralRoomID, player1, player2, data.Color); err != nil {
data.Error = err.Error()
return c.Render(http.StatusOK, "chess", data)
}
diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go
@@ -653,10 +653,16 @@ func (b *Chess) GetGames() (out []ChessGame) {
return
}
-func (b *Chess) NewGame1(roomKey string, roomID database.RoomID, player1, player2 database.User) (*ChessGame, error) {
+func (b *Chess) NewGame1(roomKey string, roomID database.RoomID, player1, player2 database.User, color string) (*ChessGame, error) {
if player1.ID == player2.ID {
return nil, errors.New("can't play yourself")
}
+ if color == "r" {
+ color = utils.RandChoice([]string{"w", "b"})
+ }
+ if color == "b" {
+ player1, player2 = player2, player1
+ }
key := uuid.New().String()
g, err := b.NewGame(key, player1, player2)
diff --git a/pkg/web/handlers/interceptors/slashInterceptor.go b/pkg/web/handlers/interceptors/slashInterceptor.go
@@ -1207,13 +1207,7 @@ func handleChessCmd(c *command.Command) (handled bool) {
c.Err = errors.New("invalid username")
return true
}
- if color == "r" {
- color = utils.RandChoice([]string{"w", "b"})
- }
- if color == "b" {
- player1, player2 = player2, player1
- }
- if _, err := ChessInstance.NewGame1(c.RoomKey, c.Room.ID, player1, player2); err != nil {
+ if _, err := ChessInstance.NewGame1(c.RoomKey, c.Room.ID, player1, player2, color); err != nil {
c.Err = err
return true
}