Browse Source

Finish register and login

master
Matthew Faltys 5 years ago
parent
commit
618a90fd38
  1. 90
      nethack-launcher.go

90
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) { func printLoginScreen(redisClient *redis.Client) {
fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay) fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay)
println("") println("")
@ -168,15 +208,40 @@ func printLoginScreen(redisClient *redis.Client) {
printWelcomeScreen(redisClient) printWelcomeScreen(redisClient)
} }
fmt.Printf("you entered '%s'\n", scanner.Text())
// check redis for user // check redis for user
usernameHash := sha3.Sum512([]byte(scanner.Text())) username := scanner.Text()
_, err := redisClient.Get(fmt.Sprintf("user:%x", usernameHash)).Result() storedHash, err := redisClient.Get(fmt.Sprintf("user:%s", username)).Result()
if err != redis.Nil { if err != nil {
// user already exists // user does not exist
fmt.Printf(" There was a problem with your last entry.\n>> ")
} else { } 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 { if err := scanner.Err(); err != nil {
@ -190,7 +255,7 @@ func printRegisterScreen(redisClient *redis.Client) {
println("") println("")
println(" Welcome new user. Please enter a username") println(" Welcome new user. Please enter a username")
println(" Only characters and numbers are allowed, with no spaces.") println(" Only characters and numbers are allowed, with no spaces.")
println(" 20 characters max.") println(" 20 characters max. (blank entry aborts)")
println("") println("")
fmt.Printf(">> ") fmt.Printf(">> ")
@ -201,8 +266,8 @@ func printRegisterScreen(redisClient *redis.Client) {
} }
// check redis for user // check redis for user
usernameHash := sha3.Sum512([]byte(scanner.Text())) username := scanner.Text()
_, err := redisClient.Get(fmt.Sprintf("user:%x", usernameHash)).Result() _, err := redisClient.Get(fmt.Sprintf("user:%s", username)).Result()
if err != redis.Nil { if err != redis.Nil {
// user already exists // user already exists
fmt.Printf("There was a problem with your last entry.\n>> ") fmt.Printf("There was a problem with your last entry.\n>> ")
@ -239,9 +304,8 @@ func printRegisterScreen(redisClient *redis.Client) {
} }
// set user in redis // set user in redis
secHash := sha3.Sum512([]byte(sec)) secHash := sha3.Sum512([]byte(sec))
redisClient.Set(fmt.Sprintf("user:%x", usernameHash), fmt.Sprintf("%x", secHash), 0).Err() redisClient.Set(fmt.Sprintf("user:%s", username), fmt.Sprintf("%x", secHash), 0).Err()
fmt.Println("Success") printUserScreen(redisClient, username)
os.Exit(0)
} }
} }
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {

Loading…
Cancel
Save