Nethack Launcher
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.

132 lines
3.7 KiB

package main
import (
"fmt"
"os"
"os/exec"
"time"
"gopkg.in/redis.v5"
)
func printUserScreen(redisClient *redis.Client, username string) string {
clearScreen()
fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay)
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(" r) Recover NetHack from crash")
fmt.Printf(" p) Play NetHack %s\n", config.NethackLauncher.NethackVersion)
println("")
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 "e":
hackRCLoc := fmt.Sprintf("%s/user/%s/.nethackrc", config.NethackLauncher.HackDir, username)
exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run()
clearScreen()
nh := exec.Command("vim", "-i", "NONE", "-Z", hackRCLoc)
nh.Stdout = os.Stdout
nh.Stdin = os.Stdin
nh.Stderr = os.Stderr
nh.Run()
clearScreen()
printUserScreen(redisClient, username)
case "f":
// check if the file has been uplifted
SOTWLoc := fmt.Sprintf("%s/user/%s/sotw/swyrm.ini", config.NethackLauncher.HackDir, username)
il, _ := os.Lstat(SOTWLoc)
if il.Mode()&os.ModeSymlink == os.ModeSymlink {
// the file is still a legacy symlink, remove and recopy
err := os.Remove(SOTWLoc)
if err != nil {
fmt.Println(err)
}
SOTWOrigin := fmt.Sprintf("%s/swyrm.ini", config.NethackLauncher.SotwRoot)
exec.Command("cp", SOTWOrigin, SOTWLoc).Run()
}
exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run()
clearScreen()
nh := exec.Command("vim", "-i", "NONE", "-Z", SOTWLoc)
nh.Stdout = os.Stdout
nh.Stdin = os.Stdin
nh.Stderr = os.Stderr
nh.Run()
clearScreen()
printUserScreen(redisClient, username)
case "c":
printChangePasswordScreen(redisClient, username)
clearScreen()
case "w":
clearScreen()
printProgressScreen(redisClient, username)
case "x":
clearScreen()
printLibraryScreen(redisClient, username)
case "h":
clearScreen()
printHighScores(redisClient, username)
case "a":
clearScreen()
printAllScores(redisClient, username)
case "p":
wg.Add(1)
currentTime := time.Now().UTC()
fulltime := currentTime.Format("2006-01-02.03:04:05")
go runGame(username, fulltime)
go runCleanup(username)
watcher := startWatcher(username, fulltime, redisClient)
wg.Wait()
close(watcher)
printUserScreen(redisClient, username)
case "o":
wg.Add(1)
currentTime := time.Now().UTC()
fulltime := currentTime.Format("2006-01-02.03:04:05")
go runSotwGame(username, fulltime)
go runCleanup(username)
watcher := startWatcher(username, fulltime, redisClient)
wg.Wait()
close(watcher)
printUserScreen(redisClient, username)
case "r":
clearScreen()
recoverSave(redisClient, username)
case "q":
exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run()
clearScreen()
os.Exit(0)
default:
}
}
}