From 8cf606a26847cd6889461b36b71d56dc8b6436b1 Mon Sep 17 00:00:00 2001 From: noscere Date: Wed, 26 Aug 2020 00:39:50 +0100 Subject: [PATCH] First commit for issue #21 - add basic framework into the library. See below for further details. This has been added into the library, but is unreferenced within the main code. The following changes have been made: + library/config/config.js: This is a new API for accessing configurations. Regardless of storage medium, having a wrapper for the configurations means that we can switch between a file-based system, or a database system, and only the functions need to be modified. The API currently consists of the following exposed functions: + **appPath()** - returns the application absolute path. + **absolutePath(relativePath)** - joins the given path with the application path to create an absolute path/file + **userConfigFile(gid)** - returns an absolute path for a given guild config file. No filesystem checks are made. + **serializeConfigObjToString(configString)** - wrapper only, no implementation. + **parseConfigStringToObj(configObj)** - wrapper only, no implementation. + **exportConfig(config)** - wrapper only, no implementation. *This may change in future, proposing writeConfig.* + **importConfig(config)** - wrapper only, no implementation. *This may change, proposing readConfig.* library/index.js: + Added the new config library to expose the API functions. config.json: + Added three new fields to store the path and filename prefix of the user-defined config files (should a file-based system be decided) - currently the global config is stored as a json within the filesystem, so development will assume that the user-defined config files will also be stored in the filesystem. package.json & package-lock.json: + Incremented the version number. New API module for accessing config files. (cherry picked from commit 22f3721c226e7a171886b79e4ed8fb659523fbb1) --- config.json | 5 +- library/config/config.js | 107 +++++++++++++++++++++++++++++++++++++++ library/index.js | 2 + package-lock.json | 2 +- package.json | 2 +- 5 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 library/config/config.js diff --git a/config.json b/config.json index 52017bb..05ed2ba 100644 --- a/config.json +++ b/config.json @@ -18,5 +18,8 @@ "botCreator": "515622530371944448", "alliancesHelpSent": 8190976, "missions": 14318999, - "tempDir": "./tmp/" + "tempDir": "./tmp/", + "userConfigurationPath": "./config/", + "userConfigurationFilePrefix": "config.", + "userConfigurationExtension": ".json" } diff --git a/library/config/config.js b/library/config/config.js new file mode 100644 index 0000000..bcf3b38 --- /dev/null +++ b/library/config/config.js @@ -0,0 +1,107 @@ +/* + * @Author: BanderDragon + * @Date: 2020-08-25 21:10:12 + * @Last Modified by: BanderDragon + * @Last Modified time: 2020-08-26 00:01:53 + */ + +/* + * library/config/config.js + * ======================== + * A library of functions related to the configuration of the bot. + * + */ + +const logger = require('winston'); +const config = require('../../config.json'); + +var fs = require('fs'); +var userConfigPath = config.userConfigurationPath; +const userConfigExt = config.userConfigurationExt; + +var path = require('path'); + +module.exports = { + + /** + * Returns the applications absolute path + */ + appPath: function() { + return path.dirname(require.main.filename); + }, + + /** + * returns the full path, prefixed with the application root directory + * @param {string} relativePath + */ + absolutePath: function(relativePath) { + // Check if a ~ has been specified (this is not required, but clearly signifies the path is relative) + var firstChar = relativePath.charAt(0); + if(firstChar == "~") { + // Remove the ~ + relativePath = relativePath.substring(1); + } + // Normalise the path, so that we can run on any OS + relativePath = path.normalize(relativePath); + + // Return the full path, prefixed with the application root directory + return path.join(this.appPath(), relativePath); + }, + + /** + * Returns the full path and filename of the user configuration file for the given guild. + * - The file is not created, so it may not yet exist. + * @param {string} gid + */ + userConfigFile: function(gid) { + var fullPath = ""; + if(gid) { + var filename = config.userConfigurationFilePrefix + gid + config.userConfigurationExtension; + var relativePathAndFile = path.join(config.userConfigurationPath, filename); + fullPath = this.absolutePath(relativePathAndFile); + } else { + throw "userConfigFile: A null guild Id was passed." + } + + return fullPath; + }, + + /* + * The following are wrappers for storing and loading the + * user-defined config files - the user-defined config files allow + * configuration to be customised on a per-guild basis. + * + * These functions wrap the config save/load functionality so that + * the storage medium can be changed at a later date without requiring + * massive code changes. If the API remains static, we can switch from + * a file storage system to a database with relative ease. + */ + serializeConfigObjToString: function(gid, configString, client) { + + }, + + parseConfigStringToObj: function(gid, configString, client) { + var returnObj; + try { + returnObj = JSON.parse(configString); + } catch (e) { + logger.error(`Unable to parse config string - exception ${e.name}`) + } + }, + + exportConfig: function (client) { + + }, + + importConfig: function (client) { + + }, + + exportGuildConfig: function (gid, client) { + + }, + + importGuildConfig: function (gid, client) { + + } +} \ No newline at end of file diff --git a/library/index.js b/library/index.js index 2403dd9..3547f27 100644 --- a/library/index.js +++ b/library/index.js @@ -15,12 +15,14 @@ const format = require('./format/format.js'); const admin = require('./admin/admin.js'); const league = require('./league/league.js'); const helper = require('./helper/helper.js'); +const config = require('./config/config.js'); module.exports = { Format: format, Admin: admin, League: league, Helper: helper, + Config: config, collateArgs: function(index, args) { var returnString = ""; diff --git a/package-lock.json b/package-lock.json index 34bf5d0..780667b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "mrdata", - "version": "1.3.0", + "version": "1.3.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 20747e8..9c36437 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mrdata", - "version": "1.3.0", + "version": "1.3.1", "description": "An ST:FC Bot to allow your alliance to input their Power Destroyed and Resources Raided scores, and compete against one another for the kudos!", "main": "bot.js", "author": "BanderDragon#5699",