Reputation: 3
I'm having troubles updating my users, my goal is to continuously add points to a user every x amount of seconds. Every time it goes through the loop, it is only adding the 10 points to the last user even though I go through each user in the loop... Any help would be appreciated!
setInterval(function(){
var fs = require("fs");
var data = fs.readFileSync('updatedPoints.txt', 'utf-8');
fs.readFile("updatedPoints.txt", {encoding: "utf8"}, function(error, data){
var i = 0;
var currentUser = "";
//Reads until nothing left in the file
while(data[i] != null){
currentUser += data[i]
i++;
}
var splitUsers = currentUser.split(",");
for(var i = 0; i < splitUsers.length - 1; i++){
//Splits up the name right until the first bracket which holds the users current points
var name = String(splitUsers[i].match(/^[^\(]+/g));
//Gets the users points
var userPoints = Number(splitUsers[i].match(/\(([^)]+)\)/)[1]);
console.log(name);
console.log(userPoints);
var test = userPoints + 10;
var newValue = data.replace(name+"("+userPoints+")", name+"("+test+")");
fs.writeFileSync('updatedPoints.txt', newValue, 'utf-8');
console.log('readFileSync complete');
}
});
}, 3000);
Upvotes: 0
Views: 50
Reputation: 61
you should reset data value after data.replace,
like that
var newValue = data.replace ...
data = newValue
because string.replace can't reset string directly
Upvotes: 1