How can I make this loop just once?
#1

Basically, when a GMX happens or a player leaves the server it keeps setting the stuff to 0, I think it's because it's looping more than once and I tried using break; to stop it, didn't work.

I know this because:
printf outputs.
pawn Код:
playersql 1
[03:51:49] bank money : 0
[03:51:49] playerskin 67 //it was this before the server gmx'd
[03:51:49] WEED 0
[03:51:53] playersql 1
[03:51:53] bank money : 0
[03:51:53] playerskin 0 //notice how it's 0 now.
[03:51:53] WEED 0
[03:52:05]

pawn Код:
stock SavePlayerData()
{
    for(new i=0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i) && LoggedIn[i] == 1)
        {
            PlayerSQLID[i] = MySQL_GetValue(PlayerSQLID[i], "id", "accounts");
            printf("playersql %d", PlayerSQLID[i]);
            MySQL_SetInteger(PlayerSQLID[i], "TotalTime", TotalTime[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "BankMoney", BankMoney[i], "accounts");
            printf("bank money : %d", BankMoney[i]);
            SaveWeapons();
            MySQL_SetInteger(PlayerSQLID[i], "Skin", GetPlayerSkin(i), "accounts");
            printf("playerskin %d", GetPlayerSkin(i));
            MySQL_SetInteger(PlayerSQLID[i], "Money", GetPlayerMoney(i), "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "Cocaine", Cocaine[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "Weed", Weed[i], "accounts");
            printf("WEED %d", Weed[i]);
            MySQL_SetInteger(PlayerSQLID[i], "SGrip", SGrip[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "MGrip", MGrip[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "LGrip", LGrip[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "SFrame", SFrame[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "MFrame", MFrame[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "LFrame", LFrame[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "SBarrel", SBarrel[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "MBarrel", MBarrel[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "LBarrel", LBarrel[i], "accounts");
            new playerip[16], query[128];
            GetPlayerIp(i, playerip, sizeof(playerip));
            format(query, sizeof(query), "UPDATE `accounts` SET `PlayerIP` = '%s' WHERE `id` = '%d' LIMIT 1", playerip,i);
            mysql_query(query);
            break;
            //LAST IP.
        }
    }
    return 1;
}
Reply
#2

Are you saving this when a player disconnects (well it doesn't seem that way).. or is it some kind of a timer(it's a stock so it isn't a timer)?

Now, about this: PlayerSQLID[i] = MySQL_GetValue(PlayerSQLID[i], "id", "accounts");
The player SQLID is changed on save... so that's where I would look. I never really worked with SQL in SAMP but, I wouldn't change the ID at the saving point. I would not use the ID for the accounts. Use the player's name. Moreover, here is another mistake.


format(query, sizeof(query), "UPDATE `accounts` SET `PlayerIP` = '%s' WHERE `id` = '%d' LIMIT 1", playerip,i);

I would change the i at the end with the PlayerSQLID[i]. And again, I suggest using the player's nickname as the focus here.

"UPDATE `accounts` SET `PlayerIP` = '%s' WHERE `nickname` = '%s" ...
You may know the rest. That is how I also would load up the user's stats. You are mixing up account ids and in-game ids.
Reply
#3

It's a function and I call it at GMX (Restart) and OnPlayerDisconnect!

I agree with what you've said, thank you very much.
Reply
#4

Quote:
Originally Posted by Dokins
Посмотреть сообщение
It's a function and I call it at GMX (Restart) and OnPlayerDisconnect!

I agree with what you've said, thank you very much.
Yes, I suggest that you save only the player that disconnects on his or her disconnect from the server. Also, make a timer that would repeat every 5-10 minutes to save everyone's stats. You don't need to save anything on the OnGameModeExit callback because I think that players disconnect before the game mode is exited.
Reply
#5

That still won't stop the loop?
Reply
#6

You want that stuff to be 0, though. Odds are when a player leaves the server, they're not going to come back with the same player ID, so the information for that ID can be used by a different player. As for restarting the server, everything is set to 0 unless particularly set to a different value upon creating the variable.

So.. I don't quite see the issue here. By the way, you should be using foreach! Also, you were declaring two strings inside the loop- you don't need to do that; it's using too much memory.

pawn Код:
stock SavePlayerData()
{
    new playerip[16], query[128];

    foreach(new i : Player)
    {
        if(LoggedIn[i] == 1)
        {
            PlayerSQLID[i] = MySQL_GetValue(PlayerSQLID[i], "id", "accounts");
            printf("playersql %d", PlayerSQLID[i]);
            MySQL_SetInteger(PlayerSQLID[i], "TotalTime", TotalTime[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "BankMoney", BankMoney[i], "accounts");
            printf("bank money : %d", BankMoney[i]);
            SaveWeapons();
            MySQL_SetInteger(PlayerSQLID[i], "Skin", GetPlayerSkin(i), "accounts");
            printf("playerskin %d", GetPlayerSkin(i));
            MySQL_SetInteger(PlayerSQLID[i], "Money", GetPlayerMoney(i), "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "Cocaine", Cocaine[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "Weed", Weed[i], "accounts");
            printf("WEED %d", Weed[i]);
            MySQL_SetInteger(PlayerSQLID[i], "SGrip", SGrip[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "MGrip", MGrip[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "LGrip", LGrip[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "SFrame", SFrame[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "MFrame", MFrame[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "LFrame", LFrame[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "SBarrel", SBarrel[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "MBarrel", MBarrel[i], "accounts");
            MySQL_SetInteger(PlayerSQLID[i], "LBarrel", LBarrel[i], "accounts");
           
            GetPlayerIp(i, playerip, sizeof(playerip));
            format(query, sizeof(query), "UPDATE `accounts` SET `PlayerIP` = '%s' WHERE `id` = '%d' LIMIT 1", playerip,i);
            mysql_query(query);
        }
    }
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)