diff --git a/be-jeopardy/internal/handlers/handlers.go b/be-jeopardy/internal/handlers/handlers.go index 9c64275..019276e 100644 --- a/be-jeopardy/internal/handlers/handlers.go +++ b/be-jeopardy/internal/handlers/handlers.go @@ -254,41 +254,28 @@ func JoinPublicGame(c *gin.Context) { func JoinGameChat(c *gin.Context) { log.Infof("Received request to join game chat") - ws, err := upgrader.Upgrade(c.Writer, c.Request, nil) - if err != nil { - log.Errorf("Error upgrading connection to WebSocket: %s", err.Error()) - respondWithError(c, http.StatusInternalServerError, ErrJoiningChatMsg) - return - } - - _, msg, err := ws.ReadMessage() + token, err := c.Cookie(jwtCookie) if err != nil { - log.Errorf("Error reading message from WebSocket: %s", err.Error()) - closeConnWithMsg(ws, socket.ServerError, ErrJoiningChatMsg) - return - } - - var req TokenRequest - if err := json.Unmarshal(msg, &req); err != nil { - log.Errorf("Error parsing chat request: %s", err.Error()) - closeConnWithMsg(ws, socket.BadRequest, ErrMalformedReqMsg) + log.Errorf("Error getting token from cookie: %s", err.Error()) + respondWithError(c, http.StatusForbidden, ErrInvalidAuthCredMsg) return } - playerId, err := auth.GetJWTSubject(req.Token) + playerId, err := auth.GetJWTSubject(token) if err != nil { log.Errorf(ErrGettingPlayerIdMsg, err.Error()) - closeConnWithMsg(ws, socket.Unauthorized, ErrInvalidAuthCredMsg) + respondWithError(c, http.StatusUnauthorized, ErrInvalidAuthCredMsg) return } - conn := socket.NewSafeConn(ws) - err = jeopardy.JoinGameChat(playerId, conn) + ws, err := upgrader.Upgrade(c.Writer, c.Request, nil) if err != nil { - log.Errorf("Error joining chat: %s", err.Error()) - closeConnWithMsg(ws, socket.BadRequest, "Unable to join chat: %s", err.Error()) + log.Errorf("Error upgrading connection to WebSocket: %s", err.Error()) + respondWithError(c, http.StatusInternalServerError, ErrJoiningChatMsg) return } + + jeopardy.JoinGameChat(playerId, ws) } func AddBot(c *gin.Context) { @@ -318,41 +305,28 @@ func AddBot(c *gin.Context) { func PlayGame(c *gin.Context) { log.Infof("Received play request") - ws, err := upgrader.Upgrade(c.Writer, c.Request, nil) - if err != nil { - log.Errorf("Error upgrading connection to WebSocket: %s", err.Error()) - respondWithError(c, http.StatusInternalServerError, UnexpectedServerErrMsg) - return - } - - _, msg, err := ws.ReadMessage() + token, err := c.Cookie(jwtCookie) if err != nil { - log.Errorf("Error reading message from WebSocket: %s", err.Error()) - closeConnWithMsg(ws, socket.ServerError, UnexpectedServerErrMsg) - return - } - - var req TokenRequest - if err := json.Unmarshal(msg, &req); err != nil { - log.Errorf("Error parsing play request: %s", err.Error()) - closeConnWithMsg(ws, socket.BadRequest, ErrMalformedReqMsg) + log.Errorf("Error getting token from cookie: %s", err.Error()) + respondWithError(c, http.StatusForbidden, ErrInvalidAuthCredMsg) return } - playerId, err := auth.GetJWTSubject(req.Token) + playerId, err := auth.GetJWTSubject(token) if err != nil { log.Errorf(ErrGettingPlayerIdMsg, err.Error()) - closeConnWithMsg(ws, socket.Unauthorized, ErrInvalidAuthCredMsg) + respondWithError(c, http.StatusUnauthorized, ErrInvalidAuthCredMsg) return } - conn := socket.NewSafeConn(ws) - err = jeopardy.PlayGame(playerId, conn) + ws, err := upgrader.Upgrade(c.Writer, c.Request, nil) if err != nil { - log.Errorf("Error playing game: %s", err.Error()) - closeConnWithMsg(ws, socket.BadRequest, "Unable to play game: %s", err.Error()) + log.Errorf("Error upgrading connection to WebSocket: %s", err.Error()) + respondWithError(c, http.StatusInternalServerError, UnexpectedServerErrMsg) return } + + jeopardy.PlayGame(playerId, ws) } func PlayAgain(c *gin.Context) { @@ -441,11 +415,6 @@ func parseBody(body io.ReadCloser, v any) error { return json.Unmarshal(msg, v) } -func closeConnWithMsg(conn *websocket.Conn, code int, msg string, args ...any) { - _ = conn.WriteJSON(jeopardy.Response{Code: code, Message: fmt.Sprintf(msg, args...)}) - _ = conn.Close() -} - func respondWithError(c *gin.Context, code int, msg string, args ...any) { c.JSON(code, jeopardy.Response{Code: code, Message: fmt.Sprintf(msg, args...)}) } diff --git a/be-jeopardy/internal/jeopardy/chat.go b/be-jeopardy/internal/jeopardy/chat.go index 9c9a505..90366af 100644 --- a/be-jeopardy/internal/jeopardy/chat.go +++ b/be-jeopardy/internal/jeopardy/chat.go @@ -7,6 +7,7 @@ import ( "github.com/gorilla/websocket" "github.com/rileythomp/jeopardy/be-jeopardy/internal/log" + "github.com/rileythomp/jeopardy/be-jeopardy/internal/socket" ) type ChatMessage struct { @@ -15,25 +16,31 @@ type ChatMessage struct { TimeStamp int64 `json:"timeStamp"` } -func JoinGameChat(playerId string, conn SafeConn) error { +func JoinGameChat(playerId string, ws *websocket.Conn) { + conn := socket.NewSafeConn(ws) + game, err := GetPlayerGame(playerId) if err != nil { - return err + log.Errorf("Error getting game for player %s: %s", playerId, err.Error()) + closeConnWithMsg(conn, socket.BadRequest, "Unable to join chat: %s", err.Error()) + return } player, err := game.getPlayerById(playerId) if err != nil { - return err + log.Errorf("Error getting player by id: %s", err.Error()) + closeConnWithMsg(conn, socket.BadRequest, "Unable to join chat: %s", err.Error()) + return } if player.chatConn() != nil { - return fmt.Errorf("Player already in chat") + log.Errorf("Error joining chat: Player already in chat") + closeConnWithMsg(conn, socket.BadRequest, "Unable to join chat: Player already in chat") + return } player.setChatConn(conn) player.sendChatPings() player.processChatMessages(game.chatChan) - - return nil } func (p *Player) processChatMessages(chatChan chan ChatMessage) { diff --git a/be-jeopardy/internal/jeopardy/jeopardy.go b/be-jeopardy/internal/jeopardy/jeopardy.go index 3da9041..c565a0b 100644 --- a/be-jeopardy/internal/jeopardy/jeopardy.go +++ b/be-jeopardy/internal/jeopardy/jeopardy.go @@ -5,6 +5,7 @@ import ( "time" "github.com/google/uuid" + "github.com/gorilla/websocket" "github.com/rileythomp/jeopardy/be-jeopardy/internal/db" "github.com/rileythomp/jeopardy/be-jeopardy/internal/log" "github.com/rileythomp/jeopardy/be-jeopardy/internal/socket" @@ -167,26 +168,32 @@ func AddBot(playerId string) error { return nil } -func PlayGame(playerId string, conn SafeConn) error { +func PlayGame(playerId string, ws *websocket.Conn) { + conn := socket.NewSafeConn(ws) + game, err := GetPlayerGame(playerId) if err != nil { - return err + log.Errorf("Error getting game for player: %s", err.Error()) + closeConnWithMsg(conn, socket.BadRequest, "Unable to play game: %s", err.Error()) + return } player, err := game.getPlayerById(playerId) if err != nil { - return err + log.Errorf("Error getting player by id: %s", err.Error()) + closeConnWithMsg(conn, socket.BadRequest, "Unable to play game: %s", err.Error()) + return } if player.conn() != nil { - return fmt.Errorf("Player already playing") + log.Errorf("Error playing game: Player already playing") + closeConnWithMsg(conn, socket.BadRequest, "Unable to play game: Player already playing") + return } player.setConn(conn) player.sendPings() player.readMessages(game.msgChan, game.pauseChan) game.handlePlayerJoined(player) - - return nil } func (g *Game) handlePlayerJoined(player GamePlayer) { @@ -285,3 +292,8 @@ func removeGame(g *Game) { delete(playerGames, p.id()) } } + +func closeConnWithMsg(conn SafeConn, code int, msg string, args ...any) { + _ = conn.WriteJSON(Response{Code: code, Message: fmt.Sprintf(msg, args...)}) + _ = conn.Close() +} diff --git a/fe-jeopardy/src/app/game/chat/chat.component.ts b/fe-jeopardy/src/app/game/chat/chat.component.ts index d848bef..2df5659 100644 --- a/fe-jeopardy/src/app/game/chat/chat.component.ts +++ b/fe-jeopardy/src/app/game/chat/chat.component.ts @@ -25,10 +25,6 @@ export class ChatComponent implements OnInit, AfterViewChecked { ngOnInit(): void { this.chatService.Connect() - this.chatService.OnOpen(() => { - this.chatService.Send({ token: this.jwt }) - }) - this.chatService.OnMessage((event: { data: string }) => { let resp = JSON.parse(event.data) diff --git a/fe-jeopardy/src/app/game/game.component.ts b/fe-jeopardy/src/app/game/game.component.ts index 2157a66..06a81f2 100644 --- a/fe-jeopardy/src/app/game/game.component.ts +++ b/fe-jeopardy/src/app/game/game.component.ts @@ -52,13 +52,6 @@ export class GameComponent implements OnInit { this.websocketService.Connect('play') - this.websocketService.OnOpen(() => { - this.websocketService.Send({ - state: GameState.PreGame, - token: this.jwt, - }) - }) - this.websocketService.OnMessage((event: { data: string }) => { let resp = JSON.parse(event.data)