Skip to content

Commit

Permalink
feat: removed some logs and fixed connection closed websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
sgatu committed May 4, 2024
1 parent c8969be commit f145c93
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
34 changes: 16 additions & 18 deletions handlers/playHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func (ph *PlayHandler) Play(c *gin.Context) {
handlers_messages.PushGameNotFoundMessage(c, idParam)
return
}
fmt.Printf("session found %+v\n", session)
gameEntity, err := ph.gameRepository.GetGame(id)
if err != nil {
handlers_messages.PushGameNotFoundMessage(c, idParam)
Expand All @@ -45,12 +44,10 @@ func (ph *PlayHandler) Play(c *gin.Context) {
if gameEntity.BlackPlayer() != session.UserId && gameEntity.WhitePlayer() != session.UserId {
if gameEntity.BlackPlayer() == 0 {
requiresUpdate = true
fmt.Printf("Setting black player to %+v\n", session.UserId)
gameEntity.SetBlackPlayer(session.UserId)
}
if gameEntity.WhitePlayer() == 0 {
requiresUpdate = true
fmt.Printf("Setting white player to %+v\n", session.UserId)
gameEntity.SetWhitePlayer(session.UserId)
}
ph.gameRepository.SaveGame(gameEntity)
Expand All @@ -66,6 +63,15 @@ func (ph *PlayHandler) Play(c *gin.Context) {
return
}

getRelation := func(g *models.Game) string {
relation := "observer"
if g.BlackPlayer() == session.UserId {
relation = "black"
} else if g.WhitePlayer() == session.UserId {
relation = "white"
}
return relation
}
go func(playerId int64) {
observeChan := make(chan *game.MoveResult)
errorCh := make(chan error)
Expand All @@ -77,12 +83,7 @@ func (ph *PlayHandler) Play(c *gin.Context) {
defer conn.Close()
defer liveGameState.RemoveObserver(observeChan)
aux := atomic.Uint32{}
relation := "observer"
if gameEntity.BlackPlayer() == session.UserId {
relation = "black"
} else if gameEntity.WhitePlayer() == session.UserId {
relation = "white"
}
relation := getRelation(gameEntity)
initMessage, err := json.Marshal(struct {
Type string `json:"type"`
Relation string `json:"relation"`
Expand All @@ -96,16 +97,14 @@ func (ph *PlayHandler) Play(c *gin.Context) {
case <-ticker.C:
conn.SetReadDeadline(time.Now().Add(time.Millisecond * 500))
message, err := wsutil.ReadClientMessage(conn, nil)
if err != nil || len(message[len(message)-1].Payload) == 0 {
if err == nil && message[len(message)-1].OpCode == ws.OpClose {
fmt.Printf("client closed connection\n")
// client closed the connection
return
}
// fmt.Printf("No message received...%+v - %+v\n", err, message)
if err == nil && len(message) > 0 && message[len(message)-1].OpCode == ws.OpClose {
// client closed connection
return
}
if err != nil {
fmt.Println(err)
continue
}
fmt.Println("New message", string(message[len(message)-1].Payload))
liveGameState.ExecuteMove(services.MoveMessage{Move: string(message[len(message)-1].Payload), ErrorsChannel: errorCh, Who: playerId})
newVal := aux.Add(1)
if newVal == 100 {
Expand All @@ -125,7 +124,6 @@ func (ph *PlayHandler) Play(c *gin.Context) {
err = wsutil.WriteServerMessage(conn, ws.OpText, []byte(outputMessage))
if err == nil {
fmt.Println("Got movement, sent to player", move)
// return
}
case error := <-errorCh:
if ferr, ok := error.(*errors.InvalidMoveError); ok {
Expand Down
2 changes: 0 additions & 2 deletions services/gameManagerService.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,9 @@ func (lgs *LiveGameState) ExecuteMove(move MoveMessage) {
func (lgs *LiveGameState) startAwaitingMoves() {
go func() {
for move := range lgs.chCommandsChannel {
fmt.Printf("Received move: %+v\n", move)
result, err := lgs.game.UpdateGame(move.Who, move.Move)
if err == nil {
lgs.notifyMoveObservers(result)
fmt.Printf("Saving game")
lgs.gameManager.gameRepository.SaveGame(lgs.game)
} else {
fmt.Println("Could not execute move due to ", err)
Expand Down

0 comments on commit f145c93

Please sign in to comment.