Exemple #1
0
func loadIni() {
	usr, _ := user.Current()
	configPath := filepath.Join(usr.HomeDir, ".goboom")
	iniFile := filepath.Join(configPath, "config.ini")
	dbFilePath = filepath.Join(configPath, "rankdb.csv")

	if _, err := os.Stat(configPath); err != nil {
		err := os.Mkdir(configPath, os.ModePerm)
		if err != nil {
			fmt.Println("failed to create config dir")
		}
	}

	config = Config{
		DmenuParams: "-b -i -nb black -nf orange -sb black -p \">\"",
		Ignore:      []string{"X", "su"},
	}
	if _, err := os.Stat(iniFile); err != nil {
		newCfg := ini.Empty()
		newCfg.NameMapper = ini.TitleUnderscore

		err := ini.ReflectFrom(newCfg, &config)
		if err != nil {
			panic(err)
		}

		newCfg.SaveTo(iniFile)
	} else {
		if err := ini.MapToWithMapper(&config, ini.TitleUnderscore, iniFile); err != nil {
			panic(err)
		}
	}
}
Exemple #2
0
/*
 * runCliApp reads configuration and run the server as a command line app

 * Configuration is read from various sources, merged if needed, and finally
 * passed to the Game instance.
 *
 * By order of precedence, this is how the configuration fields are merged:
 * - command line flags
 * - ini file values (if any)
 * - default values
 *
 * `-h` or `--help` flags is a special case, directly handled by the cli library,
 * it shows the app usage and exits
 */
func runCliApp() error {
	// get configuration, pre-filled with default values
	cfg := surviveler.NewConfig()

	// command line interface setup
	app := cli.NewApp()
	app.Name = "server"
	app.Usage = "Surviveler server"
	app.Flags = configFlags()
	app.Action = func(c *cli.Context) error {
		// read config file
		if inifile := c.String("inifile"); inifile != "" {
			if err := ini.MapToWithMapper(&cfg, ini.AllCapsUnderscore, inifile); err != nil {
				log.WithField("inifile", inifile).WithError(err).Error("Couldn't read config file")
				os.Exit(1)
			}
		}

		// override current configuration with the values of
		// the flags provided on the command line
		if c.IsSet("port") {
			cfg.Port = c.String("port")
		}
		if c.IsSet("send-tick-period") {
			cfg.SendTickPeriod = c.Int("send-tick-period")
		}
		if c.IsSet("logic-tick-period") {
			cfg.LogicTickPeriod = c.Int("logic-tick-period")
		}
		if c.IsSet("time-factor") {
			cfg.TimeFactor = c.Int("time-factor")
		}
		if c.IsSet("night-starting-time") {
			cfg.NightStartingTime = c.Int("night-starting-time")
		}
		if c.IsSet("night-ending-time") {
			cfg.NightEndingTime = c.Int("night-ending-time")
		}
		if c.IsSet("game-starting-time") {
			cfg.GameStartingTime = c.Int("game-starting-time")
		}
		if c.IsSet("telnet-port") {
			cfg.TelnetPort = c.String("telnet-port")
		}
		if c.IsSet("assets") {
			cfg.AssetsPath = c.String("assets")
		}
		if c.IsSet("log-level") {
			cfg.LogLevel = c.String("log-level")
		}

		// game setup
		inst := surviveler.NewGame(cfg)
		if inst != nil {
			// start the game
			log.Info("Starting game...")
			inst.Start()
		} else {
			log.Fatal("Game startup failed")
		}
		return nil
	}
	return app.Run(os.Args)
}