How to code a payday?
#1

Hello. I was wondering how do people code payday for roleplay servers. When i check out the scripts, payday codes are just filled with some variables. There are no numbers, nothing. Where does the server get that level 1 should recieve xxx dollars and level 50 alot more?
Reply
#2

pawn Код:
new payDAY[ MAX_PLAYERS ] = {-1, ...};

forward public Pay_Player( playerid );

#define PAY_EVERY_MINS 5 // pay every 5 minutes

public OnPlayerConnect( playerid )
{
      payDAY[ playerid ] = SetTimerEx( "Pay_Player", PAY_EVERY_MINS * 60, 1, "d", playerid );
      return 1;
}

public OnPlayerDisconnect( playerid, reason )
{
      KillTimer( payDAY[ playerid ] );
      return 1;
}


public Pay_Player( playerid )
{
      switch( GetPlayerLevel( playerid ) )
      {
             case 1: GivePlayerMoney( playerid, 500 ), SendClientMessage( playerid, -1, "Received paycheck." );
             case 2: ... ;
      }
      return 1;
}
Or just set a global timer and check if the player's connected, but this is better. This is a rather easy technique, there's a better get_time() technique, but this should work too!
Reply
#3

Example :

pawn Код:
#define PAYDAY_TIME 3600 // 3600 sec ( 1 hour )
#define PAYDAY_MONEY 1000

forward PayDay();
public PayDay()
{
    foreach(Player, i)
    {
        if(IsPlayerLogged(i))
        {
            playersData[i][pConnectSec]++;
            if(playersData[i][pConnectSec] == PAYDAY_TIME)
            {
                new money = playersData[i][pLevel] * PAYDAY_MONEY;
                GivePlayerMoney(i, money);
                playersData[i][pConnectSec] = 0;
                SendClientMessage(i, -1, "[SERVER] Payday");
            }
        }
    }
}

public OnGameModeInit()
{
    SetTimer("PayDay", 1000, true);
    return 1;
}
Reply
#4

@Rajat_Pawar - Nice try but I don't think that code would be very nice

@rickisme

pawn Код:
foreach(Player, i)
    {
        if(IsPlayerLogged(i))
        {
You serious ? lol

Here is how I would do it.

pawn Код:
#include <timerfix> // https://sampforum.blast.hk/showthread.php?tid=289675
#include <YSI\y_hooks>

static PayDayTime[MAX_PLAYERS];

// Called when it's a players payday
forward OnPlayerPayDay(playerid);

// Start payday update
hook OnGameModeInit()
{
    SetTimer("PayDayUpdate", 60000, true);
    return 1;
}

// Reset players payday time when they connect
hook OnPlayerConnect(playerid)
{
    PayDayTime[playerid] = gettime();
    return 1;
}

// Check for paydays once a minute is good
forward PayDayUpdate();
public PayDayUpdate()
{
    foreach(new i : Player)
    {
        // It's payday!
        if(gettime() - PayDayTime[i] >= 3600)
        {
            PayDayTime[i] = gettime();
            CallLocalFunction("OnPlayerPayDay", "i", i);
        }
    }
    return 1;
}

// Set the players payday time (Called from inside gamemode/other includes)
forward SetPayDayTime(playerid);
public SetPayDayTime(playerid) { PayDayTime[playerid] = gettime(); }

// Get the players payday time (Called from inside gamemode/other includes)
forward GetPayDayTime(playerid);
public GetPayDayTime(playerid) { return PayDayTime[playerid]; }

// Used when a player logs in we can restore their payday time
forward UpdatePayDayTime(playerid, seconds);
public UpdatePayDayTime(playerid, seconds) { PayDayTime[playerid] = gettime() - seconds; }
Reply
#5

Quote:
Originally Posted by Rajat_Pawar
Посмотреть сообщение
there's a better get_time() technique, but this should work too!
I know, but yeah, this is the easiest - straight forward technique! Upto the OP to choose
Cheers!
Reply
#6

@[uL]Pottus : what's wrong w my example ( count players connect time and get payday w they real connect time ) ?

Restart players time when they connect ? Are you kidding me =.=!
Reply
#7

@above:
pawn Код:
if(IsPlayerLogged(i))
is not needed when using foreach.
Reply
#8

Quote:
Originally Posted by BigGroter
Посмотреть сообщение
@above:
pawn Код:
if(IsPlayerLogged(i))
is not needed when using foreach.
IsPlayerLogged does not equal IsPlayerConnected, his IsPlayerLogged is a function he made that most likely checks if the player is logged in. If someone is logged out but still connected, you obviously do not want to perform paycheck actions on their account.
Reply
#9

Quote:
Originally Posted by BigGroter
Посмотреть сообщение
@above:
pawn Код:
if(IsPlayerLogged(i))
is not needed when using foreach.
are you sure about that ? IsPlayerLogged not IsPlayerConnected it's different
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)