From 0728d2c653cd15d4d042a64080fd856ab2b0e3b2 Mon Sep 17 00:00:00 2001 From: pieterbrandsen Date: Thu, 8 Feb 2024 21:45:50 +0000 Subject: [PATCH] Redone way to get logs of server --- .eslintrc.cjs | 54 ++-- .vscode/settings.json | 6 - docker-compose.example.yml | 2 + package-lock.json | 425 +++++++++++++++++++++++++++++ package.json | 3 +- src/helper.js | 535 +++++++++++++++++++------------------ 6 files changed, 727 insertions(+), 298 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.eslintrc.cjs b/.eslintrc.cjs index ef9c9f6..9d05680 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -1,30 +1,30 @@ module.exports = { - "env": { - "browser": true, - "es2021": true - }, - "extends": [ - "airbnb-base", - "prettier" + "env": { + "browser": true, + "es2021": true + }, + "extends": [ + "airbnb-base", + "prettier" + ], + "plugins": ["prettier"], + "overrides": [ + ], + "parserOptions": { + "ecmaVersion": "latest", + "sourceType": "module" + }, + "rules": { + "linebreak-style": ["error", (process.platform === "win32" ? "windows" : "unix")], + "no-console": "off", + "import/extensions": "off", + "no-underscore-dangle": "off", + "no-param-reassign": ["error", { "props": false }], + "prettier/prettier": [ + "error", + { + "endOfLine": "auto" + }, ], - "plugins": ["prettier"], - "overrides": [ - ], - "parserOptions": { - "ecmaVersion": "latest", - "sourceType": "module" - }, - "rules": { - "linebreak-style": ["error", (process.platform === "win32" ? "windows" : "unix")], - "no-console":"off", - "import/extensions":"off", - "no-underscore-dangle":"off", - "no-param-reassign": ["error", { "props": false }], - "prettier/prettier": [ - "error", - { - "endOfLine": "auto" - }, - ], - } + } } diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index a8c804e..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "editor.codeActionsOnSave": { - "source.fixAll.eslint": "explicit" - }, - "eslint.validate": ["javascript"] -} diff --git a/docker-compose.example.yml b/docker-compose.example.yml index 73c6195..2bf0577 100644 --- a/docker-compose.example.yml +++ b/docker-compose.example.yml @@ -2,7 +2,9 @@ version: '3' services: screeps: image: screepers/screeps-launcher + container_name: screeps_performance_server volumes: + - /var/run/docker.sock:/var/run/docker.sock - ./config.yml:/screeps/config.yml - ./bots:/screeps/bots - screeps-data:/screeps diff --git a/package-lock.json b/package-lock.json index 6bd7f86..fc37852 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "@octokit/core": "^4.2.0", "discord.js": "^14.9.0", + "dockerode": "^4.0.2", "dotenv": "^16.0.3", "get-port": "^6.1.2", "json-beautify": "^1.1.1", @@ -35,6 +36,11 @@ "eslint-plugin-prettier": "^4.2.1" } }, + "node_modules/@balena/dockerignore": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@balena/dockerignore/-/dockerignore-1.0.2.tgz", + "integrity": "sha512-wMue2Sy4GAVTk6Ic4tJVcnfdau+gx2EnG7S+uAEe+TWJFqE4YoWN4/H8MSLj4eYJKxGg26lZwboEniNiNwZQ6Q==" + }, "node_modules/@colors/colors": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/@colors/colors/-/colors-1.5.0.tgz", @@ -546,6 +552,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/asn1": { + "version": "0.2.6", + "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz", + "integrity": "sha512-ix/FxPn0MDjeyJ7i/yoHGFt/EX6LyNbxSEhPPXODPL+KB0VPk86UYfL0lMdy+KCnv+fmvIzySwaK5COwqVbWTQ==", + "dependencies": { + "safer-buffer": "~2.1.0" + } + }, "node_modules/async": { "version": "3.2.4", "resolved": "https://registry.npmjs.org/async/-/async-3.2.4.tgz", @@ -564,11 +578,48 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, + "node_modules/base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/bcrypt-pbkdf": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", + "integrity": "sha512-qeFIXtP4MSoi6NLqO12WfqARWWuCKi2Rn/9hJLEmtB5yTNr9DqFWkJRCf2qShWzPeAMRnOgCrq0sg/KLv5ES9w==", + "dependencies": { + "tweetnacl": "^0.14.3" + } + }, "node_modules/before-after-hook": { "version": "2.2.3", "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.3.tgz", "integrity": "sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==" }, + "node_modules/bl": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", + "integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==", + "dependencies": { + "buffer": "^5.5.0", + "inherits": "^2.0.4", + "readable-stream": "^3.4.0" + } + }, "node_modules/brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -578,6 +629,29 @@ "concat-map": "0.0.1" } }, + "node_modules/buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, "node_modules/bufferutil": { "version": "4.0.6", "resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.6.tgz", @@ -591,6 +665,15 @@ "node": ">=6.14.2" } }, + "node_modules/buildcheck": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/buildcheck/-/buildcheck-0.0.6.tgz", + "integrity": "sha512-8f9ZJCUXyT1M35Jx7MkBgmBMo3oHTTBIPLiY9xyL0pl3T5RwcPEY8cUHr5LBNfu/fk6c2T4DJZuVM/8ZZT2D2A==", + "optional": true, + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/busboy": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", @@ -667,6 +750,11 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, + "node_modules/chownr": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz", + "integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==" + }, "node_modules/color": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/color/-/color-3.2.1.tgz", @@ -718,6 +806,20 @@ "integrity": "sha512-JsPKdmh8ZkmnHxDk55FZ1TqVLvEQTvoByJZRN9jzI0UjxK/QgAmsphz7PGtqgPieQZ/CQcHWXCR7ATDNhGe+YA==", "dev": true }, + "node_modules/cpu-features": { + "version": "0.0.9", + "resolved": "https://registry.npmjs.org/cpu-features/-/cpu-features-0.0.9.tgz", + "integrity": "sha512-AKjgn2rP2yJyfbepsmLfiYcmtNn/2eUvocUyM/09yB0YDiz39HteK/5/T4Onf0pmdYDMgkBoGvRLvEguzyL7wQ==", + "hasInstallScript": true, + "optional": true, + "dependencies": { + "buildcheck": "~0.0.6", + "nan": "^2.17.0" + }, + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/data-uri-to-buffer": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", @@ -810,6 +912,54 @@ } } }, + "node_modules/docker-modem": { + "version": "5.0.3", + "resolved": "https://registry.npmjs.org/docker-modem/-/docker-modem-5.0.3.tgz", + "integrity": "sha512-89zhop5YVhcPEt5FpUFGr3cDyceGhq/F9J+ZndQ4KfqNvfbJpPMfgeixFgUj5OjCYAboElqODxY5Z1EBsSa6sg==", + "dependencies": { + "debug": "^4.1.1", + "readable-stream": "^3.5.0", + "split-ca": "^1.0.1", + "ssh2": "^1.15.0" + }, + "engines": { + "node": ">= 8.0" + } + }, + "node_modules/docker-modem/node_modules/debug": { + "version": "4.3.4", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", + "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "dependencies": { + "ms": "2.1.2" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/docker-modem/node_modules/ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + }, + "node_modules/dockerode": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/dockerode/-/dockerode-4.0.2.tgz", + "integrity": "sha512-9wM1BVpVMFr2Pw3eJNXrYYt6DT9k0xMcsSCjtPvyQ+xa1iPg/Mo3T/gUcwI0B2cczqCeCYRPF8yFYDwtFXT0+w==", + "dependencies": { + "@balena/dockerignore": "^1.0.2", + "docker-modem": "^5.0.3", + "tar-fs": "~2.0.1" + }, + "engines": { + "node": ">= 8.0" + } + }, "node_modules/doctrine": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", @@ -835,6 +985,14 @@ "resolved": "https://registry.npmjs.org/enabled/-/enabled-2.0.0.tgz", "integrity": "sha512-AKrN98kuwOzMIdAizXGI86UFBoo26CL21UM763y1h/GMSJ4/OHU9k2YlsmBpyScFo/wbLzWQJBMCW4+IO3/+OQ==" }, + "node_modules/end-of-stream": { + "version": "1.4.4", + "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", + "integrity": "sha512-+uw1inIHVPQoaVuHzRyXd21icM+cnt4CzD5rW+NC1wjOUSTOs+Te7FOv7AhN7vS9x/oIyhLP5PR1H+phQAHu5Q==", + "dependencies": { + "once": "^1.4.0" + } + }, "node_modules/es-abstract": { "version": "1.20.1", "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.20.1.tgz", @@ -1760,6 +1918,11 @@ "node": ">=12.20.0" } }, + "node_modules/fs-constants": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", + "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==" + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -2363,12 +2526,23 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/mkdirp-classic": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz", + "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==" + }, "node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", "dev": true }, + "node_modules/nan": { + "version": "2.18.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.18.0.tgz", + "integrity": "sha512-W7tfG7vMOGtD30sHoZSSc/JVYiyDPEyQVso/Zz+/uQd0B0L46gtC+pHha5FFMRpil6fm/AoEcRWyOVi4+E/f8w==", + "optional": true + }, "node_modules/nanoid": { "version": "3.3.4", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.4.tgz", @@ -2715,6 +2889,15 @@ "node": ">=6.0.0" } }, + "node_modules/pump": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", + "integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==", + "dependencies": { + "end-of-stream": "^1.1.0", + "once": "^1.3.1" + } + }, "node_modules/punycode": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", @@ -2884,6 +3067,11 @@ "node": ">=10" } }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, "node_modules/screeps-api": { "version": "1.16.0", "resolved": "https://registry.npmjs.org/screeps-api/-/screeps-api-1.16.0.tgz", @@ -2989,11 +3177,33 @@ "is-arrayish": "^0.3.1" } }, + "node_modules/split-ca": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/split-ca/-/split-ca-1.0.1.tgz", + "integrity": "sha512-Q5thBSxp5t8WPTTJQS59LrGqOZqOsrhDGDVm8azCqIBjSBd7nd9o2PM+mDulQQkh8h//4U6hFZnc/mul8t5pWQ==" + }, "node_modules/sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", "integrity": "sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==" }, + "node_modules/ssh2": { + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/ssh2/-/ssh2-1.15.0.tgz", + "integrity": "sha512-C0PHgX4h6lBxYx7hcXwu3QWdh4tg6tZZsTfXcdvc5caW/EMxaB4H9dWsl7qk+F7LAW762hp8VbXOX7x4xUYvEw==", + "hasInstallScript": true, + "dependencies": { + "asn1": "^0.2.6", + "bcrypt-pbkdf": "^1.0.2" + }, + "engines": { + "node": ">=10.16.0" + }, + "optionalDependencies": { + "cpu-features": "~0.0.9", + "nan": "^2.18.0" + } + }, "node_modules/stack-trace": { "version": "0.0.10", "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", @@ -3112,6 +3322,32 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/tar-fs": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.0.1.tgz", + "integrity": "sha512-6tzWDMeroL87uF/+lin46k+Q+46rAJ0SyPGz7OW7wTgblI273hsBqk2C1j0/xNadNLKDTUL9BukSjB7cwgmlPA==", + "dependencies": { + "chownr": "^1.1.1", + "mkdirp-classic": "^0.5.2", + "pump": "^3.0.0", + "tar-stream": "^2.0.0" + } + }, + "node_modules/tar-stream": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-2.2.0.tgz", + "integrity": "sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==", + "dependencies": { + "bl": "^4.0.3", + "end-of-stream": "^1.4.1", + "fs-constants": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^3.1.1" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/text-hex": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/text-hex/-/text-hex-1.0.0.tgz", @@ -3183,6 +3419,11 @@ "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.0.tgz", "integrity": "sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==" }, + "node_modules/tweetnacl": { + "version": "0.14.5", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz", + "integrity": "sha512-KXXFFdAbFXY4geFIwoyNK+f5Z1b7swfXABfL7HXCmoIWMKU3dmS26672A4EeQtDzLKy7SXmfBu51JolvEKwtGA==" + }, "node_modules/type-fest": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.20.2.tgz", @@ -3372,6 +3613,11 @@ } }, "dependencies": { + "@balena/dockerignore": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@balena/dockerignore/-/dockerignore-1.0.2.tgz", + "integrity": "sha512-wMue2Sy4GAVTk6Ic4tJVcnfdau+gx2EnG7S+uAEe+TWJFqE4YoWN4/H8MSLj4eYJKxGg26lZwboEniNiNwZQ6Q==" + }, "@colors/colors": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/@colors/colors/-/colors-1.5.0.tgz", @@ -3764,6 +4010,14 @@ "es-shim-unscopables": "^1.0.0" } }, + "asn1": { + "version": "0.2.6", + "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz", + "integrity": "sha512-ix/FxPn0MDjeyJ7i/yoHGFt/EX6LyNbxSEhPPXODPL+KB0VPk86UYfL0lMdy+KCnv+fmvIzySwaK5COwqVbWTQ==", + "requires": { + "safer-buffer": "~2.1.0" + } + }, "async": { "version": "3.2.4", "resolved": "https://registry.npmjs.org/async/-/async-3.2.4.tgz", @@ -3782,11 +4036,34 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, + "base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==" + }, + "bcrypt-pbkdf": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", + "integrity": "sha512-qeFIXtP4MSoi6NLqO12WfqARWWuCKi2Rn/9hJLEmtB5yTNr9DqFWkJRCf2qShWzPeAMRnOgCrq0sg/KLv5ES9w==", + "requires": { + "tweetnacl": "^0.14.3" + } + }, "before-after-hook": { "version": "2.2.3", "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.3.tgz", "integrity": "sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==" }, + "bl": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", + "integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==", + "requires": { + "buffer": "^5.5.0", + "inherits": "^2.0.4", + "readable-stream": "^3.4.0" + } + }, "brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -3796,6 +4073,15 @@ "concat-map": "0.0.1" } }, + "buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "requires": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, "bufferutil": { "version": "4.0.6", "resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.6.tgz", @@ -3805,6 +4091,12 @@ "node-gyp-build": "^4.3.0" } }, + "buildcheck": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/buildcheck/-/buildcheck-0.0.6.tgz", + "integrity": "sha512-8f9ZJCUXyT1M35Jx7MkBgmBMo3oHTTBIPLiY9xyL0pl3T5RwcPEY8cUHr5LBNfu/fk6c2T4DJZuVM/8ZZT2D2A==", + "optional": true + }, "busboy": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", @@ -3863,6 +4155,11 @@ } } }, + "chownr": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz", + "integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==" + }, "color": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/color/-/color-3.2.1.tgz", @@ -3914,6 +4211,16 @@ "integrity": "sha512-JsPKdmh8ZkmnHxDk55FZ1TqVLvEQTvoByJZRN9jzI0UjxK/QgAmsphz7PGtqgPieQZ/CQcHWXCR7ATDNhGe+YA==", "dev": true }, + "cpu-features": { + "version": "0.0.9", + "resolved": "https://registry.npmjs.org/cpu-features/-/cpu-features-0.0.9.tgz", + "integrity": "sha512-AKjgn2rP2yJyfbepsmLfiYcmtNn/2eUvocUyM/09yB0YDiz39HteK/5/T4Onf0pmdYDMgkBoGvRLvEguzyL7wQ==", + "optional": true, + "requires": { + "buildcheck": "~0.0.6", + "nan": "^2.17.0" + } + }, "data-uri-to-buffer": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", @@ -3982,6 +4289,42 @@ } } }, + "docker-modem": { + "version": "5.0.3", + "resolved": "https://registry.npmjs.org/docker-modem/-/docker-modem-5.0.3.tgz", + "integrity": "sha512-89zhop5YVhcPEt5FpUFGr3cDyceGhq/F9J+ZndQ4KfqNvfbJpPMfgeixFgUj5OjCYAboElqODxY5Z1EBsSa6sg==", + "requires": { + "debug": "^4.1.1", + "readable-stream": "^3.5.0", + "split-ca": "^1.0.1", + "ssh2": "^1.15.0" + }, + "dependencies": { + "debug": { + "version": "4.3.4", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", + "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "requires": { + "ms": "2.1.2" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, + "dockerode": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/dockerode/-/dockerode-4.0.2.tgz", + "integrity": "sha512-9wM1BVpVMFr2Pw3eJNXrYYt6DT9k0xMcsSCjtPvyQ+xa1iPg/Mo3T/gUcwI0B2cczqCeCYRPF8yFYDwtFXT0+w==", + "requires": { + "@balena/dockerignore": "^1.0.2", + "docker-modem": "^5.0.3", + "tar-fs": "~2.0.1" + } + }, "doctrine": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", @@ -4001,6 +4344,14 @@ "resolved": "https://registry.npmjs.org/enabled/-/enabled-2.0.0.tgz", "integrity": "sha512-AKrN98kuwOzMIdAizXGI86UFBoo26CL21UM763y1h/GMSJ4/OHU9k2YlsmBpyScFo/wbLzWQJBMCW4+IO3/+OQ==" }, + "end-of-stream": { + "version": "1.4.4", + "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", + "integrity": "sha512-+uw1inIHVPQoaVuHzRyXd21icM+cnt4CzD5rW+NC1wjOUSTOs+Te7FOv7AhN7vS9x/oIyhLP5PR1H+phQAHu5Q==", + "requires": { + "once": "^1.4.0" + } + }, "es-abstract": { "version": "1.20.1", "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.20.1.tgz", @@ -4669,6 +5020,11 @@ "fetch-blob": "^3.1.2" } }, + "fs-constants": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", + "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==" + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -5085,12 +5441,23 @@ "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==" }, + "mkdirp-classic": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz", + "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==" + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", "dev": true }, + "nan": { + "version": "2.18.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.18.0.tgz", + "integrity": "sha512-W7tfG7vMOGtD30sHoZSSc/JVYiyDPEyQVso/Zz+/uQd0B0L46gtC+pHha5FFMRpil6fm/AoEcRWyOVi4+E/f8w==", + "optional": true + }, "nanoid": { "version": "3.3.4", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.4.tgz", @@ -5327,6 +5694,15 @@ "fast-diff": "^1.1.2" } }, + "pump": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", + "integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==", + "requires": { + "end-of-stream": "^1.1.0", + "once": "^1.3.1" + } + }, "punycode": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", @@ -5415,6 +5791,11 @@ "resolved": "https://registry.npmjs.org/safe-stable-stringify/-/safe-stable-stringify-2.4.3.tgz", "integrity": "sha512-e2bDA2WJT0wxseVd4lsDP4+3ONX6HpMXQa1ZhFQ7SU+GjvORCmShbCMltrtIDfkYhVHrOcPtj+KhmDBdPdZD1g==" }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, "screeps-api": { "version": "1.16.0", "resolved": "https://registry.npmjs.org/screeps-api/-/screeps-api-1.16.0.tgz", @@ -5495,11 +5876,27 @@ "is-arrayish": "^0.3.1" } }, + "split-ca": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/split-ca/-/split-ca-1.0.1.tgz", + "integrity": "sha512-Q5thBSxp5t8WPTTJQS59LrGqOZqOsrhDGDVm8azCqIBjSBd7nd9o2PM+mDulQQkh8h//4U6hFZnc/mul8t5pWQ==" + }, "sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", "integrity": "sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==" }, + "ssh2": { + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/ssh2/-/ssh2-1.15.0.tgz", + "integrity": "sha512-C0PHgX4h6lBxYx7hcXwu3QWdh4tg6tZZsTfXcdvc5caW/EMxaB4H9dWsl7qk+F7LAW762hp8VbXOX7x4xUYvEw==", + "requires": { + "asn1": "^0.2.6", + "bcrypt-pbkdf": "^1.0.2", + "cpu-features": "~0.0.9", + "nan": "^2.18.0" + } + }, "stack-trace": { "version": "0.0.10", "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", @@ -5581,6 +5978,29 @@ "integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==", "dev": true }, + "tar-fs": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.0.1.tgz", + "integrity": "sha512-6tzWDMeroL87uF/+lin46k+Q+46rAJ0SyPGz7OW7wTgblI273hsBqk2C1j0/xNadNLKDTUL9BukSjB7cwgmlPA==", + "requires": { + "chownr": "^1.1.1", + "mkdirp-classic": "^0.5.2", + "pump": "^3.0.0", + "tar-stream": "^2.0.0" + } + }, + "tar-stream": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-2.2.0.tgz", + "integrity": "sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==", + "requires": { + "bl": "^4.0.3", + "end-of-stream": "^1.4.1", + "fs-constants": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^3.1.1" + } + }, "text-hex": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/text-hex/-/text-hex-1.0.0.tgz", @@ -5644,6 +6064,11 @@ "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.0.tgz", "integrity": "sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==" }, + "tweetnacl": { + "version": "0.14.5", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz", + "integrity": "sha512-KXXFFdAbFXY4geFIwoyNK+f5Z1b7swfXABfL7HXCmoIWMKU3dmS26672A4EeQtDzLKy7SXmfBu51JolvEKwtGA==" + }, "type-fest": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.20.2.tgz", diff --git a/package.json b/package.json index 2bd5de8..10e942a 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "dependencies": { "@octokit/core": "^4.2.0", "discord.js": "^14.9.0", + "dockerode": "^4.0.2", "dotenv": "^16.0.3", "get-port": "^6.1.2", "json-beautify": "^1.1.1", @@ -20,7 +21,7 @@ }, "scripts": { "server": "node src/index.js --maxTickCount=25000 --maxBots=5 --serverPort=21025 --cliPort=21026 --deleteLogs", - "server-test": "node src/index.js --maxTickCount=50000 --maxBots=5 --serverPort=21025 --cliPort=21026 --force --deleteLogs", + "server-test": "node src/index.js --maxTickCount=50000 --maxBots=5 --serverPort=21025 --cliPort=21026 --force --deleteLogs --debug", "lint": "eslint src/**/*", "lint:fix": "eslint src/**/* --fix" }, diff --git a/src/helper.js b/src/helper.js index 0f9493a..cd6348a 100644 --- a/src/helper.js +++ b/src/helper.js @@ -7,313 +7,320 @@ import { exec, execSync } from "child_process"; import minimist from "minimist"; import winston from "winston"; +import Docker from "dockerode"; import { RemoveLogs } from "./setup.js"; let Config; const argv = minimist(process.argv.slice(2)); +var docker = new Docker({ socketPath: '/var/run/docker.sock' }); const logger = winston.createLogger({ - level: "debug", - format: winston.format.json(), - defaultMeta: { service: "user-service" }, - transports: [ - new winston.transports.File({ - filename: "logs/logListener.log", - level: "debug", - }), - ], + level: "debug", + format: winston.format.json(), + defaultMeta: { service: "user-service" }, + transports: [ + new winston.transports.File({ + filename: "logs/logListener.log", + level: "debug", + }), + ], }); const basicCommand = "docker-compose"; const filter = { - controller: (o) => { - if (o && o.type) { - return o.type === "controller"; - } - return false; - }, - creeps: (o) => { - if (o && o.type) { - return o.type === "creep"; - } - return false; - }, - structures: (o) => { - if (o && o.type) { - return o.type === "spawn" || o.type === "extension"; - } - return false; - }, + controller: (o) => { + if (o && o.type) { + return o.type === "controller"; + } + return false; + }, + creeps: (o) => { + if (o && o.type) { + return o.type === "creep"; + } + return false; + }, + structures: (o) => { + if (o && o.type) { + return o.type === "spawn" || o.type === "extension"; + } + return false; + }, }; const hostname = "127.0.0.1"; export default class Helper { - static async setConfig(config) { - Config = config; - } + static async setConfig(config) { + Config = config; + } - /** - * followLog method - * - * Connects to the api and reads and prints the console log, if messages - * are available - * - * @param {list} rooms - The rooms - * @param {function} statusUpdater - Function to handle status updates - * @return {undefined} - */ - static async followLog(rooms, statusUpdater) { - rooms.forEach(async (room) => { - const api = new ScreepsAPI({ - email: room, - password: "password", - protocol: "http", - hostname, - port: Config.serverPort, - path: "/", - }); + /** + * followLog method + * + * Connects to the api and reads and prints the console log, if messages + * are available + * + * @param {list} rooms - The rooms + * @param {function} statusUpdater - Function to handle status updates + * @return {undefined} + */ + static async followLog(rooms, statusUpdater) { + rooms.forEach(async (room) => { + const api = new ScreepsAPI({ + email: room, + password: "password", + protocol: "http", + hostname, + port: Config.serverPort, + path: "/", + }); - await api.auth(); + await api.auth(); - api.socket.connect(); - api.socket.on("connected", () => { }); - api.socket.on("auth", () => { }); - api.socket.subscribe(`room:${room}`, statusUpdater); - api.socket.subscribe("console", (event) => { - if (event.data.messages) { - event.data.messages.log - .filter((msg) => msg.includes("

