stream-score/backend/status.go

96 lines
1.6 KiB
Go
Raw Normal View History

2025-02-25 16:26:52 +01:00
package main
import (
"bytes"
"io"
"log"
"net/http"
"github.com/gin-gonic/gin"
)
type Status struct {
Surfers []Surfer `json:"surfers"`
Round Info `json:"round"`
2025-03-03 11:33:10 +01:00
Mins int `json:"mins"`
Secs int `json:"secs"`
2025-02-25 16:26:52 +01:00
Count int `json:"surfersCount"`
}
type Surfer struct {
Id string `json:"id"`
Name string `json:"name"`
Color string `json:"color"`
Priority string `json:"priority"`
Score int `json:"score"`
Delete bool `json:"delete"`
}
type Info struct {
Round string `json:"round"`
Heat int `json:"heat"`
Category string `json:"category"`
Time int `json:"time"`
}
var status Status
func initStatus() interface{} {
return Status{
Surfers: []Surfer{
{
Name: "",
Color: "lightgray",
Priority: "",
Score: 0,
2025-03-03 11:33:10 +01:00
Delete: false,
2025-02-25 16:26:52 +01:00
},
2025-03-03 11:33:10 +01:00
{
Name: "",
Color: "lightgray",
Priority: "",
Score: 0,
Delete: false,
}
2025-02-25 16:26:52 +01:00
},
Round: Info{
Round: "",
Heat: 1,
Category: "",
Time: 0,
},
2025-03-03 11:33:10 +01:00
Mins: 0,
Secs: 0,
Count: 2,
2025-02-25 16:26:52 +01:00
}
}
func GetStatus(c *gin.Context) {
c.JSON(http.StatusOK, status)
log.Printf("get status: %+v", status)
}
func SetStatus(c *gin.Context) {
var stat = Status{}
body, _ := io.ReadAll(c.Request.Body)
log.Printf("set status body: %s", string(body))
c.Request.Body = io.NopCloser(bytes.NewReader(body))
err := c.ShouldBindJSON(&stat)
log.Printf("set status: %+v - err: %+v", stat, err)
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"message": stat,
})
var msg = Message{
Data: stat,
}
broadcast <- msg
status = stat
}