02.05.2016, 02:29
There are a few things. The following code for me prints both names with their corresponding values. Pretty much all I did was moving the print-lines below the foor-loop. Having it in the for-loop makes it print out one name twice and the other just once (Because in the first iteration, one name is obviously: "").
However, there are better ways to read your values from your file. You don't have to save the number of registered players. I would rather do something like this:
This way you can be sure that your whole file will be read and you just use one file.
Code:
local top_winrate_1_name = "";
local top_winrate_1 = 0;
local top_winrate_2_name = "";
local top_winrate_2 = 0;
function ReadTOPRating()
{
local reg_pl = 0;
local file = fileOpen("RegisteredPlayers.txt","r");
if (file)
{
local registered_players = fileRead(file,"d");
reg_pl = registered_players.tointeger();
fileClose(file);
}
file = fileOpen("Winrate.txt","r");
for (local i = 0; i < reg_pl; ++i)
{
local name = fileRead(file);
local rating = fileRead(file).tointeger();
if (rating > top_winrate_1)
{
top_winrate_1_name = name;
top_winrate_1 = rating;
}
else if (rating > top_winrate_2)
{
top_winrate_2_name = name;
top_winrate_2 = rating;
}
}
print(top_winrate_1_name);
print(top_winrate_1);
print("------------");
print(top_winrate_2_name);
print(top_winrate_2);
}
However, there are better ways to read your values from your file. You don't have to save the number of registered players. I would rather do something like this:
Code:
local read = fileRead(file);
while(read)
{
DoSomethingWithTheValues();
// Right at the end of the loop. read will be null if no new line could be found, which then ends the loop
read = fileRead(file);
}