Merge branch 'main' into 'main'

add code

See merge request spfn/match-result-collection-server!1
This commit is contained in:
Maple 2026-04-12 17:01:18 +02:00
commit e81cc35b19
3 changed files with 176 additions and 0 deletions

13
go.mod Normal file
View file

@ -0,0 +1,13 @@
module goresults
go 1.25.0
require github.com/jackc/pgx/v5 v5.9.1
require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/text v0.29.0 // indirect
)

26
go.sum Normal file
View file

@ -0,0 +1,26 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.9.1 h1:uwrxJXBnx76nyISkhr33kQLlUqjv7et7b9FjCen/tdc=
github.com/jackc/pgx/v5 v5.9.1/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

137
main.go Normal file
View file

@ -0,0 +1,137 @@
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/jackc/pgx/v5/pgxpool"
)
type CompetitionPostResults struct {
SplatfestID uint32 `json:"splatfest_id"`
Score uint32 `json:"score"`
TeamID uint8 `json:"team_id"`
TeamWin uint8 `json:"team_win"`
User uint32 `json:"user"`
}
var db *pgxpool.Pool
func main() {
var err error
connStr := os.Getenv("DATABASE_URL")
db, err = pgxpool.New(context.Background(), connStr)
if err != nil {
log.Fatalf("Unable to connect to database: %v\n", err)
}
defer db.Close()
http.HandleFunc("/results/get", handleGetResults)
http.HandleFunc("/results/votes", handleGetVotes)
http.HandleFunc("/results/post", handlePostResults)
log.Println("Server starting on :8000...")
log.Fatal(http.ListenAndServe(":8000", nil))
}
// GET /results/get?splatfest_id=<id>
func handleGetResults(w http.ResponseWriter, r *http.Request) {
festID := r.URL.Query().Get("splatfest_id")
rows, err := db.Query(context.Background(),
"SELECT splatfest_id, score, team_id, team_win, user_id FROM results WHERE splatfest_id = $1", festID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer rows.Close()
var results []CompetitionPostResults
for rows.Next() {
var res CompetitionPostResults
err := rows.Scan(&res.SplatfestID, &res.Score, &res.TeamID, &res.TeamWin, &res.User)
if err != nil {
continue
}
results = append(results, res)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(results)
}
// GET /results/votes?splatfest_id=<id>
func handleGetVotes(w http.ResponseWriter, r *http.Request) {
festID := r.URL.Query().Get("splatfest_id")
rows, err := db.Query(context.Background(),
"SELECT team_id, COUNT(user_id) FROM user_votes WHERE splatfest_id = $1 GROUP BY team_id ORDER BY team_id ASC", festID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer rows.Close()
// genuinely i can't be assed anymore
voteCounts := make(map[uint8]uint32)
maxID := 0
for rows.Next() {
var tid uint8
var count uint32
if err := rows.Scan(&tid, &count); err == nil {
voteCounts[tid] = count
if int(tid) > maxID {
maxID = int(tid)
}
}
}
resultArr := make([]string, maxID+1)
for i := 0; i <= maxID; i++ {
resultArr[i] = strconv.FormatUint(uint64(voteCounts[uint8(i)]), 10)
}
fmt.Fprintf(w, "[%s]", strings.Join(resultArr, ","))
}
// POST /results/post
func handlePostResults(w http.ResponseWriter, r *http.Request) {
var res CompetitionPostResults
if err := json.NewDecoder(r.Body).Decode(&res); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
ctx := context.Background()
tx, err := db.Begin(ctx)
if err != nil {
http.Error(w, "Transaction error", http.StatusInternalServerError)
return
}
defer tx.Rollback(ctx)
_, err = tx.Exec(ctx,
"INSERT INTO results (splatfest_id, score, team_id, team_win, user_id) VALUES ($1, $2, $3, $4, $5)",
res.SplatfestID, res.Score, res.TeamID, res.TeamWin, res.User)
if err != nil {
log.Printf("DB Error results: %v", err)
return
}
_, err = tx.Exec(ctx,
"INSERT INTO user_votes (user_id, splatfest_id, team_id) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING",
res.User, res.SplatfestID, res.TeamID)
if err != nil {
log.Printf("DB Error votes: %v", err)
return
}
tx.Commit(ctx)
w.WriteHeader(http.StatusOK)
}