Simple but confusing to me. (Timer)
#1

Basically. This timer runs every second, so it can update the time. Which it does correctly. However, I'm trying to make payday every hour paid to each player. Problem is that when it gets to the hour (on the hour) it runs the payday for the whole minute until it's say (12:01). I.e Running it for 60secs although I only want to execute the code once. Could someone tell me how I could solve this?


pawn Код:
public UpdateTimeAndWeather()
{
    foreach(Player, i)
    {
        if(IsPlayerConnected(i) && LoggedIn[i] == 1)
        {
            gettime(hour, minute,seconds);
            format(timestr,32,"%02d:%02d:%02d",hour + 2,minute,seconds);
            SetPlayerTime(i,hour+2,minute);
            seconds ++;

        if(seconds == 60)
        {
            minute ++;
        }
        if(minute == 00)
        {
            hour ++;
            foreach(Player, x)
            {
                doPayDay(x);
            }

        }
            TextDrawSetString(txtTimeDisp,timestr);
            SetWorldTime(hour);
           
        }
    }

}
Reply
#2

Can you show your doPayDay function, please?
Reply
#3

EDIT: Sorry, I never did thank you for assisting me with the one with Vehicles not working correctly (MYSQL) I did what you said and voila, it worked. Thank you.

pawn Код:
public doPayDay(playerid)
{
    new unstring[100],string[256], benefits, rank, onlinebonus, faction,subtotal,taxtotal, total;
    benefits = PlayerLevel[playerid] * RandomEx(200, 700);
    rank = FRank[playerid] * RandomEx(300, 1200);
    onlinebonus = PlayerLevel[playerid] * RandomEx(250, 700);
    faction = RandomEx(200, 800);
    //special = RandomEx(300, 1000);
    subtotal = benefits + rank + onlinebonus + faction;
    taxtotal = subtotal / 100 * 30;
    total =  subtotal-taxtotal;
    new param1[32], param2[32];
    if(Faction[playerid] == 0) {
        param1 = "Civilian (0)";
        param2 = "N/A";
    }
    else {
        strcpy(param1, FactionName[Faction[playerid]], sizeof(param1));
        switch(FRank[playerid]) { // strcpy(dest, source, length);
            case 1: strcpy(param2, FactionRank1[Faction[playerid]], sizeof(param2));
            case 2: strcpy(param2, FactionRank2[Faction[playerid]], sizeof(param2));
            case 3: strcpy(param2, FactionRank3[Faction[playerid]], sizeof(param2));
            case 4: strcpy(param2, FactionRank4[Faction[playerid]], sizeof(param2));
            case 5: strcpy(param2, FactionRank5[Faction[playerid]], sizeof(param2));
            case 6: strcpy(param2, FactionRank6[Faction[playerid]], sizeof(param2));
            case 7: strcpy(param2, FactionRank7[Faction[playerid]], sizeof(param2));
            }
        }
   
    SendClientMessage(playerid, COLOUR_LIGHTBLUE, "P_______A_______Y_______D_______A_______Y");
    if(Faction[playerid] == 0)
    {
        format(unstring, sizeof(unstring), "Benefits for Un-Employment: $%d | Faction Rank: (N/A, $0)", benefits);
        SendClientMessage(playerid, 0xFFFF90FF, unstring);
     }
     if(Faction[playerid] > 0)
     {
        format(string, sizeof(string), "Pay for %s: $%d | Faction Rank: %s (%d): $%d | ((Online Bonus)): $%d",FactionName[Faction[playerid]], faction, param2, FRank[playerid], rank, onlinebonus);
        SendClientMessage(playerid, 0xFFFF90FF, string);
     }
    format(string, sizeof(string), "Total without Tax Deduction: $%d",subtotal);
    SendClientMessage(playerid, 0xFFFF90FF, string);
    format(string, sizeof(string), "Total Tax Deduction at 30%%: $%d (Money Transferred to your bank account.)",total);
    SendClientMessage(playerid, 0xFFFF90FF, string);
     
    PlayerSQLID[playerid] = MySQL_GetValue(PlayerSQLID[playerid], "id", "accounts");
    BankMoney[playerid] = MySQL_GetValue(PlayerSQLID[playerid], "BankMoney", "accounts");
    new sum = BankMoney[playerid] += taxtotal;
    MySQL_SetInteger(PlayerSQLID[playerid], "BankMoney", sum, "accounts");
    return 1;
}
Reply
#4

I would use a global variable to keep track of the last hour that paydays were given out for.

I'm kind of confused by your code though. Why manually increase the seconds, minutes and hours instead of just waiting for that to update on the next cycle?
Reply
#5

