This commit is contained in:
2024-10-03 22:41:33 -06:00
parent 15e7f20f1a
commit 067d22f3a9
20 changed files with 476 additions and 257 deletions
+37 -26
View File
@@ -8,6 +8,7 @@ import (
"net"
"os"
"sshpong/internal/client"
"sshpong/internal/config"
"sshpong/internal/lobby"
"strings"
)
@@ -15,25 +16,43 @@ import (
var username string
func main() {
slog.SetLogLoggerLevel(slog.LevelDebug)
slog.Debug("Debug logs active...")
if len(os.Args) == 1 {
config.LoadConfig("")
} else {
config.LoadConfig(os.Args[1])
}
slog.SetLogLoggerLevel(slog.Level(config.Config.LogLevel))
fmt.Println("Welcome to sshpong!")
fmt.Println("Please enter your username")
egress := make(chan []byte)
ingress := make(chan []byte)
interrupter := make(chan client.InterrupterMessage, 100)
exit := make(chan string)
buf := make([]byte, 1024)
n, err := os.Stdin.Read(buf)
if err != nil {
log.Panic("Bro your input is no good...")
var usernameOk = false
// In the future make a DB call as well?
isUsernameOk := func(un string) bool {
if strings.Contains(un, ":") || len(strings.Split(un, " ")) > 1 || len(un) < 1 {
fmt.Println(client.Red, "Sorry, please pick a username that has no special characters or spaces.", client.Normal)
return false
}
return true
}
username = string(buf[:n-1])
for !usernameOk {
fmt.Println("Please enter your username")
buf := make([]byte, 1024)
n, err := os.Stdin.Read(buf)
if err != nil {
log.Panic("Bro your input is no good...")
}
username = string(buf[:n-1])
usernameOk = isUsernameOk(username)
}
fmt.Println("username is...", username)
conn, err := ConnectToLobby(username)
if err != nil {
log.Panic(err)
@@ -55,6 +74,11 @@ func main() {
select {
case msg := <-interrupter:
if msg.InterruptType == "start_game" {
slog.Debug("closing input handler with start_game message and sending exit signal")
exit <- msg.Content
return
}
userMessage, err := client.HandleInterruptInput(msg, args, username)
if err != nil {
userMessage, err = client.HandleUserInput(args, username)
@@ -67,16 +91,6 @@ func main() {
}
}
egress <- userMessage
if userMessage[0] == lobby.Accept || userMessage[0] == lobby.Disconnect {
slog.Debug("Closing input handler with accept or disconnect message")
return
}
if userMessage[0] == lobby.StartGame {
slog.Debug("closing input handler with start_game message and sending exit signal")
exit <- msg.Content
return
}
default:
userMessage, err = client.HandleUserInput(args, username)
@@ -88,9 +102,7 @@ func main() {
continue
}
egress <- userMessage
}
}
}()
@@ -103,6 +115,9 @@ func main() {
if err != nil {
log.Panic("Error handling server message disconnecting...")
}
if interrupterMsg.InterruptType == "start_game" {
exit <- interrupterMsg.Content
}
if interrupterMsg.InterruptType != "" {
interrupter <- interrupterMsg
}
@@ -120,10 +135,6 @@ func main() {
if err == io.EOF {
log.Panic("Server disconnected, sorry...")
}
if msg[0] == lobby.StartGame || msg[0] == lobby.Disconnect {
slog.Debug("closing network writer ")
return
}
}
}()
@@ -139,7 +150,6 @@ func main() {
}
}()
fmt.Println("Waiting for an exit message")
isStartGame := <-exit
if isStartGame != "" {
fmt.Println("Connecting to game", isStartGame)
@@ -157,6 +167,7 @@ func main() {
}
func ConnectToLobby(username string) (net.Conn, error) {
slog.Debug("connecting to server...")
conn, err := net.Dial("tcp", "127.0.0.1:12345")
if err != nil {
return nil, fmt.Errorf("Sorry, failed to connect to server...")