From 8c6ab34e6a891637cd5c43823e715c35436dd96c Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Mon, 28 Feb 2022 13:21:28 +0100 Subject: [PATCH] Fetch player pos from postgres --- cmd/mtwebmapper/players.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/cmd/mtwebmapper/players.go b/cmd/mtwebmapper/players.go index 72c0982..bc9485f 100644 --- a/cmd/mtwebmapper/players.go +++ b/cmd/mtwebmapper/players.go @@ -7,6 +7,8 @@ package main import ( "bufio" "bytes" + "context" + "database/sql" "encoding/json" "html/template" "log" @@ -16,6 +18,8 @@ import ( "sort" "sync" "time" + + "bitbucket.org/s_l_teichmann/mtsatellite/common" ) const sleepInterval = time.Second * 5 @@ -78,7 +82,38 @@ func (pls sortPlayersByName) Swap(i, j int) { pls[i], pls[j] = pls[j], pls[i] } +const selectPlayersSQL = ` +SELECT posx, posy, posz, name +FROM player +WHERE modification_date > now() - '2m'::interval` + +func playersFromPostgreSQL(connS string) ([]*player, error) { + db, err := sql.Open("pgx", connS) + if err != nil { + return nil, err + } + defer db.Close() + rows, err := db.QueryContext(context.Background(), selectPlayersSQL) + if err != nil { + return nil, err + } + defer rows.Close() + var pls []*player + for rows.Next() { + var p player + if err := rows.Scan(&p.Y, &p.Y, &p.Z, &p.Name); err != nil { + return nil, err + } + pls = append(pls, &p) + } + return pls, rows.Err() +} + func (ps *players) readFromFIFO() ([]*player, error) { + if host, ok := common.IsPostgreSQL(ps.fifo); ok { + return playersFromPostgreSQL(host) + } + file, err := os.Open(ps.fifo) if err != nil { return nil, err