Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TOP
#1
Can you help me? I don't know what's wrong.

Code:
SCRIPT (SERVER)

local top_winrate_1_name = "";
local top_winrate_1 = 0;
local top_winrate_2_name = "";
local top_winrate_2 = 0;

function onInit()
{
ReadTOPRating();
}



function ReadTOPRating()
{

{
    local reg_pl = 0;
    local file = fileOpen("DB/RegisteredPlayers.txt","r");
    if (file)
    {
        local registered_players = fileRead(file,"d");
        reg_pl = registered_players.tointeger();
        fileClose(file);
    }
    reg_pl = reg_pl + 1;
    file = fileOpen("DB/Rating/Winrate.txt","r");
    for (local i = 1; i < reg_pl; ++i)
    {
        local name = fileRead(file);
        local rating = fileRead(file);
        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);
    }
}

Code:
FILE WINRATE

OsmithREV
7700
Bagoga
665

Code:
OUTPUT (if print top_winrate_1_name and top_winrate_2_name)

Bagoga
7700

Bagoga
655
Discord: I'm not a spy...#9943.
GG: 60474243.

[Image: OsmithREV.gif]
[Image: 76561198181768479.png]
Reply
#2
Well, what IS wrong? Why do you need help?
Reply
#3
(01.05.2016, 22:12)HammelGammel Wrote: Well, what IS wrong? Why do you need help?

Script read the file, remember it, and print. In file 2 names, but script write only second name twice.
Discord: I'm not a spy...#9943.
GG: 60474243.

[Image: OsmithREV.gif]
[Image: 76561198181768479.png]
Reply
#4
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: "").

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);
}
This way you can be sure that your whole file will be read and you just use one file.
Reply
#5
(02.05.2016, 02:29)HammelGammel Wrote: 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: "").

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);
}
This way you can be sure that your whole file will be read and you just use one file.

The problem is that part of the code with writing of TOP-1 is called only once, but the name of the TOP-2 is written instead of TOP-1, thus not overwriting the value top_winrate_1. How does it works? I don't understand.

P.S. I check your code. Nothing is changed.
Discord: I'm not a spy...#9943.
GG: 60474243.

[Image: OsmithREV.gif]
[Image: 76561198181768479.png]
Reply
#6
It prints out both names just fine for me. This is not an ideal approach though to be honest: If the second value in your file is higher than the first one, your top_winrate_2 will just stay 0 and "". I would probably read everything in a table and an array, use .sort() on the array and then use the sorted values as key for the table. This way you could be sure that it doesn't depend on your file whether or not you will get the correct values, and also you could read out more than just 2 values.
Reply


Forum Jump:


Users browsing this thread: