Initial commit

This commit is contained in:
torsten committed 2026-08-13 18:33:10 +02:00
commit b7d90b9afe
16245 files changed
+3396666

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
package websocket
import (
"log"
"net/http"
)
func (s *Server) exerciseIds(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()
c.WriteJSON(&s.ExerciseIds)
}
+41
View File
@@ -0,0 +1,41 @@
package websocket
import (
"fmt"
participanthandler "legatus-rme/backend/participantHandler"
partydata "legatus-rme/backend/partyData"
serviceportsinformation "legatus-rme/backend/servicePortsInformation"
typedata "legatus-rme/backend/typeData"
"log"
"net/url"
"github.com/gorilla/websocket"
)
func OpenWebsocket(websocketType string, port []int) interface{} {
var sockets []*websocket.Conn
for _, port := range port {
u := url.URL{Scheme: "ws", Host: fmt.Sprintf("localhost:%d", port)}
socket, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("Error connecting to websocket:", err)
}
sockets = append(sockets, socket)
}
switch websocketType {
case "servicePortsInformation":
return serviceportsinformation.ServicePortsInformation{Websocket: sockets[0]}
case "typeBaseData":
return typedata.TypeData{Websocket: sockets[0]}
case "partyBaseData":
return partydata.PartyData{Websocket: sockets[0]}
case "participantHandler":
return participanthandler.ParticipantHandler{ParticipantManagement: sockets[0], MariusSocket: sockets[1]}
default:
log.Fatal("Invalid websocket type")
return nil
}
}
@@ -0,0 +1,20 @@
package websocket
import (
"log"
"net/http"
"github.com/gorilla/websocket"
)
func OpenWebsocketServer(s Server) {
s.Upgrader = &websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
http.HandleFunc("/ports", s.ports)
http.HandleFunc("/exerciseIds", s.exerciseIds)
http.HandleFunc("/partyData", s.partyData)
http.HandleFunc("/participantHandler", s.participantHandler)
log.Fatal("Websocket Server - Error while serving websockt server:", http.ListenAndServe("localhost:6969", nil))
}
@@ -0,0 +1,85 @@
package websocket
import (
"fmt"
participanthandler "legatus-rme/backend/participantHandler"
"log"
"net/http"
"github.com/go-playground/validator/v10"
)
func (s *Server) participantHandler(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 ParticipantHandlerWebsocketRequestWithoutData
if err := c.ReadJSON(&req); err != nil {
c.WriteJSON(&ErrorWebsocketResponse{
Error: err.Error(),
})
return
}
if req.Type != "getAllParticipants" {
req2 := ParticipantHandlerWebsocketRequest(req)
err = validator.New().Struct(req2)
if err != nil {
c.WriteJSON(&ErrorWebsocketResponse{
Error: err.Error(),
})
return
}
} else {
err = validator.New().Struct(req)
if err != nil {
c.WriteJSON(&ErrorWebsocketResponse{
Error: err.Error(),
})
return
}
}
var (
participantHandlerByExcersiseId participanthandler.ParticipantHandler
ok bool
)
if participantHandlerByExcersiseId, ok = s.ParticipantHandlers[req.ExcerciseId]; !ok {
participantHandlerByExcersiseId = OpenWebsocket("participantHandler", []int{s.ExerciseServicePortMap[fmt.Sprintf("%d-ParticipantManagement1", req.ExcerciseId)], req.ExcerciseId * 102}).(participanthandler.ParticipantHandler)
}
switch req.Type {
case "checkIfParticipantIsValid":
participantData := participantHandlerByExcersiseId.CheckIfParticipantIsValid(req.ParticipantData.ParticipantId)
c.WriteJSON(&participantData)
continue
case "getAllParticipants":
participantData := participantHandlerByExcersiseId.GetAllParticipants()
c.WriteJSON(&participantData)
continue
case "changeAbcProtection":
if err := participantHandlerByExcersiseId.ChangeAbcProtection(req.ParticipantData.ParticipantId, req.ParticipantData.AbcProtection); err != nil {
c.WriteJSON(&ErrorWebsocketResponse{
Error: err.Error(),
})
}
c.WriteJSON(req.ParticipantData)
continue
case "changePosition":
if err := participantHandlerByExcersiseId.ChangePosition(req.ParticipantData.ParticipantId, &participanthandler.GeoPosition{
Lat: req.ParticipantData.Positions[0].Lat,
Lng: req.ParticipantData.Positions[0].Lng,
}); err != nil {
c.WriteJSON(&ErrorWebsocketResponse{
Error: err.Error(),
})
}
continue
}
}
}
+68
View File
@@ -0,0 +1,68 @@
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
}
}
}
+17
View File
@@ -0,0 +1,17 @@
package websocket
import (
"log"
"net/http"
)
func (s *Server) ports(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()
c.WriteJSON(&s.ExerciseServicePortMap)
}
+56
View File
@@ -0,0 +1,56 @@
package websocket
import (
participanthandler "legatus-rme/backend/participantHandler"
typedata "legatus-rme/backend/typeData"
"github.com/gorilla/websocket"
)
type Server struct {
ExerciseIds []int
ExerciseServicePortMap map[string]int
TypeData typedata.TypeDataResponse
ParticipantHandlers map[int]participanthandler.ParticipantHandler
Upgrader *websocket.Upgrader
}
type PartyDataWebsocketResponse struct {
Id int `json:"id"`
Party string `json:"party"`
Color string `json:"color"`
}
type PartyDataWebsocketRequest struct {
ExcerciseId int `json:"excerciseId" validate:"required"`
}
type ParticipantHandlerWebsocketRequestWithoutData struct {
Type string `json:"type" validate:"required"`
ExcerciseId int `json:"excerciseId" validate:"required"`
ParticipantData struct {
ParticipantId int `json:"participantId"`
Positions []struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"positions"`
AbcProtection bool `json:"abcProtection"`
} `json:"participantData"`
}
type ParticipantHandlerWebsocketRequest struct {
Type string `json:"type" validate:"required"`
ExcerciseId int `json:"excerciseId" validate:"required"`
ParticipantData struct {
ParticipantId int `json:"participantId" validate:"required"`
Positions []struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"positions"`
AbcProtection bool `json:"abcProtection"`
} `json:"participantData" validate:"required"`
}
type ErrorWebsocketResponse struct {
Error string `json:"error"`
}