dkforest

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

commit 7acef0e0b7d8992186051bcde9a41e8195a5da23
parent a641956e9b11f6d07de0d89b412a35159bfbbd8e
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon, 12 Jun 2023 14:18:21 -0700

simplify code & reduce bandwidth

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

diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -5133,6 +5133,15 @@ Loop: continue } + if authUser.ChessSoundsEnabled { + if payload.Move.HasTag(chess.Capture) || payload.Move.HasTag(chess.EnPassant) { + send(`<audio src="/public/sounds/chess/Capture.ogg" autoplay></audio>`) + } else { + send(`<audio src="/public/sounds/chess/Move.ogg" autoplay></audio>`) + } + } + + styles := make([]string, 0) const animationMs = 400 animate := func(s1, s2 chess.Square, id string) { @@ -5147,8 +5156,10 @@ Loop: } animationIdx++ animationName := fmt.Sprintf("move_anim_%d", animationIdx) - send(fmt.Sprintf(`<style>@keyframes %s { from { left: calc(%d*12.5%%); top: calc(%d*12.5%%); } to { left: calc(%d*12.5%%); top: calc(%d*12.5%%); } }</style>`, animationName, x1, y1, x2, y2)) - send(fmt.Sprintf(`<style>#%s { animation-name: %s; animation-duration: %dms; animation-fill-mode: forwards; }</style>`, id, animationName, animationMs)) + style1 := fmt.Sprintf(`@keyframes %s { from { left: calc(%d*12.5%%); top: calc(%d*12.5%%); } to { left: calc(%d*12.5%%); top: calc(%d*12.5%%); } }`, animationName, x1, y1, x2, y2) + style2 := fmt.Sprintf(`#%s { animation-name: %s; animation-duration: %dms; animation-fill-mode: forwards; }`, id, animationName, animationMs) + styles = append(styles, style1) + styles = append(styles, style2) } animate(payload.Move.S1(), payload.Move.S2(), payload.IDStr1) @@ -5156,8 +5167,8 @@ Loop: if payload.Move.Promo() != chess.NoPieceType || payload.IDStr2 != "" { // Ensure the capturing piece is draw above the one being captured if payload.IDStr2 != "" { - send(fmt.Sprintf(`<style>#%s { z-index: 2; }</style>`, payload.IDStr2)) - send(fmt.Sprintf(`<style>#%s { z-index: 3; }</style>`, payload.IDStr1)) + styles = append(styles, fmt.Sprintf(`#%s { z-index: 2; }`, payload.IDStr2)) + styles = append(styles, fmt.Sprintf(`#%s { z-index: 3; }`, payload.IDStr1)) } // Wait until end of moving animation before hiding the captured piece or change promotion image go func(payload interceptors.ChessMove, c echo.Context) { @@ -5190,39 +5201,33 @@ Loop: } // En passant if payload.EnPassant != "" { - send(fmt.Sprintf(`<style>#%s { display: none; }</style>`, payload.EnPassant)) - } - - if authUser.ChessSoundsEnabled { - if payload.Move.HasTag(chess.Capture) || payload.Move.HasTag(chess.EnPassant) { - send(`<audio src="/public/sounds/chess/Capture.ogg" autoplay></audio>`) - } else { - send(`<audio src="/public/sounds/chess/Move.ogg" autoplay></audio>`) - } + styles = append(styles, fmt.Sprintf(`#%s { display: none; }`, payload.EnPassant)) } // Render advantages whiteAdv, whiteScore, blackAdv, blackScore := interceptors.CalcAdvantage(game.Position()) - send(fmt.Sprintf(`<style>#white-advantage:before { content: "%s" !important; }</style>`, whiteAdv)) - send(fmt.Sprintf(`<style>#white-advantage .score:after { content: "%s" !important; }</style>`, whiteScore)) - send(fmt.Sprintf(`<style>#black-advantage:before { content: "%s" !important; }</style>`, blackAdv)) - send(fmt.Sprintf(`<style>#black-advantage .score:after { content: "%s" !important; }</style>`, blackScore)) + styles = append(styles, fmt.Sprintf(`#white-advantage:before { content: "%s" !important; }`, whiteAdv)) + styles = append(styles, fmt.Sprintf(`#white-advantage .score:after { content: "%s" !important; }`, whiteScore)) + styles = append(styles, fmt.Sprintf(`#black-advantage:before { content: "%s" !important; }`, blackAdv)) + styles = append(styles, fmt.Sprintf(`#black-advantage .score:after { content: "%s" !important; }`, blackScore)) // Render last move - send(`<style>.square { background-color: transparent !important; }</style>`) - send(fmt.Sprintf(`<style>.square_%d, .square_%d { background-color: %s !important; }</style>`, + styles = append(styles, `.square { background-color: transparent !important; }`) + styles = append(styles, fmt.Sprintf(`.square_%d, .square_%d { background-color: %s !important; }`, int(payload.Move.S1()), int(payload.Move.S2()), interceptors.LastMoveColor)) // Reset kings background to transparent - send(fmt.Sprintf(`<style>#%s { background-color: transparent !important; }</style>`, interceptors.WhiteKingID)) - send(fmt.Sprintf(`<style>#%s { background-color: transparent !important; }</style>`, interceptors.BlackKingID)) + styles = append(styles, fmt.Sprintf(`#%s { background-color: transparent !important; }`, interceptors.WhiteKingID)) + styles = append(styles, fmt.Sprintf(`#%s { background-color: transparent !important; }`, interceptors.BlackKingID)) // Render "checks" red background if payload.CheckW { - send(fmt.Sprintf(`<style>#%s { background-color: %s !important; }</style>`, interceptors.WhiteKingID, interceptors.CheckColor)) + styles = append(styles, fmt.Sprintf(`#%s { background-color: %s !important; }`, interceptors.WhiteKingID, interceptors.CheckColor)) } else if payload.CheckB { - send(fmt.Sprintf(`<style>#%s { background-color: %s !important; }</style>`, interceptors.BlackKingID, interceptors.CheckColor)) + styles = append(styles, fmt.Sprintf(`#%s { background-color: %s !important; }`, interceptors.BlackKingID, interceptors.CheckColor)) } + send(fmt.Sprintf(`<style>%s</style>`, strings.Join(styles, " "))) + c.Response().Flush() } return nil