MVP
This commit is contained in:
+37
-26
@@ -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...")
|
||||
|
||||
+74
-63
@@ -5,7 +5,11 @@ import (
|
||||
"log"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"sshpong/internal/config"
|
||||
"sshpong/internal/lobby"
|
||||
"sshpong/internal/pong"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -13,14 +17,20 @@ var exit chan bool
|
||||
var games sync.Map
|
||||
|
||||
func main() {
|
||||
slog.SetLogLoggerLevel(slog.LevelDebug)
|
||||
if len(os.Args) == 1 {
|
||||
config.LoadConfig("")
|
||||
} else {
|
||||
config.LoadConfig(os.Args[1])
|
||||
}
|
||||
|
||||
slog.SetLogLoggerLevel(slog.Level(config.Config.LogLevel))
|
||||
fmt.Println("Starting sshpong lobby...")
|
||||
go LobbyListen()
|
||||
fmt.Println("Lobby started")
|
||||
|
||||
// fmt.Println("Starting game listener...")
|
||||
// go GamesListen()
|
||||
// fmt.Println("Game listener started")
|
||||
fmt.Println("Starting game listener...")
|
||||
go GamesListen()
|
||||
fmt.Println("Game listener started")
|
||||
|
||||
_ = <-exit
|
||||
}
|
||||
@@ -28,10 +38,9 @@ func main() {
|
||||
// Starts listening on port 12345 for TCP connections
|
||||
// Also creates client pool and game connection singletons
|
||||
func LobbyListen() {
|
||||
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:12345")
|
||||
if err != nil {
|
||||
slog.Error("Error setting up listener for lobby. Exiting...")
|
||||
slog.Error("Error setting up listener for lobby. Exiting...", err)
|
||||
}
|
||||
|
||||
defer listener.Close()
|
||||
@@ -64,60 +73,62 @@ func LobbyListen() {
|
||||
}
|
||||
}
|
||||
|
||||
// func GamesListen() {
|
||||
//
|
||||
// slog.SetLogLoggerLevel(slog.LevelDebug)
|
||||
// slog.Debug("Debug level logs are active")
|
||||
//
|
||||
// gameListener, err := net.Listen("tcp", "127.0.0.1:42069")
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
//
|
||||
// for {
|
||||
// defer gameListener.Close()
|
||||
// conn, err := gameListener.Accept()
|
||||
// if err != nil {
|
||||
// log.Println(err)
|
||||
// continue
|
||||
// }
|
||||
//
|
||||
// slog.Debug("Received game connection")
|
||||
//
|
||||
// go func(conn net.Conn) {
|
||||
// messageBytes := make([]byte, 126)
|
||||
//
|
||||
// n, err := conn.Read(messageBytes)
|
||||
// if err != nil {
|
||||
// log.Printf("Error reading game ID on connection %s", err)
|
||||
// }
|
||||
//
|
||||
// gInfo := strings.SplitAfter(string(messageBytes[:n]), ":")
|
||||
// if err != nil {
|
||||
// log.Printf("Game id was not a string? %s", err)
|
||||
// }
|
||||
//
|
||||
// slog.Debug("Game request data", slog.Any("game info", gInfo))
|
||||
//
|
||||
// game, ok := games.Load(gInfo[0])
|
||||
// if !ok {
|
||||
// games.Store(gInfo[0], GameClients{Client1: Client{
|
||||
// Username: gInfo[1],
|
||||
// Conn: conn,
|
||||
// }, Client2: Client{}})
|
||||
// } else {
|
||||
// gameclients, _ := game.(GameClients)
|
||||
// client2 := Client{
|
||||
// Username: gInfo[1],
|
||||
// Conn: conn,
|
||||
// }
|
||||
//
|
||||
// games.Store(gInfo[0], GameClients{
|
||||
// Client1: gameclients.Client1,
|
||||
// Client2: client2})
|
||||
//
|
||||
// go pong.StartGame(gameclients.Client1.Conn, client2.Conn, gameclients.Client1.Username, client2.Username)
|
||||
// }
|
||||
// }(conn)
|
||||
// }
|
||||
// }
|
||||
func GamesListen() {
|
||||
|
||||
type GameClients struct {
|
||||
Client1 lobby.Client
|
||||
Client2 lobby.Client
|
||||
}
|
||||
|
||||
gameListener, err := net.Listen("tcp", "127.0.0.1:42069")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for {
|
||||
defer gameListener.Close()
|
||||
conn, err := gameListener.Accept()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
slog.Debug("Received game connection")
|
||||
|
||||
go func(conn net.Conn) {
|
||||
messageBytes := make([]byte, 126)
|
||||
|
||||
n, err := conn.Read(messageBytes)
|
||||
if err != nil {
|
||||
log.Printf("Error reading game ID on connection %s", err)
|
||||
}
|
||||
|
||||
gInfo := strings.SplitAfter(string(messageBytes[:n]), ":")
|
||||
if err != nil {
|
||||
log.Printf("Game id was not a string? %s", err)
|
||||
}
|
||||
|
||||
slog.Debug("Game request data", slog.Any("game info", gInfo))
|
||||
|
||||
game, ok := games.Load(gInfo[0])
|
||||
if !ok {
|
||||
games.Store(gInfo[0], GameClients{Client1: lobby.Client{
|
||||
Username: gInfo[1],
|
||||
Conn: conn,
|
||||
}, Client2: lobby.Client{}})
|
||||
} else {
|
||||
gameclients, _ := game.(GameClients)
|
||||
client2 := lobby.Client{
|
||||
Username: gInfo[1],
|
||||
Conn: conn,
|
||||
}
|
||||
|
||||
games.Store(gInfo[0], GameClients{
|
||||
Client1: gameclients.Client1,
|
||||
Client2: client2})
|
||||
|
||||
go pong.StartGame(gameclients.Client1.Conn, client2.Conn, gameclients.Client1.Username, client2.Username)
|
||||
}
|
||||
}(conn)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user