sshpong/internal/netwrk/lobby.go

137 lines
3.5 KiB
Go
Raw Normal View History

2024-08-14 17:26:51 -06:00
package netwrk
import (
2024-08-14 23:33:05 -06:00
"io"
2024-08-14 17:26:51 -06:00
"log"
"net"
"google.golang.org/protobuf/proto"
)
func handleLobbyConnection(conn net.Conn) {
defer conn.Close()
messageBytes := make([]byte, 4096)
2024-08-15 20:08:23 -06:00
ingress := make(chan *LobbyMessage)
egress := make(chan *LobbyMessage)
// Network Reader
2024-08-14 23:33:05 -06:00
go func() {
for {
n, err := conn.Read(messageBytes)
if err == io.EOF {
return
}
if err != nil {
log.Printf("Error reading message %v", err)
return
}
message := LobbyMessage{}
err = proto.Unmarshal(messageBytes[:n], &message)
if err != nil {
2024-08-15 20:08:23 -06:00
log.Println("Invalid message received from client", err)
2024-08-14 23:33:05 -06:00
}
2024-08-15 20:08:23 -06:00
ingress <- &message
2024-08-14 17:26:51 -06:00
}
2024-08-14 23:33:05 -06:00
}()
2024-08-15 20:08:23 -06:00
// Network Writer
go func() {
for {
msg := <-egress
bytes, err := proto.Marshal(msg)
if err != nil {
log.Println("Error marshalling message to send to user...", err)
}
_, err = conn.Write(bytes)
if err == io.EOF {
log.Println("User has disconnected", err)
ingress <- &LobbyMessage{Type: "disconnect"}
}
if err != nil {
log.Println("Error writing to user...", err)
}
}
}()
2024-08-14 17:26:51 -06:00
2024-08-15 20:08:23 -06:00
// Client message handler
go func() {
for {
msg := <-ingress
serverMsg, err := handleClientLobbyMessage(msg)
if err != nil {
log.Println("Error handling client lobby message...", err)
}
if serverMsg != nil {
egress <- serverMsg
2024-08-14 23:33:05 -06:00
}
2024-08-14 17:26:51 -06:00
}
2024-08-15 20:08:23 -06:00
}()
2024-08-14 17:26:51 -06:00
}
// Returns a bool of whether the player has disconnected from the lobby and an error
2024-08-15 20:08:23 -06:00
func handleClientLobbyMessage(message *LobbyMessage) (*LobbyMessage, error) {
2024-08-14 17:26:51 -06:00
switch message.Type {
case "name":
2024-08-15 20:08:23 -06:00
_, ok := lobbyMembers.Load(message.Content)
2024-08-14 17:26:51 -06:00
if ok {
2024-08-15 20:08:23 -06:00
return &LobbyMessage{Type: "name_error", Content: "Sorry, that name is already taken, please try a different name"}, nil
2024-08-14 17:26:51 -06:00
}
2024-08-15 20:08:23 -06:00
username := message.Content
// Send all client messages
lobbyMembers.Range(func(lobbyUsername string, client Client) bool {
externalMessageChan <- ExternalMessage{Target: username, Message: &LobbyMessage{Type: "connect", Content: lobbyUsername}}
return true
})
2024-08-14 17:26:51 -06:00
2024-08-14 23:33:05 -06:00
log.Println("Broadcasting new player", message.Content)
2024-08-15 20:08:23 -06:00
broadcastToLobby(&LobbyMessage{PlayerId: "", Type: "connect", Content: username})
2024-08-14 17:26:51 -06:00
2024-08-15 20:08:23 -06:00
return &LobbyMessage{PlayerId: username, Type: "name", Content: username}, nil
2024-08-14 23:33:05 -06:00
case "invite":
log.Println("Got invite for player:", message.Content)
2024-08-15 20:08:23 -06:00
invitee, ok := lobbyMembers[message.Content]
2024-08-14 17:26:51 -06:00
if !ok {
2024-08-15 20:08:23 -06:00
return &LobbyMessage{Type: "text", Content: "Sorry, that player is not available..."}, nil
2024-08-14 17:26:51 -06:00
}
2024-08-15 20:08:23 -06:00
return &LobbyMessage{Type: "invite", Content: message.PlayerId}, nil
2024-08-14 17:26:51 -06:00
case "accept_game":
2024-08-15 20:08:23 -06:00
player := lobbyMembers[message.Content]
2024-08-14 17:26:51 -06:00
2024-08-15 20:08:23 -06:00
return &LobbyMessage{Type: "accept", Content: ""}, nil
2024-08-14 17:26:51 -06:00
2024-08-14 23:33:05 -06:00
case "chat":
broadcastToLobby(&LobbyMessage{PlayerId: message.PlayerId, Type: "text", Content: message.Content})
2024-08-15 20:08:23 -06:00
return nil, nil
2024-08-14 17:26:51 -06:00
case "decline_game":
2024-08-15 20:08:23 -06:00
inviter := lobbyMembers[message.Content]
return &LobbyMessage{Type: "decline_game", Content: message.PlayerId}, nil
2024-08-14 17:26:51 -06:00
case "quit":
2024-08-15 20:08:23 -06:00
delete(lobbyMembers, message.PlayerId)
2024-08-14 17:26:51 -06:00
broadcastToLobby(&LobbyMessage{Type: "disconnect", Content: message.PlayerId})
2024-08-15 20:08:23 -06:00
return nil, nil
2024-08-14 17:26:51 -06:00
case "ping":
2024-08-15 20:08:23 -06:00
return &LobbyMessage{Type: "pong", Content: "pong"}, nil
2024-08-14 17:26:51 -06:00
default:
2024-08-15 20:08:23 -06:00
return &LobbyMessage{Type: "pong", Content: "pong"}, nil
2024-08-14 17:26:51 -06:00
}
}
func broadcastToLobby(message *LobbyMessage) {
2024-08-15 20:08:23 -06:00
for _, player := range lobbyMembers {
bytes, err := proto.Marshal(message)
if err != nil {
log.Println("Error marshalling broadcast message", err)
}
_, err = player.Conn.Write(bytes)
2024-08-14 23:33:05 -06:00
if err != nil {
log.Println("Error broadcasting to clients...", err)
}
2024-08-14 17:26:51 -06:00
}
}