dkforest

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

commit 4fb779e3c4a8317b62727a0251f02ca63d5e1224
parent 9b97cece312b254e7100ec516e780eaf28b5637c
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Sat,  9 Nov 2024 16:13:10 -0800

cleanup

Diffstat:
Mpkg/web/handlers/chess.go | 36+++++++++++++++++++++++++-----------
1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go @@ -659,16 +659,7 @@ Loop: // If we loaded the page and game was ongoing, we will stop the infinite loading page and display pgn if game.Outcome() != chess.NoOutcome && !gameLoadedOver { send(`<audio src="/public/sounds/chess/GenericNotify.ogg" autoplay></audio>`) - if game.Outcome() == chess.WhiteWon { - send(`<style>#piece_e1_winner, #piece_e8_loser { animation: 2s 0s forwards winner_anim }</style>`) - send(`<style>#piece_e1_winner, #piece_e8_loser { display: block !important; }</style>`) - } else if game.Outcome() == chess.BlackWon { - send(`<style>#piece_e8_winner, #piece_e1_loser { animation: 2s 0s forwards winner_anim }</style>`) - send(`<style>#piece_e8_winner, #piece_e1_loser { display: block !important; }</style>`) - } else if game.Outcome() == chess.Draw { - send(`<style>#piece_e8_draw, #piece_e1_draw { animation: 2s 0s forwards winner_anim }</style>`) - send(`<style>#piece_e8_draw, #piece_e1_draw { display: block !important; }</style>`) - } + send(getWinnerStyle(game.Outcome())) send(`<style>#outcome:after { content: "` + game.Outcome().String() + `" }</style>`) send(`<style>.gameover { display: none !important; }</style>`) send(`<div style="position: absolute; width: 200px; left: calc(50% - 100px); bottom: 20px">`) @@ -692,7 +683,8 @@ Loop: if game.Outcome() != chess.NoOutcome && gameLoadedOver { moveIdx := payload.MoveIdx if moveIdx != 0 { - pos := game.Positions()[moveIdx] + positions := game.Positions() + pos := positions[moveIdx] moves := game.Moves()[:moveIdx] lastMove := moves[len(moves)-1] piecesCache := interceptors.InitPiecesCache(moves) @@ -721,6 +713,7 @@ Loop: renderLastMove(&styles, *lastMove) renderBestMove(&styles, bestMove, isFlipped) renderShowVisiblePieceInPosition(&styles, &animationIdx, squareMap, piecesCache, piecesCache1, isFlipped) + renderWinnerBadges(&styles, moveIdx == len(positions)-1, game.Outcome()) send(styles.Build()) c.Response().Flush() @@ -849,6 +842,27 @@ func renderHideAllPieces(styles *StylesBuilder, piecesCache map[chess.Square]str styles.Appendf(`%s { display: none !important; }`, strings.Join(toHide, ", ")) } +func renderWinnerBadges(styles *StylesBuilder, isLastPosition bool, outcome chess.Outcome) { + styles.Append(`#piece_e8_draw, #piece_e1_draw, #piece_e8_winner, #piece_e1_loser, #piece_e8_loser, #piece_e1_winner { display: none !important; }`) + if isLastPosition { + styles.Append(getWinnerStyle(outcome)) + } +} + +func getWinnerStyle(outcome chess.Outcome) (out string) { + if outcome == chess.WhiteWon { + out += `<style>#piece_e1_winner, #piece_e8_loser { animation: 2s 0s forwards winner_anim }</style>` + out += `<style>#piece_e1_winner, #piece_e8_loser { display: block !important; }</style>` + } else if outcome == chess.BlackWon { + out += `<style>#piece_e8_winner, #piece_e1_loser { animation: 2s 0s forwards winner_anim }</style>` + out += `<style>#piece_e8_winner, #piece_e1_loser { display: block !important; }</style>` + } else if outcome == chess.Draw { + out += `<style>#piece_e8_draw, #piece_e1_draw { animation: 2s 0s forwards winner_anim }</style>` + out += `<style>#piece_e8_draw, #piece_e1_draw { display: block !important; }</style>` + } + return +} + func calcDisc(x1, y1, x2, y2 int) (d int, isDiag, isLine bool) { dx := int(math.Abs(float64(x2 - x1))) dy := int(math.Abs(float64(y2 - y1)))