dkforest

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

commit 97a8e7a489e81742b3eeff843c3636e1404cb698
parent 7acef0e0b7d8992186051bcde9a41e8195a5da23
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon, 12 Jun 2023 14:27:48 -0700

cleanup

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

diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -4983,6 +4983,20 @@ func ChessGameFormHandler(c echo.Context) error { return c.HTML(http.StatusOK, buf.String()) } +type StylesBuilder []string + +func (b *StylesBuilder) Append(v string) { + *b = append(*b, v) +} + +func (b *StylesBuilder) Appendf(format string, a ...any) { + *b = append(*b, fmt.Sprintf(format, a...)) +} + +func (b *StylesBuilder) Build() string { + return fmt.Sprintf("<style>%s</style>", strings.Join(*b, " ")) +} + func ChessGameHandler(c echo.Context) error { debugChess := true @@ -5141,7 +5155,7 @@ Loop: } } - styles := make([]string, 0) + var styles StylesBuilder const animationMs = 400 animate := func(s1, s2 chess.Square, id string) { @@ -5156,10 +5170,8 @@ Loop: } animationIdx++ animationName := fmt.Sprintf("move_anim_%d", animationIdx) - 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) + styles.Appendf(`@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) + styles.Appendf(`#%s { animation-name: %s; animation-duration: %dms; animation-fill-mode: forwards; }`, id, animationName, animationMs) } animate(payload.Move.S1(), payload.Move.S2(), payload.IDStr1) @@ -5167,8 +5179,8 @@ Loop: if payload.Move.Promo() != chess.NoPieceType || payload.IDStr2 != "" { // Ensure the capturing piece is draw above the one being captured if payload.IDStr2 != "" { - styles = append(styles, fmt.Sprintf(`#%s { z-index: 2; }`, payload.IDStr2)) - styles = append(styles, fmt.Sprintf(`#%s { z-index: 3; }`, payload.IDStr1)) + styles.Appendf(`#%s { z-index: 2; }`, payload.IDStr2) + styles.Appendf(`#%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) { @@ -5201,32 +5213,32 @@ Loop: } // En passant if payload.EnPassant != "" { - styles = append(styles, fmt.Sprintf(`#%s { display: none; }`, payload.EnPassant)) + styles.Appendf(`#%s { display: none; }`, payload.EnPassant) } // Render advantages whiteAdv, whiteScore, blackAdv, blackScore := interceptors.CalcAdvantage(game.Position()) - 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)) + styles.Appendf(`#white-advantage:before { content: "%s" !important; }`, whiteAdv) + styles.Appendf(`#white-advantage .score:after { content: "%s" !important; }`, whiteScore) + styles.Appendf(`#black-advantage:before { content: "%s" !important; }`, blackAdv) + styles.Appendf(`#black-advantage .score:after { content: "%s" !important; }`, blackScore) // Render last move - 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)) + styles.Appendf(`.square { background-color: transparent !important; }`) + styles.Appendf(`.square_%d, .square_%d { background-color: %s !important; }`, + int(payload.Move.S1()), int(payload.Move.S2()), interceptors.LastMoveColor) // Reset kings background to transparent - styles = append(styles, fmt.Sprintf(`#%s { background-color: transparent !important; }`, interceptors.WhiteKingID)) - styles = append(styles, fmt.Sprintf(`#%s { background-color: transparent !important; }`, interceptors.BlackKingID)) + styles.Appendf(`#%s { background-color: transparent !important; }`, interceptors.WhiteKingID) + styles.Appendf(`#%s { background-color: transparent !important; }`, interceptors.BlackKingID) // Render "checks" red background if payload.CheckW { - styles = append(styles, fmt.Sprintf(`#%s { background-color: %s !important; }`, interceptors.WhiteKingID, interceptors.CheckColor)) + styles.Appendf(`#%s { background-color: %s !important; }`, interceptors.WhiteKingID, interceptors.CheckColor) } else if payload.CheckB { - styles = append(styles, fmt.Sprintf(`#%s { background-color: %s !important; }`, interceptors.BlackKingID, interceptors.CheckColor)) + styles.Appendf(`#%s { background-color: %s !important; }`, interceptors.BlackKingID, interceptors.CheckColor) } - send(fmt.Sprintf(`<style>%s</style>`, strings.Join(styles, " "))) + send(styles.Build()) c.Response().Flush() }