diff --git a/.gitignore b/.gitignore index 1377554..111069d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.swp +savedata/ diff --git a/config.gcfg b/config.gcfg index a01802c..da4f875 100644 --- a/config.gcfg +++ b/config.gcfg @@ -2,6 +2,7 @@ loglevel = "debug" serverdisplay = "unixvoid.com underground nethack server" nethackversion = "3.6.0" + inprogressdir = "savedata/dgldir/inprogress-nh343" recordlocation = "record" reclistlocation = "reclist" bootstrapdelay = 1 diff --git a/nethack-launcher.go b/nethack-launcher.go index fff37aa..c2d3ef7 100644 --- a/nethack-launcher.go +++ b/nethack-launcher.go @@ -3,6 +3,7 @@ package main import ( "bufio" "fmt" + "io" "io/ioutil" "net/http" "os" @@ -21,6 +22,7 @@ type Config struct { Loglevel string ServerDisplay string NethackVersion string + InProgressDir string RecordLocation string ReclistLocation string BootstrapDelay time.Duration @@ -147,6 +149,7 @@ func printWelcomeScreen(redisClient *redis.Client) string { printRegisterScreen(redisClient) case "w": clearScreen() + printProgressScreen() return ("w") case "q": clearScreen() @@ -164,6 +167,7 @@ func printUserScreen(redisClient *redis.Client, username string) string { println("") println(" l) Logout") println(" w) Watch games in progress") + println(" e) Edit config") fmt.Printf(" p) Play NetHack %s\n", config.NethackLauncher.NethackVersion) println(" q) Quit") println("") @@ -319,6 +323,71 @@ func printRegisterScreen(redisClient *redis.Client) { } } +func printProgressScreen() { + // print header + fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay) + println("") + + // check directory for live players + isEmpty, err := checkDir(config.NethackLauncher.InProgressDir) + if err != nil { + panic(err) + } + if isEmpty { + println(" No live players currently (blank entry returns)") + } else { + println(" Choose a player to spectate (blank entry aborts)") + } + println("") + + // populate inProg map + // create map for storing in progress data + inProg := make(map[int]string) + inProgTimer := 1 + + progFiles, _ := ioutil.ReadDir(config.NethackLauncher.InProgressDir) + + for _, f := range progFiles { + // grab username and path into user[] + user := strings.Split(f.Name(), ":") + + // get resolution lines from file + res0 := "" + res1 := "" + filePath := fmt.Sprintf("%s/%s", config.NethackLauncher.InProgressDir, f.Name()) + fileIO, err := os.OpenFile(filePath, os.O_RDWR, 0600) + if err != nil { + panic(err) + } + defer fileIO.Close() + rawBytes, err := ioutil.ReadAll(fileIO) + if err != nil { + panic(err) + } + lines := strings.Split(string(rawBytes), "\n") + for i, line := range lines { + if i == 1 { + res1 = line + } + if i == 2 { + res0 = line + } + } + + // print user line + fmt.Printf(" %d) %s\t%sx%s\t%s\n", inProgTimer, user[0], res0, res1, user[1]) + + // add user line to map + inProg[inProgTimer] = f.Name() + inProgTimer++ + } + println("") + println(">>") + // print out map + + os.Exit(0) +} + func gethighscore(w http.ResponseWriter, r *http.Request) { // run script output, err := exec.Command(config.NethackLauncher.ReclistLocation, @@ -330,3 +399,17 @@ func gethighscore(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%s\n", output) } + +func checkDir(dirName string) (bool, error) { + f, err := os.Open(dirName) + if err != nil { + return false, err + } + defer f.Close() + + _, err = f.Readdirnames(1) + if err == io.EOF { + return true, nil + } + return false, err +}