69 lines
1.5 KiB
Go
Executable File
69 lines
1.5 KiB
Go
Executable File
package websocket
|
|
|
|
import (
|
|
"fmt"
|
|
partydata "legatus-rme/backend/partyData"
|
|
"log"
|
|
"net/http"
|
|
"slices"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
func paintIt(color int) string {
|
|
hex := fmt.Sprintf("%06x", color+16777216)
|
|
return "#" + hex
|
|
}
|
|
|
|
func (s *Server) partyData(w http.ResponseWriter, r *http.Request) {
|
|
c, err := s.Upgrader.Upgrade(w, r, nil)
|
|
if err != nil {
|
|
log.Println("Websocket Server - Error upgrading connection:", err)
|
|
return
|
|
}
|
|
defer c.Close()
|
|
|
|
for {
|
|
var req PartyDataWebsocketRequest
|
|
if err := c.ReadJSON(&req); err != nil {
|
|
c.WriteJSON(&ErrorWebsocketResponse{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
err = validator.New().Struct(req)
|
|
if err != nil {
|
|
c.WriteJSON(&ErrorWebsocketResponse{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
if slices.Contains(s.ExerciseIds, req.ExcerciseId) {
|
|
partyDataSocket := OpenWebsocket("partyBaseData", []int{s.ExerciseServicePortMap[fmt.Sprintf("%d-ParticipantManagement1", req.ExcerciseId)]}).(partydata.PartyData)
|
|
partyData := partyDataSocket.GetPartyData()
|
|
|
|
var res []PartyDataWebsocketResponse
|
|
for _, party := range partyData.Data {
|
|
if party.ID == 0 || party.ID == 1 {
|
|
continue
|
|
}
|
|
|
|
colorInHex := paintIt(party.Color)
|
|
|
|
res = append(res, PartyDataWebsocketResponse{
|
|
Id: party.ID,
|
|
Party: party.Name,
|
|
Color: colorInHex,
|
|
})
|
|
}
|
|
|
|
c.WriteJSON(&res)
|
|
} else {
|
|
c.WriteJSON(&ErrorWebsocketResponse{
|
|
Error: "Invalid exerciseId",
|
|
})
|
|
continue
|
|
}
|
|
}
|
|
}
|