SQLite Problem.
#6

I know it's solved, but you can save only the total time and then convert it, even if you want it on disconnect; on command. I just made it and I don't want to tell to myself that it was waste of time; it might be helpfull for some people, so here it is.
pawn Код:
#define FILTERSCRIPT

#include <a_samp>

enum PlayerData
{
    Hours,
    Minutes,
    Seconds,
    TotalTime,
    ConnectTime
};

new
    P_DATA[MAX_PLAYERS][PlayerData],
    DB:DB
;

public OnFilterScriptInit()
{
    DB = db_open("DB.db");
    db_free_result( db_query( DB, "CREATE TABLE IF NOT EXISTS `Users` (`Name`, `Total_Time`)"));
    return 1;
}

public OnFilterScriptExit()
{
    db_close(DB);
    return 1;
}

public OnPlayerConnect(playerid)
{
    P_DATA[playerid][Hours] = 0;
    P_DATA[playerid][Minutes] = 0;
    P_DATA[playerid][Seconds] = 0;
    P_DATA[playerid][TotalTime] = 0;
    P_DATA[playerid][ConnectTime] = gettime();

    new
        Query[256],
        DBResult:Result
    ;
    format(Query,sizeof(Query),"SELECT * FROM `Users` WHERE `Name` = '%s'",Name(playerid));
    Result = db_query(DB,Query);
    if(db_num_rows(Result))
    {
        new
            Field[ 30 ]
        ;
        db_get_field_assoc(Result,"Total_Time",Field,30);

        P_DATA[playerid][TotalTime] = strval(Field);
       
       
        GetOnlineTime( P_DATA[playerid][TotalTime], P_DATA[playerid][Hours], P_DATA[playerid][Minutes], P_DATA[playerid][Seconds] );
    }
    else
    {
        format(Query,sizeof(Query),"INSERT INTO `Users`(`Name`,`Total_Time`) VALUES ('%s','%d')",Name(playerid),P_DATA[playerid][TotalTime]);
        db_free_result(db_query(DB,Query));
    }
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    new
        Query2[256]
    ;
    new time = gettime() - P_DATA[playerid][ConnectTime];
    GetOnlineTime(time,P_DATA[playerid][Hours], P_DATA[playerid][Minutes], P_DATA[playerid][Seconds]);
    printf("Online for; Hours: %d - Minutes: %d - Seconds: %d", P_DATA[playerid][Hours], P_DATA[playerid][Minutes], P_DATA[playerid][Seconds]);
    P_DATA[playerid][TotalTime] = P_DATA[playerid][TotalTime] + time;
    printf("Total time = %d", P_DATA[playerid][TotalTime]);
    format(Query2,sizeof(Query2),"UPDATE `Users` SET Total_Time = '%d' WHERE `Name` = '%s'",P_DATA[playerid][TotalTime],Name(playerid));
    db_free_result(db_query(DB,Query2));
    return 1;
}

stock Name(i)
{
    new n[24];
    GetPlayerName(i,n,24);
    return n;
}

stock GetOnlineTime(var, &h, &m, &s)
{
    h = (var / (60 * 60)) % 24;
    m = (var / 60) % 60;
    s = var % 60;
}
Reply


Messages In This Thread
SQLite Problem. - by Hardwell - 29.11.2012, 21:57
Re : SQLite Problem. - by Hardwell - 30.11.2012, 10:13
Re: SQLite Problem. - by Ballu Miaa - 30.11.2012, 10:32
Re : SQLite Problem. - by Hardwell - 30.11.2012, 10:49
Re : SQLite Problem. - by Hardwell - 30.11.2012, 11:09
Re: SQLite Problem. - by Konstantinos - 30.11.2012, 11:27

Forum Jump:


Users browsing this thread: 2 Guest(s)