【发布时间】:2020-05-11 06:53:25
【问题描述】:
我想创建一个 Steam 机器人,它从 CS:GO 请求玩家数据(我为此使用 node-globaloffensive)。 因为我从来没有用 javascript 做过什么,所以我只是试着用 php 来解决。
所以我创建了一个到我的数据库的连接,创建了一个 forEach 行。有一个问题,脚本同时请求所有数据,而我只取回我的第一个数据。
所以我决定在 sleep() 中添加一些延迟。 有时这实际上是有效的(在它对 7 个请求没有问题之前,但现在它只生成 2 个并在 setInterval() 之后再次重新启动它),但即便如此有时也不会......这就是为什么我现在想改进我的代码,这样我的机器人几乎可以正常工作并且更干净了。
这是我当前的代码:
const SteamUser = require('steam-user');
const SteamTotp = require('steam-totp');
const GlobalOffensive = require('globaloffensive');
const config = require('./config');
const db = require('./database');
var SteamID = require('steamid');
var mysql = require('mysql');
let user = new SteamUser();
let csgo = new GlobalOffensive(user);
csgo.on('debug', console.log);
user.on('error', console.error);
var pool = mysql.createPool({
supportBigNumbers : true,
bigNumberStrings : true,
connectionLimit : 100,
connectTimeout : 20000,
acquireTimeout : 20000,
host : db.host,
user : db.user,
password : db.password,
database : db.dbname
});
const logInOptions = {
accountName: config.accountName,
password: config.password
}
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
user.logOn(logInOptions);
user.on('loggedOn', res => {
console.log("Logged into Steam as " + user.steamID.getSteam3RenderedID());
user.setPersona(SteamUser.EPersonaState.Online);
user.gamesPlayed(730);
});
csgo.on("connectedToGC", function() {
console.log("connectedToGC");
const checkData = setInterval(()=>{
//Delete friends from list if they are not in a official Steamgroup
pool.getConnection(function(err, connection) {
if (err) throw err;
connection.query("SELECT * FROM Users WHERE SteamGroup1 = '0' && SteamGroup2 = '0''", function (err, rows, fields) {
connection.release();
if (err) throw err;
rows.forEach( (row) => {
console.log(`${row.SteamID64}` + " got kicked.");
user.chatMessage(`${row.SteamID64}`, "You got kicked, because you're not in an official Steamgroup");
sleep(5000);
user.removeFriend(`${row.SteamID64}`);
});
sleep(500);
//Check connection to game coordinator
if ( csgo.haveGCSession ) {
//Check Database
pool.getConnection(function(err, connection) {
if (err) throw err;
connection.query("SELECT * FROM Users WHERE (MainSteamGroup = '1' || CommunitySteamGroup = '1' || vip = '1') && BotInFriendlist = '1'", function (err, rows, fields) {
connection.release();
if (err) throw err;
rows.forEach( (row) => {
sleep(1000);
var account_id = new SteamID(`${row.SteamID64}`);
console.log(account_id);
//Request Data from CS:GO
csgo.requestPlayersProfile(account_id, function(ranking) {
var rankid = ranking.ranking.rank_id;
var wins = ranking.ranking.wins;
var private = ranking.player_level;
console.log(rankid);
console.log(wins);
console.log(private);
sleep(1000);
pool.getConnection(function(err, connection) {
if (err) throw err;
connection.query("UPDATE Users SET CSGOMM = '" + rankid + "', CSGOWins = " + wins + ", CSGOPrivate = " + private + " WHERE SteamID64 = " + `${row.SteamID64}` + "");
connection.release();
});
});
});
});
});
};
});
});
}, 100000);
});
就是这个样子:
Logged into Steam as [U:1:xxxxxxxxxx]
Sending GC message ClientHello
Sending hello, setting timer for next attempt to 2000 ms
Sending GC message ClientHello
Sending hello, setting timer for next attempt to 4000 ms
Got handled GC message ClientWelcome
Unknown SO type 2 with 1 items
Unknown SO type 7 with 1 items
GC connection established
connectedToGC
SteamID { universe: 1, type: 1, instance: 1, accountid: 2621xxxxx }
Sending GC message ClientRequestPlayersProfile
SteamID { universe: 1, type: 1, instance: 1, accountid: 1591xxxxx }
Sending GC message ClientRequestPlayersProfile
SteamID { universe: 1, type: 1, instance: 1, accountid: 1204xxxxx }
Sending GC message ClientRequestPlayersProfile
SteamID { universe: 1, type: 1, instance: 1, accountid: 2886xxxxx }
Sending GC message ClientRequestPlayersProfile
SteamID { universe: 1, type: 1, instance: 1, accountid: 1143xxxxx }
Sending GC message ClientRequestPlayersProfile
SteamID { universe: 1, type: 1, instance: 1, accountid: 4421xxxxx }
Sending GC message ClientRequestPlayersProfile
SteamID { universe: 1, type: 1, instance: 1, accountid: 1825xxxxx }
Sending GC message ClientRequestPlayersProfile
Got handled GC message PlayersProfile
Legendary Eagle Master
Got handled GC message PlayersProfile
Legendary Eagle Master
//and here it's just start again
SteamID { universe: 1, type: 1, instance: 1, accountid: 2621xxxxx }
Sending GC message ClientRequestPlayersProfile
SteamID { universe: 1, type: 1, instance: 1, accountid: 1591xxxxx }
Sending GC message ClientRequestPlayersProfile
...
我怎样才能改进我的 forEach 所以也许它正在等待第一个请求结束,然后开始第二个等等......
我想给你所有你需要的信息:)
【问题讨论】:
-
使用 Promise 怎么样?
-
由于我从未使用过 javascript,所以我不知道它的作用和工作原理
-
检查您的代码,查询中有额外的
'。另外,连接在'连接内=不推荐。 -
@Anuga 是的,我需要它,因为排名是一个字符串,如果我想将它更新到我的数据库,我应该怎么做?
标签: javascript node.js