Browse Source

Restructure layout

Add configuration file
Add redis connection
master
Matthew Faltys 5 years ago
parent
commit
e28f7686cc
  1. 9
      config.gcfg
  2. 191
      nethack-launcher.go

9
config.gcfg

@ -0,0 +1,9 @@
[nethacklauncher]
loglevel = "debug"
recordlocation = "record"
reclistlocation = "reclist"
bootstrapdelay = 1
[redis]
host = "localhost:6379"
password = ""

191
nethack-launcher.go

@ -1,103 +1,116 @@
package main package main
import ( import (
"flag" "bufio"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"time"
"github.com/unixvoid/glogger" "github.com/unixvoid/glogger"
"gopkg.in/gcfg.v1"
"gopkg.in/redis.v5"
) )
type Config struct {
NethackLauncher struct {
Loglevel string
RecordLocation string
ReclistLocation string
BootstrapDelay time.Duration
}
Redis struct {
Host string
Password string
}
}
var ( var (
loglevel string config = Config{}
recordlocation string
reclistlocation string
port = 8080
) )
func main() { func main() {
// init runtime args // read conf file
flag.StringVar(&loglevel, "loglevel", "debug", loglevel) readConf()
flag.StringVar(&recordlocation, "recordlocation", "record", recordlocation)
flag.StringVar(&reclistlocation, "reclistlocation", "reclist", reclistlocation)
flag.Parse()
// init config file and logger // init config file and logger
initLogger(loglevel) initLogger()
// init redis connection
//// // make sure record file exists redisClient, redisErr := initRedisConnection()
//// if _, err := os.Stat(recordlocation); os.IsNotExist(err) { if redisErr != nil {
//// glogger.Info.Printf("record file not found in %s\n", recordlocation) glogger.Debug.Printf("redis connection cannot be made, trying again in %s second(s)\n", config.NethackLauncher.BootstrapDelay*time.Second)
//// fmt.Printf("%s\n", err) time.Sleep(config.NethackLauncher.BootstrapDelay * time.Second)
//// os.Exit(1) redisClient, redisErr = initRedisConnection()
//// } if redisErr != nil {
//// // make sure reclist bin exists glogger.Error.Println("redis connection cannot be made, exiting.")
//// if _, err := os.Stat(reclistlocation); os.IsNotExist(err) { panic(redisErr)
//// glogger.Info.Printf("reclist binary not found in %s\n", reclistlocation) }
//// fmt.Printf("%s\n", err) } else {
//// os.Exit(1) glogger.Debug.Println("connection to redis succeeded.")
//// } }
clearScreen()
printWelcomeScreen()
// disable input buffering screenFunction := printWelcomeScreen(redisClient)
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run() fmt.Printf("screen %s recieved\n", screenFunction)
// do not display entered characters on the screen }
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
var b []byte = make([]byte, 1) func readConf() {
for { // init config file
os.Stdin.Read(b) err := gcfg.ReadFileInto(&config, "config.gcfg")
//fmt.Println("I got the byte", b, "("+string(b)+")") if err != nil {
switch string(b) { panic(fmt.Sprintf("Could not load config.gcfg, error: %s\n", err))
case "q":
clearScreen()
os.Exit(0)
default:
//fmt.Printf("command '%s' not implimented\n", string(b))
//fmt.Printf(">> ")
}
} }
//scanner := bufio.NewScanner(os.Stdin)
//for scanner.Scan() {
// //fmt.Println(scanner.Text())
// switch scanner.Text() {
// case "q":
// os.Exit(0)
// default:
// fmt.Printf("command '%s' not implimented\n", scanner.Text())
// fmt.Printf(">> ")
// }
//}
//if err := scanner.Err(); err != nil {
// log.Println(err)
//}
} }
func initLogger(loglevel string) { func initLogger() {
// init logger // init logger
if loglevel == "debug" { if config.NethackLauncher.Loglevel == "debug" {
glogger.LogInit(os.Stdout, os.Stdout, os.Stdout, os.Stderr) glogger.LogInit(os.Stdout, os.Stdout, os.Stdout, os.Stderr)
} else if loglevel == "cluster" { } else if config.NethackLauncher.Loglevel == "cluster" {
glogger.LogInit(os.Stdout, os.Stdout, ioutil.Discard, os.Stderr) glogger.LogInit(os.Stdout, os.Stdout, ioutil.Discard, os.Stderr)
} else if loglevel == "info" { } else if config.NethackLauncher.Loglevel == "info" {
glogger.LogInit(os.Stdout, ioutil.Discard, ioutil.Discard, os.Stderr) glogger.LogInit(os.Stdout, ioutil.Discard, ioutil.Discard, os.Stderr)
} else { } else {
glogger.LogInit(ioutil.Discard, ioutil.Discard, ioutil.Discard, os.Stderr) glogger.LogInit(ioutil.Discard, ioutil.Discard, ioutil.Discard, os.Stderr)
} }
} }
func initRedisConnection() (*redis.Client, error) {
// init redis connection
redisClient := redis.NewClient(&redis.Options{
Addr: config.Redis.Host,
Password: config.Redis.Password,
DB: 0,
})
_, redisErr := redisClient.Ping().Result()
return redisClient, redisErr
}
func checkFiles() {
// make sure record file exists
if _, err := os.Stat(config.NethackLauncher.RecordLocation); os.IsNotExist(err) {
glogger.Info.Printf("record file not found in %s\n", config.NethackLauncher.RecordLocation)
fmt.Printf("%s\n", err)
os.Exit(1)
}
// make sure reclist bin exists
if _, err := os.Stat(config.NethackLauncher.ReclistLocation); os.IsNotExist(err) {
glogger.Info.Printf("reclist binary not found in %s\n", config.NethackLauncher.ReclistLocation)
fmt.Printf("%s\n", err)
os.Exit(1)
}
}
func clearScreen() { func clearScreen() {
cmd := exec.Command("clear") cmd := exec.Command("clear")
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Run() cmd.Run()
} }
func printWelcomeScreen() { func printWelcomeScreen(redisClient *redis.Client) string {
clearScreen()
println("unixvoid.com underground nethack server") println("unixvoid.com underground nethack server")
println("") println("")
println(" Not logged in.") println(" Not logged in.")
@ -108,13 +121,61 @@ func printWelcomeScreen() {
println(" q) Quit") println(" q) Quit")
println("") println("")
fmt.Printf(">> ") 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").Run()
clearScreen()
printLoginScreen(redisClient)
case "r":
clearScreen()
return ("r")
case "w":
clearScreen()
return ("w")
case "q":
clearScreen()
os.Exit(0)
default:
}
}
}
func printLoginScreen(redisClient *redis.Client) {
println("unixvoid.com underground nethack server")
println("")
println(" Please enter your username. (blank entry aborts)")
println("")
fmt.Printf(">> ")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
if scanner.Text() == "" {
printWelcomeScreen(redisClient)
}
fmt.Printf("you entered '%s'\n", scanner.Text())
// check redis for user
}
if err := scanner.Err(); err != nil {
fmt.Printf("%s\n", err)
}
} }
func gethighscore(w http.ResponseWriter, r *http.Request) { func gethighscore(w http.ResponseWriter, r *http.Request) {
// run script // run script
output, err := exec.Command(reclistlocation, output, err := exec.Command(config.NethackLauncher.ReclistLocation,
"-f", "-f",
recordlocation).CombinedOutput() config.NethackLauncher.RecordLocation).CombinedOutput()
if err != nil { if err != nil {
fmt.Printf("%s\n", err) fmt.Printf("%s\n", err)
} }

Loading…
Cancel
Save