sshpong/internal/client/client_utils.go

127 lines
3.0 KiB
Go
Raw Normal View History

2024-08-15 20:08:23 -06:00
package client
import (
"fmt"
"io"
"sshpong/internal/netwrk"
"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-08-24 16:09:12 -06:00
func HandleUserInput(args []string) (*netwrk.LobbyMessage, error) {
2024-08-23 11:59:19 -06:00
if len(args) == 0 {
return nil, help
}
2024-08-15 20:08:23 -06:00
switch args[0] {
case "invite":
if args[1] != "" {
return &netwrk.LobbyMessage{
Type: "invite",
Content: args[1],
}, nil
} else {
fmt.Println("Please provide a player to invite ")
}
case "chat":
if args[1] != "" {
return &netwrk.LobbyMessage{
Type: "chat",
Content: strings.Join(args[1:], " "),
}, nil
}
case "/":
if args[1] != "" {
return &netwrk.LobbyMessage{
Type: "chat",
Content: strings.Join(args[1:], " "),
}, nil
}
case "quit":
return nil, io.EOF
case "q":
return nil, io.EOF
case "help":
2024-08-23 11:59:19 -06:00
return nil, help
2024-08-15 20:08:23 -06:00
case "h":
2024-08-23 11:59:19 -06:00
return nil, help
2024-08-15 20:08:23 -06:00
default:
2024-08-24 16:09:12 -06:00
if strings.Index(args[0], "/") == 0 {
return &netwrk.LobbyMessage{
Type: "chat",
Content: strings.Join(args, " ")[1:],
}, nil
}
2024-08-23 11:59:19 -06:00
return nil, help
2024-08-15 20:08:23 -06:00
}
return nil, nil
}
2024-08-24 16:09:12 -06:00
func HandleInterruptInput(incoming InterrupterMessage, args []string) (*netwrk.LobbyMessage, error) {
switch incoming.InterruptType {
case "invite":
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":
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) {
2024-08-15 20:08:23 -06:00
switch message.Type {
2024-08-24 16:09:12 -06:00
case "name":
fmt.Printf("Current Players\n%s\n", message.Content)
2024-08-15 20:08:23 -06:00
case "invite":
2024-08-24 16:09:12 -06:00
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...")
2024-08-15 20:08:23 -06:00
case "accepted":
2024-08-24 16:09:12 -06:00
fmt.Println(message.PlayerId, "accepted your invite.\n", "Starting game...")
case "game_start":
fmt.Println("Invited accepted\n", "Starting game...")
2024-08-15 20:08:23 -06:00
case "text":
2024-08-24 16:09:12 -06:00
fmt.Println(message.PlayerId, ":", message.Content)
2024-08-15 20:08:23 -06:00
case "decline_game":
2024-08-24 16:09:12 -06:00
fmt.Println(message.Content, "declined your game invite")
2024-08-15 20:08:23 -06:00
case "disconnect":
2024-08-24 16:09:12 -06:00
fmt.Println(message.Content, "has disconnected")
2024-08-15 20:08:23 -06:00
case "connect":
2024-08-24 16:09:12 -06:00
fmt.Println(message.Content, "has connected")
2024-08-15 20:08:23 -06:00
case "pong":
2024-08-24 16:09:12 -06:00
fmt.Println("Received", message.Content)
2024-08-15 20:08:23 -06:00
default:
2024-08-24 16:09:12 -06:00
fmt.Println("Received", message.Content)
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
}