From 618a90fd38dbf44bb761f5001399fdcd2dc20c6a Mon Sep 17 00:00:00 2001 From: Matthew Faltys Date: Fri, 8 Nov 2019 20:45:44 +0000 Subject: [PATCH] Finish register and login --- nethack-launcher.go | 90 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 13 deletions(-) diff --git a/nethack-launcher.go b/nethack-launcher.go index 39568e5..b0360c4 100644 --- a/nethack-launcher.go +++ b/nethack-launcher.go @@ -155,6 +155,46 @@ func printWelcomeScreen(redisClient *redis.Client) string { } } +func printUserScreen(redisClient *redis.Client, username string) string { + clearScreen() + fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay) + println("") + fmt.Printf(" Logged in as: %s\n", username) + println("") + println(" l) Logout") + println(" w) Watch games in progress") + println(" p) Play NetHack 3.4.3") + println(" q) Quit") + println("") + fmt.Printf(">> ") + + // disable input buffering + exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run() + // do not display entered characters on the screen + exec.Command("stty", "-F", "/dev/tty", "-echo").Run() + + var b []byte = make([]byte, 1) + for { + os.Stdin.Read(b) + switch string(b) { + case "l": + // restart display + exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run() + clearScreen() + printWelcomeScreen(redisClient) + case "p": + // restart display + exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run() + clearScreen() + os.Exit(0) + case "q": + clearScreen() + os.Exit(0) + default: + } + } +} + func printLoginScreen(redisClient *redis.Client) { fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay) println("") @@ -168,15 +208,40 @@ func printLoginScreen(redisClient *redis.Client) { printWelcomeScreen(redisClient) } - fmt.Printf("you entered '%s'\n", scanner.Text()) // check redis for user - usernameHash := sha3.Sum512([]byte(scanner.Text())) - _, err := redisClient.Get(fmt.Sprintf("user:%x", usernameHash)).Result() - if err != redis.Nil { - // user already exists - + username := scanner.Text() + storedHash, err := redisClient.Get(fmt.Sprintf("user:%s", username)).Result() + if err != nil { + // user does not exist + fmt.Printf(" There was a problem with your last entry.\n>> ") } else { - // set up user + // get password from user and compare + // turn off echo display + exec.Command("stty", "-F", "/dev/tty", "-echo").Run() + + noPass := true + for noPass { + fmt.Printf("\n Please enter your password (blank entry aborts).\n>> ") + reader := bufio.NewReader(os.Stdin) + + typedAuth, _ := reader.ReadString('\n') + typedAuth = strings.Replace(typedAuth, "\n", "", -1) + + if typedAuth == "" { + // exit to main menu + printWelcomeScreen(redisClient) + } + + // get hash of typedAuth + typedHash := sha3.Sum512([]byte(typedAuth)) + if fmt.Sprintf("%x", typedHash) == storedHash { + // user authed + printUserScreen(redisClient, username) + noPass = false + } else { + fmt.Printf("\n There was a problem with your last entry.") + } + } } } if err := scanner.Err(); err != nil { @@ -190,7 +255,7 @@ func printRegisterScreen(redisClient *redis.Client) { println("") println(" Welcome new user. Please enter a username") println(" Only characters and numbers are allowed, with no spaces.") - println(" 20 characters max.") + println(" 20 characters max. (blank entry aborts)") println("") fmt.Printf(">> ") @@ -201,8 +266,8 @@ func printRegisterScreen(redisClient *redis.Client) { } // check redis for user - usernameHash := sha3.Sum512([]byte(scanner.Text())) - _, err := redisClient.Get(fmt.Sprintf("user:%x", usernameHash)).Result() + username := scanner.Text() + _, err := redisClient.Get(fmt.Sprintf("user:%s", username)).Result() if err != redis.Nil { // user already exists fmt.Printf("There was a problem with your last entry.\n>> ") @@ -239,9 +304,8 @@ func printRegisterScreen(redisClient *redis.Client) { } // set user in redis secHash := sha3.Sum512([]byte(sec)) - redisClient.Set(fmt.Sprintf("user:%x", usernameHash), fmt.Sprintf("%x", secHash), 0).Err() - fmt.Println("Success") - os.Exit(0) + redisClient.Set(fmt.Sprintf("user:%s", username), fmt.Sprintf("%x", secHash), 0).Err() + printUserScreen(redisClient, username) } } if err := scanner.Err(); err != nil {