fixed displaying empty tables

This commit is contained in:
Philipp 2021-09-24 13:19:44 +02:00
parent 19e94f036e
commit 47a14e6b82
6 changed files with 268 additions and 11 deletions

View file

@ -12,14 +12,13 @@ import (
)
func main() {
dsn := "postgres://postgres:secret@localhost:5432/postgres?sslmode=disable"
dsn := "postgres://postgres:secret@postgres:5432/postgres?sslmode=disable"
store, err := postgres.NewStore(dsn)
if err != nil {
log.Fatal(err)
}
ss, err := store.Servers()
if err != nil {
log.Fatal("There is an issue with the databse: %w", err)

View file

@ -5,6 +5,7 @@ import (
"git.snrd.de/Spaenny/steamServer"
"github.com/jmoiron/sqlx"
"github.com/google/uuid"
)
type GamemodeStore struct {
@ -27,3 +28,15 @@ func (s *GamemodeStore) Gamemodes() ([]steamServer.Gamemode, error) {
}
return gg, nil
}
func (s *GamemodeStore) GamemodeCount(region_id uuid.UUID) ([]steamServer.GamemodeCount, error) {
var c []steamServer.GamemodeCount
var query = `SELECT DISTINCT servers.gamemode_id, COUNT(DISTINCT servers.id)
FROM servers, gamemodes
WHERE servers.region_id = $1
GROUP BY servers.gamemode_id`
if err := s.Select(&c, query, region_id); err != nil {
return c, fmt.Errorf("error getting gamemode count: %w", err)
}
return c, nil
}

View file

@ -31,6 +31,11 @@ type Flag struct {
Flag string `db:"flag"`
}
type GamemodeCount struct {
ID uuid.UUID `db:"gamemode_id"`
Count int `db:"count"`
}
type ServerStore interface {
Server(id uuid.UUID) (Server, error)
Servers() ([]Server, error)
@ -48,6 +53,7 @@ type RegionStore interface {
type GamemodeStore interface {
Gamemode(name string) (Gamemode, error)
Gamemodes() ([]Gamemode, error)
GamemodeCount(region_id uuid.UUID) ([]GamemodeCount, error)
}
type FlagStore interface {

View file

@ -1,7 +1,9 @@
<html lang="en"><head>
<title>Serverlist :: {{.Region}}</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/x-icon" href="https://spenny.tf/favicon.ico"/>
<link href="https://unpkg.com/tailwindcss@1.0.4/dist/tailwind.min.css" rel="stylesheet">
<style>
tr#content {
@ -15,7 +17,7 @@
}
</style>
</head>
<body class="flex items-center justify-center" style="background: #edf2f7;">
<body class="flex justify-center" style="background: #edf2f7;">
<section class="container mx-auto px-8 my-1 flex flex-wrap -m-4">
<div class="p-2 md:w-40">
<a href="/" class="flex items-center p-4 bg-blue-200 rounded-lg shadow-xs cursor-pointer hover:bg-blue-500 hover:text-gray-100">
@ -39,14 +41,14 @@
</a>
</div>
<div class="p-2 md:w-40">
<a href="#" class="flex items-center p-4 bg-blue-200 rounded-lg shadow-xs cursor-pointer hover:bg-blue-500 hover:text-gray-100">
<a href="/australia" class="flex items-center p-4 bg-blue-200 rounded-lg shadow-xs cursor-pointer hover:bg-blue-500 hover:text-gray-100">
<div>
<p class=" text-xs font-medium ">Australia</p>
</div>
</a>
</div>
<div class="p-2 md:w-40">
<a href="#" class="flex items-center p-4 bg-blue-200 rounded-lg shadow-xs cursor-pointer hover:bg-blue-500 hover:text-gray-100">
<a href="/asia" class="flex items-center p-4 bg-blue-200 rounded-lg shadow-xs cursor-pointer hover:bg-blue-500 hover:text-gray-100">
<div>
<p class=" text-xs font-medium ">Asia</p>
</div>
@ -89,7 +91,7 @@
<td class="px-5 border-b border-gray-200 bg-white" id="flag">
{{range $flag := $flags}}
{{if eq $server.ServerIP $flag.ServerIP}}
<img src="https://flagcdn.com/16x12/{{$flag.Flag}}.png">
<img src="https://flagcdn.com/16x12/{{$flag.Flag}}.png" srcset="https://flagcdn.com/32x24/{{$flag.Flag}}.png 2x, https://flagcdn.com/48x36/{{$flag.Flag}}.png 3x" width="16" height="12">
{{end}}
{{end}}
</td>

127
templates/region.html Normal file
View file

@ -0,0 +1,127 @@
<html lang="en"><head>
<title>Serverlist :: {{.Region}}</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/x-icon" href="https://spenny.tf/favicon.ico"/>
<link href="https://unpkg.com/tailwindcss@1.0.4/dist/tailwind.min.css" rel="stylesheet">
<style>
tr#content {
line-height: 0;
}
td#flag {
padding-right: 0;
}
td#name {
padding-left: 0;
}
</style>
</head>
<body class="flex justify-center" style="background: #edf2f7;">
<section class="container mx-auto px-8 my-1 flex flex-wrap -m-4">
<div class="p-2 md:w-40">
<a href="/" class="flex items-center p-4 bg-blue-200 rounded-lg shadow-xs cursor-pointer hover:bg-blue-500 hover:text-gray-100">
<div>
<p class=" text-xs font-medium ">All Regions</p>
</div>
</a>
</div>
<div class="p-2 md:w-40">
<a href="/america" class="flex items-center p-4 bg-blue-200 rounded-lg shadow-xs cursor-pointer hover:bg-blue-500 hover:text-gray-100">
<div>
<p class=" text-xs font-medium ">North America</p>
</div>
</a>
</div>
<div class="p-2 md:w-40">
<a href="/europe" class="flex items-center p-4 bg-blue-200 rounded-lg shadow-xs cursor-pointer hover:bg-blue-500 hover:text-gray-100">
<div>
<p class=" text-xs font-medium ">Europe</p>
</div>
</a>
</div>
<div class="p-2 md:w-40">
<a href="/australia" class="flex items-center p-4 bg-blue-200 rounded-lg shadow-xs cursor-pointer hover:bg-blue-500 hover:text-gray-100">
<div>
<p class=" text-xs font-medium ">Australia</p>
</div>
</a>
</div>
<div class="p-2 md:w-40">
<a href="/asia" class="flex items-center p-4 bg-blue-200 rounded-lg shadow-xs cursor-pointer hover:bg-blue-500 hover:text-gray-100">
<div>
<p class=" text-xs font-medium ">Asia</p>
</div>
</a>
</div>
{{ $gamemodes := .Gamemodes}}
{{ $gamemodesCount := .GamemodeCount}}
{{ $servers := .Servers}}
{{ $flags := .Flags}}
<div class="container mx-auto px-4 sm:px-8">
<div class="py-8">
<div>
<h2 class="text-2xl font-semibold leading-tight">{{.Region}}</h2>
</div>
{{range $gamemode := $gamemodes}}
{{range $gamemodeCount := $gamemodesCount}}
{{if eq $gamemode.ID $gamemodeCount.ID}}
<div class="-mx-4 sm:-mx-8 px-4 sm:px-8 py-4 overflow-x-auto">
<div class="inline-block min-w-full shadow rounded-lg overflow-hidden">
<table class="min-w-full leading-normal">
<thead>
<tr>
<th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
</th>
<th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
{{$gamemode.Name}}
</th>
<th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
Map
</th>
<th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
Players
</th>
</tr>
</thead>
<tbody>
{{range $server := $servers}}
{{if eq $gamemode.ID $server.GamemodeID}}
<tr id="content">
<td class="px-5 border-b border-gray-200 bg-white" id="flag">
{{range $flag := $flags}}
{{if eq $server.ServerIP $flag.ServerIP}}
<img src="https://flagcdn.com/16x12/{{$flag.Flag}}.png" srcset="https://flagcdn.com/32x24/{{$flag.Flag}}.png 2x, https://flagcdn.com/48x36/{{$flag.Flag}}.png 3x" width="16" height="12">
{{end}}
{{end}}
</td>
<td class="px-5 py-5 border-b border-gray-200 bg-white text-sm" id="name">
<div class="flex items-center">
<div class="ml-3">
<a href="steam://connect/{{$server.ServerIP}}" class="text-gray-900 whitespace-no-wrap">{{$server.Name}}</a>
</div>
</div>
</td>
<td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
<p class="text-gray-900 whitespace-no-wrap">{{$server.Map}}</p>
</td>
<td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
<p class="text-gray-900 whitespace-no-wrap">{{$server.Players}}/{{$server.MaxPlayers}}</p>
</td>
</tr>
{{end}}
{{end}}
</tbody>
</table>
</div>
</div>
{{end}}
{{end}}
{{end}}
</div>
</div>
</body>
</html>

View file

@ -20,6 +20,8 @@ func NewHandler(store steamServer.Store) *Handler {
h.Get("/", h.Home())
h.Get("/europe", h.Europe())
h.Get("/america", h.America())
h.Get("/australia", h.Australia())
h.Get("/asia", h.Asia())
return h
}
@ -58,7 +60,7 @@ func (h *Handler) Home() http.HandlerFunc {
}
tmpl.Execute(w, data {
Region: "All Regions - Serverlist",
Region: "All Regions",
Servers: ss,
Gamemodes: gg,
Flags: ff,
@ -71,10 +73,11 @@ func (h *Handler) Europe() http.HandlerFunc {
Region string
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
GamemodeCount []steamServer.GamemodeCount
Flags []steamServer.Flag
}
tmpl := template.Must(template.ParseFiles("templates/home.html"))
tmpl := template.Must(template.ParseFiles("templates/region.html"))
return func(w http.ResponseWriter, r *http.Request) {
reg, err := h.store.Region("Europe")
if err != nil {
@ -97,27 +100,32 @@ func (h *Handler) Europe() http.HandlerFunc {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
c, err := h.store.GamemodeCount(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
tmpl.Execute(w, data {
Region: "Europe",
Servers: ss,
Gamemodes: gg,
GamemodeCount: c,
Flags: ff,
})
}
}
func (h *Handler) America() http.HandlerFunc {
type data struct {
Region string
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
GamemodeCount []steamServer.GamemodeCount
Flags []steamServer.Flag
}
tmpl := template.Must(template.ParseFiles("templates/home.html"))
tmpl := template.Must(template.ParseFiles("templates/region.html"))
return func(w http.ResponseWriter, r *http.Request) {
reg, err := h.store.Region("North America")
if err != nil {
@ -141,10 +149,112 @@ func (h *Handler) America() http.HandlerFunc {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
c, err := h.store.GamemodeCount(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
tmpl.Execute(w, data {
Region: "North America",
Servers: ss,
Gamemodes: gg,
GamemodeCount: c,
Flags: ff,
})
}
}
func (h *Handler) Asia() http.HandlerFunc {
type data struct {
Region string
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
GamemodeCount []steamServer.GamemodeCount
Flags []steamServer.Flag
}
tmpl := template.Must(template.ParseFiles("templates/region.html"))
return func(w http.ResponseWriter, r *http.Request) {
reg, err := h.store.Region("Asia")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
gg, err := h.store.Gamemodes()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
ss, err := h.store.ServersByRegion(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ff, err := h.store.Flags()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
c, err := h.store.GamemodeCount(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
tmpl.Execute(w, data {
Region: "Asia",
Servers: ss,
Gamemodes: gg,
GamemodeCount: c,
Flags: ff,
})
}
}
func (h *Handler) Australia() http.HandlerFunc {
type data struct {
Region string
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
GamemodeCount []steamServer.GamemodeCount
Flags []steamServer.Flag
}
tmpl := template.Must(template.ParseFiles("templates/region.html"))
return func(w http.ResponseWriter, r *http.Request) {
reg, err := h.store.Region("Australia")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
gg, err := h.store.Gamemodes()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
ss, err := h.store.ServersByRegion(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ff, err := h.store.Flags()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
c, err := h.store.GamemodeCount(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
tmpl.Execute(w, data {
Region: "Australia",
Servers: ss,
Gamemodes: gg,
GamemodeCount: c,
Flags: ff,
})
}