working lobby!
This commit is contained in:
parent
e5df746e7f
commit
b3df666196
|
@ -7,6 +7,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"sshpong/internal/client"
|
"sshpong/internal/client"
|
||||||
"sshpong/internal/netwrk"
|
"sshpong/internal/netwrk"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
@ -19,6 +20,7 @@ func main() {
|
||||||
|
|
||||||
egress := make(chan *netwrk.LobbyMessage)
|
egress := make(chan *netwrk.LobbyMessage)
|
||||||
ingress := make(chan *netwrk.LobbyMessage)
|
ingress := make(chan *netwrk.LobbyMessage)
|
||||||
|
interrupter := make(chan client.InterrupterMessage, 100)
|
||||||
|
|
||||||
buf := make([]byte, 1024)
|
buf := make([]byte, 1024)
|
||||||
n, err := os.Stdin.Read(buf)
|
n, err := os.Stdin.Read(buf)
|
||||||
|
@ -42,7 +44,16 @@ func main() {
|
||||||
log.Panic("Bro your input wack as fuck")
|
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 {
|
if err == io.EOF {
|
||||||
exit <- true
|
exit <- true
|
||||||
}
|
}
|
||||||
|
@ -50,9 +61,23 @@ func main() {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
}
|
||||||
userMessage.PlayerId = username
|
userMessage.PlayerId = username
|
||||||
egress <- userMessage
|
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)
|
}(egress)
|
||||||
|
|
||||||
|
@ -61,7 +86,13 @@ func main() {
|
||||||
for {
|
for {
|
||||||
msg := <-ingress
|
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)
|
}(ingress)
|
||||||
|
|
|
@ -3,16 +3,18 @@ package client
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"sshpong/internal/netwrk"
|
"sshpong/internal/netwrk"
|
||||||
"strings"
|
"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")
|
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(args []string) (*netwrk.LobbyMessage, error) {
|
||||||
input := string(buf)
|
|
||||||
args := strings.Fields(input)
|
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return nil, help
|
return nil, help
|
||||||
}
|
}
|
||||||
|
@ -49,28 +51,76 @@ func HandleUserInput(buf []byte) (*netwrk.LobbyMessage, error) {
|
||||||
case "h":
|
case "h":
|
||||||
return nil, help
|
return nil, help
|
||||||
default:
|
default:
|
||||||
|
if strings.Index(args[0], "/") == 0 {
|
||||||
|
return &netwrk.LobbyMessage{
|
||||||
|
Type: "chat",
|
||||||
|
Content: strings.Join(args, " ")[1:],
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
return nil, help
|
return nil, help
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleServerMessage(message *netwrk.LobbyMessage) {
|
func HandleInterruptInput(incoming InterrupterMessage, args []string) (*netwrk.LobbyMessage, error) {
|
||||||
switch message.Type {
|
|
||||||
|
switch incoming.InterruptType {
|
||||||
case "invite":
|
case "invite":
|
||||||
log.Println(message.PlayerId, "is inviting you to a game.", message.Content)
|
if len(args) < 1 {
|
||||||
case "accepted":
|
return &netwrk.LobbyMessage{
|
||||||
log.Println(message.PlayerId, "accepted your invite.", message.Content)
|
Type: "decline",
|
||||||
case "text":
|
Content: incoming.Content,
|
||||||
log.Println(message.PlayerId, ":", message.Content)
|
}, nil
|
||||||
case "decline_game":
|
} else {
|
||||||
log.Println("Invite was declined:", message.Content)
|
if strings.ToLower(args[0]) == "y" || strings.ToLower(args[0]) == "yes" {
|
||||||
case "disconnect":
|
return &netwrk.LobbyMessage{Type: "accept", Content: incoming.Content}, nil
|
||||||
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)
|
|
||||||
default:
|
|
||||||
log.Println("Received", message.Content)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cancel waiting for invite?
|
||||||
|
case "decline":
|
||||||
|
|
||||||
|
// Disconnect and connect to game
|
||||||
|
case "accepted":
|
||||||
|
return &netwrk.LobbyMessage{
|
||||||
|
Type: "disconnect",
|
||||||
|
Content: "",
|
||||||
|
}, nil
|
||||||
|
default:
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
@ -46,7 +47,6 @@ 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()
|
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 for each of the lobbyMembers to the new player
|
||||||
// Send a connection message to all members in the lobby
|
// 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
|
||||||
|
@ -95,20 +94,17 @@ func handleClientLobbyMessage(message *LobbyMessage, conn net.Conn) (*LobbyMessa
|
||||||
|
|
||||||
lobbyMembers.Store(username, Client{Username: username, Conn: conn})
|
lobbyMembers.Store(username, Client{Username: username, Conn: conn})
|
||||||
|
|
||||||
log.Println("Storing new user!")
|
// Build current lobby list
|
||||||
// Send all client messages
|
var lobby []string
|
||||||
lobbyMembers.Range(func(lobbyUsername any, client any) bool {
|
lobbyMembers.Range(func(lobbyUsername any, client any) bool {
|
||||||
log.Println("ranging over users!")
|
|
||||||
usernameString, _ := lobbyUsername.(string)
|
usernameString, _ := lobbyUsername.(string)
|
||||||
externalMessageChan <- ExternalMessage{Target: username, Message: &LobbyMessage{Type: "connect", Content: usernameString}}
|
lobby = append(lobby, usernameString)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Println("Broadcasting new player", message.Content)
|
|
||||||
|
|
||||||
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: strings.Join(lobby, "\n")}, nil
|
||||||
|
|
||||||
// Handle an invite message by sending a message to the target player
|
// Handle an invite message by sending a message to the target player
|
||||||
// Send an invite message to the invitee: message.Content
|
// Send an invite message to the invitee: message.Content
|
||||||
|
@ -119,18 +115,18 @@ func handleClientLobbyMessage(message *LobbyMessage, conn net.Conn) (*LobbyMessa
|
||||||
Message: message,
|
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
|
// Handle a accept message from a player that was invited
|
||||||
// Send an ack message back to the player: message.PlayerId
|
// Send a game_start message back to the player: message.PlayerId
|
||||||
// Send an invite ack message back to the inviter: message.Content
|
// Send an accepted message back to the inviter: message.Content
|
||||||
case "accept_game":
|
case "accept":
|
||||||
externalMessageChan <- ExternalMessage{
|
externalMessageChan <- ExternalMessage{
|
||||||
Target: message.Content,
|
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
|
// Handle a chat message from a player with PlayerId
|
||||||
case "chat":
|
case "chat":
|
||||||
|
@ -166,6 +162,7 @@ func handleClientLobbyMessage(message *LobbyMessage, conn net.Conn) (*LobbyMessa
|
||||||
}
|
}
|
||||||
|
|
||||||
func broadcastToLobby(message *LobbyMessage) {
|
func broadcastToLobby(message *LobbyMessage) {
|
||||||
|
var disconnectedUsers []string
|
||||||
lobbyMembers.Range(func(playerId, player interface{}) bool {
|
lobbyMembers.Range(func(playerId, player interface{}) bool {
|
||||||
bytes, err := proto.Marshal(message)
|
bytes, err := proto.Marshal(message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -176,7 +173,13 @@ func broadcastToLobby(message *LobbyMessage) {
|
||||||
_, err = client.Conn.Write(bytes)
|
_, 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)
|
||||||
|
disconnectedUsers = append(disconnectedUsers, playerId.(string))
|
||||||
|
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
for _, player := range disconnectedUsers {
|
||||||
|
lobbyMembers.Delete(player)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,11 +40,16 @@ func init() {
|
||||||
msg := <-externalMessageChan
|
msg := <-externalMessageChan
|
||||||
player, ok := lobbyMembers.Load(msg.Target)
|
player, ok := lobbyMembers.Load(msg.Target)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
log.Println("failed to send to target", msg.Target)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
client, _ := player.(Client)
|
client, _ := player.(Client)
|
||||||
bytes, _ := proto.Marshal(msg.Message)
|
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 {
|
for {
|
||||||
conn, err := listener.Accept()
|
conn, err := listener.Accept()
|
||||||
log.Println("got a connection!")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in New Issue
Block a user