Quote:
Originally Posted by WillWinter
Посмотреть сообщение
I would use a global variable to keep track of the last hour that paydays were given out for.

I'm kind of confused by your code though. Why manually increase the seconds, minutes and hours instead of just waiting for that to update on the next cycle?
What do you mean?
Reply
#6

No worries about the other time I helped you, Dokins.

Either way, your code confused the hell out of me. I wasn't sure why you were adding one to each variable (hour, minute, second). See if this helps at all:

pawn Код:
public UpdateTimeAndWeather()
{
    new
        hour,
        minute,
        second
    ;
   
    gettime(hour, minute, second);
    SetWorldTime(hour);
   
    format(timestr,32,"%02d:%02d:%02d",hour + 2,minute,seconds);
    TextDrawSetString(txtTimeDisp,timestr);
   
    foreach(Player, i)
    {
        if(IsPlayerConnected(i) && LoggedIn[i] == 1)
        {
            SetPlayerTime(i, hour+2, minute);
            if(minute == 0) doPayDay(i);
        }
    }
    return 1;
}
I also noticed in your other code that you were running a foreach loop inside of another foreach loop and doing things you didn't need to do more than once; yet you did it per player. [If that made sense at all.]
Reply
#7

Quote:
Originally Posted by RealCop228
Посмотреть сообщение
No worries about the other time I helped you, Dokins.

Either way, your code confused the hell out of me. I wasn't sure why you were adding one to each variable (hour, minute, second). See if this helps at all:

pawn Код:
public UpdateTimeAndWeather()
{
    new
        hour,
        minute,
        second
    ;
   
    gettime(hour, minute, second);
    SetWorldTime(hour);
   
    format(timestr,32,"%02d:%02d:%02d",hour + 2,minute,seconds);
    TextDrawSetString(txtTimeDisp,timestr);
   
    foreach(Player, i)
    {
        if(IsPlayerConnected(i) && LoggedIn[i] == 1)
        {
            SetPlayerTime(i, hour+2, minute);
            if(minute == 0) doPayDay(i);
        }
    }
    return 1;
}
I also noticed in your other code that you were running a foreach loop inside of another foreach loop and doing things you didn't need to do more than once; yet you did it per player. [If that made sense at all.]
Forgive me for the mistakes, I've learned to code what I know through observation mostly. Thank you. The reason I'm +1ing the seconds is because I want the textdraw to increase the seconds. I guess I'm not familiar with the function GetTime or? I'm a little confused could you explain why I don't need to increase the minutes or seconds?
Reply
#8

Guess I'm a little late on this, but here's my version:

pawn Код:
new gLastPayday = -1;

public UpdateTimeAndWeather()
{
    // this code doesn't belong in the foreach loop, you can just do it once
    gettime(hour, minute,seconds);
    hour += 2;          // increase the hour by 2 here instead of doing it twice below
    if (hour > 23)
        hour -= 24;     // hour can't be greater than 23, so if it is, fix it

    new bool:timeForPaycheck = (minute == 0);

    format(timestr,32,"%02d:%02d:%02d",hour,minute,seconds);

    TextDrawSetString(txtTimeDisp,timestr);
    SetWorldTime(hour);

    foreach(Player, i)
    {
        if(IsPlayerConnected(i) && LoggedIn[i] == 1)
        {
            SetPlayerTime(i,hour,minute);        
            if (timeForPaycheck)
                doPayDay(i);
        }
    }
}
Reply
#9

Quote:
Originally Posted by WillWinter
Посмотреть сообщение
Guess I'm a little late on this, but here's my version:

pawn Код:
new gLastPayday = -1;

public UpdateTimeAndWeather()
{
    // this code doesn't belong in the foreach loop, you can just do it once
    gettime(hour, minute,seconds);
    hour += 2;          // increase the hour by 2 here instead of doing it twice below
    if (hour > 23)
        hour -= 24;     // hour can't be greater than 23, so if it is, fix it

    new bool:timeForPaycheck = (minute == 0);

    format(timestr,32,"%02d:%02d:%02d",hour,minute,seconds);

    TextDrawSetString(txtTimeDisp,timestr);
    SetWorldTime(hour);

    foreach(Player, i)
    {
        if(IsPlayerConnected(i) && LoggedIn[i] == 1)
        {
            SetPlayerTime(i,hour,minute);        
            if (timeForPaycheck)
                doPayDay(i);
        }
    }
}
Thank you anyway, your version looks so complicated aha! Thanks again for the time taken to help me!
Reply
#10

WillWinter's version is just a little better than mine because it accounts for the hours and stuff.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)