From 1916bea6b902a8317e4e5c6ebd2bf11443afdb0e Mon Sep 17 00:00:00 2001
From: Arnas Udovic
Date: Sat, 27 Jan 2024 22:43:44 +0200
Subject: [PATCH] add data preview
---
db.go | 54 +++++++++++++++++++++++++++++++++++++++
main.go | 8 ++++++
views/about.en.html | 12 ++++++++-
views/about.sgs.html | 12 ++++++++-
views/data.html | 48 ++++++++++++++++++++++++++++++++++
views/layouts/master.html | 25 ++++++++++--------
6 files changed, 147 insertions(+), 12 deletions(-)
create mode 100644 views/data.html
diff --git a/db.go b/db.go
index e61f9cf..02745bf 100644
--- a/db.go
+++ b/db.go
@@ -142,3 +142,57 @@ func getWinnerNextStep(db *sql.DB, game *Game) string {
return gameFlow[len(game.gameFlow) : len(game.gameFlow)+1]
}
+
+func getStatsTable(db *sql.DB) [][]string {
+ result := make([][]string, 0)
+ rows, err := db.Query("SELECT id, name, IFNULL(count, ''), IFNULL(comment, '') FROM Stats")
+ if err != nil {
+ return result
+ }
+
+ defer rows.Close()
+ for rows.Next() {
+ var (
+ id string
+ name string
+ count string
+ comment string
+ )
+ if err := rows.Scan(&id, &name, &count, &comment); err != nil {
+ return result
+ }
+ result = append(result, []string{id, name, count, comment})
+ }
+ if err := rows.Err(); err != nil {
+ return result
+ }
+
+ return result
+}
+
+func getGamesTable(db *sql.DB) [][]string {
+ result := make([][]string, 0)
+ rows, err := db.Query("SELECT * FROM Games ORDER BY count DESC")
+ if err != nil {
+ return result
+ }
+
+ defer rows.Close()
+ for rows.Next() {
+ var (
+ id string
+ gameFlow string
+ firstWon string
+ count string
+ )
+ if err := rows.Scan(&id, &gameFlow, &firstWon, &count); err != nil {
+ return result
+ }
+ result = append(result, []string{id, gameFlow, firstWon, count})
+ }
+ if err := rows.Err(); err != nil {
+ return result
+ }
+
+ return result
+}
diff --git a/main.go b/main.go
index 40d7bd7..850153d 100644
--- a/main.go
+++ b/main.go
@@ -38,6 +38,14 @@ func main() {
c.Redirect(http.StatusMovedPermanently, fmt.Sprintf("/%s/0/x", lang))
})
+ r.GET("/data", func(c *gin.Context) {
+ c.HTML(http.StatusOK, "data", gin.H{
+ "stats": getStatsTable(db),
+ "games": getGamesTable(db),
+ "single": true,
+ })
+ })
+
r.GET("/:lang", func(c *gin.Context) {
lang := getLang(c)
c.Redirect(http.StatusMovedPermanently, fmt.Sprintf("/%s/0/x", lang))
diff --git a/views/about.en.html b/views/about.en.html
index cd476dd..3042bf3 100644
--- a/views/about.en.html
+++ b/views/about.en.html
@@ -11,9 +11,19 @@
- Project written on GPL3 license
what you can get here. Project source are https://g.arns.lt/zordsdavini/tic-tac-toe.
+ Project written on GPL3 license
what you can get here. Project source are https://g.arns.lt/zordsdavini/tic-tac-toe. Data can be checked here.
+
+ History
+
+
+
+ - 2024-01-02: First version. AI checks won games.
+ - 2024-01-27: AI check the mostly won game.
+ - todo: AI check competitor's next move to avoid defeat.
+
+
About myself
diff --git a/views/about.sgs.html b/views/about.sgs.html
index c32581e..a92f49f 100644
--- a/views/about.sgs.html
+++ b/views/about.sgs.html
@@ -11,9 +11,19 @@
- Pruojekts parašīts ont GPL3 licenzėjės
, katron galat gautė ėš če (onglėškā). Pruojekta kuods īr https://g.arns.lt/zordsdavini/tic-tac-toe.
+ Pruojekts parašīts ont GPL3 licenzėjės
, katron galat gautė ėš če (onglėškā). Pruojekta kuods īr https://g.arns.lt/zordsdavini/tic-tac-toe. Doumenis gal liousā paveizietė če.
+
+ Istuorėjė
+
+
+
+ - 2024-01-02: Pėrmuojė veikontė versėjė. DI veiz i laimietus žaidėmus.
+ - 2024-01-27: DI veiz i daugiausē kartu laimietus žaidėmus.
+ - planūs: DI veiz i varžuova būsėma laimiejėma, ka bluokoutė.
+
+
Aple monėm pati
diff --git a/views/data.html b/views/data.html
new file mode 100644
index 0000000..23bb887
--- /dev/null
+++ b/views/data.html
@@ -0,0 +1,48 @@
+{{define "head"}}
+{{end}}
+
+{{define "content"}}
+
+ 🔙
+
+
+Stats
+
+
+
+
+ ID |
+ Name |
+ Count |
+ Comment |
+
+
+ {{ range .stats }}
+
+ {{ range . }}
+ {{ . }} |
+ {{ end }}
+
+ {{ end }}
+
+
+Games
+
+
+
+
+ ID |
+ Game Flow |
+ First Won |
+ Count |
+
+
+ {{ range .games }}
+
+ {{ range . }}
+ {{ . }} |
+ {{ end }}
+
+ {{ end }}
+
+{{end}}
diff --git a/views/layouts/master.html b/views/layouts/master.html
index c662382..5ebf491 100644
--- a/views/layouts/master.html
+++ b/views/layouts/master.html
@@ -16,28 +16,33 @@
- {{ if eq .lang "sgs"}}
- {{include "title.sgs"}}
- {{ else }}
- {{include "title.en"}}
+ {{ if not .single }}
+ {{ if eq .lang "sgs"}}
+ {{include "title.sgs"}}
+ {{ else }}
+ {{include "title.en"}}
+ {{ end }}
{{ end }}
-
{{template "content" .}}
- {{ if eq .lang "sgs"}}
- {{include "about.sgs"}}
- {{ else }}
- {{include "about.en"}}
+ {{ if not .single }}
+ {{ if eq .lang "sgs"}}
+ {{include "about.sgs"}}
+ {{ else }}
+ {{include "about.en"}}
+ {{ end }}
{{ end }}
- {{include "layouts/footer"}}
+ {{ if not .single }}
+ {{include "layouts/footer"}}
+ {{ end }}