diff --git a/Makefile b/Makefile index da99eda..9d43abd 100644 --- a/Makefile +++ b/Makefile @@ -11,10 +11,11 @@ all: stat run: go run \ nethack-launcher/check_dir.go \ - nethack-launcher/cleanup.go \ + nethack-launcher/cleanup.go \ nethack-launcher/create_initial_files.go \ nethack-launcher/create_user_files.go \ nethack-launcher/janitor.go \ + nethack-launcher/library.go \ nethack-launcher/nethack-launcher.go \ nethack-launcher/print_change_password_screen.go \ nethack-launcher/print_high_scores.go \ diff --git a/nethack-launcher/library.go b/nethack-launcher/library.go new file mode 100644 index 0000000..652ec0f --- /dev/null +++ b/nethack-launcher/library.go @@ -0,0 +1,144 @@ +package main + +import ( + "bufio" + "fmt" + "io/ioutil" + "os" + "os/exec" + "strconv" + "strings" + + "gopkg.in/redis.v5" +) + +func printLibraryScreen(redisClient *redis.Client, username string) { + // print header + fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay) + println("") + + println(" Choose a users library to view ('enter' without selection returns)") + println("") + + us, err := ioutil.ReadDir(fmt.Sprintf("/%s/user", config.NethackLauncher.HackDir)) + if err != nil { + println(" No users exist ('enter' without selection returns)") + panic(err) + } + + users := make(map[int]string) + usersTimer := 1 + for _, u := range us { + fmt.Printf(" %d) %s\n", usersTimer, u.Name()) + + // add user to line map + users[usersTimer] = fmt.Sprintf("%s", u.Name()) + usersTimer++ + } + + fmt.Println("") + fmt.Printf(">> ") + // start user input + + // 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) + s, _ := strconv.Atoi(string(b)) + + // check if user is trying to navigate + if string(b) == "\n" { + exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run() + clearScreen() + if username == "" { + clearScreen() + printWelcomeScreen(redisClient) + } else { + clearScreen() + printUserScreen(redisClient, username) + } + } + // check if selection is in out map + if users[s] != "" { + user := strings.Split(users[s], ":") + fmt.Printf("going to spectate '%s'\n", user[0]) + clearScreen() + printUserLibraryScreen(redisClient, username, user[0]) + } + } +} + +func printUserLibraryScreen(redisClient *redis.Client, username, currentUser string) { + // print header + fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay) + println("") + + println(" Choose a ttyrec to view ('enter' without selection returns)") + println(" The following keybinds are available during playback:") + println(" • + or f: double the speed of playback") + println(" • - or s: halve the speed of playback") + println(" • 0: set playback speed to 0, pausing playback") + println(" • 1: set playback speed to 1 again") + println("") + + t, err := ioutil.ReadDir(fmt.Sprintf("/%s/user/%s/ttyrec", config.NethackLauncher.HackDir, currentUser)) + if err != nil { + println(" No ttyrecs exist ('enter' without selection returns)") + panic(err) + } + + ttyrecs := make(map[int]string) + ttyrecsTimer := 1 + for _, ty := range t { + fmt.Printf(" %d) %s\n", ttyrecsTimer, ty.Name()) + + // add user to line map + ttyrecs[ttyrecsTimer] = fmt.Sprintf("%s", ty.Name()) + ttyrecsTimer++ + } + + fmt.Println("") + fmt.Printf(">> ") + // start user input + + // disable input buffering + exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run() + // enable showing input on screen + exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run() + + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + if scanner.Text() == "" { + clearScreen() + printLibraryScreen(redisClient, username) + } + // save input + s := scanner.Text() + sel, err := strconv.Atoi(s) + if err != nil { + fmt.Printf(" There was a problem with your last entry.\n>> ") + } + // check if selection is in our map + if ttyrecs[sel] != "" { + fmt.Printf("going to watch '%s'\n", ttyrecs[sel]) + clearScreen() + + // set ttyrec path + ttyrecPath := fmt.Sprintf("%s/user/%s/ttyrec/%s", config.NethackLauncher.HackDir, username, ttyrecs[sel]) + tp := exec.Command("ttyplay", ttyrecPath) + tp.Stdout = os.Stdout + tp.Stdin = os.Stdin + tp.Stderr = os.Stderr + err := tp.Run() + if err != nil { + fmt.Print(err) + } + } + clearScreen() + printUserLibraryScreen(redisClient, username, currentUser) + } +} diff --git a/nethack-launcher/print_user_screen.go b/nethack-launcher/print_user_screen.go index 0601cc6..70b601a 100644 --- a/nethack-launcher/print_user_screen.go +++ b/nethack-launcher/print_user_screen.go @@ -12,19 +12,25 @@ import ( 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("Global") println(" l) Logout") println(" c) Change password") println(" w) Watch games in progress") + println(" x) Browse library of ttyrecs") + println("") + println("Shadow of the Wyrm") + println(" f) Edit SOTW config") + fmt.Printf(" o) Play Shadow of the Wyrm %s\n", config.NethackLauncher.SotwVersion) + println("") + println("Nethack") println(" h) View NetHack highscores") println(" a) View all NetHack scores") println(" e) Edit NetHack config") - println(" f) Edit SOTW config") println(" r) Recover NetHack from crash") fmt.Printf(" p) Play NetHack %s\n", config.NethackLauncher.NethackVersion) - fmt.Printf(" o) Play Shadow of the Wyrm %s\n", config.NethackLauncher.SotwVersion) + println("") println(" q) Quit") println("") fmt.Printf(">> ") @@ -83,6 +89,9 @@ func printUserScreen(redisClient *redis.Client, username string) string { case "w": clearScreen() printProgressScreen(redisClient, username) + case "x": + clearScreen() + printLibraryScreen(redisClient, username) case "h": clearScreen() printHighScores(redisClient, username) diff --git a/nethack-launcher/print_welcome_screen.go b/nethack-launcher/print_welcome_screen.go index 61e568b..9d2c81c 100644 --- a/nethack-launcher/print_welcome_screen.go +++ b/nethack-launcher/print_welcome_screen.go @@ -11,14 +11,11 @@ import ( func printWelcomeScreen(redisClient *redis.Client) string { clearScreen() fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay) - println("") println(" Not logged in.") println("") println(" l) Login") println(" r) Register new user") println(" w) Watch games in progress") - println(" h) View highscores") - println(" a) View all scores") println(" q) Quit") println("") fmt.Printf(">> ") @@ -45,12 +42,6 @@ func printWelcomeScreen(redisClient *redis.Client) string { case "w": clearScreen() printProgressScreen(redisClient, "") - case "h": - clearScreen() - printHighScores(redisClient, "") - case "a": - clearScreen() - printAllScores(redisClient, "") case "q": exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run() clearScreen()