The Word Game. Post #2
Алгоритм
//Создать таблицы table.js
//(поменять название таблицы)
//Затем добавить сами данные. Addpost.js и addcom.js
//(поменять название таблицы)
//Create_day.js - чтобы наполнить дневную табличку
//(поменять название таблицы)
//Sortpost.js - чтобы посчитать баллы и наполнить дневную
//(поменять название таблицы)
//Sortcom.js - чтобы посчитать баллы и наполнить дневную
//(поменять название таблицы)
//Sum.js - чтобы посчитать баллы и наполнить дневную
//(поменять название таблицы)
//save.js
// вывод общего списка list_day.js list_all.js
//(поменять название таблицы)
//Создать пост в стимите.
table.js
//(поменять название таблицы)
//Создать пост в стимите.
let date_know;
const mysql = require("mysql2");
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxxxxx"
});
let mytable_p = 'p1810';
let mytable_c = 'c1810';
let mytable = 'a1810';
let sql = `create table if not exists ${mytable_p}(
id int primary key auto_increment,
author varchar(255) not null,
created varchar(255) not null,
length int not null,
url varchar(255) not null,
comments int not null,
upvotes int not null,
points float not null)`;
let sql2 = `create table if not exists ${mytable_c}(
id int primary key auto_increment,
author varchar(255) not null,
count int not null,
points float not null
)`;
let sql3 = `create table if not exists ${mytable}(
id int primary key auto_increment,
author varchar(255) not null,
comments float not null,
posts float not null,
points float not null
)`;
console.log(sql);
connection.query(sql, function(err, results) {
if(err) console.log(err);
else console.log("Таблица создана");
});
connection.query(sql2, function(err, results) {
if(err) console.log(err);
else console.log("Таблица создана");
});
connection.query(sql3, function(err, results) {
if(err) console.log(err);
else console.log("Таблица создана");
});
connection.end();
Addpost.js
const fs = require("fs");
const steem = require('steem');
let body3R = '';
let accounts = fs.readFileSync('spisok.txt').toString().split(" ");
console.log(accounts);
let mytable = 'p1810';
accounts.forEach(function (element,i,array){
steem.api.getDiscussionsByAuthorBeforeDate(element,null, new Date().toISOString().split('.')[0],20 , function(err, result) {
// console.log(result);
var i, len = result.length;
for (i = 0; i < len; i++)
{
var raw = result[i];
console.log('Автор');
console.log(raw.author);
console.log('Заголовок');
console.log(raw.author);
console.log('Сообщество');
console.log(raw.category);
console.log('Длина поста');
console.log(raw.body_length);
console.log('Количество ожидаемого вознаграждения');
console.log(raw.pending_payout_value);
console.log('Количество комментариев');
console.log(raw.replies.length);
console.log('Количество upvote');
let upvote = raw.active_votes.length-1;
console.log(raw.active_votes.length-1);
console.log('Дата создания');
console.log(raw.created);
let input = raw.created;
var d = new Date(); // Today!
d.setDate(d.getDate() - 1); // Yesterday!
d = d.toJSON().split("T")[0];
let title = raw.title;
title = title.replace(/[^a-zа-яё0-9\s]/gi, ' ');
if ((input.indexOf(d) !== -1)&& (raw.category === 'hive-171319'))
{
body3R = `| `+raw.author + `| <a href="`+raw.url+`">` + title + `</a>| `+ upvote + `| ` + `????` + `| ` + raw.children+`| ` + raw.body_length+ ' | \n';
console.log(body3R);
const mysql = require("mysql2");
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxxxxx"
});
connection.on('error', function() {});
const sql = `INSERT INTO ${mytable}(author, created, length, url, comments, upvotes, points) VALUES('${raw.author}', '${raw.created}', '${raw.body_length}', '${raw.url}', '${raw.children}', '${upvote}','0')`;
connection.query(sql, function(err, results) {
if(err) console.log(err);
console.log(results);
});
connection.end();
console.log(raw.created);
fs.appendFileSync("rank.txt",body3R);
}
}
});
});
Addcom.js
const fs = require("fs");
const steem = require('steem');
let input, hive, fullbody, x1;
var author,author2;
var permlinkR;
var err;
let stat, stat2;
let mytable = 'c1810';
let array = fs.readFileSync('spisok.txt').toString().split("\n");
let test_a = array[0].split(' ');
console.log(test_a[2]);
len1 = test_a.length;
for (x1 = 0; x1 < len1; x1++) {
steem.api.getDiscussionsByComments({
"start_author": test_a[x1],
"limit": 100
}, function (err, result) {
// console.log(err, result);
if (err === null) {
stat = 0;
console.log('asdasdasdas');
var i, len = result.length;
for (i = 0; i < len; i++) {
var discussion = result[i];
var d = new Date(); // Today!
d.setDate(d.getDate() - 1); // Yesterday!
d = d.toJSON().split("T")[0];
permlinkR = discussion.permlink;
author = discussion.author;
author2 = author;
input = discussion.created;
hive = discussion.category;
if ((input.indexOf(d) !== -1) && (hive === 'hive-171319')) {
stat=stat+1;
stat2 = stat;
fullbody = fullbody + ' ' + discussion.body;
}}
const mysql = require("mysql2");
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxxxxx"
});
connection.on('error', function() {});
const sql = `INSERT INTO ${mytable}(author, count,points) VALUES('${author}', '${stat}','0')`;
connection.query(sql, function(err, results) {
if(err) console.log(err);
console.log(results);
});
connection.end();
console.log('68 строка');
console.log(stat,author);
} else {
console.log('внимание поломка поломка поломка поломка');
}}
);
}
Addcom.js
const fs = require("fs");
const steem = require('steem');
let input, hive, fullbody, x1;
var author,author2;
var permlinkR;
var err;
let stat, stat2;
let mytable = 'c1810';
let array = fs.readFileSync('spisok.txt').toString().split("\n");
let test_a = array[0].split(' ');
console.log(test_a[2]);
len1 = test_a.length;
for (x1 = 0; x1 < len1; x1++) {
steem.api.getDiscussionsByComments({
"start_author": test_a[x1],
"limit": 100
}, function (err, result) {
// console.log(err, result);
if (err === null) {
stat = 0;
console.log('asdasdasdas');
var i, len = result.length;
for (i = 0; i < len; i++) {
var discussion = result[i];
var d = new Date(); // Today!
d.setDate(d.getDate() - 1); // Yesterday!
d = d.toJSON().split("T")[0];
permlinkR = discussion.permlink;
author = discussion.author;
author2 = author;
input = discussion.created;
hive = discussion.category;
if ((input.indexOf(d) !== -1) && (hive === 'hive-171319')) {
stat=stat+1;
stat2 = stat;
fullbody = fullbody + ' ' + discussion.body;
}}
const mysql = require("mysql2");
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxxxxx"
});
connection.on('error', function() {});
const sql = `INSERT INTO ${mytable}(author, count,points) VALUES('${author}', '${stat}','0')`;
connection.query(sql, function(err, results) {
if(err) console.log(err);
console.log(results);
});
connection.end();
console.log('68 строка');
console.log(stat,author);
} else {
console.log('внимание поломка поломка поломка поломка');
}}
);
}
Create_day.js
const mysql = require("mysql2");
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxxxxx"
});
let mytable_p = 'p1810';
let mytable_c = 'c1810';
let mytable_day = 'a1810';
const sql = `SELECT * FROM ${mytable_c} ORDER BY points DESC` ;
connection.query(sql, function(err, results) {
if(err) console.log(err);
const users = results;
console.log(users);
for(let i=0; i < users.length; i++){
let formula = (users.length - i)/10;
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxxxxx"
});
let author = users[i].author;
const sql4 = `INSERT INTO ${mytable_day}(author, comments, posts, points) VALUES('${author}', '0','0','0')`;
connection.query(sql4,function(err, results) {
if(err) console.log(err); });
connection.end();
}
});
connection.end();
Sortpost.js
const mysql = require("mysql2");
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxxxxx"
});
let mytable = 'p1810';
let mytable_day = 'a1810';
const sql = `SELECT * FROM ${mytable} ORDER BY length DESC` ;
connection.query(sql, function(err, results) {
if(err) console.log(err);
const users = results;
console.log(users);
for(let i=0; i < users.length; i++){
let formula = (users.length - i)/10;
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxxxxx"
});
const sql = `UPDATE ${mytable} SET points=${formula} WHERE id=${users[i].id}`;
connection.query(sql,function(err, results) {
if(err) console.log(err);
//console.log(results);
});
const sql12 = `UPDATE ${mytable_day} SET posts=${formula} WHERE id=${users[i].id}`;
connection.query(sql12,function(err, results) {
if(err) console.log(err);
//console.log(results);
});
connection.end();
console.log('|',i+1,'|',users[i].author,'|','<a href="',users[i].url,'">',users[i].length,'</a>','|',formula,'|');
// console.log('|',i+1,'|',users[i].author,'|',users[i].upvotes);
// console.log('|',i+1,'|',users[i].author,'|',users[i].comments);
}
});
connection.end();
Sortcom.js
const mysql = require("mysql2");
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxxxxx"
});
let formula;
let mytable = 'c1810';
let mytable_day = 'a1810';
const sql = `SELECT * FROM ${mytable} ORDER BY count DESC` ;
connection.query(sql, function(err, results) {
if(err) console.log(err);
const users = results;
for(let i=0; i < users.length; i++){
if (users[i].count>0) {
formula = (users.length - i)/10;
// console.log(users[i].count);
} else { formula = 0;
// console.log('sdfsdfs');
}
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxxxxx"
});
const sql = `UPDATE ${mytable} SET points='${formula}' WHERE author='${users[i].author}' LIMIT 1000`;
//console.log(sql);
// const sql = `UPDATE ${mytable} SET points=${formula} WHERE id=${users[i].id}`;
connection.query(sql,function(err, results) {
if(err) console.log(err);
});
const sql12 = `UPDATE ${mytable_day} SET comments=${formula} WHERE id=${users[i].id}`;
connection.query(sql12,function(err, results) {
if(err) console.log(err);
//console.log(results);
});
//console.log (users[i].id);
connection.end();
console.log('|',i+1,'|',users[i].author,'|',users[i].count,'|',formula,'|');
}
});
connection.end();
Sum.js
const mysql = require("mysql2");
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxx"
});
let mytable = 'p1810';
let mytable_day = 'a1810';
const sql = `SELECT * FROM ${mytable_day}` ;
connection.query(sql, function(err, results) {
if(err) console.log(err);
const users = results;
console.log(users);
for(let i=0; i < users.length; i++){
let formula = (users.length - i)/10;
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxx"
});
let count_day = users[i].comments + users[i].posts;
const sql12 = `UPDATE ${mytable_day} SET points=comments+posts WHERE id=${users[i].id}`;
connection.query(sql12,function(err, results) {
if(err) console.log(err);
//console.log(results);
});
connection.end();
console.log('|',i+1,'|',users[i].author,'|','<a href="',users[i].url,'">',users[i].length,'</a>','|',formula,'|');
// console.log('|',i+1,'|',users[i].author,'|',users[i].upvotes);
// console.log('|',i+1,'|',users[i].author,'|',users[i].comments);
}
});
connection.end();
Save.js
const mysql = require("mysql2");
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxxxxx"
});
let mytable_day = 'a1810';
const sql = `SELECT * FROM ${mytable_day} ORDER BY points DESC` ;
connection.query(sql, function(err, results) {
if(err) console.log(err);
const users = results;
console.log(users);
for(let i=0; i < users.length; i++){
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxxxxx"
});
let author = users[i].author;
sql3 = `UPDATE authors SET points=points+'${users[i].points}' WHERE name='${author}' LIMIT 1000`;
console.log (sql3);
connection.query(sql3,function(err, results) {
if(err) console.log(err);
});
connection.end();
}
});
connection.end();
list_day.js
const mysql = require("mysql2");
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxxxxx"
});
let formula;
let mytable = 'c1810';
let mytable_day = 'a1810';
const sql = `SELECT * FROM ${mytable_day} ORDER BY points DESC` ;
connection.query(sql, function(err, results) {
if(err) console.log(err);
const users = results;
for(let i=0; i < users.length; i++){
if (users[i].count>0) {
formula = (users.length - i)/10;
// console.log(users[i].count);
} else { formula = 0;
// console.log('sdfsdfs');
}
console.log('|',i+1,'|',users[i].author,'|',users[i].comments,'|',users[i].posts ,'|',users[i].points,'|');
}
});
connection.end();
list_all.js
const mysql = require("mysql2");
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "thewordgame",
password: "xxxxxx"
});
let formula;
let mytable = 'c1810';
let mytable_day = 'a1810';
const sql = `SELECT * FROM authors ORDER BY points DESC` ;
connection.query(sql, function(err, results) {
if(err) console.log(err);
const users = results;
for(let i=0; i < users.length; i++){
if (users[i].count>0) {
formula = (users.length - i)/10;
// console.log(users[i].count);
} else { formula = 0;
// console.log('sdfsdfs');
}
console.log('|',i+1,'|',users[i].name,'|',users[i].points,'|');
}
});
connection.end();
Интересно. Но я думаю, что пользователям, не знакомым с программированием, кто только узнал о рейтинге, было бы интересно узнать принцип формирования баллов.
Стал писать в самих рейтингах, спасибо