commit fa56e693ce1d1d666be5e05f267a90f20320249d
parent 5343430bb983f115175c9282c2e5920e85993380
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Sun, 11 Jun 2023 06:33:06 -0700
trash code, but awesome chess board
Diffstat:
17 files changed, 240 insertions(+), 75 deletions(-)
diff --git a/pkg/web/handlers/data.go b/pkg/web/handlers/data.go
@@ -918,6 +918,9 @@ type stego1RoadChallengeData struct {
FlagMessage string
}
+type chess1Data struct {
+}
+
type chessData struct {
Games []interceptors.ChessGame
Error string
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -4793,6 +4793,11 @@ func Stego1ChallengeHandler(c echo.Context) error {
return c.Render(http.StatusOK, "vip.stego1", data)
}
+func Chess1Handler(c echo.Context) error {
+ var data chess1Data
+ return c.Render(http.StatusOK, "chess1", data)
+}
+
func ChessHandler(c echo.Context) error {
authUser := c.Get("authUser").(*database.User)
db := c.Get("database").(*database.DkfDB)
@@ -4868,17 +4873,17 @@ html, body {
func ChessGameHandler(c echo.Context) error {
authUser := c.Get("authUser").(*database.User)
- //db := c.Get("database").(*database.DkfDB)
+ db := c.Get("database").(*database.DkfDB)
key := c.Param("key")
g := interceptors.ChessInstance.GetGame(key)
if g == nil {
// Chess debug
- //user1, _ := db.GetUserByID(1)
- //user2, _ := db.GetUserByID(24132)
- //v1.ChessInstance.NewGame(key, user1, user2)
- //g = v1.ChessInstance.GetGame(key)
- return c.Redirect(http.StatusFound, "/")
+ user1, _ := db.GetUserByID(1)
+ user2, _ := db.GetUserByID(30814)
+ interceptors.ChessInstance.NewGame(key, user1, user2)
+ g = interceptors.ChessInstance.GetGame(key)
+ //return c.Redirect(http.StatusFound, "/")
}
isFlipped := authUser.ID == g.Player2.ID
diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go
@@ -99,6 +99,103 @@ const (
boardSize = 8 * sqSize
)
+func renderBoardHTML(position *chess.Position, isFlipped bool, imgB64 string, spectator bool) string {
+ boardMap := position.Board().SquareMap()
+ out := `<style>
+.newBoard {
+ position: relative;
+ aspect-ratio: 1 / 1;
+ width: 100%;
+ min-height: 360px;
+ background-repeat: no-repeat;
+ background-size: cover;
+ background-image: url(data:image/png;base64,` + imgB64 + `);
+}
+.newBoard td {
+}
+.newBoard img {
+ position: absolute;
+ width: 12.5%;
+ height: 12.5%;
+}
+.idk {
+ width: 100%;
+ height: 100%;
+}
+label {
+ position: absolute;
+ width: 12.5%;
+ height: 12.5%;
+}
+input[type=checkbox] {
+ display:none;
+}
+input[type=checkbox] + label {
+ display: inline-block;
+ padding: 0 0 0 0;
+ margin: 0 0 0 0;
+ background-size: 100%;
+ border: 3px solid transparent;
+ box-sizing: border-box;
+}
+input[type=checkbox]:checked + label {
+ display: inline-block;
+ background-size: 100%;
+ border: 3px solid red;
+}
+</style>`
+ out += `<table class="newBoard">`
+ for i := 0; i < 64; i++ {
+ id := i
+ mm := 0
+ switch i % 8 {
+ case 0:
+ mm = -7
+ case 1:
+ mm = -5
+ case 2:
+ mm = -3
+ case 3:
+ mm = -1
+ case 4:
+ mm = 1
+ case 5:
+ mm = 3
+ case 6:
+ mm = 5
+ case 7:
+ mm = 7
+ }
+ if !isFlipped {
+ id = (63 - i) + mm
+ } else {
+ id = i - mm
+ }
+ if i%8 == 0 {
+ if i != 0 {
+ out += "</tr>"
+ }
+ out += "<tr>"
+ }
+ out += "<td>"
+ sq := chess.Square(id)
+ sqPiece := boardMap[sq]
+ p := sqPiece
+ out += `<div class="idk">`
+ if p != chess.NoPiece {
+ out += `<img src="/public/img/chess/` + p.Color().String() + pieceTypeMap[p.Type()] + `.png" alt="" />`
+ }
+ if !spectator {
+ idStr := strconv.Itoa(id)
+ out += `<input name="sq_` + idStr + `" ID="sq_` + idStr + `" type="checkbox" value="1" /><label for="sq_` + idStr + `"></label>`
+ }
+ out += "</div>"
+ out += "</td>"
+ }
+ out += "</tr></table>"
+ return out
+}
+
func renderBoardPng(last *chess.Move, position *chess.Position, isFlipped bool) image.Image {
boardMap := position.Board().SquareMap()
ctx := gg.NewContext(boardSize, boardSize)
@@ -170,14 +267,14 @@ func renderSquare(ctx *gg.Context, sq chess.Square, last *chess.Move, turn chess
}
ctx.Pop()
- // draw piece
- p := sqPiece
- if p != chess.NoPiece {
- img := getFile("img/chess/" + p.Color().String() + pieceTypeMap[p.Type()] + ".png")
- ctx.Push()
- ctx.DrawImage(img, x, y)
- ctx.Pop()
- }
+ //// draw piece
+ //p := sqPiece
+ //if p != chess.NoPiece {
+ // img := getFile("img/chess/" + p.Color().String() + pieceTypeMap[p.Type()] + ".png")
+ // ctx.Push()
+ // ctx.DrawImage(img, x, y)
+ // ctx.Pop()
+ //}
}
var pieceTypeMap = map[chess.PieceType]string{
@@ -260,6 +357,12 @@ input[type=checkbox]:checked + label {
return buf.String()
}
+func (g *ChessGame) renderBoardHTML(isFlipped bool, imgB64 string, spectator bool) string {
+ position := g.Game.Position()
+ out := renderBoardHTML(position, isFlipped, imgB64, spectator)
+ return out
+}
+
func (g *ChessGame) renderBoardB64(isFlipped bool) string {
position := g.Game.Position()
moves := g.Game.Moves()
@@ -284,54 +387,80 @@ func (g *ChessGame) drawPlayerCard(roomName string, isBlack, isYourTurn bool) st
imgB64 := g.renderBoardB64(isBlack)
htmlTmpl := `
-<div style="margin: 0 auto; width: 360px;">
- <div style="color: #eee;">
- <span {{ .White.UserStyle | attr }}>@{{ .White.Username }}</span> (white) VS
- <span {{ .Black.UserStyle | attr }}>@{{ .Black.Username }}</span> (black)
- </div>
-
- {{ if .GameOver }}
- <div style="width: 360px; height: 360px; background-image: url(data:image/png;base64,{{ .ImgB64 }})"></div>
- {{ else }}
- <form method="post">
- <input type="hidden" name="message" value="resign" />
- <button type="submit" style="background-color: #aaa; margin: 5px 0;">Resign</button>
- </form>
- {{ if .IsYourTurn }}
- <form method="post"{{ if .InChat }} action="/api/v1/chess"{{ end }}>
- {{ .Table }}
- {{ if .InChat }}
- <input type="hidden" name="room" value="{{ .RoomName }}" />
- <input type="hidden" name="enemyUsername" value="{{ .Username }}" />
- <input type="hidden" name="move" value="move" />
+<style>
+.tmp {
+ width: 100%;
+ height: 100%;
+}
+</style>
+<table class="tmp">
+ <tr><td align="center">
+ <table style="aspect-ratio: 1/1; height: 70%; max-width: 90%;">
+ <tr>
+ <td style="padding: 10px 0;">
+ <div style="color: #eee;">
+ <span {{ .White.UserStyle | attr }}>@{{ .White.Username }}</span> (white) VS
+ <span {{ .Black.UserStyle | attr }}>@{{ .Black.Username }}</span> (black)
+ </div>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ {{ if .GameOver }}
+ {{ .Table }}
{{ else }}
- <input type="hidden" name="message" value="/pm {{ .Username }} /c move" />
- {{ end }}
- <div style="width: 100%; display: flex; margin: 5px 0;">
+ <form method="post">
+ <input type="hidden" name="message" value="resign" />
+ <button type="submit" style="background-color: #aaa; margin: 5px 0;">Resign</button>
+ </form>
<div>
- <button type="submit" style="background-color: #aaa;">Move</button>
+ {{ if .IsYourTurn }}
+ <form method="post"{{ if .InChat }} action="/api/v1/chess"{{ end }} style="aspect-ratio: 1/1; height: 70%;">
+ {{ .Table }}
+ {{ if .InChat }}
+ <input type="hidden" name="room" value="{{ .RoomName }}" />
+ <input type="hidden" name="enemyUsername" value="{{ .Username }}" />
+ <input type="hidden" name="move" value="move" />
+ {{ else }}
+ <input type="hidden" name="message" value="/pm {{ .Username }} /c move" />
+ {{ end }}
+ <div style="width: 100%; display: flex; margin: 5px 0;">
+ <div>
+ <button type="submit" style="background-color: #aaa;">Move</button>
+ </div>
+ <div style="margin-left: auto;">
+ <span style="color: #aaa; margin-left: 20px;">Promo:</span>
+ <select name="promotion" style="background-color: #aaa;">
+ <option value="queen">Queen</option>
+ <option value="rook">Rook</option>
+ <option value="knight">Knight</option>
+ <option value="bishop">Bishop</option>
+ </select>
+ </div>
+ </div>
+ </form>
+ {{ else }}
+ {{ .Table }}
+ {{ end }}
</div>
- <div style="margin-left: auto;">
- <span style="color: #aaa; margin-left: 20px;">Promo:</span>
- <select name="promotion" style="background-color: #aaa;">
- <option value="queen">Queen</option>
- <option value="rook">Rook</option>
- <option value="knight">Knight</option>
- <option value="bishop">Bishop</option>
- </select>
- </div>
- </div>
- </form>
- {{ else }}
- <div style="width: 360px; height: 360px; background-image: url(data:image/png;base64,{{ .ImgB64 }})"></div>
+ {{ end }}
+ </td>
+ </tr>
+ <tr style="height: 100%;">
+ <td>
+ <div style="color: #eee;">Outcome: {{ .Outcome }}</div>
+ </td>
+ </tr>
+
+ {{ if .GameOver }}
+ <tr>
+ <td>
+ <div><textarea>{{ .PGN }}</textarea></div>
+ </td>
+ </tr>
{{ end }}
- {{ end }}
- <div style="color: #eee;">Outcome: {{ .Outcome }}</div>
-
- {{ if .GameOver }}
- <div><textarea>{{ .PGN }}</textarea></div>
- {{ end }}
-</div>
+ </table>
+</td></tr></table>
`
data := map[string]any{
@@ -341,7 +470,7 @@ func (g *ChessGame) drawPlayerCard(roomName string, isBlack, isYourTurn bool) st
"White": g.Player1,
"Black": g.Player2,
"Username": enemy.Username,
- "Table": template.HTML(renderTable(imgB64, isBlack)),
+ "Table": template.HTML(g.renderBoardHTML(isBlack, imgB64, !isYourTurn)),
"ImgB64": imgB64,
"Outcome": g.Game.Outcome().String(),
"GameOver": g.Game.Outcome() != chess.NoOutcome,
@@ -363,27 +492,45 @@ func (g *ChessGame) DrawSpectatorCard(isFlipped bool) string {
imgB64 := g.renderBoardB64(isFlipped)
htmlTmpl := `
-<div style="margin: 0 auto; width: 360px;">
- <div style="color: #eee;">
- <span {{ .White.UserStyle | attr }}>@{{ .White.Username }}</span> (white) VS
- <span {{ .Black.UserStyle | attr }}>@{{ .Black.Username }}</span> (black)
- </div>
- <div style="width: 360px; height: 360px; background-image: url(data:image/png;base64,{{ .ImgB64 }})"></div>
- <div style="width: 100%; display: flex; margin: 5px 0;">
- <div>
- <a href="?{{ if not .IsFlipped }}r=1{{ end }}" style="color: #eee;">Flip board</a>
- </div>
- </div>
- <div style="color: #eee;">Outcome: {{ .Outcome }}</div>
- {{ if .GameOver }}
- <div><textarea>{{ .PGN }}</textarea></div>
- {{ end }}
-</div>
+<style>
+.tmp {
+ width: 100%;
+ height: 100%;
+}
+</style>
+<table class="tmp">
+ <tr>
+ <td align="center">
+ <table style="aspect-ratio: 1/1; height: 70%; max-width: 90%;">
+ <tr>
+ <td style="padding: 10px 0;">
+ <div style="color: #eee;">
+ <span {{ .White.UserStyle | attr }}>@{{ .White.Username }}</span> (white) VS
+ <span {{ .Black.UserStyle | attr }}>@{{ .Black.Username }}</span> (black)
+ </div>
+ </td>
+ </tr>
+ <tr><td>{{ .Table }}</td></tr>
+ <tr><td style="padding: 10px 0;"><a href="?{{ if not .IsFlipped }}r=1{{ end }}" style="color: #eee;">Flip board</a></td></tr>
+ <tr style="height: 100%;"><td><div style="color: #eee;">Outcome: {{ .Outcome }}</div></td></tr>
+
+ {{ if .GameOver }}
+ <tr>
+ <td>
+ <div><textarea>{{ .PGN }}</textarea></div>
+ </td>
+ </tr>
+ {{ end }}
+ </table>
+ </td>
+ </tr>
+</table>
`
data := map[string]any{
"White": g.Player1,
"Black": g.Player2,
+ "Table": template.HTML(g.renderBoardHTML(isFlipped, imgB64, true)),
"ImgB64": imgB64,
"Outcome": g.Game.Outcome().String(),
"GameOver": g.Game.Outcome() != chess.NoOutcome,
diff --git a/pkg/web/public/img/chess/bB.png b/pkg/web/public/img/chess/bB.png
Binary files differ.
diff --git a/pkg/web/public/img/chess/bK.png b/pkg/web/public/img/chess/bK.png
Binary files differ.
diff --git a/pkg/web/public/img/chess/bN.png b/pkg/web/public/img/chess/bN.png
Binary files differ.
diff --git a/pkg/web/public/img/chess/bP.png b/pkg/web/public/img/chess/bP.png
Binary files differ.
diff --git a/pkg/web/public/img/chess/bQ.png b/pkg/web/public/img/chess/bQ.png
Binary files differ.
diff --git a/pkg/web/public/img/chess/bR.png b/pkg/web/public/img/chess/bR.png
Binary files differ.
diff --git a/pkg/web/public/img/chess/wB.png b/pkg/web/public/img/chess/wB.png
Binary files differ.
diff --git a/pkg/web/public/img/chess/wK.png b/pkg/web/public/img/chess/wK.png
Binary files differ.
diff --git a/pkg/web/public/img/chess/wN.png b/pkg/web/public/img/chess/wN.png
Binary files differ.
diff --git a/pkg/web/public/img/chess/wP.png b/pkg/web/public/img/chess/wP.png
Binary files differ.
diff --git a/pkg/web/public/img/chess/wQ.png b/pkg/web/public/img/chess/wQ.png
Binary files differ.
diff --git a/pkg/web/public/img/chess/wR.png b/pkg/web/public/img/chess/wR.png
Binary files differ.
diff --git a/pkg/web/public/views/pages/chess1.gohtml b/pkg/web/public/views/pages/chess1.gohtml
@@ -0,0 +1,8 @@
+{{ define "title" }}dkf - chess{{ end }}
+
+{{ define "content" }}
+ <div class="container">
+ TEST
+ <img style="width: 100px;" src="/public/img/chess/bP.svg" alt="" />
+ </div>
+{{ end }}
+\ No newline at end of file
diff --git a/pkg/web/web.go b/pkg/web/web.go
@@ -97,6 +97,7 @@ func getMainServer(db *database.DkfDB, i18nBundle *i18n.Bundle, renderer *tmp.Te
authGroup.POST("/captcha", handlers.CaptchaHandler, middlewares.AuthRateLimitMiddleware(time.Second, 1))
authGroup.GET("/donate", handlers.DonateHandler)
authGroup.GET("/shop", handlers.ShopHandler)
+ authGroup.GET("/chess1", handlers.Chess1Handler)
authGroup.GET("/chess", handlers.ChessHandler)
authGroup.POST("/chess", handlers.ChessHandler)
authGroup.GET("/chess/:key", handlers.ChessGameHandler)