From cc0ae8cd5314efff132d5ece11733ff79e9b8e8e Mon Sep 17 00:00:00 2001 From: Andrey <66987756+Charkos0ff@users.noreply.github.com> Date: Thu, 23 Sep 2021 23:34:58 +0700 Subject: [PATCH] Hello, World! --- config.js | 12 ++ index.js | 28 +++++ libs/mysql.js | 24 ++++ libs/vimelibrary.js | 277 +++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 280 ++++++++++++++++++++++++++++++++++++++++++++ package.json | 16 +++ statsStorage.js | 77 ++++++++++++ 7 files changed, 714 insertions(+) create mode 100644 config.js create mode 100644 index.js create mode 100644 libs/mysql.js create mode 100644 libs/vimelibrary.js create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 statsStorage.js diff --git a/config.js b/config.js new file mode 100644 index 0000000..32d69ab --- /dev/null +++ b/config.js @@ -0,0 +1,12 @@ +module.exports = { + mysql: { + host: "", //IP-адрес MySQL / MariaDB + user: "", //Используемый пользователь + database: "", //Название базы данных + password: "" //Пароль + }, + vimeworld: { + dev_token: "" //Например: DuuaUb249dJxZuXSEqovuKfYLlJLLq0 + }, + debug: true +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..174516a --- /dev/null +++ b/index.js @@ -0,0 +1,28 @@ +const mysql = require('./libs/mysql') +const config = require('./config') +const vimeworld = require('./libs/vimelibrary') + +const core = require('./statsStorage') + +const Storage = new core.Top(new vimeworld.Total(config.vimeworld.dev_token), new core.Storage(mysql)) + +async function start(){ + let info = await mysql.execute("SELECT * FROM info") + + if(info[0].month - Date.now()/1000 >= 1){ + console.log(" До обнуления сбора информации: " + Math.floor(info[0].month - Date.now()/1000) + " секунд") + } + else { + console.log(" Новые сезон") + + await Storage.push("leaderboards_last_month") + await mysql.execute("UPDATE info SET month = ?", [Math.floor(Date.now()/1000) + (60 * 60 * 24 * 7 * 29)]) + } + + info = await mysql.execute("SELECT * FROM info") + setTimeout(async function () { + await Storage.push("leaderboards_last_month") + }, Math.floor(info[0].month - Date.now()/1000)); +} + +start() \ No newline at end of file diff --git a/libs/mysql.js b/libs/mysql.js new file mode 100644 index 0000000..eea2eed --- /dev/null +++ b/libs/mysql.js @@ -0,0 +1,24 @@ +const mysql = require('mysql2'); +const config = require('../config') + +const connection = mysql.createConnection({ + host: config.mysql.host, + user: config.mysql.user, + database: config.mysql.database, + password: config.mysql.password, +}); + +async function execute(query, params = []) { + return await new Promise((resolve, reject) => { + connection.query( + query, + params, + function(err, results, fields) { + if(err) return reject(err.stack); + resolve(results); + } + ); + }); +} + +module.exports = { execute } \ No newline at end of file diff --git a/libs/vimelibrary.js b/libs/vimelibrary.js new file mode 100644 index 0000000..37f88f6 --- /dev/null +++ b/libs/vimelibrary.js @@ -0,0 +1,277 @@ +const axios = require("axios") + +class Request { + async get(url, token) { + const req = await axios.get(url, { + params: { + token: token + } + }); + return req.data + } + + async post(url, data) { + const req = await axios.post(url, data, {dataType: 'json',}); + return req.data + } +} + + +class User { + constructor(token) { + this.token = token + this.requests = new Request() + } + + /** Получение информации об игроке */ + async get(data, by) { + if (!data || !by) return console.error("[VimeLibrary] Один из обязательных параметров отсутствует") + switch (by) { + case 'nick': + case 'nickname': + case 'username': + case 'name': + return this.requests.get(`https://api.vimeworld.ru/user/name/${data}`, this.token) + case 'id': + return this.requests.get(`https://api.vimeworld.ru/user/${data}`, this.token) + } + } + + /** Получение друзей игрока */ + async friends(data, by) { + if (!data || !by) return console.error("[VimeLibrary] Один из обязательных параметров отсутствует") + switch (by) { + case 'nick': + case 'nickname': + case 'username': + case 'name': + return this.requests.get(`https://api.vimeworld.ru/user/name/${data}/friends`, this.token) + case 'id': + return this.requests.get(`https://api.vimeworld.ru/user/${data}/friends`, this.token) + } + } + + /** Получение сессии игрока */ + async session(data, by) { + if (!data || !by) return console.error("[VimeLibrary] Один из обязательных параметров отсутствует") + switch (by) { + case 'nick': + case 'nickname': + case 'username': + case 'name': + return this.requests.get(`https://api.vimeworld.ru/user/name/${data}/session`, this.token) + case 'id': + return this.requests.get(`https://api.vimeworld.ru/user/${data}/session`, this.token) + } + } + + /** Очень массовое получение статуса (до 1000) */ + async sessionBig(data) { + if (!data) return console.error("[VimeLibrary] Необходимо указать массив поиска") + if (!data instanceof Array) return console.error("[VimeLibrary] Необходимо указать имеено МАССИВ поиска") + return this.requests.post(`https://api.vimeworld.ru/user/session`, data) + } + + /** Получение статистики игрока */ + async stats(data, by) { + if (!data || !by) return console.error("[VimeLibrary] Один из обязательных параметров отсутствует") + switch (by) { + case 'nick': + case 'nickname': + case 'username': + case 'name': + return this.requests.get(`https://api.vimeworld.ru/user/name/${data}/stats`, this.token) + case 'id': + return this.requests.get(`https://api.vimeworld.ru/user/${data}/stats`, this.token) + } + } + + /** Получение достижений игрока */ + async achievements(data, by) { + if (!data || !by) return console.error("[VimeLibrary] Один из обязательных параметров отсутствует") + switch (by) { + case 'nick': + case 'nickname': + case 'username': + case 'name': + return this.requests.get(`https://api.vimeworld.ru/user/name/${data}/achievements`, this.token) + case 'id': + return this.requests.get(`https://api.vimeworld.ru/user/${data}/achievements`, this.token) + } + } + + /** Получение топов игрока */ + async leaderboards(data, by) { + if (!data || !by) return console.error("[VimeLibrary] Один из обязательных параметров отсутствует") + switch (by) { + case 'nick': + case 'nickname': + case 'username': + case 'name': + return this.requests.get(`https://api.vimeworld.ru/user/name/${data}/leaderboards`, this.token) + case 'id': + return this.requests.get(`https://api.vimeworld.ru/user/${data}/leaderboards`, this.token) + } + } + + /** Получение последних матчей игрока */ + async matches(data, by) { + if (!data || !by) return console.error("[VimeLibrary] Один из обязательных параметров отсутствует") + switch (by) { + case 'nick': + case 'nickname': + case 'username': + case 'name': + return this.requests.get(`https://api.vimeworld.ru/user/name/${data}/matches`, this.token) + case 'id': + return this.requests.get(`https://api.vimeworld.ru/user/${data}/matches`, this.token) + } + } +} + +class Online { + constructor(token) { + this.token = token + this.requests = new Request() + } + + /** Получение общего онлайна на MiniGames */ + async get() { + return this.requests.get(`https://api.vimeworld.ru/online`, this.token) + } + + /** Получение модераторов онлайн */ + async staff() { + return this.requests.get(`https://api.vimeworld.ru/online/staff`, this.token) + } + + /** Получение стримов онлайн */ + async streams() { + return this.requests.get(`https://api.vimeworld.ru/online/streams`, this.token) + } +} + +class Misc { + constructor(token) { + this.token = token + this.requests = new Request() + } + + /** Возвращает информацию о токене */ + async getToken(data) { + if (!data) return console.error("[VimeLibrary] Необходимо указать токен для просмотра") + return this.requests.get(`https://api.vimeworld.ru/misc/token/${data}`) + } + + /** Список всех возможных достижений */ + async achievements() { + return this.requests.get(`https://api.vimeworld.ru/misc/achievements`, this.token) + } + + /** Список карт, сгруппированный по играм */ + async maps() { + return this.requests.get(`https://api.vimeworld.ru/misc/maps`, this.token) + } + + /** Список игр, по которым ведется статистика */ + async games() { + return this.requests.get(`https://api.vimeworld.ru/misc/games`, this.token) + } +} + +class Match { + constructor(token) { + this.token = token + this.requests = new Request() + } + + /** Полная информация о матче */ + async get(id) { + return this.requests.get(`https://api.vimeworld.ru/match/${id}`, this.token) + } + + /** Список последних матчей на сервере */ + async latest() { + return this.requests.get(`https://api.vimeworld.ru/match/latest`, this.token) + } + + /** Список матчей на сервере */ + async list() { + return this.requests.get(`https://api.vimeworld.ru/match/list`, this.token) + } +} + +class Guild { + constructor(token) { + this.token = token + this.requests = new Request() + } + + /** Получает информацию о гильдии */ + async get(data, by) { + if (!data || !by) return console.error("[VimeLibrary] Один из необходимых аргументов (data, by) отсутствует") + switch (by) { + case 'id': + return this.requests.get(`https://api.vimeworld.ru/guild/get?id=${data}`, this.token) + case 'name': + return this.requests.get(`https://api.vimeworld.ru/guild/get?name=${data}`, this.token) + case 'tag': + return this.requests.get(`https://api.vimeworld.ru/guild/get?tag=${data}`, this.token) + default: + return console.error("[VimeLibrary] Неверный тип поиска, доступно: id, name, tag") + } + } + + /** Ищет гильдии по названию или тегу */ + async search(data) { + if (!data) return console.error("[VimeLibrary] Необходимо указать ключевое слово/ключевые слова для поиска") + return this.requests.get(`https://api.vimeworld.ru/guild/search?query=${data}`, this.token) + } +} + +class Locale { + constructor(token) { + this.token = token + this.requests = new Request() + } + + /** Человекочитаемые названия игр, статистики, рангов */ + async get(parts) { + if (!parts) parts = `games,game_stats,ranks` + return this.requests.get(`https://api.vimeworld.ru/locale/ru?parts=${parts}`, this.token) + } +} + +class Leaderboard { + constructor(token) { + this.token = token + this.requests = new Request() + } + + /** Список таблиц рекордов */ + async list() { + return this.requests.get(`https://api.vimeworld.ru/leaderboard/list`, this.token) + } + + /** Возвращает таблицу рекордов */ + async get(type, sort, size, offset) { + if (!size) size = 100 + if (!offset) offset = 0 + + return this.requests.get(`https://api.vimeworld.ru/leaderboard/get/${type}/${sort}?size=${size}&offset=${offset}`, this.token) + } +} + +class Total { + constructor(token) { + this.User = new User(token) + this.Online = new Online(token) + this.Misc = new Misc(token) + this.Locale = new Locale(token) + this.Guild = new Guild(token) + this.Match = new Match(token) + this.Leaderboard = new Leaderboard(token) + } +} + +module.exports = {User, Online, Misc, Locale, Match, Guild, Leaderboard, Total} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..3c1b916 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,280 @@ +{ + "name": "StatsStorage", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "axios": "^0.21.4", + "mysql2": "^2.3.0" + } + }, + "node_modules/axios": { + "version": "0.21.4", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz", + "integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==", + "dependencies": { + "follow-redirects": "^1.14.0" + } + }, + "node_modules/denque": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz", + "integrity": "sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw==", + "engines": { + "node": ">=0.10" + } + }, + "node_modules/follow-redirects": { + "version": "1.14.4", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.4.tgz", + "integrity": "sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/generate-function": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz", + "integrity": "sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==", + "dependencies": { + "is-property": "^1.0.2" + } + }, + "node_modules/iconv-lite": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", + "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-property": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz", + "integrity": "sha1-V/4cTkhHTt1lsJkR8msc1Ald2oQ=" + }, + "node_modules/long": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", + "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==" + }, + "node_modules/lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "dependencies": { + "yallist": "^4.0.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/mysql2": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-2.3.0.tgz", + "integrity": "sha512-0t5Ivps5Tdy5YHk5NdKwQhe/4Qyn2pload+S+UooDBvsqngtzujG1BaTWBihQLfeKO3t3122/GtusBtmHEHqww==", + "dependencies": { + "denque": "^1.4.1", + "generate-function": "^2.3.1", + "iconv-lite": "^0.6.2", + "long": "^4.0.0", + "lru-cache": "^6.0.0", + "named-placeholders": "^1.1.2", + "seq-queue": "^0.0.5", + "sqlstring": "^2.3.2" + }, + "engines": { + "node": ">= 8.0" + } + }, + "node_modules/named-placeholders": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.2.tgz", + "integrity": "sha512-wiFWqxoLL3PGVReSZpjLVxyJ1bRqe+KKJVbr4hGs1KWfTZTQyezHFBbuKj9hsizHyGV2ne7EMjHdxEGAybD5SA==", + "dependencies": { + "lru-cache": "^4.1.3" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/named-placeholders/node_modules/lru-cache": { + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz", + "integrity": "sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==", + "dependencies": { + "pseudomap": "^1.0.2", + "yallist": "^2.1.2" + } + }, + "node_modules/named-placeholders/node_modules/yallist": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", + "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=" + }, + "node_modules/pseudomap": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", + "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=" + }, + "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/seq-queue": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/seq-queue/-/seq-queue-0.0.5.tgz", + "integrity": "sha1-1WgS4cAXpuTnw+Ojeh2m143TyT4=" + }, + "node_modules/sqlstring": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.2.tgz", + "integrity": "sha512-vF4ZbYdKS8OnoJAWBmMxCQDkiEBkGQYU7UZPtL8flbDRSNkhaXvRJ279ZtI6M+zDaQovVU4tuRgzK5fVhvFAhg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" + } + }, + "dependencies": { + "axios": { + "version": "0.21.4", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz", + "integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==", + "requires": { + "follow-redirects": "^1.14.0" + } + }, + "denque": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz", + "integrity": "sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw==" + }, + "follow-redirects": { + "version": "1.14.4", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.4.tgz", + "integrity": "sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g==" + }, + "generate-function": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz", + "integrity": "sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==", + "requires": { + "is-property": "^1.0.2" + } + }, + "iconv-lite": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", + "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + } + }, + "is-property": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz", + "integrity": "sha1-V/4cTkhHTt1lsJkR8msc1Ald2oQ=" + }, + "long": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", + "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==" + }, + "lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "requires": { + "yallist": "^4.0.0" + } + }, + "mysql2": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-2.3.0.tgz", + "integrity": "sha512-0t5Ivps5Tdy5YHk5NdKwQhe/4Qyn2pload+S+UooDBvsqngtzujG1BaTWBihQLfeKO3t3122/GtusBtmHEHqww==", + "requires": { + "denque": "^1.4.1", + "generate-function": "^2.3.1", + "iconv-lite": "^0.6.2", + "long": "^4.0.0", + "lru-cache": "^6.0.0", + "named-placeholders": "^1.1.2", + "seq-queue": "^0.0.5", + "sqlstring": "^2.3.2" + } + }, + "named-placeholders": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.2.tgz", + "integrity": "sha512-wiFWqxoLL3PGVReSZpjLVxyJ1bRqe+KKJVbr4hGs1KWfTZTQyezHFBbuKj9hsizHyGV2ne7EMjHdxEGAybD5SA==", + "requires": { + "lru-cache": "^4.1.3" + }, + "dependencies": { + "lru-cache": { + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz", + "integrity": "sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==", + "requires": { + "pseudomap": "^1.0.2", + "yallist": "^2.1.2" + } + }, + "yallist": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", + "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=" + } + } + }, + "pseudomap": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", + "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=" + }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "seq-queue": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/seq-queue/-/seq-queue-0.0.5.tgz", + "integrity": "sha1-1WgS4cAXpuTnw+Ojeh2m143TyT4=" + }, + "sqlstring": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.2.tgz", + "integrity": "sha512-vF4ZbYdKS8OnoJAWBmMxCQDkiEBkGQYU7UZPtL8flbDRSNkhaXvRJ279ZtI6M+zDaQovVU4tuRgzK5fVhvFAhg==" + }, + "yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..ef3c487 --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "StatsStorage", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "CharkosOff", + "license": "ISC", + "dependencies": { + "axios": "^0.21.4", + "mysql2": "^2.3.0" + } +} diff --git a/statsStorage.js b/statsStorage.js new file mode 100644 index 0000000..e1ab595 --- /dev/null +++ b/statsStorage.js @@ -0,0 +1,77 @@ +class Storage { + constructor(mysql) { + this.mysql = mysql + } + + async add(table, game, sort, list) { + try { + const strs = [] + for (const p of list) { + strs.push(`('${p.username}', ${p.place}, '${game}', '${sort}')`) + } + + await this.mysql.execute(`INSERT INTO ${table}(username, place, game, sort) VALUES ` + strs.join(',')) + return true + } catch (e) { + console.error(e) + + return false + } + } + + async clear(game, table) { + return (game) ? this.mysql.execute("DELETE FROM ? WHERE `game` = ?", [table, game]) : this.mysql.execute("TRUNCATE ?", [table]) + } +} + +class Top { + constructor(vimeworld, storage) { + this.vimeworld = vimeworld + this.storage = storage + } + + async push(table) { + await this.storage.clear(null, table) + const games = await this.getGames() + + for (const game of games) { + const t = await this.getGame(game.game, game.sort) + const p = [] + + + let i = 0 + for (const l of t.records) { + i++ + const username = (l.user) ? l.user.username : l.username + p.push({ + username: username, + place: i + }) + + } + + if (config.debug) console.log(`${game.game}_${game.sort} ` + await this.storage.add(table, game.game, game.sort, p)) + } + } + + async getGames() { + const list = await this.vimeworld.Leaderboard.list(); + const games = [] + + for (const g of list) { + if (g.type === "guild") continue + + for (const sort of g.sort) { + games.push({game: g.type, sort: sort}) + } + } + + return games + } + + async getGame(game, sort) { + return await this.vimeworld.Leaderboard.get(game, sort, 1000) + } +} + +module.exports = {Storage, Top} \ No newline at end of file