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.
124 lines
2.9 KiB
124 lines
2.9 KiB
5 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
|
||
|
"github.com/unixvoid/glogger"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
loglevel string
|
||
|
recordlocation string
|
||
|
reclistlocation string
|
||
|
port = 8080
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
// init runtime args
|
||
|
flag.StringVar(&loglevel, "loglevel", "debug", loglevel)
|
||
|
flag.StringVar(&recordlocation, "recordlocation", "record", recordlocation)
|
||
|
flag.StringVar(&reclistlocation, "reclistlocation", "reclist", reclistlocation)
|
||
|
flag.Parse()
|
||
|
|
||
|
// init config file and logger
|
||
|
initLogger(loglevel)
|
||
|
|
||
|
//// // make sure record file exists
|
||
|
//// if _, err := os.Stat(recordlocation); os.IsNotExist(err) {
|
||
|
//// glogger.Info.Printf("record file not found in %s\n", recordlocation)
|
||
|
//// fmt.Printf("%s\n", err)
|
||
|
//// os.Exit(1)
|
||
|
//// }
|
||
|
//// // make sure reclist bin exists
|
||
|
//// if _, err := os.Stat(reclistlocation); os.IsNotExist(err) {
|
||
|
//// glogger.Info.Printf("reclist binary not found in %s\n", reclistlocation)
|
||
|
//// fmt.Printf("%s\n", err)
|
||
|
//// os.Exit(1)
|
||
|
//// }
|
||
|
|
||
|
clearScreen()
|
||
|
printWelcomeScreen()
|
||
|
|
||
|
// 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)
|
||
|
//fmt.Println("I got the byte", b, "("+string(b)+")")
|
||
|
switch string(b) {
|
||
|
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) {
|
||
|
// init logger
|
||
|
if loglevel == "debug" {
|
||
|
glogger.LogInit(os.Stdout, os.Stdout, os.Stdout, os.Stderr)
|
||
|
} else if loglevel == "cluster" {
|
||
|
glogger.LogInit(os.Stdout, os.Stdout, ioutil.Discard, os.Stderr)
|
||
|
} else if loglevel == "info" {
|
||
|
glogger.LogInit(os.Stdout, ioutil.Discard, ioutil.Discard, os.Stderr)
|
||
|
} else {
|
||
|
glogger.LogInit(ioutil.Discard, ioutil.Discard, ioutil.Discard, os.Stderr)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func clearScreen() {
|
||
|
cmd := exec.Command("clear")
|
||
|
cmd.Stdout = os.Stdout
|
||
|
cmd.Run()
|
||
|
}
|
||
|
|
||
|
func printWelcomeScreen() {
|
||
|
println("unixvoid.com underground nethack server")
|
||
|
println("")
|
||
|
println(" Not logged in.")
|
||
|
println("")
|
||
|
println(" l) Login")
|
||
|
println(" r) Register new user")
|
||
|
println(" w) Watch games in progress")
|
||
|
println(" q) Quit")
|
||
|
println("")
|
||
|
fmt.Printf(">> ")
|
||
|
}
|
||
|
|
||
|
func gethighscore(w http.ResponseWriter, r *http.Request) {
|
||
|
// run script
|
||
|
output, err := exec.Command(reclistlocation,
|
||
|
"-f",
|
||
|
recordlocation).CombinedOutput()
|
||
|
if err != nil {
|
||
|
fmt.Printf("%s\n", err)
|
||
|
}
|
||
|
|
||
|
fmt.Fprintf(w, "%s\n", output)
|
||
|
}
|