# Configuration

## CLI

You can configure JsBattle by providing shell arguments on startup. To get more information run

```bash
./jsbattle.js --help
```

## Configuration file
Most of the configuration is done via a configuration file.

```bash
./jsbattle.js start --config jsbattle.config.json
```

When the config file is not provided, the default configuration is applied. If any value is missing in the config file, default settings are used. CLI arguments have a higher priority than settings from the config file.  

## Full configuration file
List of all settings of JsBattle:

```js
{
  // supported log levels: "error", "warn", "info", "debug", "trace"
  "loglevel": "info",

  // additional settings of the logger
  // (see https://moleculer.services/docs/0.14/logging.html for more details)
  "logger": {

      // built in loggers: Console, File
      "type": "Console",

      "options": {

          // Using colors on the output
          "colors": true,

          // Print module names with different colors (like docker-compose for containers)
          "moduleColors": false,

          // Line formatter. It can be "json", "short", "simple", "full", a `Function` or a template string like "{timestamp} {level} {nodeID}/{mod}: {msg}"
          "formatter": "full",

          // Auto-padding the module name in order to messages begin at the same column.
          "autoPadding": false
      }
  },

  // do not inject config data from environmental variables"
  "skipEnv": false,

  // nodes (workers) with the same cluster name will by connected automatically.
  // services within a cluster can communicate between each other while running on
  // different hosts
  "cluster": {

    // enable cluster mode
    "enabled": true

    // name of cluster.
    "name": "jsbattle-cluster",

    // transporter configuration (see Moleculer networking for mor info: https://moleculer.services/docs/0.14/networking.html)
    "transporter": {
      "type": "TCP",
      "options": {
        "udpDiscovery": true
      }
    }
  }

  // data persistence settings
  "data": {

    // name of db adapter. See DB Adapters section for more details
    "adapter": "nedb"

    // path to directory where the data is stored. When not provided, an in-memory DB is used
    "path": "./jsbattle-data"
  },

  // configuration of web server
  "web": {

    // path to static files. In most cases you do not need to change that
    "webroot": path.resolve(__dirname, "./public"),

    // host where web server will bind to
    "host": "127.0.0.1",

    // port where web server will bind to
    "port": "8080",

    // url where JsBattle is available. Required to generate URL links properly (e.g. oAth callback)
    "baseUrl": "http://localhost:8080",

      // Configures the Access-Control-Allow-Origin CORS header.
    "corsOrigin": ["http://localhost:8080"],

    // Google Analytics code (stats are disabled when not provided)
    "gaCode": ""
  },

  // authorisation settings
  "auth": {

    // list of users that will be granted administration permissions (admin role)
    "admins": [
      {
        "provider": "github",
        "username": "thejack6318"
      }
    ],

    // list of OAuth providers. For each providers there are two routes created:
    // - /auth/{provider_name} - redirects to provider login page
    // - /auth/{provider_name}/callback - retrieve oAuth token from provider
    "providers": [

      // define each provider as separate object
      {
        // name of provider. Supported values are: github, facebook, google
        // authStrategies["github"] = require("passport-github2");
        "name": "github",

        // client ID generated by OAuth provider
        "clientID": "XXXXXXXXXXXXXXXXXXXX",
        // client secret generated by OAuth provider
        "clientSecret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
      }
    ]
  },

  // configuration of the league
  "league": {

    // how often new battles are scheduled (ms). It does not mean the actual
    // frequency of league battles since it is also limited by settings of UbdPlayer.
    // If scheduleInterval is too aggressive, queue of UbdPlayer will be filed up
    // quickly and further schedules will be skipped until there is space in the queue
    "scheduleInterval": 30000,

    // length of league battles
    "timeLimit": 20000,

    // size of teams that fights in the league
    "teamSize": 3,

    // whether code submitted to the league should be obfuscated
    "obfuscate": true,

    // how long league battles should be kept (ms)
    "historyDuration": 3*24*60*60*1000,

    // remove unsucessful AIs from the league that meets two criteria:
    // 1. They completed `cutOffFightCount` of fights
    // 2. Their win ratio is below `cutOffWinRatio`
    "cutOffFightCount": 100,
    "cutOffWinRatio": 0.05

  },

  // Player for Ultimate Battle Descriptors. the service process battles on server side
  "ubdPlayer": {

    // maximum length of battle requests queue
    "queueLimit": 2,

    // interval of querying the queue for new battles to be played
    "queueQueryTime": 5000,

    // how fast battles should be played on the server side. Tweak that for CPU optimisation.
    "speed": 1,

    // timeout for battles in ms. Keep it above (league.timeLimit / ubdPlayer.speed)
    "timeout": 60000
  },

  // history of battles
  "battleStore": {

    // default duration of keeping the history
    "defaultExpireTime": 7*24*60*60*1000,

    // how often the cleanup of expired battles should be ran
    "cleanupInterval": 60*60*1000,
  }
}
```

## DB Adapters

### NeDb
```js
{
  // name of the adapter
  "adapter": "nedb",

  // path to directory where the data is stored. When not provided, an in-memory DB is used
  "path": "./jsbattle-data"
}
```

### MongoDB
```js
{
  // name of the adapter
  "adapter": "mongo",

  // DB connection string
  "uri": "mongodb://localhost/jsbattle",

  // Options passed to Mongo Client
  "options": {

    // example:
    "useUnifiedTopology": true
  }
}
```

## Environment variables

Part of the configuration could be provided as env vars. To disable this feature set `config.skipEnv` to `true`;

### OAuth providers
To configure OAuth provider set up following env vars

```bash
OAUTH_{PROVIDER_NAME}_CLIENT_ID=XXXXXXXXXXXXXXXXXXXX
OAUTH_{PROVIDER_NAME}_CLIENT_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
```

Where `{PROVIDER_NAME}` is one of supported providers (upper case)
