add data preview

This commit is contained in:
Arnas Udovic 2024-01-27 22:43:44 +02:00
parent 34334c773a
commit 1916bea6b9
6 changed files with 147 additions and 12 deletions

54
db.go
View file

@ -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
}

View file

@ -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))

View file

@ -11,9 +11,19 @@
</p>
<p>
Project written on GPL3 license <img src="https://www.gnu.org/graphics/gplv3-127x51.png" height="18"> what you can get <a href="https://www.gnu.org/licenses/gpl-3.0.html" target="_blank">here</a>. Project source are <a href="https://g.arns.lt/zordsdavini/tic-tac-toe" target="_blank">https://g.arns.lt/zordsdavini/tic-tac-toe</a>.
Project written on GPL3 license <img src="https://www.gnu.org/graphics/gplv3-127x51.png" height="18"> what you can get <a href="https://www.gnu.org/licenses/gpl-3.0.html" target="_blank">here</a>. Project source are <a href="https://g.arns.lt/zordsdavini/tic-tac-toe" target="_blank">https://g.arns.lt/zordsdavini/tic-tac-toe</a>. Data can be checked <a href="/data">here</a>.
</p>
<p>
<strong>History</strong>
</p>
<ul>
<li>2024-01-02: First version. AI checks won games.</li>
<li>2024-01-27: AI check the mostly won game.</li>
<li><i class="text-muted">todo: AI check competitor's next move to avoid defeat.</i></li>
</ul>
<h3>About myself</h3>
<p>

View file

@ -11,9 +11,19 @@
</p>
<p>
Pruojekts parašīts ont GPL3 licenzėjės <img src="https://www.gnu.org/graphics/gplv3-127x51.png" height="18">, katron galat gautė ėš <a href="https://www.gnu.org/licenses/gpl-3.0.html" target="_blank">če (onglėškā)</a>. Pruojekta kuods īr <a href="https://g.arns.lt/zordsdavini/tic-tac-toe" target="_blank">https://g.arns.lt/zordsdavini/tic-tac-toe</a>.
Pruojekts parašīts ont GPL3 licenzėjės <img src="https://www.gnu.org/graphics/gplv3-127x51.png" height="18">, katron galat gautė ėš <a href="https://www.gnu.org/licenses/gpl-3.0.html" target="_blank">če (onglėškā)</a>. Pruojekta kuods īr <a href="https://g.arns.lt/zordsdavini/tic-tac-toe" target="_blank">https://g.arns.lt/zordsdavini/tic-tac-toe</a>. Doumenis gal liousā paveizietė <a href="/data">če</a>.
</p>
<p>
<strong>Istuorėjė</strong>
</p>
<ul>
<li>2024-01-02: Pėrmuojė veikontė versėjė. DI veiz i laimietus žaidėmus.</li>
<li>2024-01-27: DI veiz i daugiausē kartu laimietus žaidėmus.</li>
<li><i class="text-muted">planūs: DI veiz i varžuova būsėma laimiejėma, ka bluokoutė.</i></li>
</ul>
<h3>Aple monėm pati</h3>
<p>

48
views/data.html Normal file
View file

@ -0,0 +1,48 @@
{{define "head"}}
{{end}}
{{define "content"}}
<p>
<a href="/">🔙</a>
</p>
<h3>Stats</h3>
<table class="table table-sm table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Count</th>
<th>Comment</th>
</tr>
</thead>
{{ range .stats }}
<tr>
{{ range . }}
<td>{{ . }}</td>
{{ end }}
</tr>
{{ end }}
</table>
<h3>Games</h3>
<table class="table table-sm table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>Game Flow</th>
<th>First Won</th>
<th>Count</th>
</tr>
</thead>
{{ range .games }}
<tr>
{{ range . }}
<td>{{ . }}</td>
{{ end }}
</tr>
{{ end }}
</table>
{{end}}

View file

@ -16,28 +16,33 @@
<body>
<div class="container py-2">
{{ 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 }}
<div class="row">
<div class="col">
{{template "content" .}}
</div>
<div class="col">
{{ 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 }}
</div>
</div>
{{include "layouts/footer"}}
{{ if not .single }}
{{include "layouts/footer"}}
{{ end }}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</div>
</body>