From 48bb7b895e12c0177c5cbe12bf77296fdb939939 Mon Sep 17 00:00:00 2001 From: Beric Bearnson <37596980+bericyb@users.noreply.github.com> Date: Thu, 24 Oct 2024 18:15:39 -0600 Subject: [PATCH] fix used name --- cmd/server/main.go | 7 ++++++- internal/lobby/lobby.go | 10 +++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 108ae69..d00e930 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -55,7 +55,12 @@ func LobbyListen() { } go func() { - client, msgOut := l.InitialConnectionHandler(conn) + client, msgOut, err := l.InitialConnectionHandler(conn) + if err != nil { + conn.Write(msgOut) + return + } + _, err = conn.Write(msgOut) if err != nil { slog.Debug("error writing to new player... disconnecting") diff --git a/internal/lobby/lobby.go b/internal/lobby/lobby.go index 1429140..832f55c 100644 --- a/internal/lobby/lobby.go +++ b/internal/lobby/lobby.go @@ -303,7 +303,7 @@ func (l *Lobby) BroadcastToLobby(bytes []byte) { l.lobbyMembers.Delete(player) } } -func (l *Lobby) InitialConnectionHandler(conn net.Conn) (Client, []byte) { +func (l *Lobby) InitialConnectionHandler(conn net.Conn) (Client, []byte, error) { msg := make([]byte, 256) nb, err := conn.Read(msg) if err != nil { @@ -320,7 +320,7 @@ func (l *Lobby) InitialConnectionHandler(conn net.Conn) (Client, []byte) { if err != nil { slog.Error("error marshalling error message for incorrectly formatted username") } - return Client{}, msgOut + return Client{}, msgOut, err } _, ok := l.lobbyMembers.Load(n.Name) if ok { @@ -330,14 +330,14 @@ func (l *Lobby) InitialConnectionHandler(conn net.Conn) (Client, []byte) { if err != nil { slog.Error("error marshalling error on name already taken msg") } - return Client{}, msg + return Client{}, msg, err } h, err := Marshal(ConnectData{ From: n.Name, }, Connect) if err != nil { slog.Debug("error marshalling broadcast connect message on player connect", "error", err) - return Client{Username: n.Name, Conn: conn}, h + return Client{Username: n.Name, Conn: conn}, h, err } l.BroadcastToLobby(h) @@ -360,5 +360,5 @@ func (l *Lobby) InitialConnectionHandler(conn net.Conn) (Client, []byte) { l.lobbyMembers.Store(n.Name, client) - return client, msgOut + return client, msgOut, err }