Browse Source

Add rudimentary in progress screen

master
Matthew Faltys 5 years ago
parent
commit
dff9c54844
  1. 1
      .gitignore
  2. 1
      config.gcfg
  3. 83
      nethack-launcher.go

1
.gitignore vendored

@ -1 +1,2 @@
*.swp *.swp
savedata/

1
config.gcfg

@ -2,6 +2,7 @@
loglevel = "debug" loglevel = "debug"
serverdisplay = "unixvoid.com underground nethack server" serverdisplay = "unixvoid.com underground nethack server"
nethackversion = "3.6.0" nethackversion = "3.6.0"
inprogressdir = "savedata/dgldir/inprogress-nh343"
recordlocation = "record" recordlocation = "record"
reclistlocation = "reclist" reclistlocation = "reclist"
bootstrapdelay = 1 bootstrapdelay = 1

83
nethack-launcher.go

@ -3,6 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
@ -21,6 +22,7 @@ type Config struct {
Loglevel string Loglevel string
ServerDisplay string ServerDisplay string
NethackVersion string NethackVersion string
InProgressDir string
RecordLocation string RecordLocation string
ReclistLocation string ReclistLocation string
BootstrapDelay time.Duration BootstrapDelay time.Duration
@ -147,6 +149,7 @@ func printWelcomeScreen(redisClient *redis.Client) string {
printRegisterScreen(redisClient) printRegisterScreen(redisClient)
case "w": case "w":
clearScreen() clearScreen()
printProgressScreen()
return ("w") return ("w")
case "q": case "q":
clearScreen() clearScreen()
@ -164,6 +167,7 @@ func printUserScreen(redisClient *redis.Client, username string) string {
println("") println("")
println(" l) Logout") println(" l) Logout")
println(" w) Watch games in progress") println(" w) Watch games in progress")
println(" e) Edit config")
fmt.Printf(" p) Play NetHack %s\n", config.NethackLauncher.NethackVersion) fmt.Printf(" p) Play NetHack %s\n", config.NethackLauncher.NethackVersion)
println(" q) Quit") println(" q) Quit")
println("") 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) { func gethighscore(w http.ResponseWriter, r *http.Request) {
// run script // run script
output, err := exec.Command(config.NethackLauncher.ReclistLocation, 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) 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
}

Loading…
Cancel
Save