2024-08-14 23:33:05 -06:00
|
|
|
package netwrk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
2024-08-15 20:08:23 -06:00
|
|
|
func ConnectToLobby(username string) (net.Conn, error) {
|
2024-08-14 23:33:05 -06:00
|
|
|
conn, err := net.Dial("tcp", "127.0.0.1:12345")
|
|
|
|
if err != nil {
|
2024-08-15 20:08:23 -06:00
|
|
|
return nil, fmt.Errorf("Sorry, failed to connect to server...")
|
2024-08-14 23:33:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
loginMsg, err := proto.Marshal(&LobbyMessage{Type: "name", Content: username})
|
|
|
|
if err != nil {
|
2024-08-15 20:08:23 -06:00
|
|
|
return nil, fmt.Errorf("Sorry bro but your username is wack AF...")
|
2024-08-14 23:33:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Write(loginMsg)
|
|
|
|
if err != nil {
|
2024-08-15 20:08:23 -06:00
|
|
|
return nil, fmt.Errorf("Sorry, could not communicate with server...")
|
2024-08-14 23:33:05 -06:00
|
|
|
}
|
|
|
|
|
2024-08-15 20:08:23 -06:00
|
|
|
return conn, nil
|
2024-08-14 23:33:05 -06:00
|
|
|
}
|