Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. require('dotenv').config();
  2. const cluster = require('cluster');
  3. const config = require('../config.json');
  4. const winston = require('winston');
  5. const path = require('path');
  6. const fs = require('fs');
  7. const figlet = require('figlet')
  8.  
  9. const HistoricalDataClass = require('./classes/historicalData');
  10.  
  11. global.config = config;
  12. // Relevant data fields, used to enumerate
  13. global.dataFields = ['score', 'kills', 'deaths', 'assists', 'suicides', 'tk', 'shots', 'hits', 'headshots', 'rounds_tr', 'rounds_ct', 'knife', 'glock', 'hkp2000', `usp_silencer`, `p250`, `deagle`, `elite`, `fiveseven`, `tec9`, `cz75a`, `revolver`, `nova`, `xm1014`, `mag7`, `sawedoff`, `bizon`, `mac10`, `mp9`, `mp7`, `ump45`, `p90`, `galilar`, `ak47`, `scar20`, `famas`, `m4a1`, `m4a1_silencer`, `aug`, `ssg08`, `sg556`, `awp`, `g3sg1`, `m249`, `negev`, `hegrenade`, `flashbang`, `smokegrenade`, `inferno`, `decoy`, `taser`, `mp5sd`, `head`, `chest`, `stomach`, `left_arm`, `right_arm`, `left_leg`, `right_leg`, `c4_planted`, `c4_exploded`, `c4_defused`, `ct_win`, `tr_win`, `hostages_rescued`, `vip_killed`, `vip_escaped`, `vip_played`, `mvp`, `damage`, `match_win`, `match_draw`, `match_lose`]
  14.  
  15. /*
  16. _ ____ _____ _____ _____ _ _ _____
  17. | | / __ \ / ____|/ ____|_ _| \ | |/ ____|
  18. | | | | | | | __| | __ | | | \| | | __
  19. | | | | | | | |_ | | |_ | | | | . ` | | |_ |
  20. | |___| |__| | |__| | |__| |_| |_| |\ | |__| |
  21. |______\____/ \_____|\_____|_____|_| \_|\_____|
  22.  
  23. */
  24.  
  25. // ensure log directory exists
  26. var logDirectory = path.resolve(process.cwd(), "logs");
  27. fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory);
  28.  
  29. var options = {
  30. infofile: {
  31. level: config.logLevel,
  32. filename: path.resolve(logDirectory, "info.log"),
  33. handleExceptions: true,
  34. maxsize: 5242880, // 5MB
  35. maxFiles: 5,
  36. format: winston.format.combine(
  37. winston.format.timestamp(),
  38. winston.format.colorize(),
  39. winston.format.simple()
  40. ),
  41. },
  42. errorfile: {
  43. level: "error",
  44. filename: path.resolve(logDirectory, "error.log"),
  45. handleExceptions: true,
  46. maxsize: 5242880, // 5MB
  47. maxFiles: 5,
  48. format: winston.format.combine(
  49. winston.format.timestamp(),
  50. winston.format.colorize(),
  51. winston.format.simple()
  52. ),
  53. }
  54. };
  55.  
  56. const logger = winston.createLogger({
  57. transports: [
  58. new winston.transports.File(options.infofile),
  59. new winston.transports.File(options.errorfile)
  60. ]
  61. });
  62.  
  63. if (process.env.NODE_ENV !== 'production') {
  64. logger.add(new winston.transports.Console({
  65. level: config.logLevel,
  66. timestamp: true,
  67. format: winston.format.combine(
  68. winston.format.colorize(),
  69. winston.format.simple()
  70. ),
  71. }));
  72. }
  73.  
  74. global.logger = logger;
  75.  
  76. /*
  77. ______ ___ _____ ___ ______ ___ _____ _____
  78. | _ \/ _ \_ _/ _ \| ___ \/ _ \/ ___| ___|
  79. | | | / /_\ \| |/ /_\ \ |_/ / /_\ \ `--.| |__
  80. | | | | _ || || _ | ___ \ _ |`--. \ __|
  81. | |/ /| | | || || | | | |_/ / | | /\__/ / |___
  82. |___/ \_| |_/\_/\_| |_|____/\_| |_|____/\____/
  83. */
  84.  
  85.  
  86. const Sequelize = require('sequelize');
  87.  
  88. const sequelize = new Sequelize(process.env.MYSQL_DB, process.env.MYSQL_USER, process.env.MYSQL_PASSWORD, {
  89. host: process.env.MYSQL_HOST,
  90. dialect: 'mysql',
  91.  
  92. pool: {
  93. max: 5,
  94. min: 0,
  95. acquire: 30000,
  96. idle: 10000
  97. },
  98. logging: (msg) => {
  99. if (config.logSQL) {
  100. logger.debug(msg)
  101. }
  102. },
  103. });
  104.  
  105. // Test to make sure we can connect to the database. If not, it will exit the process.
  106. sequelize
  107. .authenticate()
  108. .then(() => {
  109. if (cluster.isMaster) {
  110. logger.info('Database connection has been established successfully.');
  111. }
  112. }).catch(e => {
  113. logger.error('Fatal error while connecting to database ' + e);
  114. process.exit(1);
  115. });
  116.  
  117.  
  118. // Initiate the player model
  119. const Player = require("./models/player")(sequelize, Sequelize);
  120. // Initialize the historicalData model
  121. const HistoricalData = require('./models/historicalData')(sequelize, Sequelize);
  122.  
  123. Player.sync();
  124.  
  125. // Bind models to object
  126. const Models = {
  127. Player,
  128. HistoricalData
  129. }
  130.  
  131. global.models = Models;
  132. global.sequelize = sequelize;
  133.  
  134.  
  135. if (cluster.isMaster) {
  136.  
  137. // Count the machine's CPUs
  138. const cpuCount = require('os').cpus().length;
  139. logger.info(`Detected ${cpuCount} available CPUs, scaling webserver to ${cpuCount} processes`)
  140.  
  141. // Create a worker for each CPU
  142. if (process.env.NODE_ENV !== 'test') {
  143. for (var i = 0; i < cpuCount; i += 1) {
  144. cluster.fork();
  145. }
  146. }
  147.  
  148.  
  149.  
  150. /*
  151. ______ _____ _____ _____ _________________
  152. | _ \_ _/ ___/ __ \ _ | ___ \ _ \
  153. | | | | | | \ `--.| / \/ | | | |_/ / | | |
  154. | | | | | | `--. \ | | | | | /| | | |
  155. | |/ / _| |_/\__/ / \__/\ \_/ / |\ \| |/ /
  156. |___/ \___/\____/ \____/\___/\_| \_|___/
  157. */
  158.  
  159. if (config.discordBot.enabled) {
  160. const DiscordBot = require('./classes/discordBot');
  161. global.discordBot = new DiscordBot();
  162. }
  163.  
  164. /*
  165. _ _ _ _ _ _ _____ _
  166. | | | (_) | | (_) | | | __ \ | |
  167. | |__| |_ ___| |_ ___ _ __ _ ___ __ _| | | | | | __ _| |_ __ _
  168. | __ | / __| __/ _ \| '__| |/ __/ _` | | | | | |/ _` | __/ _` |
  169. | | | | \__ \ || (_) | | | | (_| (_| | | | |__| | (_| | || (_| |
  170. |_| |_|_|___/\__\___/|_| |_|\___\__,_|_| |_____/ \__,_|\__\__,_|
  171. */
  172.  
  173. if (config.historicalData.enabled) {
  174. // Start the historicalData class
  175. HistoricalData.sync().then(() => {
  176. const historicalDataHook = new HistoricalDataClass();
  177. })
  178. }
  179.  
  180. // Code to run if we're in a worker process
  181. } else {
  182. logger.debug(`Started a child process with ID ${cluster.worker.id}`)
  183.  
  184. /*
  185. __ __ _
  186. \ \ / / | |
  187. \ \ /\ / /__| |__ ___ ___ _ ____ _____ _ __
  188. \ \/ \/ / _ \ '_ \/ __|/ _ \ '__\ \ / / _ \ '__|
  189. \ /\ / __/ |_) \__ \ __/ | \ V / __/ |
  190. \/ \/ \___|_.__/|___/\___|_| \_/ \___|_|
  191. */
  192.  
  193. if (config.webserver.enabled) {
  194. const Web = require('./classes/web');
  195. const webserver = new Web();
  196.  
  197. global.app = webserver.app;
  198. }
  199.  
  200. }
  201.  
  202. if (cluster.isMaster) {
  203. figlet('RankMe stats', function (err, data) {
  204. if (err) {
  205. console.log('Something went wrong...');
  206. console.log(err);
  207. return;
  208. }
  209. console.log(data)
  210. });
  211. }
  212.  
  213.  
  214. process.on('unhandledRejection', (reason, p) => {
  215. global.logger.info('Unhandled Rejection at: Promise', p, 'reason:', reason);
  216. process.exit(1);
  217. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement