seperated gamemodes into seprate tables
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Philipp 2021-09-22 14:12:49 +02:00
parent f85fb7434d
commit 9dae1ca401
8 changed files with 210 additions and 17 deletions

View File

@ -22,7 +22,7 @@ type Server struct {
}
func main() {
dsn := "postgres://postgres:secret@postgres:5432/postgres?sslmode=disable"
dsn := "postgres://postgres:secret@u0k.de:5432/postgres?sslmode=disable"
store, err := postgres.NewStore(dsn)
if err != nil {
@ -32,13 +32,13 @@ func main() {
ss, err := store.Servers()
if err != nil {
log.Fatal("There is no server in the list.")
log.Fatal("There is an issue with the databse: %w", err)
}
for _, s := range ss {
serverString := s.ServerIP
c := cron.New()
c.AddFunc("*/5 * * * *", func() { updateServers(serverString, store) })
c.AddFunc("*/1 * * * *", func() { updateServers(serverString, store) })
c.Start()
}
@ -48,10 +48,10 @@ func main() {
}
func updateServers(serverString string, store steamServer.Store) {
url := "http://steam-server-api:8080/server/" + serverString
url := "http://localhost:8090/server/" + serverString
reqClient := http.Client {
Timeout: time.Second * 2,
Timeout: time.Second * 10,
}
req, err := http.NewRequest(http.MethodGet, url, nil)

View File

@ -0,0 +1,29 @@
package postgres
import (
"fmt"
"git.snrd.de/Spaenny/steamServer"
"github.com/jmoiron/sqlx"
)
type GamemodeStore struct {
*sqlx.DB
}
func (s *GamemodeStore) Gamemode(name string) (steamServer.Gamemode, error) {
var g steamServer.Gamemode
if err := s.Get(&g, `SELECT * FROM gamemodes WHERE name = $1`, name); err != nil {
return steamServer.Gamemode{}, fmt.Errorf("error getting gamemode id: %w", err)
}
return g, nil
}
func (s *GamemodeStore) Gamemodes() ([]steamServer.Gamemode, error) {
var gg []steamServer.Gamemode
var query = `SELECT * FROM gamemodes ORDER BY name ASC`
if err := s.Select(&gg, query); err != nil {
return []steamServer.Gamemode{}, fmt.Errorf("error gettings servers: %w", err)
}
return gg, nil
}

View File

@ -39,6 +39,15 @@ func (s *ServerStore) ServersByRegion(regionID uuid.UUID) ([]steamServer.Server,
return ss, nil
}
func (s *ServerStore) ServersByRegionAndGamemode(regionID uuid.UUID, gamemodeID uuid.UUID) ([]steamServer.Server, error) {
var ss []steamServer.Server
var query = `SELECT * FROM servers WHERE gamemode_id = $1 AND region_id = $2`
if err := s.Select(&ss, query, regionID, gamemodeID); err != nil {
return []steamServer.Server{}, fmt.Errorf("error gettings region servers: %w", err)
}
return ss, nil
}
func (s *ServerStore) ServersByGamemode(gamemodeID uuid.UUID) ([]steamServer.Server, error) {
var ss []steamServer.Server

View File

@ -21,6 +21,7 @@ func NewStore(dataSourceName string) (*Store, error) {
return &Store {
ServerStore: &ServerStore{DB: db},
RegionStore: &RegionStore{DB: db},
GamemodeStore: &GamemodeStore{DB: db},
}, nil
}
@ -28,5 +29,6 @@ func NewStore(dataSourceName string) (*Store, error) {
type Store struct {
*ServerStore
*RegionStore
*GamemodeStore
}

View File

@ -1,6 +1,8 @@
package steamServer
import "github.com/google/uuid"
import (
"github.com/google/uuid"
)
type Server struct {
ID uuid.UUID `db:"id"`
@ -19,10 +21,16 @@ type Region struct {
Name string `db:"name"`
}
type Gamemode struct {
ID uuid.UUID `db:"id"`
Name string `db:"name"`
}
type ServerStore interface {
Server(id uuid.UUID) (Server, error)
Servers() ([]Server, error)
ServersByRegion(regionID uuid.UUID) ([]Server, error)
ServersByRegionAndGamemode(regionID uuid.UUID, gamemodeID uuid.UUID) ([]Server, error)
ServersByGamemode(gamemodeID uuid.UUID) ([]Server, error)
CreateServer(ser *Server) error
UpdateServerByQuery(ser *Server, server_ip string) error
@ -32,7 +40,14 @@ type RegionStore interface {
Region(name string) (Region, error)
}
type GamemodeStore interface {
Gamemode(name string) (Gamemode, error)
Gamemodes() ([]Gamemode, error)
}
type Store interface {
ServerStore
RegionStore
GamemodeStore
}

View File

@ -6,6 +6,13 @@
</head>
<body class="h-screen overflow-hidden flex items-center 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>
@ -35,19 +42,21 @@
</a>
</div>
{{ $gamemodes := .Gamemodes}}
{{ $servers := .Servers}}
<div class="container mx-auto px-4 sm:px-8">
<div class="py-8">
<div>
<h2 class="text-2xl font-semibold leading-tight">Serverlist</h2>
</div>
{{range $gamemode := $gamemodes}}
<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">
Servername
{{$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
@ -59,27 +68,30 @@
</tr>
</thead>
<tbody>
{{range .Servers}}
{{range $server := $servers}}
{{if eq $gamemode.ID $server.GamemodeID}}
<tr>
<td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
<div class="flex items-center">
<div class="ml-3">
<a href="steam://connect/{{.ServerIP}}" class="text-gray-900 whitespace-no-wrap">{{.Name}}</a>
<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">{{.Map}}</p>
<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">{{.Players}}/{{.MaxPlayers}}</p>
<p class="text-gray-900 whitespace-no-wrap">{{$server.Players}}/{{$server.MaxPlayers}}</p>
</td>
</tr>
{{end}}
{{end}}
</tbody>
</table>
</div>
</div>
{{end}}
</div>
</div>
</body>

98
templates/region.html Normal file
View File

@ -0,0 +1,98 @@
<html lang="en"><head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://unpkg.com/tailwindcss@1.0.4/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="h-screen overflow-hidden flex items-center 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="#" 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">
<div>
<p class=" text-xs font-medium ">Asia</p>
</div>
</a>
</div>
{{ $gamemodes := .Gamemodes}}
{{ $servers := .Servers}}
<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}}
<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">
{{$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>
<td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
<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}}
</div>
</div>
</body>
</html>

View File

@ -33,6 +33,7 @@ type Handler struct {
func (h *Handler) Home() http.HandlerFunc {
type data struct {
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
}
tmpl := template.Must(template.ParseFiles("templates/home.html"))
@ -43,50 +44,74 @@ func (h *Handler) Home() http.HandlerFunc {
return
}
gg, err := h.store.Gamemodes()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
tmpl.Execute(w, data {
Servers: ss,
Gamemodes: gg,
})
}
}
func (h *Handler) Europe() http.HandlerFunc {
type data struct {
Region string
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
}
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 {
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
}
tmpl.Execute(w, data {
Region: "Europe",
Servers: ss,
Gamemodes: gg,
})
}
}
func (h *Handler) America() http.HandlerFunc {
type data struct {
Region string
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
}
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 {
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)
@ -94,7 +119,10 @@ func (h *Handler) America() http.HandlerFunc {
}
tmpl.Execute(w, data {
Region: "North America",
Servers: ss,
Gamemodes: gg,
})
}
}