Browse Source

Add ttyrec library function

Remove welcome screen data
Cleanup user screen menu
pull/1/head
Matthew Faltys 5 years ago
parent
commit
7e2bf9949e
  1. 1
      Makefile
  2. 144
      nethack-launcher/library.go
  3. 15
      nethack-launcher/print_user_screen.go
  4. 9
      nethack-launcher/print_welcome_screen.go

1
Makefile

@ -15,6 +15,7 @@ run:
nethack-launcher/create_initial_files.go \ nethack-launcher/create_initial_files.go \
nethack-launcher/create_user_files.go \ nethack-launcher/create_user_files.go \
nethack-launcher/janitor.go \ nethack-launcher/janitor.go \
nethack-launcher/library.go \
nethack-launcher/nethack-launcher.go \ nethack-launcher/nethack-launcher.go \
nethack-launcher/print_change_password_screen.go \ nethack-launcher/print_change_password_screen.go \
nethack-launcher/print_high_scores.go \ nethack-launcher/print_high_scores.go \

144
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)
}
}

15
nethack-launcher/print_user_screen.go

@ -12,19 +12,25 @@ import (
func printUserScreen(redisClient *redis.Client, username string) string { func printUserScreen(redisClient *redis.Client, username string) string {
clearScreen() clearScreen()
fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay) fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay)
println("")
fmt.Printf(" Logged in as: %s\n", username) fmt.Printf(" Logged in as: %s\n", username)
println("") println("")
println("Global")
println(" l) Logout") println(" l) Logout")
println(" c) Change password") println(" c) Change password")
println(" w) Watch games in progress") 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(" h) View NetHack highscores")
println(" a) View all NetHack scores") println(" a) View all NetHack scores")
println(" e) Edit NetHack config") println(" e) Edit NetHack config")
println(" f) Edit SOTW config")
println(" r) Recover NetHack from crash") println(" r) Recover NetHack from crash")
fmt.Printf(" p) Play NetHack %s\n", config.NethackLauncher.NethackVersion) 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(" q) Quit")
println("") println("")
fmt.Printf(">> ") fmt.Printf(">> ")
@ -83,6 +89,9 @@ func printUserScreen(redisClient *redis.Client, username string) string {
case "w": case "w":
clearScreen() clearScreen()
printProgressScreen(redisClient, username) printProgressScreen(redisClient, username)
case "x":
clearScreen()
printLibraryScreen(redisClient, username)
case "h": case "h":
clearScreen() clearScreen()
printHighScores(redisClient, username) printHighScores(redisClient, username)

9
nethack-launcher/print_welcome_screen.go

@ -11,14 +11,11 @@ import (
func printWelcomeScreen(redisClient *redis.Client) string { func printWelcomeScreen(redisClient *redis.Client) string {
clearScreen() clearScreen()
fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay) fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay)
println("")
println(" Not logged in.") println(" Not logged in.")
println("") println("")
println(" l) Login") println(" l) Login")
println(" r) Register new user") println(" r) Register new user")
println(" w) Watch games in progress") println(" w) Watch games in progress")
println(" h) View highscores")
println(" a) View all scores")
println(" q) Quit") println(" q) Quit")
println("") println("")
fmt.Printf(">> ") fmt.Printf(">> ")
@ -45,12 +42,6 @@ func printWelcomeScreen(redisClient *redis.Client) string {
case "w": case "w":
clearScreen() clearScreen()
printProgressScreen(redisClient, "") printProgressScreen(redisClient, "")
case "h":
clearScreen()
printHighScores(redisClient, "")
case "a":
clearScreen()
printAllScores(redisClient, "")
case "q": case "q":
exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run() exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run()
clearScreen() clearScreen()

Loading…
Cancel
Save