commit 9b92d684c3e963e9403bf6ec5be4222d07e942ea
parent 53008220edde72cf5480a1800bb983e51992e57c
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Mon, 12 Jun 2023 16:40:31 -0700
confirm popup for resign
Diffstat:
2 files changed, 54 insertions(+), 29 deletions(-)
diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go
@@ -115,14 +115,59 @@ func ChessGameFormHandler(c echo.Context) error {
csrf, _ := c.Get("csrf").(string)
authUser := c.Get("authUser").(*database.User)
g := interceptors.ChessInstance.GetGame(key)
+ game := g.Game
isFlipped := authUser.ID == g.Player2.ID
- if g.Game.Outcome() != chess.NoOutcome {
+ if game.Outcome() != chess.NoOutcome {
return c.NoContent(http.StatusOK)
}
+ if c.Request().Method == http.MethodPost {
+ if !g.IsPlayer(authUser.ID) {
+ return c.Redirect(http.StatusFound, c.Request().Referer())
+ }
+
+ btnSubmit := c.Request().PostFormValue("btn_submit")
+ if btnSubmit == "resign-cancel" {
+ return c.Redirect(http.StatusFound, c.Request().Referer())
+
+ } else if btnSubmit == "resign" {
+
+ htmlTmpl := `<form method="post">
+ <input type="hidden" name="csrf" value="{{ .CSRF }}" />
+ <div style="position: fixed; top: calc(50% - 80px); left: calc(50% - 100px); width: 200px; height: 80px; background-color: #444; border-radius: 5px;">
+ <div style="padding: 10px;">
+ <span style="margin-bottom: 5px; display: block; color: #eee;">Confirm resign:</span>
+ <button type="submit" name="btn_submit" value="resign-confirm" style="background-color: #aaa;">Confirm resign</button>
+ <button type="submit" name="btn_submit" value="resign-cancel" style="background-color: #aaa;">Cancel</button>
+ </div>
+ </div>
+</form>`
+
+ data := map[string]any{
+ "CSRF": csrf,
+ }
+
+ var buf bytes.Buffer
+ _ = utils.Must(template.New("").Parse(htmlTmpl)).Execute(&buf, data)
+
+ return c.HTML(http.StatusOK, buf.String())
+
+ } else if btnSubmit == "resign-confirm" {
+ resignColor := utils.Ternary(isFlipped, chess.Black, chess.White)
+ game.Resign(resignColor)
+ interceptors.ChessPubSub.Pub(key, interceptors.ChessMove{})
+
+ } else {
+ if err := interceptors.ChessInstance.SendMove(key, authUser.ID, g, c); err != nil {
+ logrus.Error(err)
+ }
+ }
+ return c.Redirect(http.StatusFound, c.Request().Referer())
+ }
+
htmlTmpl := cssReset + interceptors.ChessCSS + `
-<form method="post" action="/chess/{{ .Key }}">
+<form method="post">
<input type="hidden" name="csrf" value="{{ .CSRF }}" />
<table class="newBoard">
{{ range $row := .Rows }}
@@ -138,7 +183,8 @@ func ChessGameFormHandler(c echo.Context) error {
{{ end }}
</table>
<div style="width: 100%; display: flex; margin: 5px 0;">
- <div><button type="submit" style="background-color: #aaa;">Move</button></div>
+ <div><button type="submit" name="btn_submit" style="background-color: #aaa;">Move</button></div>
+ <div><button type="submit" name="btn_submit" value="resign" style="background-color: #aaa; margin-left: 50px;">Resign</button></div>
<div style="margin-left: auto;">
<span style="color: #aaa; margin-left: 20px;">Promo:</span>
<select name="promotion" style="background-color: #aaa;">
@@ -193,23 +239,6 @@ func ChessGameHandler(c echo.Context) error {
isFlipped := authUser.ID == g.Player2.ID
- if c.Request().Method == http.MethodPost {
- if !g.IsPlayer(authUser.ID) {
- return c.Redirect(http.StatusFound, c.Request().Referer())
- }
- msg := c.Request().PostFormValue("message")
- if msg == "resign" {
- resignColor := utils.Ternary(isFlipped, chess.Black, chess.White)
- game.Resign(resignColor)
- interceptors.ChessPubSub.Pub(key, interceptors.ChessMove{})
- } else {
- if err := interceptors.ChessInstance.SendMove(key, authUser.ID, g, c); err != nil {
- logrus.Error(err)
- }
- }
- return c.Redirect(http.StatusFound, c.Request().Referer())
- }
-
isSpectator := !g.IsPlayer(authUser.ID)
if isSpectator && c.QueryParam("r") != "" {
isFlipped = true
@@ -321,9 +350,11 @@ Loop:
}
if authUser.ChessSoundsEnabled {
- isCapture := payload.Move.HasTag(chess.Capture) || payload.Move.HasTag(chess.EnPassant)
- audioFile := utils.Ternary(isCapture, "Capture.ogg", "Move.ogg")
- send(`<audio src="/public/sounds/chess/` + audioFile + `" autoplay></audio>`)
+ if game.Method() != chess.Resignation {
+ isCapture := payload.Move.HasTag(chess.Capture) || payload.Move.HasTag(chess.EnPassant)
+ audioFile := utils.Ternary(isCapture, "Capture.ogg", "Move.ogg")
+ send(`<audio src="/public/sounds/chess/` + audioFile + `" autoplay></audio>`)
+ }
}
var styles StylesBuilder
diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go
@@ -345,12 +345,6 @@ func (g *ChessGame) drawPlayerCard(key string, isBlack, isSpectator, isYourTurn,
</tr>
<tr>
<td>
- {{ if and (not .IsSpectator) (not .GameOver) }}
- <form method="post" style="display: inline-block;" class="gameover">
- <input type="hidden" name="message" value="resign" />
- <button type="submit" style="background-color: #aaa; margin: 5px 0;">Resign</button>
- </form>
- {{ end }}
<span style="color: #eee; display: inline-block;">
(<span id="white-advantage" style="color: #888;" title="white advantage"><span class="score"></span></span> |
<span id="black-advantage" style="color: #888;" title="black advantage"><span class="score"></span></span>)