#pragma semicolon 1
#define PLUGIN_AUTHOR "jenz"
#define PLUGIN_VERSION "1.0"
#include <sourcemod>

Database g_hDatabase;
Database g_hDatabase_ips;

public Plugin myinfo =
{
    name = "UNLOZE_player_time",
    author = PLUGIN_AUTHOR,
    description = "checks playtime on servers",
    version = PLUGIN_VERSION,
    url = "www.unloze.com"
};

public void OnPluginStart()
{
    Database.Connect(SQL_OnDatabaseConnect_ips, "PlayerManager");
    Database.Connect(SQL_OnDatabaseConnect, "unloze_playtimestats");
}

public void SQL_OnDatabaseConnect_ips(Database db, const char[] error, any data)
{
    if(!db || strlen(error))
    {
            LogError("Database error: %s", error);
            return;
    }
    g_hDatabase_ips = db;
}

public void SQL_OnDatabaseConnect(Database db, const char[] error, any data)
{
    if(!db || strlen(error))
    {
            LogError("Database error: %s", error);
            return;
    }
    g_hDatabase = db;
    char sQuery[512];
    Format(sQuery, sizeof(sQuery), "select auth, name, time from unloze_playtimestats.playtimeze_scuffed_crap where time > 0 and loaded_time is null limit 10000");
    if (g_hDatabase_ips != null)
    {
        g_hDatabase.Query(sql_select_auths, sQuery, _, DBPrio_High);
    }
    else
    {
        PrintToChatAll("ip db not connected");
    }

}

public void sql_select_auths(Database db, DBResultSet results, const char[] error, any data)
{
    if(!db || strlen(error))
    {
        LogError("Query error: %s", error);
        return;
    }
    while (results.RowCount && results.FetchRow())
    {
        int auth;
        int time;
        char sName[MAX_NAME_LENGTH];
        auth = results.FetchInt(0);
        results.FetchString(1, sName, sizeof(sName));
        time = results.FetchInt(2);

        int size2 = 2 * strlen(sName) + 1;
        char[] sEscapedName = new char[size2 + 1];
        g_hDatabase_ips.Escape(sName, sEscapedName, size2 + 1);

        char auth_steam_id2[32];
        GetSteam2FromAccountId(auth_steam_id2, sizeof(auth_steam_id2), auth);

        char sQuery[512];
        Format(sQuery, sizeof(sQuery), "select address from `unloze_anti-spoofing`.connections c where c.auth = '%s'", auth_steam_id2);

        DataPack hDataPack = new DataPack();
        hDataPack.WriteString(sEscapedName);
        hDataPack.WriteCell(auth);
        hDataPack.WriteCell(time);

        g_hDatabase_ips.Query(sql_get_ip, sQuery, hDataPack, DBPrio_High);
    }
    delete results;
}

public void sql_get_ip(Database db, DBResultSet results, const char[] error, DataPack data)
{
    if(!db || strlen(error))
    {
        LogError("Query error: %s", error);
        return;
    }
    char sName[MAX_NAME_LENGTH];
    ResetPack(data);
    data.ReadString(sName, sizeof(sName));
    int auth = data.ReadCell();
    int time = data.ReadCell();
    CloseHandle(data);
    char sQuery[512];
    char auth_steam_id2[32];
    GetSteam2FromAccountId(auth_steam_id2, sizeof(auth_steam_id2), auth);

    while (results.RowCount && results.FetchRow())
    {
        char address[32];
        results.FetchString(0, address, sizeof(address));
        
        DataPack hDataPack = new DataPack();
        hDataPack.WriteCell(auth);
        Format(sQuery, sizeof(sQuery), "INSERT INTO `player_time` (`steam_id`, `ipv4`, `player_name`, `ze_time`, `mg_time`, `zr_time`, `jb_time`) VALUES ('%s', '%s', '%s', '%i', 0, 0, 0) ON DUPLICATE KEY UPDATE `ze_time` = `ze_time` + '%i'", auth_steam_id2, address, sName, time, time);
        g_hDatabase.Query(sql_update_dummy, sQuery, hDataPack, DBPrio_High);
    }
    delete results;
}


int GetSteam2FromAccountId(char[] result, int maxlen, int account_id)
{
    return Format(result, maxlen, "STEAM_0:%d:%d", view_as<bool>(account_id % 2), account_id / 2);
}

public void sql_update_dummy(Database db, DBResultSet results, const char[] error, DataPack data)
{
    if (!db || strlen(error))
    {
        LogError("Query error 2 dummy: %s", error);
        return;
    }

    ResetPack(data);
    int auth = data.ReadCell();
    CloseHandle(data);

    char sQuery[512];
    Format(sQuery, sizeof(sQuery), "UPDATE playtimeze_scuffed_crap SET loaded_time = true WHERE auth = '%i'", auth);
    //PrintToChatAll("sQuery: %s", sQuery);
    g_hDatabase.Query(sql_update_bool, sQuery, DBPrio_High);
    delete results;
}

public void sql_update_bool(Database db, DBResultSet results, const char[] error, any data)
{
    if (!db || strlen(error))
    {
        LogError("Query error dummy: %s", error);
        return;
    }
    delete results;
}