")) - .forEach((msg) => { - logger.debug(msg); - }); - } + api.socket.connect(); + api.socket.on("connected", () => { }); + api.socket.on("auth", () => { }); + api.socket.subscribe(`room:${room}`, statusUpdater); + api.socket.subscribe("console", (event) => { + if (event.data.messages) { + event.data.messages.log + .filter((msg) => msg.includes("

")) + .forEach((msg) => { + logger.debug(msg); }); - }); - } - /** - * Spawn bot - * @param {string} botName - * @param {string} roomName - */ + } + }); + }); + } + /** + * Spawn bot + * @param {string} botName + * @param {string} roomName + */ - static async spawnBot(botName, roomName, roomsSeen, cliPort) { - console.log(`Spawn ${botName} in ${roomName}`); - await this.executeCliCommand( - `bots.spawn('${botName}', '${roomName}', {username: '${roomName}', auto:'true'})\r\n`, - cliPort - ); - await this.setPassword(roomName, roomsSeen, Config.playerRooms, cliPort); - } + static async spawnBot(botName, roomName, roomsSeen, cliPort) { + console.log(`Spawn ${botName} in ${roomName}`); + await this.executeCliCommand( + `bots.spawn('${botName}', '${roomName}', {username: '${roomName}', auto:'true'})\r\n`, + cliPort + ); + await this.setPassword(roomName, roomsSeen, Config.playerRooms, cliPort); + } - /** - * sets password for user - * - * @param {string} line - * @param {object} socket - * @param {list} rooms - * @param {object} roomsSeen - * @param {stringMap} playerRooms - * @return {boolean} - */ - static async setPassword(roomName, roomsSeen, playerRooms, cliPort) { - // eslint-disable-next-line no-param-reassign - roomsSeen[roomName] = true; - console.log(`Set password for ${roomName}`); - // Password is 'password' - /* eslint max-len: ["error", 1300] */ - await this.executeCliCommand( - `storage.db.users.update({username: '${roomName}'}, {$set: {password: 'd0347d74b308e046b399e151c3674297ddd1aba6d6e380c94ea8ec070393d17297a3407e9c17d3d4a308043e3fd219faecc9d0d4c548a6eab87549ec83fd0688197d14b84fa810935f694c14eadd6eac3b36e19405190b1e216b5c3b0b79f03815670ba8c0eb2e23d00f556b8fdfc35eaa6d3f8f734132196c70c921f29160b1f1a0ac1fe4c196c15aa7c2a5d8358ed89fff3ad4ddbe45f7fc5ecb1b4538940f31188a9a65af59b8481f6aa00fecebf4f8e7a91be877ec8610350a06bac16d666f255a73768a96cd1797c25c68aded637f96c7b0e9ad8e9f85997bced58c288f8df06f78b096750fadc128a345c01b76ab4f0feff6f5b89712ddfe6d9b7a713b05add43bd0c4b1c59b4a72d5b81a42570c0b1f7980a969913ba31baf88ef1213e46cb09577e249688e1d10be958e7c5dae4033a5cc174261b837b29134ea090df426ad9a3624fa2be2dbfd47c6a56d7cda99c30d74c05102b1ee05e09eba4cf3f785d40c94f22b24c4e47409f5ba123b98fa30d23498e07ee26d542487b3be480f7b51f23712aef06630d1ea1a057e44e0bb8fcc1709e457544051730140852e7b493b7d3cd23202405f3d81d605be47c792681ce2d548388feddad94f790d58fb887d89358c4c0b8a6d0148e01f7f2cfd613ac371d3e3bdc606189eafba726df2959c2ac6b4780068713cb79a687e65298a4aeee75a3ef47aab3a9b853407be', salt: '8592666ec92a801874b463ea4c0a0da519936246d54bc4c40391f9ac7c5a8000'}})`, - cliPort - ); + /** + * sets password for user + * + * @param {string} line + * @param {object} socket + * @param {list} rooms + * @param {object} roomsSeen + * @param {stringMap} playerRooms + * @return {boolean} + */ + static async setPassword(roomName, roomsSeen, playerRooms, cliPort) { + // eslint-disable-next-line no-param-reassign + roomsSeen[roomName] = true; + console.log(`Set password for ${roomName}`); + // Password is 'password' + /* eslint max-len: ["error", 1300] */ + await this.executeCliCommand( + `storage.db.users.update({username: '${roomName}'}, {$set: {password: 'd0347d74b308e046b399e151c3674297ddd1aba6d6e380c94ea8ec070393d17297a3407e9c17d3d4a308043e3fd219faecc9d0d4c548a6eab87549ec83fd0688197d14b84fa810935f694c14eadd6eac3b36e19405190b1e216b5c3b0b79f03815670ba8c0eb2e23d00f556b8fdfc35eaa6d3f8f734132196c70c921f29160b1f1a0ac1fe4c196c15aa7c2a5d8358ed89fff3ad4ddbe45f7fc5ecb1b4538940f31188a9a65af59b8481f6aa00fecebf4f8e7a91be877ec8610350a06bac16d666f255a73768a96cd1797c25c68aded637f96c7b0e9ad8e9f85997bced58c288f8df06f78b096750fadc128a345c01b76ab4f0feff6f5b89712ddfe6d9b7a713b05add43bd0c4b1c59b4a72d5b81a42570c0b1f7980a969913ba31baf88ef1213e46cb09577e249688e1d10be958e7c5dae4033a5cc174261b837b29134ea090df426ad9a3624fa2be2dbfd47c6a56d7cda99c30d74c05102b1ee05e09eba4cf3f785d40c94f22b24c4e47409f5ba123b98fa30d23498e07ee26d542487b3be480f7b51f23712aef06630d1ea1a057e44e0bb8fcc1709e457544051730140852e7b493b7d3cd23202405f3d81d605be47c792681ce2d548388feddad94f790d58fb887d89358c4c0b8a6d0148e01f7f2cfd613ac371d3e3bdc606189eafba726df2959c2ac6b4780068713cb79a687e65298a4aeee75a3ef47aab3a9b853407be', salt: '8592666ec92a801874b463ea4c0a0da519936246d54bc4c40391f9ac7c5a8000'}})`, + cliPort + ); - if (playerRooms[roomName]) { - console.log(`Set steam id for ${roomName}`); - await this.executeCliCommand( - `storage.db.users.update({username: '${roomName}'}, {$set: {steam: {id: '${playerRooms[roomName]}'}}})`, - cliPort - ); - } + if (playerRooms[roomName]) { + console.log(`Set steam id for ${roomName}`); + await this.executeCliCommand( + `storage.db.users.update({username: '${roomName}'}, {$set: {steam: {id: '${playerRooms[roomName]}'}}})`, + cliPort + ); } + } - /** - * sleep method - * - * Helper method to sleep for amount of seconds. - * @param {number} seconds Amount of seconds to sleep - * @return {object} - */ - static sleep(seconds) { - // eslint-disable-next-line no-promise-executor-return - return new Promise((resolve) => setTimeout(resolve, seconds * 1000)); - } + /** + * sleep method + * + * Helper method to sleep for amount of seconds. + * @param {number} seconds Amount of seconds to sleep + * @return {object} + */ + static sleep(seconds) { + // eslint-disable-next-line no-promise-executor-return + return new Promise((resolve) => setTimeout(resolve, seconds * 1000)); + } - /** - * startServer method - * - * Starts the private server - * @return {object} - */ - static async startServer() { - const stopCommand = `${basicCommand} down --volumes --remove-orphans --rmi all`; - const upCommand = `${basicCommand} up`; - const serverLogsCommand = `${basicCommand} logs -f screeps`; + static async waitForServerStart() { + return new Promise(async (resolve) => { + let hitCountMissing = 1; + // Subscribe to container logs + docker.getContainer("screeps_performance_server").logs({ + follow: true, + stdout: true, + stderr: true + }, function (err, stream) { + if (err) { + console.error('Error getting container logs: ', err); + return; + } - const maxTime = new Promise((resolve) => { - setTimeout(resolve, 30 * 60 * 1000, "Timeout"); + // Parse each log line and filter based on condition + stream.setEncoding('utf8'); + stream.on('data', function (chunk) { + const lines = chunk.split('\n'); + lines.forEach(function (line) { + if (argv.debug) console.log(line); + if (line.includes("[main] exec: screeps-engine-main")) { + hitCountMissing -= 1; + if (hitCountMissing === 0) { + resolve(true); + stream.destroy(); + } + } + }); }); + }); - const startServer = new Promise(async (resolve) => { - console.log("\r\nProcess: Starting server..."); - console.log("Stopping server..."); - execSync(stopCommand, { stdio: argv.debug ? "inherit" : "ignore" }); - RemoveLogs(); + console.log("end") + }); + } - console.log("Starting server, this will take a while..."); - try { - execSync("mkdir -p ./logs && chmod 777 ./logs"); - // eslint-disable-next-line no-empty - } catch { } - exec(upCommand); - await this.sleep(10); + /** + * startServer method + * + * Starts the private server + * @return {object} + */ + static async startServer() { + const stopCommand = `${basicCommand} down --volumes --remove-orphans --rmi all`; - let hitCountMissing = 1; - const child = exec(serverLogsCommand, { stdio: "pipe" }); - child.stdout.on("data", (data) => { - const lines = data ? data.split(/(\r?\n)/g) : []; - lines.forEach((line) => { - if (argv.debug) console.log(line); - if (line.includes("[main] exec: screeps-engine-main")) { - hitCountMissing -= 1; - if (hitCountMissing === 0) { - child.kill(); - resolve(true); - } - } - }); - }); - }); - return Promise.race([startServer, maxTime]) - .then((result) => { - if (result === "Timeout") { - console.log("Timeout starting server!"); - return false; - } - return true; - }) - .catch((result) => { - console.error("error", { data: result }); - return false; - }); - } + const maxTime = new Promise((resolve) => { + setTimeout(resolve, 30 * 60 * 1000, "Timeout"); + }); - static async restartServer() { - const restartCommand = `${basicCommand} restart screeps`; - const serverLogsCommand = `${basicCommand} logs -f screeps`; + const startServer = new Promise(async (resolve) => { + console.log("\r\nProcess: Starting server..."); + console.log("Stopping server..."); + execSync(stopCommand, { stdio: argv.debug ? "inherit" : "ignore" }); + RemoveLogs(); - const maxTime = new Promise((resolve) => { - setTimeout(resolve, 30 * 60 * 1000, "Timeout"); - }); + console.log("Starting server, this will take a while..."); + try { + execSync("mkdir -p ./logs && chmod 777 ./logs"); + // eslint-disable-next-line no-empty + } catch { } + const upCommand = `${basicCommand} up -d`; + execSync(upCommand); + await this.sleep(10); + await this.waitForServerStart(); + resolve(true); + }); + return Promise.race([startServer, maxTime]) + .then((result) => { + if (result === "Timeout") { + console.log("Timeout starting server!"); + return false; + } + return true; + }) + .catch((result) => { + console.error("error", { data: result }); + return false; + }); + } - const restartServer = new Promise(async (resolve) => { - console.log("\r\nProcess: Restart server..."); - console.log("Restarting server...\r\n"); - exec(restartCommand); - await this.sleep(10); + static async restartServer() { + const restartCommand = `${basicCommand} restart screeps`; - let hitCountMissing = 2; - const child = exec(serverLogsCommand, { stdio: "pipe" }); - child.stdout.on("data", (data) => { - const lines = data ? data.split(/(\r?\n)/g) : []; - lines.forEach((line) => { - if (argv.debug) console.log(line); - if (line.includes("[main] exec: screeps-engine-main")) { - hitCountMissing -= 1; - if (hitCountMissing === 0) { - child.kill(); - resolve(true); - } - } - }); - }); - }); - return Promise.race([restartServer, maxTime]) - .then((result) => { - if (result === "Timeout") { - console.log("Timeout starting server!"); - return false; - } - return true; - }) - .catch((result) => { - console.error("error", { data: result }); - return false; - }); - } + const maxTime = new Promise((resolve) => { + setTimeout(resolve, 30 * 60 * 1000, "Timeout"); + }); - static initControllerID(event, status, controllerRooms) { - if (status[event.id].controller === null) { - // eslint-disable-next-line prefer-destructuring - status[event.id].controller = _.filter( - event.data.objects, - filter.controller - )[0]; - // eslint-disable-next-line no-underscore-dangle - status[event.id].controller = status[event.id].controller._id; - controllerRooms[status[event.id].controller] = event.id; + const restartServer = new Promise(async (resolve) => { + console.log("\r\nProcess: Restart server..."); + console.log("Restarting server...\r\n"); + exec(restartCommand); + await this.sleep(10); + await this.waitForServerStart(); + resolve(true); + }); + return Promise.race([restartServer, maxTime]) + .then((result) => { + if (result === "Timeout") { + console.log("Timeout starting server!"); + return false; } - } + return true; + }) + .catch((result) => { + console.error("error", { data: result }); + return false; + }); + } - static updateCreeps(event, status) { - const creeps = _.filter(event.data.objects, filter.creeps); - if (_.size(creeps) > 0) { - status[event.id].creeps += _.size(creeps); - } + static initControllerID(event, status, controllerRooms) { + if (status[event.id].controller === null) { + // eslint-disable-next-line prefer-destructuring + status[event.id].controller = _.filter( + event.data.objects, + filter.controller + )[0]; + // eslint-disable-next-line no-underscore-dangle + status[event.id].controller = status[event.id].controller._id; + controllerRooms[status[event.id].controller] = event.id; } + } - static updateStructures(event, status) { - const structures = _.filter(event.data.objects, filter.structures); - if (_.size(structures) > 0) { - status[event.id].structures += _.size(structures); - } + static updateCreeps(event, status) { + const creeps = _.filter(event.data.objects, filter.creeps); + if (_.size(creeps) > 0) { + status[event.id].creeps += _.size(creeps); } + } - static updateController(event, status, controllerRooms) { - const controllers = _.pick( - event.data.objects, - Object.keys(controllerRooms) - ); - Object.keys(controllers).forEach((controllerId) => { - const controller = controllers[controllerId]; - const roomName = controllerRooms[controllerId]; - if (status[roomName] === undefined) { - return; - } - if (controller.progress >= 0) { - status[roomName].progress = controller.progress; - } - if (controller.level >= 0) { - status[roomName].level = controller.level; - } - }); + static updateStructures(event, status) { + const structures = _.filter(event.data.objects, filter.structures); + if (_.size(structures) > 0) { + status[event.id].structures += _.size(structures); } + } - static async executeCliCommand(command, cliPort) { - try { - const result = await fetch(`http://${hostname}:${cliPort}/cli`, { - method: "POST", - body: command, - headers: { "Content-Type": "text/plain" }, - }); - const text = await result.text(); - console.log(`> ${command}`); - if (argv.debug) console.log(text); - await this.sleep(1); - return text; - } catch (error) { - console.log(error); - return undefined; - } + static updateController(event, status, controllerRooms) { + const controllers = _.pick( + event.data.objects, + Object.keys(controllerRooms) + ); + Object.keys(controllers).forEach((controllerId) => { + const controller = controllers[controllerId]; + const roomName = controllerRooms[controllerId]; + if (status[roomName] === undefined) { + return; + } + if (controller.progress >= 0) { + status[roomName].progress = controller.progress; + } + if (controller.level >= 0) { + status[roomName].level = controller.level; + } + }); + } + + static async executeCliCommand(command, cliPort) { + try { + const result = await fetch(`http://${hostname}:${cliPort}/cli`, { + method: "POST", + body: command, + headers: { "Content-Type": "text/plain" }, + }); + const text = await result.text(); + console.log(`> ${command}`); + if (argv.debug) console.log(text); + await this.sleep(1); + return text; + } catch (error) { + console.log(error); + return undefined; } + } }