dkforest

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

commit 8b1adb41f6a05847128b292d3535ce1cd303a243
parent 20342e8a024de88c6c3c974fa229ee5f19b73a93
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon, 12 Jun 2023 13:37:37 -0700

cleanup

Diffstat:
Mpkg/web/handlers/handlers.go | 14++++++++------
Mpkg/web/handlers/interceptors/chess.go | 38+++++++++++++++++++++-----------------
2 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -5004,6 +5004,8 @@ func ChessGameHandler(c echo.Context) error { } } + game := g.Game + isFlipped := authUser.ID == g.Player2.ID if c.Request().Method == http.MethodPost { @@ -5013,7 +5015,7 @@ func ChessGameHandler(c echo.Context) error { msg := c.Request().PostFormValue("message") if msg == "resign" { resignColor := utils.Ternary(isFlipped, chess.Black, chess.White) - g.Game.Resign(resignColor) + game.Resign(resignColor) interceptors.ChessPubSub.Pub(key, interceptors.ChessMove{}) } else { if err := interceptors.ChessInstance.SendMove(key, authUser.ID, g, c); err != nil { @@ -5029,8 +5031,8 @@ func ChessGameHandler(c echo.Context) error { } //isYourTurnFn := func() bool { - // return authUser.ID == g.Player1.ID && g.Game.Position().Turn() == chess.White || - // authUser.ID == g.Player2.ID && g.Game.Position().Turn() == chess.Black + // return authUser.ID == g.Player1.ID && game.Position().Turn() == chess.White || + // authUser.ID == g.Player2.ID && game.Position().Turn() == chess.Black //} //isYourTurn := isYourTurnFn() @@ -5039,7 +5041,7 @@ func ChessGameHandler(c echo.Context) error { } // If you are not a spectator, and it's your turn to play, we just render the form directly. - if g.Game.Outcome() != chess.NoOutcome { + if game.Outcome() != chess.NoOutcome { c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8) c.Response().WriteHeader(http.StatusOK) send(cssReset) @@ -5118,7 +5120,7 @@ Loop: default: } - if g.Game.Outcome() != chess.NoOutcome { + if game.Outcome() != chess.NoOutcome { send(`<meta http-equiv="refresh" content="0" />`) break } @@ -5199,7 +5201,7 @@ Loop: } // Render advantages - whiteAdv, whiteScore, blackAdv, blackScore := interceptors.CalcAdvantage(g.Game.Position()) + 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)) diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go @@ -167,7 +167,8 @@ input[type=checkbox]:checked + label { </style>` func (g *ChessGame) renderBoardHTML1(position *chess.Position, isFlipped bool, imgB64 string) string { - moves := g.Game.Moves() + game := g.Game + moves := game.Moves() var last *chess.Move if len(moves) > 0 { last = moves[len(moves)-1] @@ -175,7 +176,7 @@ func (g *ChessGame) renderBoardHTML1(position *chess.Position, isFlipped bool, i boardMap := position.Board().SquareMap() pieceInCheck := func(p chess.Piece) bool { - return last != nil && p.Color() == g.Game.Position().Turn() && p.Type() == chess.King && last.HasTag(chess.Check) + return last != nil && p.Color() == game.Position().Turn() && p.Type() == chess.King && last.HasTag(chess.Check) } sqIsLastMove := func(sq chess.Square) bool { return last != nil && (last.S1() == sq || last.S2() == sq) @@ -393,9 +394,10 @@ func (g *ChessGame) drawPlayerCard(key string, isBlack, isSpectator, isYourTurn, </table> ` + game := g.Game enemy := utils.Ternary(isBlack, g.Player1, g.Player2) imgB64 := g.renderBoardB64(isBlack) - whiteAdvantage, whiteScore, blackAdvantage, blackScore := CalcAdvantage(g.Game.Position()) + whiteAdvantage, whiteScore, blackAdvantage, blackScore := CalcAdvantage(game.Position()) data := map[string]any{ "SoundsEnabled": soundsEnabled, @@ -407,9 +409,9 @@ func (g *ChessGame) drawPlayerCard(key string, isBlack, isSpectator, isYourTurn, "Username": enemy.Username, "Table": template.HTML(g.renderBoardHTML(isBlack, imgB64)), "ImgB64": imgB64, - "Outcome": g.Game.Outcome().String(), - "GameOver": g.Game.Outcome() != chess.NoOutcome, - "PGN": g.Game.String(), + "Outcome": game.Outcome().String(), + "GameOver": game.Outcome() != chess.NoOutcome, + "PGN": game.String(), "WhiteAdvantage": whiteAdvantage, "WhiteScore": whiteScore, "BlackAdvantage": blackAdvantage, @@ -480,8 +482,9 @@ func (b *Chess) newGame(gameKey string, user1, user2 database.User) *ChessGame { } func (b *Chess) SendMove(gameKey string, userID database.UserID, g *ChessGame, c echo.Context) error { - if (g.Game.Position().Turn() == chess.White && userID != g.Player1.ID) || - (g.Game.Position().Turn() == chess.Black && userID != g.Player2.ID) { + game := g.Game + if (game.Position().Turn() == chess.White && userID != g.Player1.ID) || + (game.Position().Turn() == chess.Black && userID != g.Player2.ID) { return errors.New("not your turn") } @@ -489,7 +492,7 @@ func (b *Chess) SendMove(gameKey string, userID database.UserID, g *ChessGame, c currentPlayer := g.Player1 opponentPlayer := g.Player2 - if g.Game.Position().Turn() == chess.Black { + if game.Position().Turn() == chess.Black { currentPlayer = g.Player2 opponentPlayer = g.Player1 } @@ -518,13 +521,13 @@ func (b *Chess) SendMove(gameKey string, userID database.UserID, g *ChessGame, c } var moveStr string - validMoves := g.Game.Position().ValidMoves() + validMoves := game.Position().ValidMoves() var found bool var mov chess.Move for _, move := range validMoves { if (move.S1() == selectedSquares[0] && move.S2() == selectedSquares[1] && (move.Promo() == chess.NoPieceType || move.Promo() == promo)) || (move.S1() == selectedSquares[1] && move.S2() == selectedSquares[0] && (move.Promo() == chess.NoPieceType || move.Promo() == promo)) { - moveStr = chess.AlgebraicNotation{}.Encode(g.Game.Position(), move) + moveStr = chess.AlgebraicNotation{}.Encode(game.Position(), move) found = true mov = *move break @@ -537,7 +540,7 @@ func (b *Chess) SendMove(gameKey string, userID database.UserID, g *ChessGame, c //fmt.Println(moveStr) - _ = g.Game.MoveStr(moveStr) + _ = game.MoveStr(moveStr) g.lastUpdated = time.Now() idStr1 := piecesCache[mov.S1()] idStr2 := piecesCache[mov.S2()] @@ -555,8 +558,8 @@ func (b *Chess) SendMove(gameKey string, userID database.UserID, g *ChessGame, c IDStr1: idStr1, IDStr2: idStr2, EnPassant: idStr3, - CheckW: g.Game.Position().Turn() == chess.White && mov.HasTag(chess.Check), - CheckB: g.Game.Position().Turn() == chess.Black && mov.HasTag(chess.Check), + CheckW: game.Position().Turn() == chess.White && mov.HasTag(chess.Check), + CheckB: game.Position().Turn() == chess.Black && mov.HasTag(chess.Check), Move: mov, } ChessPubSub.Pub(gameKey, chessMov) @@ -580,10 +583,11 @@ func (g *ChessGame) IsPlayer(userID database.UserID) bool { } func (g *ChessGame) MoveStr(m string) { - validMoves := g.Game.Position().ValidMoves() + game := g.Game + validMoves := game.Position().ValidMoves() var mov chess.Move for _, move := range validMoves { - moveStr := chess.AlgebraicNotation{}.Encode(g.Game.Position(), move) + moveStr := chess.AlgebraicNotation{}.Encode(game.Position(), move) if moveStr == m { mov = *move break @@ -592,7 +596,7 @@ func (g *ChessGame) MoveStr(m string) { g.updatePiecesCache(mov) - _ = g.Game.MoveStr(m) + _ = game.MoveStr(m) } const (