Matthew Faltys
5 years ago
4 changed files with 158 additions and 13 deletions
@ -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) |
||||
} |
||||
} |
Loading…
Reference in new issue