package structs import ( "errors" "sync" "github.com/gofiber/websocket/v2" "krakatoa.net/backend/modules/logger" ) type WebClientDb struct { Uuid string VoiceSessionToken string MobileSessionToken string } type A struct { WebCmdID int CmdIDFromMinecraftServer int DestFromMinecraftServer int } type WebClient struct { Uuid string MobileConn *websocket.Conn mobileConnMu sync.Mutex MobileCmdIDs []int // cache for received cmdIDs mobileCmdIDMu sync.Mutex MobileCmdIDsByBackend []*A mobileCmdIDsByBackendMu sync.Mutex CurrentMobileVoiceCmdIDIndexByBackend int VoiceConn *websocket.Conn voiceConnMu sync.Mutex VoiceCmdIDs []int // cache for received cmdIDs voiceCmdIDMu sync.Mutex VoiceCmdIDsByBackend []*A // messages from backend to voice, when response from voice then response to target requester voiceCmdIDsByBackendMu sync.Mutex CurrentVoiceCmdIDIndexByBackend int SendVoiceQueueMessages []*SendQueueMessage SendMobileQueueMessages []*SendQueueMessage } func (webClient *WebClient) SendBinaryMessage(conn *websocket.Conn, msg []byte) error { if conn == webClient.MobileConn { webClient.mobileConnMu.Lock() defer webClient.mobileConnMu.Unlock() return conn.WriteMessage(websocket.BinaryMessage, msg) } webClient.voiceConnMu.Lock() defer webClient.voiceConnMu.Unlock() return conn.WriteMessage(websocket.BinaryMessage, msg) } func (webClient *WebClient) RemoveVoiceCmdID(cmdID int) { webClient.voiceCmdIDMu.Lock() defer webClient.voiceCmdIDMu.Unlock() newArr, err := removeCmdIDFromList(webClient.VoiceCmdIDs, cmdID) if err != nil { logger.WebVoice.Warnln("removeCmdIDFromList:", err) } else { webClient.VoiceCmdIDs = newArr } } func (webClient *WebClient) RemoveMobileCmdID(cmdID int) { webClient.mobileCmdIDMu.Lock() defer webClient.mobileCmdIDMu.Unlock() newArr, err := removeCmdIDFromList(webClient.MobileCmdIDs, cmdID) if err != nil { logger.WebMobile.Warnln("removeCmdIDFromList:", err) } else { webClient.MobileCmdIDs = newArr } } func (webClient *WebClient) RemoveVoiceCmdIDByBackend(cmdID int) { webClient.voiceCmdIDsByBackendMu.Lock() defer webClient.voiceCmdIDsByBackendMu.Unlock() for i, data := range webClient.VoiceCmdIDsByBackend { if data.WebCmdID == cmdID { newArr := append(webClient.VoiceCmdIDsByBackend[:i], webClient.VoiceCmdIDsByBackend[i+1:]...) webClient.VoiceCmdIDsByBackend = newArr return } } } func (webClient *WebClient) RemoveMobileCmdIDByBackend(cmdID int) { webClient.mobileCmdIDsByBackendMu.Lock() defer webClient.mobileCmdIDsByBackendMu.Unlock() for i, data := range webClient.MobileCmdIDsByBackend { if data.WebCmdID == cmdID { newArr := append(webClient.MobileCmdIDsByBackend[:i], webClient.MobileCmdIDsByBackend[i+1:]...) webClient.MobileCmdIDsByBackend = newArr return } } } func removeCmdIDFromList(arr []int, cmdID int) ([]int, error) { index := getCmdIDIndexFromList(arr, cmdID) if index == -1 { return []int{}, errors.New("index not found") } newArr := append(arr[:index], arr[index+1:]...) return newArr, nil } func getCmdIDIndexFromList(arr []int, cmdID int) int { for i := 0; i < len(arr); i++ { if arr[i] == cmdID { return i } } return -1 } func GenerateWebCmdID(currentIndex int) int { if currentIndex >= 250 || currentIndex < 10 { return 10 } currentIndex++ return currentIndex }