sshpong/internal/netwrk/lobby.go

186 lines
4.9 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"
2024-08-24 16:09:12 -06:00
"strings"
2024-08-14 17:26:51 -06:00
"google.golang.org/protobuf/proto"
)
func handleLobbyConnection(conn net.Conn) {
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 {
2024-08-23 11:59:19 -06:00
conn.Close()
2024-08-14 23:33:05 -06:00
return
}
if err != nil {
2024-08-23 11:59:19 -06:00
conn.Close()
2024-08-14 23:33:05 -06:00
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 {
2024-08-23 11:59:19 -06:00
conn.Close()
2024-08-15 20:08:23 -06:00
log.Println("User has disconnected", err)
2024-08-23 11:59:19 -06:00
ingress <- &LobbyMessage{
Type: "disconnect",
Content: msg.PlayerId,
}
2024-08-15 20:08:23 -06:00
}
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
2024-08-23 11:59:19 -06:00
serverMsg, err := handleClientLobbyMessage(msg, conn)
2024-08-15 20:08:23 -06:00
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-23 11:59:19 -06:00
func handleClientLobbyMessage(message *LobbyMessage, conn net.Conn) (*LobbyMessage, error) {
2024-08-14 17:26:51 -06:00
switch message.Type {
2024-08-23 11:59:19 -06:00
// 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
2024-08-14 17:26:51 -06:00
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
2024-08-23 11:59:19 -06:00
lobbyMembers.Store(username, Client{Username: username, Conn: conn})
2024-08-24 16:09:12 -06:00
// Build current lobby list
var lobby []string
2024-08-23 11:59:19 -06:00
lobbyMembers.Range(func(lobbyUsername any, client any) bool {
usernameString, _ := lobbyUsername.(string)
2024-08-24 16:09:12 -06:00
lobby = append(lobby, usernameString)
2024-08-15 20:08:23 -06:00
return true
})
2024-08-14 17:26:51 -06:00
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-24 16:09:12 -06:00
return &LobbyMessage{PlayerId: username, Type: "name", Content: strings.Join(lobby, "\n")}, nil
2024-08-23 11:59:19 -06:00
// 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
2024-08-14 23:33:05 -06:00
case "invite":
2024-08-23 11:59:19 -06:00
externalMessageChan <- ExternalMessage{
Target: message.Content,
Message: message,
2024-08-14 17:26:51 -06:00
}
2024-08-23 11:59:19 -06:00
2024-08-24 16:09:12 -06:00
return &LobbyMessage{Type: "pending_invite", Content: message.Content}, nil
2024-08-23 11:59:19 -06:00
// Handle a accept message from a player that was invited
2024-08-24 16:09:12 -06:00
// Send a game_start message back to the player: message.PlayerId
// Send an accepted message back to the inviter: message.Content
case "accept":
2024-08-23 11:59:19 -06:00
externalMessageChan <- ExternalMessage{
Target: message.Content,
2024-08-24 16:09:12 -06:00
Message: &LobbyMessage{Type: "game_start", Content: ""},
2024-08-23 11:59:19 -06:00
}
2024-08-14 17:26:51 -06:00
2024-08-24 16:09:12 -06:00
return &LobbyMessage{PlayerId: message.PlayerId, Type: "accepted", Content: ""}, nil
2024-08-14 17:26:51 -06:00
2024-08-23 11:59:19 -06:00
// Handle a chat message from a player with PlayerId
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-23 11:59:19 -06:00
// 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
2024-08-14 17:26:51 -06:00
case "decline_game":
2024-08-23 11:59:19 -06:00
externalMessageChan <- ExternalMessage{
Target: message.Content,
Message: &LobbyMessage{Type: "decline", Content: ""},
}
2024-08-15 20:08:23 -06:00
return &LobbyMessage{Type: "decline_game", Content: message.PlayerId}, nil
2024-08-23 11:59:19 -06:00
// Handle a quit message from a player that was connected
// broadcast the player quit to the lobby
2024-08-14 17:26:51 -06:00
case "quit":
2024-08-23 11:59:19 -06:00
lobbyMembers.Delete(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-23 11:59:19 -06:00
// Ping and pong
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-23 11:59:19 -06:00
// Ping and pong
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-24 16:09:12 -06:00
var disconnectedUsers []string
2024-08-23 11:59:19 -06:00
lobbyMembers.Range(func(playerId, player interface{}) bool {
2024-08-15 20:08:23 -06:00
bytes, err := proto.Marshal(message)
if err != nil {
log.Println("Error marshalling broadcast message", err)
}
2024-08-23 11:59:19 -06:00
client := player.(Client)
_, err = client.Conn.Write(bytes)
2024-08-14 23:33:05 -06:00
if err != nil {
log.Println("Error broadcasting to clients...", err)
2024-08-24 16:09:12 -06:00
disconnectedUsers = append(disconnectedUsers, playerId.(string))
2024-08-14 23:33:05 -06:00
}
2024-08-23 11:59:19 -06:00
return true
})
2024-08-24 16:09:12 -06:00
for _, player := range disconnectedUsers {
lobbyMembers.Delete(player)
}
2024-08-14 17:26:51 -06:00
}