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.
102 lines
2.1 KiB
102 lines
2.1 KiB
package main |
|
|
|
import ( |
|
"fmt" |
|
"os" |
|
"os/exec" |
|
"sync" |
|
"time" |
|
|
|
"gopkg.in/gcfg.v1" |
|
"gopkg.in/redis.v5" |
|
) |
|
|
|
type Config struct { |
|
NethackLauncher struct { |
|
Loglevel string |
|
ServerDisplay string |
|
NethackVersion string |
|
HackDir string |
|
NhdatLocation string |
|
RecoverBinary string |
|
BootstrapDelay time.Duration |
|
} |
|
|
|
Redis struct { |
|
Host string |
|
Password string |
|
} |
|
} |
|
|
|
var ( |
|
config = Config{} |
|
wg sync.WaitGroup |
|
) |
|
|
|
func main() { |
|
// read conf file |
|
readConf() |
|
|
|
// init redis connection |
|
redisClient, redisErr := initRedisConnection() |
|
if redisErr != nil { |
|
time.Sleep(config.NethackLauncher.BootstrapDelay * time.Second) |
|
redisClient, redisErr = initRedisConnection() |
|
if redisErr != nil { |
|
panic(redisErr) |
|
} |
|
} else { |
|
} |
|
|
|
// create initial files needed by nethack |
|
createInitialFiles() |
|
|
|
// start janitor |
|
go janitor(redisClient) |
|
|
|
// start homescreen |
|
screenFunction := printWelcomeScreen(redisClient) |
|
fmt.Printf("screen %s recieved\n", screenFunction) |
|
} |
|
|
|
func readConf() { |
|
// init config file |
|
err := gcfg.ReadFileInto(&config, "config.gcfg") |
|
if err != nil { |
|
panic(fmt.Sprintf("Could not load config.gcfg, error: %s\n", err)) |
|
} |
|
} |
|
|
|
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(fmt.Sprintf("%s/record", config.NethackLauncher.HackDir)); os.IsNotExist(err) { |
|
fmt.Printf("record file not found in %s/record\n", config.NethackLauncher.HackDir) |
|
fmt.Printf("%s\n", err) |
|
os.Exit(1) |
|
} |
|
// make sure initial rcfile exists |
|
hackRCLoc := fmt.Sprintf("%s/.nethackrc", config.NethackLauncher.HackDir) |
|
if _, err := os.Stat(hackRCLoc); os.IsNotExist(err) { |
|
fmt.Printf("initial config file not found at: %s\n", hackRCLoc) |
|
fmt.Printf("%s\n", err) |
|
os.Exit(1) |
|
} |
|
} |
|
|
|
func clearScreen() { |
|
cmd := exec.Command("clear") |
|
cmd.Stdout = os.Stdout |
|
cmd.Run() |
|
}
|
|
|