fix used name

This commit is contained in:
Beric Bearnson 2024-10-24 18:15:39 -06:00
parent d25f9c987a
commit 48bb7b895e
2 changed files with 11 additions and 6 deletions

View File

@ -55,7 +55,12 @@ func LobbyListen() {
} }
go func() { go func() {
client, msgOut := l.InitialConnectionHandler(conn) client, msgOut, err := l.InitialConnectionHandler(conn)
if err != nil {
conn.Write(msgOut)
return
}
_, err = conn.Write(msgOut) _, err = conn.Write(msgOut)
if err != nil { if err != nil {
slog.Debug("error writing to new player... disconnecting") slog.Debug("error writing to new player... disconnecting")

View File

@ -303,7 +303,7 @@ func (l *Lobby) BroadcastToLobby(bytes []byte) {
l.lobbyMembers.Delete(player) 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) msg := make([]byte, 256)
nb, err := conn.Read(msg) nb, err := conn.Read(msg)
if err != nil { if err != nil {
@ -320,7 +320,7 @@ func (l *Lobby) InitialConnectionHandler(conn net.Conn) (Client, []byte) {
if err != nil { if err != nil {
slog.Error("error marshalling error message for incorrectly formatted username") slog.Error("error marshalling error message for incorrectly formatted username")
} }
return Client{}, msgOut return Client{}, msgOut, err
} }
_, ok := l.lobbyMembers.Load(n.Name) _, ok := l.lobbyMembers.Load(n.Name)
if ok { if ok {
@ -330,14 +330,14 @@ func (l *Lobby) InitialConnectionHandler(conn net.Conn) (Client, []byte) {
if err != nil { if err != nil {
slog.Error("error marshalling error on name already taken msg") slog.Error("error marshalling error on name already taken msg")
} }
return Client{}, msg return Client{}, msg, err
} }
h, err := Marshal(ConnectData{ h, err := Marshal(ConnectData{
From: n.Name, From: n.Name,
}, Connect) }, Connect)
if err != nil { if err != nil {
slog.Debug("error marshalling broadcast connect message on player connect", "error", err) 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) l.BroadcastToLobby(h)
@ -360,5 +360,5 @@ func (l *Lobby) InitialConnectionHandler(conn net.Conn) (Client, []byte) {
l.lobbyMembers.Store(n.Name, client) l.lobbyMembers.Store(n.Name, client)
return client, msgOut return client, msgOut, err
} }