working lobby-ish

This commit is contained in:
Beric Bearnson 2024-08-23 11:59:19 -06:00
parent e90b9c26b1
commit e5df746e7f
6 changed files with 94 additions and 35 deletions

View File

@ -25,7 +25,7 @@ func main() {
if err != nil { if err != nil {
log.Panic("Bro your input is no good...") log.Panic("Bro your input is no good...")
} }
username := string(buf[:n]) username := string(buf[:n-1])
conn, err := netwrk.ConnectToLobby(username) conn, err := netwrk.ConnectToLobby(username)
if err != nil { if err != nil {
@ -43,6 +43,9 @@ func main() {
} }
userMessage, err := client.HandleUserInput(buf[:n]) userMessage, err := client.HandleUserInput(buf[:n])
if err == io.EOF {
exit <- true
}
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
continue continue
@ -88,7 +91,7 @@ func main() {
if err == io.EOF { if err == io.EOF {
log.Panic("Server disconnected sorry...") log.Panic("Server disconnected sorry...")
} else if err != nil { } else if err != nil {
log.Panic("Error reading from server connection...") log.Panic("Error reading from server connection...", err)
} }
message := &netwrk.LobbyMessage{} message := &netwrk.LobbyMessage{}

View File

@ -10,7 +10,7 @@ var exit chan bool
func main() { func main() {
fmt.Println("Starting sshpong server!") fmt.Println("Starting sshpong server!")
netwrk.Listen() netwrk.LobbyListen()
_ = <-exit _ = <-exit
} }

View File

@ -8,9 +8,14 @@ import (
"strings" "strings"
) )
var help = fmt.Errorf("use invite <player name> to invite a player\nchat or / to send a message to the lobby\nq or quit to leave the game")
func HandleUserInput(buf []byte) (*netwrk.LobbyMessage, error) { func HandleUserInput(buf []byte) (*netwrk.LobbyMessage, error) {
input := string(buf) input := string(buf)
args := strings.Fields(input) args := strings.Fields(input)
if len(args) == 0 {
return nil, help
}
switch args[0] { switch args[0] {
case "invite": case "invite":
if args[1] != "" { if args[1] != "" {
@ -40,11 +45,11 @@ func HandleUserInput(buf []byte) (*netwrk.LobbyMessage, error) {
case "q": case "q":
return nil, io.EOF return nil, io.EOF
case "help": case "help":
return nil, fmt.Errorf("use invite <player name> to invite a player\nchat or / to send a message to the lobby\nq or quit to leave the game") return nil, help
case "h": case "h":
return nil, fmt.Errorf("use invite <player name> to invite a player\nchat or / to send a message to the lobby\nq or quit to leave the game") return nil, help
default: default:
return nil, fmt.Errorf("use invite <player name> to invite a player\nchat or / to send a message to the lobby\nq or quit to leave the game") return nil, help
} }
return nil, nil return nil, nil
} }

View File

@ -15,12 +15,12 @@ func handleGameConnection(conn net.Conn) {
log.Printf("Error reading game ID on connection", err) log.Printf("Error reading game ID on connection", err)
} }
gameID := string(messageBytes[:n]) _ = string(messageBytes[:n])
if err != nil { if err != nil {
log.Printf("Game id was not a string?", err) log.Printf("Game id was not a string?", err)
} }
clientChan := make(chan GameMessage) _ = make(chan GameMessage)
n, err = conn.Read(messageBytes) n, err = conn.Read(messageBytes)
if err != nil { if err != nil {
@ -28,16 +28,6 @@ func handleGameConnection(conn net.Conn) {
return return
} }
gameClients, ok := gameChans.games[gameID]
if !ok {
newGameClients := GameClients{
client1: clientChan,
client2: nil,
}
gameChans.games[gameID] = newGameClients
} else {
gameClients.client2 = clientChan
}
} }
func handleGameMessage(conn net.Conn, message *GameMessage) error { func handleGameMessage(conn net.Conn, message *GameMessage) error {

View File

@ -9,8 +9,6 @@ import (
) )
func handleLobbyConnection(conn net.Conn) { func handleLobbyConnection(conn net.Conn) {
defer conn.Close()
messageBytes := make([]byte, 4096) messageBytes := make([]byte, 4096)
ingress := make(chan *LobbyMessage) ingress := make(chan *LobbyMessage)
@ -21,9 +19,11 @@ func handleLobbyConnection(conn net.Conn) {
for { for {
n, err := conn.Read(messageBytes) n, err := conn.Read(messageBytes)
if err == io.EOF { if err == io.EOF {
conn.Close()
return return
} }
if err != nil { if err != nil {
conn.Close()
log.Printf("Error reading message %v", err) log.Printf("Error reading message %v", err)
return return
} }
@ -46,10 +46,15 @@ func handleLobbyConnection(conn net.Conn) {
if err != nil { if err != nil {
log.Println("Error marshalling message to send to user...", err) log.Println("Error marshalling message to send to user...", err)
} }
log.Println("sending message to user!")
_, err = conn.Write(bytes) _, err = conn.Write(bytes)
if err == io.EOF { if err == io.EOF {
conn.Close()
log.Println("User has disconnected", err) log.Println("User has disconnected", err)
ingress <- &LobbyMessage{Type: "disconnect"} ingress <- &LobbyMessage{
Type: "disconnect",
Content: msg.PlayerId,
}
} }
if err != nil { if err != nil {
log.Println("Error writing to user...", err) log.Println("Error writing to user...", err)
@ -61,7 +66,7 @@ func handleLobbyConnection(conn net.Conn) {
go func() { go func() {
for { for {
msg := <-ingress msg := <-ingress
serverMsg, err := handleClientLobbyMessage(msg) serverMsg, err := handleClientLobbyMessage(msg, conn)
if err != nil { if err != nil {
log.Println("Error handling client lobby message...", err) log.Println("Error handling client lobby message...", err)
} }
@ -73,18 +78,29 @@ func handleLobbyConnection(conn net.Conn) {
} }
// Returns a bool of whether the player has disconnected from the lobby and an error // Returns a bool of whether the player has disconnected from the lobby and an error
func handleClientLobbyMessage(message *LobbyMessage) (*LobbyMessage, error) { func handleClientLobbyMessage(message *LobbyMessage, conn net.Conn) (*LobbyMessage, error) {
switch message.Type { switch message.Type {
// Handle an name/login message from a player
// Store the new player in the lobbyMembers
// Send a connection message for each of the lobbyMembers to the new player
// Send a connection message to all members in the lobby
case "name": case "name":
log.Println("Got a name message!", message)
_, ok := lobbyMembers.Load(message.Content) _, ok := lobbyMembers.Load(message.Content)
if ok { if ok {
return &LobbyMessage{Type: "name_error", Content: "Sorry, that name is already taken, please try a different name"}, nil return &LobbyMessage{Type: "name_error", Content: "Sorry, that name is already taken, please try a different name"}, nil
} }
username := message.Content username := message.Content
lobbyMembers.Store(username, Client{Username: username, Conn: conn})
log.Println("Storing new user!")
// Send all client messages // Send all client messages
lobbyMembers.Range(func(lobbyUsername string, client Client) bool { lobbyMembers.Range(func(lobbyUsername any, client any) bool {
externalMessageChan <- ExternalMessage{Target: username, Message: &LobbyMessage{Type: "connect", Content: lobbyUsername}} log.Println("ranging over users!")
usernameString, _ := lobbyUsername.(string)
externalMessageChan <- ExternalMessage{Target: username, Message: &LobbyMessage{Type: "connect", Content: usernameString}}
return true return true
}) })
@ -93,44 +109,74 @@ func handleClientLobbyMessage(message *LobbyMessage) (*LobbyMessage, error) {
broadcastToLobby(&LobbyMessage{PlayerId: "", Type: "connect", Content: username}) broadcastToLobby(&LobbyMessage{PlayerId: "", Type: "connect", Content: username})
return &LobbyMessage{PlayerId: username, Type: "name", Content: username}, nil return &LobbyMessage{PlayerId: username, Type: "name", Content: username}, nil
// Handle an invite message by sending a message to the target player
// Send an invite message to the invitee: message.Content
// Send an ack message to the inviter: message.PlayerId
case "invite": case "invite":
log.Println("Got invite for player:", message.Content) externalMessageChan <- ExternalMessage{
invitee, ok := lobbyMembers[message.Content] Target: message.Content,
if !ok { Message: message,
return &LobbyMessage{Type: "text", Content: "Sorry, that player is not available..."}, nil
} }
return &LobbyMessage{Type: "invite", Content: message.PlayerId}, nil return &LobbyMessage{Type: "invite", Content: message.PlayerId}, nil
// Handle a accept message from a player that was invited
// Send an ack message back to the player: message.PlayerId
// Send an invite ack message back to the inviter: message.Content
case "accept_game": case "accept_game":
player := lobbyMembers[message.Content] externalMessageChan <- ExternalMessage{
Target: message.Content,
Message: &LobbyMessage{Type: "accept", Content: ""},
}
return &LobbyMessage{Type: "accept", Content: ""}, nil return &LobbyMessage{Type: "accept", Content: ""}, nil
// Handle a chat message from a player with PlayerId
case "chat": case "chat":
broadcastToLobby(&LobbyMessage{PlayerId: message.PlayerId, Type: "text", Content: message.Content}) broadcastToLobby(&LobbyMessage{PlayerId: message.PlayerId, Type: "text", Content: message.Content})
return nil, nil return nil, nil
// Handle a decline_game message from a player that was invited
// Send an ack message back to the invitee: message.PlayerId
// Send an ack message to the inviter: message.Content
case "decline_game": case "decline_game":
inviter := lobbyMembers[message.Content] externalMessageChan <- ExternalMessage{
Target: message.Content,
Message: &LobbyMessage{Type: "decline", Content: ""},
}
return &LobbyMessage{Type: "decline_game", Content: message.PlayerId}, nil return &LobbyMessage{Type: "decline_game", Content: message.PlayerId}, nil
// Handle a quit message from a player that was connected
// broadcast the player quit to the lobby
case "quit": case "quit":
delete(lobbyMembers, message.PlayerId) lobbyMembers.Delete(message.PlayerId)
broadcastToLobby(&LobbyMessage{Type: "disconnect", Content: message.PlayerId}) broadcastToLobby(&LobbyMessage{Type: "disconnect", Content: message.PlayerId})
return nil, nil return nil, nil
// Ping and pong
case "ping": case "ping":
return &LobbyMessage{Type: "pong", Content: "pong"}, nil return &LobbyMessage{Type: "pong", Content: "pong"}, nil
// Ping and pong
default: default:
return &LobbyMessage{Type: "pong", Content: "pong"}, nil return &LobbyMessage{Type: "pong", Content: "pong"}, nil
} }
} }
func broadcastToLobby(message *LobbyMessage) { func broadcastToLobby(message *LobbyMessage) {
for _, player := range lobbyMembers { lobbyMembers.Range(func(playerId, player interface{}) bool {
bytes, err := proto.Marshal(message) bytes, err := proto.Marshal(message)
if err != nil { if err != nil {
log.Println("Error marshalling broadcast message", err) log.Println("Error marshalling broadcast message", err)
} }
_, err = player.Conn.Write(bytes)
client := player.(Client)
_, err = client.Conn.Write(bytes)
if err != nil { if err != nil {
log.Println("Error broadcasting to clients...", err) log.Println("Error broadcasting to clients...", err)
} }
} return true
})
} }

View File

@ -4,6 +4,8 @@ import (
"log" "log"
"net" "net"
sync "sync" sync "sync"
"google.golang.org/protobuf/proto"
) )
type Client struct { type Client struct {
@ -32,6 +34,19 @@ func init() {
externalMessageChan = make(chan ExternalMessage) externalMessageChan = make(chan ExternalMessage)
lobbyMembers = sync.Map{} lobbyMembers = sync.Map{}
go func() {
for {
msg := <-externalMessageChan
player, ok := lobbyMembers.Load(msg.Target)
if !ok {
continue
}
client, _ := player.(Client)
bytes, _ := proto.Marshal(msg.Message)
client.Conn.Write(bytes)
}
}()
} }
// Starts listening on port 12345 for TCP connections // Starts listening on port 12345 for TCP connections