working lobby!

This commit is contained in:
Beric Bearnson 2024-08-24 16:09:12 -06:00
parent e5df746e7f
commit b3df666196
4 changed files with 135 additions and 47 deletions

View File

@ -7,6 +7,7 @@ import (
"os"
"sshpong/internal/client"
"sshpong/internal/netwrk"
"strings"
"google.golang.org/protobuf/proto"
)
@ -19,6 +20,7 @@ func main() {
egress := make(chan *netwrk.LobbyMessage)
ingress := make(chan *netwrk.LobbyMessage)
interrupter := make(chan client.InterrupterMessage, 100)
buf := make([]byte, 1024)
n, err := os.Stdin.Read(buf)
@ -42,7 +44,16 @@ func main() {
log.Panic("Bro your input wack as fuck")
}
userMessage, err := client.HandleUserInput(buf[:n])
input := string(buf[:n-1])
args := strings.Fields(input)
userMessage := &netwrk.LobbyMessage{}
select {
case msg := <-interrupter:
userMessage, err := client.HandleInterruptInput(msg, args)
if err != nil {
userMessage, err = client.HandleUserInput(args)
if err == io.EOF {
exit <- true
}
@ -50,9 +61,23 @@ func main() {
fmt.Println(err)
continue
}
}
userMessage.PlayerId = username
egress <- userMessage
default:
userMessage, err = client.HandleUserInput(args)
if err == io.EOF {
exit <- true
}
if err != nil {
fmt.Println(err)
continue
}
userMessage.PlayerId = username
egress <- userMessage
}
}
}(egress)
@ -61,7 +86,13 @@ func main() {
for {
msg := <-ingress
client.HandleServerMessage(msg)
interrupterMsg, err := client.HandleServerMessage(msg)
if err != nil {
log.Panic("Error handling server message disconnecting...")
}
if interrupterMsg.InterruptType != "" {
interrupter <- interrupterMsg
}
}
}(ingress)

View File

