diff --git a/cmd/main.go b/cmd/main.go index dbf72ec..6fd7a3c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,6 +1,7 @@ package main import ( + "time" "log" "net/http" @@ -24,20 +25,49 @@ func main() { log.Fatal("There is an issue with the databse: %w", err) } + var addedServers []string + for _, server := range addedServers { + println(server) + } for _, s := range ss { serverString := s.ServerIP - c := cron.New() - c.AddFunc("*/1 * * * *", func() { updateServers(serverString, store) }) - c.Start() + if !stringInSlice(s.ServerIP, addedServers) { + addedServers = append(addedServers, s.ServerIP) + c := cron.New() + c.AddFunc("*/1 * * * *", func() { updateServers(serverString, store) }) + c.Start() + } } + cronjob := cron.New() + cronjob.AddFunc("*/5 * * * *", func() { + for _, s := range ss { + serverString := s.ServerIP + if !stringInSlice(s.ServerIP, addedServers) { + addedServers = append(addedServers, s.ServerIP) + c := cron.New() + c.AddFunc("*/1 * * * *", func() { updateServers(serverString, store) }) + c.Start() + } + } + }) + cronjob.Start() h := web.NewHandler(store) http.ListenAndServe(":3000", h) } +func stringInSlice(a string, list []string) bool { + for _, b := range list { + if b == a { + return true + } + } + return false +} + func updateServers(host string, store steamServer.Store) { - client, err := a2s.NewClient(host) + client, err := a2s.NewClient(host, a2s.TimeoutOption(time.Second * 10)) if err != nil { log.Fatal("Creation of client failed: %w", err) @@ -50,6 +80,13 @@ func updateServers(host string, store steamServer.Store) { if err != nil { log.Printf("Querying failed: %s", err) + server := steamServer.Server { + Name: host, + Map: "Server didn't reply.", + Players: 0, + } + + store.UpdateServerByQuery(&server, host) return } diff --git a/postgres/gamemode_store.go b/postgres/gamemode_store.go index 17071d3..1b66a14 100644 --- a/postgres/gamemode_store.go +++ b/postgres/gamemode_store.go @@ -22,7 +22,7 @@ func (s *GamemodeStore) Gamemode(name string) (steamServer.Gamemode, error) { func (s *GamemodeStore) Gamemodes() ([]steamServer.Gamemode, error) { var gg []steamServer.Gamemode - var query = `SELECT * FROM gamemodes ORDER BY name ASC` + var query = `SELECT * FROM public.gamemodes ORDER BY CASE name WHEN 'Deathmatch' THEN 1 WHEN 'MGE' THEN 2 WHEN 'Jumpserver' THEN 3 WHEN 'Casual' THEN 4 END` if err := s.Select(&gg, query); err != nil { return []steamServer.Gamemode{}, fmt.Errorf("error gettings servers: %w", err) } diff --git a/templates/home.html b/templates/home.html index ff210f5..4e696a9 100644 --- a/templates/home.html +++ b/templates/home.html @@ -1,125 +1,70 @@ - -
-Europe
-Asia
-- - | -- {{$gamemode.Name}} - | -- Map - | -- Players - | - -
---|---|---|---|
- {{range $flag := $flags}} - {{if eq $server.ServerIP $flag.ServerIP}} - - {{end}} - {{end}} - | -
-
-
-
- {{$server.Name}}
-
- |
-
- {{$server.Map}} - |
-
- {{$server.Players}}/{{$server.MaxPlayers}} - |
-
+ + | ++ {{$gamemode.Name}} + | ++ Map + | ++ Players + | + +
---|---|---|---|
+ {{range $flag := $flags}} + {{if eq $server.ServerIP $flag.ServerIP}} + + {{end}} + {{end}} + | +
+
+
+
+ {{$server.Name}}
+
+ |
+
+ {{$server.Map}} + |
+
+ {{$server.Players}}/{{$server.MaxPlayers}} + |
+
Europe
+Asia
++ © 2021 Serverlist.tf +
+Europe
-Asia
-- - | -- {{$gamemode.Name}} - | -- Map - | -- Players - | - -
---|---|---|---|
- {{range $flag := $flags}} - {{if eq $server.ServerIP $flag.ServerIP}} - - {{end}} - {{end}} - | -
-
-
-
- {{$server.Name}}
-
- |
-
- {{$server.Map}} - |
-
- {{$server.Players}}/{{$server.MaxPlayers}} - |
-
+ + | ++ {{$gamemode.Name}} + | ++ Map + | ++ Players + | + +
---|---|---|---|
+ {{range $flag := $flags}} + {{if eq $server.ServerIP $flag.ServerIP}} + + {{end}} + {{end}} + | +
+
+
+
+ {{$server.Name}}
+
+ |
+
+ {{$server.Map}} + |
+
+ {{$server.Players}}/{{$server.MaxPlayers}} + |
+