2024-08-15 20:08:23 -06:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2024-09-26 19:49:59 -06:00
|
|
|
"errors"
|
2024-08-15 20:08:23 -06:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-09-26 19:49:59 -06:00
|
|
|
"log/slog"
|
|
|
|
"sshpong/internal/lobby"
|
2024-08-15 20:08:23 -06:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-08-24 16:09:12 -06:00
|
|
|
type InterrupterMessage struct {
|
|
|
|
InterruptType string
|
|
|
|
Content string
|
|
|
|
}
|
|
|
|
|
2024-08-23 11:59:19 -06:00
|
|
|
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")
|
|
|
|
|
2024-09-26 19:49:59 -06:00
|
|
|
var red = "\x1b[31m"
|
|
|
|
var normal = "\033[0m"
|
|
|
|
|
|
|
|
func HandleUserInput(args []string, username string) (lobby.LobbyMessage, error) {
|
2024-08-23 11:59:19 -06:00
|
|
|
if len(args) == 0 {
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{}, help
|
2024-08-23 11:59:19 -06:00
|
|
|
}
|
2024-08-15 20:08:23 -06:00
|
|
|
switch args[0] {
|
|
|
|
case "invite":
|
|
|
|
if args[1] != "" {
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{
|
|
|
|
MessageType: "invite",
|
|
|
|
Message: lobby.Invite{From: username, To: args[1]}}, nil
|
2024-08-15 20:08:23 -06:00
|
|
|
} else {
|
|
|
|
fmt.Println("Please provide a player to invite ")
|
|
|
|
}
|
|
|
|
case "chat":
|
|
|
|
if args[1] != "" {
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{
|
|
|
|
MessageType: "chat",
|
|
|
|
Message: lobby.Chat{
|
|
|
|
From: username,
|
|
|
|
Message: args[1],
|
|
|
|
},
|
2024-08-15 20:08:23 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
case "/":
|
|
|
|
if args[1] != "" {
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{
|
|
|
|
MessageType: "chat",
|
|
|
|
Message: lobby.Chat{
|
|
|
|
From: username,
|
|
|
|
Message: args[1],
|
|
|
|
},
|
2024-08-15 20:08:23 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
case "quit":
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{}, io.EOF
|
2024-08-15 20:08:23 -06:00
|
|
|
case "q":
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{}, io.EOF
|
2024-08-15 20:08:23 -06:00
|
|
|
case "help":
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{}, help
|
2024-08-15 20:08:23 -06:00
|
|
|
case "h":
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{}, help
|
2024-08-15 20:08:23 -06:00
|
|
|
default:
|
2024-08-24 16:09:12 -06:00
|
|
|
if strings.Index(args[0], "/") == 0 {
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{
|
|
|
|
MessageType: "chat",
|
|
|
|
Message: lobby.Chat{
|
|
|
|
From: username,
|
|
|
|
Message: args[1],
|
|
|
|
},
|
2024-08-24 16:09:12 -06:00
|
|
|
}, nil
|
|
|
|
}
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{}, help
|
2024-08-15 20:08:23 -06:00
|
|
|
}
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{}, nil
|
2024-08-15 20:08:23 -06:00
|
|
|
}
|
|
|
|
|
2024-09-26 19:49:59 -06:00
|
|
|
func HandleInterruptInput(incoming InterrupterMessage, args []string, username string) (lobby.LobbyMessage, error) {
|
2024-08-24 16:09:12 -06:00
|
|
|
|
|
|
|
switch incoming.InterruptType {
|
|
|
|
case "invite":
|
|
|
|
if len(args) < 1 {
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{
|
|
|
|
MessageType: "decline",
|
|
|
|
Message: lobby.Decline{
|
|
|
|
From: username,
|
|
|
|
To: incoming.Content,
|
|
|
|
},
|
2024-08-24 16:09:12 -06:00
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
if strings.ToLower(args[0]) == "y" || strings.ToLower(args[0]) == "yes" {
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{MessageType: "accept", Message: lobby.Accept{
|
|
|
|
From: username,
|
|
|
|
To: incoming.Content,
|
|
|
|
},
|
|
|
|
}, nil
|
2024-08-24 16:09:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-26 19:49:59 -06:00
|
|
|
// // Cancel waiting for invite? we aren't doing this I guess.
|
|
|
|
// case "decline":
|
|
|
|
// return nil,
|
2024-08-24 16:09:12 -06:00
|
|
|
// Disconnect and connect to game
|
|
|
|
case "accepted":
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{
|
|
|
|
MessageType: "disconnect",
|
|
|
|
Message: lobby.Disconnect{
|
|
|
|
From: incoming.Content,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case "start_game":
|
|
|
|
return lobby.LobbyMessage{
|
|
|
|
MessageType: "start_game",
|
|
|
|
Message: lobby.StartGame{GameID: incoming.Content},
|
2024-08-24 16:09:12 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-09-26 19:49:59 -06:00
|
|
|
return lobby.LobbyMessage{}, fmt.Errorf("received a interrupt message that could not be handled %v", incoming)
|
2024-08-24 16:09:12 -06:00
|
|
|
}
|
|
|
|
|
2024-09-26 19:49:59 -06:00
|
|
|
func HandleServerMessage(message lobby.LobbyMessage) (InterrupterMessage, error) {
|
|
|
|
|
|
|
|
msg := message.Message
|
|
|
|
switch message.MessageType {
|
2024-08-24 16:09:12 -06:00
|
|
|
case "name":
|
2024-09-26 19:49:59 -06:00
|
|
|
|
|
|
|
nmsg, ok := msg.(lobby.Name)
|
|
|
|
if !ok {
|
|
|
|
return InterrupterMessage{}, errors.New("Not a properly formatted name message")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Current Players\n%s\n", nmsg)
|
2024-08-15 20:08:23 -06:00
|
|
|
case "invite":
|
2024-09-26 19:49:59 -06:00
|
|
|
|
|
|
|
imsg, ok := msg.(lobby.Invite)
|
|
|
|
if !ok {
|
|
|
|
return InterrupterMessage{}, errors.New("Not a propertly formatted invite message")
|
|
|
|
}
|
|
|
|
fmt.Println(imsg.From, "is inviting you to a game\nType y to accept...")
|
2024-08-24 16:09:12 -06:00
|
|
|
return InterrupterMessage{
|
|
|
|
InterruptType: "invite",
|
2024-09-26 19:49:59 -06:00
|
|
|
Content: imsg.From,
|
2024-08-24 16:09:12 -06:00
|
|
|
}, nil
|
|
|
|
case "pending_invite":
|
2024-09-26 19:49:59 -06:00
|
|
|
|
|
|
|
pimsg, ok := msg.(lobby.PendingInvite)
|
|
|
|
if !ok {
|
|
|
|
return InterrupterMessage{}, errors.New("Not a properly formatted pending invite message")
|
|
|
|
}
|
|
|
|
fmt.Println("Invite sent to", pimsg.Recipient, "\nWaiting for response...")
|
2024-08-15 20:08:23 -06:00
|
|
|
case "accepted":
|
2024-09-26 19:49:59 -06:00
|
|
|
|
|
|
|
amsg, ok := msg.(lobby.Accepted)
|
|
|
|
if !ok {
|
|
|
|
return InterrupterMessage{}, errors.New("Not a properly formatted accepted message")
|
|
|
|
}
|
|
|
|
fmt.Println(amsg.Accepter, "accepted your invite.", "Press Enter to connect to game...")
|
|
|
|
return InterrupterMessage{
|
|
|
|
InterruptType: "start_game",
|
|
|
|
Content: amsg.GameID,
|
|
|
|
}, nil
|
|
|
|
case "start_game":
|
|
|
|
|
|
|
|
sgmsg, ok := msg.(lobby.StartGame)
|
|
|
|
if !ok {
|
|
|
|
return InterrupterMessage{}, errors.New("Not a properly formatted start game message")
|
|
|
|
}
|
|
|
|
return InterrupterMessage{
|
|
|
|
InterruptType: "start_game",
|
|
|
|
Content: sgmsg.GameID,
|
|
|
|
}, nil
|
|
|
|
case "chat":
|
|
|
|
cmsg, ok := msg.(lobby.Chat)
|
|
|
|
if !ok {
|
|
|
|
return InterrupterMessage{}, errors.New("Not a properly formatted chat message")
|
|
|
|
}
|
|
|
|
fmt.Println(cmsg.From, ":", cmsg.Message)
|
|
|
|
case "decline":
|
|
|
|
dmsg, ok := msg.(lobby.Decline)
|
|
|
|
if !ok {
|
|
|
|
return InterrupterMessage{}, errors.New("Not a properly formatted decline message")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(dmsg.From, "declined your game invite")
|
2024-08-15 20:08:23 -06:00
|
|
|
case "disconnect":
|
2024-09-26 19:49:59 -06:00
|
|
|
|
|
|
|
dmsg, ok := msg.(lobby.Disconnect)
|
|
|
|
if !ok {
|
|
|
|
return InterrupterMessage{}, errors.New("Not a properly formatted disconnect message")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(dmsg.From, "has disconnected")
|
2024-08-15 20:08:23 -06:00
|
|
|
case "connect":
|
2024-09-26 19:49:59 -06:00
|
|
|
|
|
|
|
cmsg, ok := msg.(lobby.Connect)
|
|
|
|
if !ok {
|
|
|
|
return InterrupterMessage{}, errors.New("Not a properly formated connect message")
|
|
|
|
}
|
|
|
|
fmt.Println(cmsg.From, "has connected")
|
2024-08-15 20:08:23 -06:00
|
|
|
case "pong":
|
2024-09-26 19:49:59 -06:00
|
|
|
fmt.Println("Received pong")
|
|
|
|
case "error":
|
|
|
|
em, ok := msg.(lobby.Error)
|
|
|
|
if !ok {
|
|
|
|
slog.Debug("Received an indecipherable error message...", slog.Any("msg", msg))
|
|
|
|
}
|
|
|
|
fmt.Println(red, em.Message, normal)
|
2024-08-15 20:08:23 -06:00
|
|
|
default:
|
2024-09-26 19:49:59 -06:00
|
|
|
fmt.Println("Received", message.MessageType, message.Message)
|
2024-08-15 20:08:23 -06:00
|
|
|
}
|
2024-08-24 16:09:12 -06:00
|
|
|
return InterrupterMessage{}, nil
|
2024-08-15 20:08:23 -06:00
|
|
|
}
|