@ -3,16 +3,18 @@ package client
import (
"fmt"
"io"
"log"
"sshpong/internal/netwrk"
"strings"
)
type InterrupterMessage struct {
InterruptType string
Content string
}
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) {
input := string(buf)
args := strings.Fields(input)
func HandleUserInput(args []string) (*netwrk.LobbyMessage, error) {
if len(args) == 0 {
return nil, help
}
@ -49,28 +51,76 @@ func HandleUserInput(buf []byte) (*netwrk.LobbyMessage, error) {
case "h":
return nil, help
default:
if strings.Index(args[0], "/") == 0 {
return &netwrk.LobbyMessage{
Type: "chat",
Content: strings.Join(args, " ")[1:],
}, nil
}
return nil, help
}
return nil, nil
}
func HandleServerMessage(message *netwrk.LobbyMessage) {
switch message.Type {
func HandleInterruptInput(incoming InterrupterMessage, args []string) (*netwrk.LobbyMessage, error) {
switch incoming.InterruptType {
case "invite":
log.Println(message.PlayerId, "is inviting you to a game.", message.Content)
if len(args) < 1 {
return &netwrk.LobbyMessage{
Type: "decline",
Content: incoming.Content,
}, nil
} else {
if strings.ToLower(args[0]) == "y" || strings.ToLower(args[0]) == "yes" {
return &netwrk.LobbyMessage{Type: "accept", Content: incoming.Content}, nil
}
}
// Cancel waiting for invite?
case "decline":
// Disconnect and connect to game
case "accepted":
log.Println(message.PlayerId, "accepted your invite.", message.Content)
case "text":
log.Println(message.PlayerId, ":", message.Content)
case "decline_game":
log.Println("Invite was declined:", message.Content)
case "disconnect":
log.Println("Got disconnect for player:", message.Content)
case "connect":
log.Println("Got connect for player:", message.Content)
case "pong":
log.Println("Received", message.Content)
return &netwrk.LobbyMessage{
Type: "disconnect",
Content: "",
}, nil
default:
log.Println("Received", message.Content)
return nil, fmt.Errorf("received a interrupt message that could not be handled %v", incoming)
}
return nil, nil
}
func HandleServerMessage(message *netwrk.LobbyMessage) (InterrupterMessage, error) {
switch message.Type {
case "name":
fmt.Printf("Current Players\n%s\n", message.Content)
case "invite":
fmt.Println(message.PlayerId, "is inviting you to a game\nType y to accept...")
return InterrupterMessage{
InterruptType: "invite",
Content: message.PlayerId,
}, nil
case "pending_invite":
fmt.Println("Invite sent to", message.Content, "\nWaiting for response...")
case "accepted":
fmt.Println(message.PlayerId, "accepted your invite.\n", "Starting game...")
case "game_start":
fmt.Println("Invited accepted\n", "Starting game...")
case "text":
fmt.Println(message.PlayerId, ":", message.Content)
case "decline_game":
fmt.Println(message.Content, "declined your game invite")
case "disconnect":
fmt.Println(message.Content, "has disconnected")
case "connect":
fmt.Println(message.Content, "has connected")
case "pong":
fmt.Println("Received", message.Content)
default:
fmt.Println("Received", message.Content)
}
return InterrupterMessage{}, nil
}

View File

@ -4,6 +4,7 @@ import (
"io"
"log"
"net"
"strings"
"google.golang.org/protobuf/proto"
)
@ -46,7 +47,6 @@ func handleLobbyConnection(conn net.Conn) {
if err != nil {
log.Println("Error marshalling message to send to user...", err)
}
log.Println("sending message to user!")
_, err = conn.Write(bytes)
if err == io.EOF {
conn.Close()
@ -86,7 +86,6 @@ func handleClientLobbyMessage(message *LobbyMessage, conn net.Conn) (*LobbyMessa
// 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":
log.Println("Got a name message!", message)
_, ok := lobbyMembers.Load(message.Content)
if ok {
return &LobbyMessage{Type: "name_error", Content: "Sorry, that name is already taken, please try a different name"}, nil
@ -95,20 +94,17 @@ func handleClientLobbyMessage(message *LobbyMessage, conn net.Conn) (*LobbyMessa
lobbyMembers.Store(username, Client{Username: username, Conn: conn})
log.Println("Storing new user!")
// Send all client messages
// Build current lobby list
var lobby []string
lobbyMembers.Range(func(lobbyUsername any, client any) bool {
log.Println("ranging over users!")
usernameString, _ := lobbyUsername.(string)
externalMessageChan <- ExternalMessage{Target: username, Message: &LobbyMessage{Type: "connect", Content: usernameString}}
lobby = append(lobby, usernameString)
return true
})
log.Println("Broadcasting new player", message.Content)
broadcastToLobby(&LobbyMessage{PlayerId: "", Type: "connect", Content: username})
return &LobbyMessage{PlayerId: username, Type: "name", Content: username}, nil
return &LobbyMessage{PlayerId: username, Type: "name", Content: strings.Join(lobby, "\n")}, nil
// Handle an invite message by sending a message to the target player
// Send an invite message to the invitee: message.Content
@ -119,18 +115,18 @@ func handleClientLobbyMessage(message *LobbyMessage, conn net.Conn) (*LobbyMessa
Message: message,
}
return &LobbyMessage{Type: "invite", Content: message.PlayerId}, nil
return &LobbyMessage{Type: "pending_invite", Content: message.Content}, 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":
// Send a game_start message back to the player: message.PlayerId
// Send an accepted message back to the inviter: message.Content
case "accept":
externalMessageChan <- ExternalMessage{
Target: message.Content,
Message: &LobbyMessage{Type: "accept", Content: ""},
Message: &LobbyMessage{Type: "game_start", Content: ""},
}
return &LobbyMessage{Type: "accept", Content: ""}, nil
return &LobbyMessage{PlayerId: message.PlayerId, Type: "accepted", Content: ""}, nil
// Handle a chat message from a player with PlayerId
case "chat":
@ -166,6 +162,7 @@ func handleClientLobbyMessage(message *LobbyMessage, conn net.Conn) (*LobbyMessa
}
func broadcastToLobby(message *LobbyMessage) {
var disconnectedUsers []string
lobbyMembers.Range(func(playerId, player interface{}) bool {
bytes, err := proto.Marshal(message)
if err != nil {
@ -176,7 +173,13 @@ func broadcastToLobby(message *LobbyMessage) {
_, err = client.Conn.Write(bytes)
if err != nil {
log.Println("Error broadcasting to clients...", err)
disconnectedUsers = append(disconnectedUsers, playerId.(string))
}
return true
})
for _, player := range disconnectedUsers {
lobbyMembers.Delete(player)
}
}

View File

@ -40,11 +40,16 @@ func init() {
msg := <-externalMessageChan
player, ok := lobbyMembers.Load(msg.Target)
if !ok {
log.Println("failed to send to target", msg.Target)
continue
}
client, _ := player.(Client)
bytes, _ := proto.Marshal(msg.Message)
client.Conn.Write(bytes)
_, err := client.Conn.Write(bytes)
if err != nil {
log.Println("Could not write to target", msg.Target, err)
}
}
}()
}
@ -62,7 +67,6 @@ func LobbyListen() {
for {
conn, err := listener.Accept()
log.Println("got a connection!")
if err != nil {
log.Println(err)
continue