sshpong/internal/client/client_utils.go

223 lines
6.2 KiB
Go
Raw Permalink Normal View History

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-10-03 22:41:33 -06:00
"github.com/google/uuid"
2024-08-15 20:08:23 -06:00
)
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-10-03 22:41:33 -06:00
var Red = "\x1b[31m"
var Normal = "\033[0m"
2024-09-26 19:49:59 -06:00
2024-09-28 23:26:06 -06:00
func HandleUserInput(args []string, username string) ([]byte, error) {
2024-08-23 11:59:19 -06:00
if len(args) == 0 {
2024-09-28 23:26:06 -06:00
return []byte{}, 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-10-03 22:41:33 -06:00
if args[1] == username {
fmt.Println("You cannot invite yourself to a game ;)")
} else {
msg, err := lobby.Marshal(lobby.InviteData{From: username, To: args[1]}, lobby.Invite)
if err != nil {
slog.Debug("invite message was not properly marshalled", "error", err)
}
return msg, err
2024-09-28 23:26:06 -06:00
}
2024-08-15 20:08:23 -06:00
} else {
fmt.Println("Please provide a player to invite ")
}
case "chat":
if args[1] != "" {
2024-09-28 23:26:06 -06:00
msg, err := lobby.Marshal(lobby.ChatData{
From: username,
Message: strings.Join(args[1:], " "),
}, lobby.Chat)
if err != nil {
slog.Debug("chat message was not properly marshalled", "error", err)
}
return msg, err
2024-08-15 20:08:23 -06:00
}
case "/":
if args[1] != "" {
2024-09-28 23:26:06 -06:00
msg, err := lobby.Marshal(lobby.ChatData{
From: username,
Message: strings.Join(args[1:], " "),
}, lobby.Chat)
if err != nil {
slog.Debug("chat slash message was not properly marshalled", "error", err)
}
return msg, err
2024-08-15 20:08:23 -06:00
}
case "quit":
2024-09-28 23:26:06 -06:00
return []byte{}, io.EOF
2024-08-15 20:08:23 -06:00
case "q":
2024-09-28 23:26:06 -06:00
return []byte{}, io.EOF
2024-08-15 20:08:23 -06:00
case "help":
2024-09-28 23:26:06 -06:00
return []byte{}, help
2024-08-15 20:08:23 -06:00
case "h":
2024-09-28 23:26:06 -06:00
return []byte{}, 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-28 23:26:06 -06:00
msg, err := lobby.Marshal(lobby.ChatData{
From: username,
Message: strings.Join(args, " ")[1:],
}, lobby.Chat)
if err != nil {
slog.Debug("chat slash default message was not properly marshalled", "error", err)
}
return msg, err
}
return []byte{}, help
2024-08-15 20:08:23 -06:00
}
2024-09-28 23:26:06 -06:00
return []byte{}, nil
2024-08-15 20:08:23 -06:00
}
2024-09-28 23:26:06 -06:00
func HandleInterruptInput(incoming InterrupterMessage, args []string, username string) ([]byte, error) {
2024-08-24 16:09:12 -06:00
switch incoming.InterruptType {
2024-09-28 23:26:06 -06:00
// Respond with yes if you accept game
2024-08-24 16:09:12 -06:00
case "invite":
2024-10-03 22:41:33 -06:00
slog.Debug("handling invite interrupt")
2024-08-24 16:09:12 -06:00
if len(args) < 1 {
2024-09-28 23:26:06 -06:00
return []byte{}, nil
2024-08-24 16:09:12 -06:00
} else {
if strings.ToLower(args[0]) == "y" || strings.ToLower(args[0]) == "yes" {
2024-09-28 23:26:06 -06:00
msg, err := lobby.Marshal(lobby.AcceptData{
2024-10-03 22:41:33 -06:00
From: username,
To: incoming.Content,
GameID: uuid.NewString(),
2024-09-28 23:26:06 -06:00
}, lobby.Accept)
if err != nil {
slog.Debug("accept message was not properly marshalled", "error", err)
}
return msg, err
2024-08-24 16:09:12 -06:00
}
}
2024-10-03 22:41:33 -06:00
// TODO: Do we need this accepted? Disconnect and connect to game
// case "accepted":
// msg, err := lobby.Marshal(lobby.DisconnectData{
// From: incoming.Content,
// }, lobby.Disconnect)
// if err != nil {
// slog.Debug("disconnect message was not properly marshalled", "error", err)
// }
// return msg, err
2024-09-26 19:49:59 -06:00
case "start_game":
2024-09-28 23:26:06 -06:00
msg, err := lobby.Marshal(lobby.StartGameData{
To: "",
GameID: incoming.Content,
2024-10-03 22:41:33 -06:00
}, lobby.StartGame)
2024-09-28 23:26:06 -06:00
if err != nil {
slog.Debug("start game message was not properly marshalled", "error", err)
}
return msg, err
2024-08-24 16:09:12 -06:00
}
2024-09-28 23:26:06 -06:00
return []byte{}, fmt.Errorf("received a interrupt message that could not be handled %v", incoming)
2024-08-24 16:09:12 -06:00
}
2024-09-28 23:26:06 -06:00
func HandleServerMessage(msg []byte) (InterrupterMessage, error) {
header := msg[0]
switch header {
case lobby.Invite:
imsg, err := lobby.Unmarshal[lobby.InviteData](msg)
if err != nil {
2024-09-26 19:49:59 -06:00
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
2024-09-26 19:49:59 -06:00
2024-09-28 23:26:06 -06:00
case lobby.PendingInvite:
pimsg, err := lobby.Unmarshal[lobby.PendingInviteData](msg)
if err != nil {
2024-09-26 19:49:59 -06:00
return InterrupterMessage{}, errors.New("Not a properly formatted pending invite message")
}
fmt.Println("Invite sent to", pimsg.Recipient, "\nWaiting for response...")
2024-09-28 23:26:06 -06:00
case lobby.Accepted:
amsg, err := lobby.Unmarshal[lobby.AcceptedData](msg)
if err != nil {
2024-09-26 19:49:59 -06:00
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
2024-09-28 23:26:06 -06:00
case lobby.StartGame:
sgmsg, err := lobby.Unmarshal[lobby.StartGameData](msg)
if err != nil {
2024-09-26 19:49:59 -06:00
return InterrupterMessage{}, errors.New("Not a properly formatted start game message")
}
2024-10-03 22:41:33 -06:00
fmt.Println("Your invite was accepted. Press Enter to join game")
2024-09-26 19:49:59 -06:00
return InterrupterMessage{
InterruptType: "start_game",
Content: sgmsg.GameID,
}, nil
2024-09-28 23:26:06 -06:00
case lobby.Chat:
cmsg, err := lobby.Unmarshal[lobby.ChatData](msg)
if err != nil {
2024-09-26 19:49:59 -06:00
return InterrupterMessage{}, errors.New("Not a properly formatted chat message")
}
fmt.Println(cmsg.From, ":", cmsg.Message)
2024-09-28 23:26:06 -06:00
case lobby.Decline:
dmsg, err := lobby.Unmarshal[lobby.DeclineData](msg)
if err != nil {
2024-09-26 19:49:59 -06:00
return InterrupterMessage{}, errors.New("Not a properly formatted decline message")
}
fmt.Println(dmsg.From, "declined your game invite")
2024-09-28 23:26:06 -06:00
case lobby.Disconnect:
dmsg, err := lobby.Unmarshal[lobby.DisconnectData](msg)
if err != nil {
2024-09-26 19:49:59 -06:00
return InterrupterMessage{}, errors.New("Not a properly formatted disconnect message")
}
fmt.Println(dmsg.From, "has disconnected")
2024-09-28 23:26:06 -06:00
case lobby.Connect:
cmsg, err := lobby.Unmarshal[lobby.ConnectData](msg)
if err != nil {
2024-09-26 19:49:59 -06:00
return InterrupterMessage{}, errors.New("Not a properly formated connect message")
}
fmt.Println(cmsg.From, "has connected")
2024-09-28 23:26:06 -06:00
case lobby.CurrentlyConnected:
ccmsg, err := lobby.Unmarshal[lobby.CurrentlyConnectedData](msg)
if err != nil {
return InterrupterMessage{}, errors.New("Not a properly formated connect message")
}
fmt.Printf("Current Players\n%s\n", ccmsg.Players)
case lobby.Error:
em, err := lobby.Unmarshal[lobby.ErrorData](msg)
if err != nil {
slog.Debug("Received an indecipherable error message...", slog.Any("msg", msg[1:]))
2024-09-26 19:49:59 -06:00
}
2024-10-03 22:41:33 -06:00
fmt.Println(Red, em.Message, Normal)
2024-09-28 23:26:06 -06:00
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
}