dkforest

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

commit 17d63b1fce242475a54959586a4c76ef422eb41b
parent f82341f31a4db67d7fec7ff2a78cddf14f65b363
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Fri, 16 Jun 2023 22:48:23 -0700

dedup code

Diffstat:
Mpkg/web/handlers/chess.go | 51++++++++++++++++++++-------------------------------
1 file changed, 20 insertions(+), 31 deletions(-)

diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go @@ -497,6 +497,20 @@ func initPiecesCache(game *chess.Game) map[string]chess.Square { return piecesCache } +const animationMs = 400 + +func animate(s1, s2 chess.Square, id string, isFlipped bool, animationIdx *int, styles *StylesBuilder) { + x1, y1 := squareCoord(s1, isFlipped) + x2, y2 := squareCoord(s2, isFlipped) + *animationIdx++ + animationName := fmt.Sprintf("move_anim_%d", *animationIdx) + keyframes := "@keyframes %s {" + + "from { left: calc(%d*12.5%%); top: calc(%d*12.5%%); }" + + " to { left: calc(%d*12.5%%); top: calc(%d*12.5%%); } }\n" + styles.Appendf(keyframes, animationName, x1, y1, x2, y2) + styles.Appendf("#%s { animation: %s %dms forwards; }\n", id, animationName, animationMs) +} + func ChessGameHandler(c echo.Context) error { debugChess := true @@ -695,21 +709,8 @@ Loop: } var styles StylesBuilder - const animationMs = 400 - - animate := func(s1, s2 chess.Square, id string) { - x1, y1 := squareCoord(s1, isFlipped) - x2, y2 := squareCoord(s2, isFlipped) - animationIdx++ - animationName := fmt.Sprintf("move_anim_%d", animationIdx) - keyframes := "@keyframes %s {" + - "from { left: calc(%d*12.5%%); top: calc(%d*12.5%%); }" + - " to { left: calc(%d*12.5%%); top: calc(%d*12.5%%); } }" - styles.Appendf(keyframes, animationName, x1, y1, x2, y2) - styles.Appendf(`#%s { animation: %s %dms forwards; }`, id, animationName, animationMs) - } - animate(payload.Move.S1(), payload.Move.S2(), payload.IDStr1) + animate(payload.Move.S1(), payload.Move.S2(), payload.IDStr1, isFlipped, &animationIdx, &styles) if payload.Move.Promo() != chess.NoPieceType || payload.IDStr2 != "" { // Ensure the capturing piece is draw above the one being captured @@ -739,15 +740,15 @@ Loop: // Animate rook during castle if payload.Move.HasTag(chess.KingSideCastle) { if payload.Move.S1() == chess.E1 { - animate(chess.H1, chess.F1, interceptors.WhiteKingSideRookID) + animate(chess.H1, chess.F1, interceptors.WhiteKingSideRookID, isFlipped, &animationIdx, &styles) } else if payload.Move.S1() == chess.E8 { - animate(chess.H8, chess.F8, interceptors.BlackKingSideRookID) + animate(chess.H8, chess.F8, interceptors.BlackKingSideRookID, isFlipped, &animationIdx, &styles) } } else if payload.Move.HasTag(chess.QueenSideCastle) { if payload.Move.S1() == chess.E1 { - animate(chess.A1, chess.D1, interceptors.WhiteQueenSideRookID) + animate(chess.A1, chess.D1, interceptors.WhiteQueenSideRookID, isFlipped, &animationIdx, &styles) } else if payload.Move.S1() == chess.E8 { - animate(chess.A8, chess.D8, interceptors.BlackQueenSideRookID) + animate(chess.A8, chess.D8, interceptors.BlackQueenSideRookID, isFlipped, &animationIdx, &styles) } } // En passant @@ -767,18 +768,6 @@ Loop: } func renderShowVisiblePieceInPosition(styles *StylesBuilder, animationIdx *int, visiblePieces map[chess.Square]struct{}, squareMap map[chess.Square]chess.Piece, piecesCache map[chess.Square]string, piecesCache1 map[string]chess.Square, isFlipped bool) { - animate := func(s1, s2 chess.Square, id string) { - x1, y1 := squareCoord(s1, isFlipped) - x2, y2 := squareCoord(s2, isFlipped) - *animationIdx++ - animationName := fmt.Sprintf("move_anim_%d", *animationIdx) - keyframes := "@keyframes %s {" + - "from { left: calc(%d*12.5%%); top: calc(%d*12.5%%); }" + - " to { left: calc(%d*12.5%%); top: calc(%d*12.5%%); } }\n" - styles.Appendf(keyframes, animationName, x1, y1, x2, y2) - styles.Appendf("#%s { animation: %s %dms forwards; }\n", id, animationName, 400) - } - for newSq := range visiblePieces { sqID := piecesCache[newSq] // Get ID of piece on square newSq currentSq := piecesCache1[sqID] // Get current square location of the piece @@ -794,7 +783,7 @@ func renderShowVisiblePieceInPosition(styles *StylesBuilder, animationIdx *int, bStyle += "}\n" styles.Append(bStyle) if currentSq != newSq { - animate(currentSq, newSq, sqID) // Move piece from current square to the new square where we want it to be + animate(currentSq, newSq, sqID, isFlipped, animationIdx, styles) // Move piece from current square to the new square where we want it to be } //animate(currentSq, newSq, sqID) piecesCache1[sqID] = newSq // Update cache of location of the piece