dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 2b0bced5bb7cb662f6691cb6dbac3011ffc11549
parent 31a334c67af02a4aa7802b37529b607630df2696
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Wed,  4 Oct 2023 03:16:37 -0400

allow to pick color from chess UI form page

Diffstat:
Mpkg/web/handlers/chess.go | 11++++++++++-
Mpkg/web/handlers/data.go | 1+
Mpkg/web/public/views/pages/chess.gohtml | 11+++++++++--
3 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go @@ -102,12 +102,21 @@ func ChessHandler(c echo.Context) error { if c.Request().Method == http.MethodPost { data.Username = database.Username(c.Request().PostFormValue("username")) + data.Color = c.Request().PostFormValue("color") + player1 := *authUser player2, err := db.GetUserByUsername(data.Username) if err != nil { data.Error = "invalid username" return c.Render(http.StatusOK, "chess", data) } - if _, err := interceptors.ChessInstance.NewGame1("", config.GeneralRoomID, *authUser, player2); err != nil { + 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 { data.Error = err.Error() return c.Render(http.StatusOK, "chess", data) } diff --git a/pkg/web/handlers/data.go b/pkg/web/handlers/data.go @@ -925,6 +925,7 @@ type chessData struct { Games []interceptors.ChessGame Error string Username database.Username + Color string } type powHelperData struct { diff --git a/pkg/web/public/views/pages/chess.gohtml b/pkg/web/public/views/pages/chess.gohtml @@ -4,10 +4,17 @@ <div class="container"> <h3>Games</h3> <p>You can create a chess game with someone by using the <code>/chess username</code> command.</p> - <form method="post" style="width: 400px;"> + <form method="post" style="width: 500px;"> <input type="hidden" name="csrf" value="{{ .CSRF }}" /> <div class="input-group"> - <input type="text" name="username" value="{{ .Data.Username }}" placeholder="opponent username" class="form-control form-control-sm{{ if .Data.Error }} is-invalid{{ end }}" /> + <input style="width: 200px;" type="text" name="username" value="{{ .Data.Username }}" placeholder="opponent username" class="form-control form-control-sm{{ if .Data.Error }} is-invalid{{ end }}" /> + + <select name="color" class="form-control form-control-sm"> + <option value="w"{{ if eq .Data.Color "w" }}} selected{{ end }}>White</option> + <option value="b"{{ if eq .Data.Color "b" }}} selected{{ end }}>Black</option> + <option value="r"{{ if eq .Data.Color "r" }}} selected{{ end }}>Random</option> + </select> + <div class="input-group-append"> <button class="btn btn-sm btn-primary">Create chess game</button> </div>