You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.3 KiB
96 lines
2.3 KiB
package main |
|
|
|
import ( |
|
"fmt" |
|
"os" |
|
"os/exec" |
|
"strconv" |
|
"strings" |
|
|
|
"gopkg.in/redis.v5" |
|
) |
|
|
|
func printProgressScreen(redisClient *redis.Client, username string) { |
|
// print header |
|
fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay) |
|
println("") |
|
|
|
// check directory for live players |
|
isNotEmpty, err := redisClient.Exists("inprogress").Result() |
|
if err != nil { |
|
panic(err) |
|
} |
|
if isNotEmpty { |
|
println(" Choose a player to spectate ('enter' without selection returns)") |
|
} else { |
|
println(" No live players currently (blank entry returns)") |
|
} |
|
println("") |
|
|
|
inProg := make(map[int]string) |
|
inProgTimer := 1 |
|
inprogress, err := redisClient.SMembers("inprogress").Result() |
|
if err != nil { |
|
panic(err) |
|
} |
|
for _, i := range inprogress { |
|
// loop through users |
|
fmt.Printf(" %d) %s\n", inProgTimer, i) |
|
|
|
// add user to line map |
|
inProg[inProgTimer] = i |
|
inProgTimer++ |
|
} |
|
|
|
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 == "" { |
|
printWelcomeScreen(redisClient) |
|
} else { |
|
printUserScreen(redisClient, username) |
|
} |
|
} |
|
|
|
// check if selection is in out map |
|
if inProg[s] != "" { |
|
user := strings.Split(inProg[s], ":") |
|
fmt.Printf("going to spectate '%s'\n", user[0]) |
|
|
|
// set ttyrec path |
|
ttyName, _ := redisClient.Get(fmt.Sprintf("inprogress:%s", user[0])).Result() |
|
ttyrecPath := fmt.Sprintf("%s/user/%s/ttyrec/%s.ttyrec", config.NethackLauncher.HackDir, user[0], ttyName) |
|
|
|
// restart display |
|
exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run() |
|
clearScreen() |
|
nh := exec.Command("ttyplay", "-p", ttyrecPath) |
|
//nh := exec.Command("termplay", "-f", "live", ttyrecPath) |
|
nh.Stdout = os.Stdout |
|
nh.Stdin = os.Stdin |
|
nh.Stderr = os.Stderr |
|
nh.Run() |
|
if username == "" { |
|
printWelcomeScreen(redisClient) |
|
} else { |
|
printUserScreen(redisClient, username) |
|
} |
|
// TODO fix bug where user has to <ctrl-c> when game exists |
|
} |
|
} |
|
}
|
|
|