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

Database g_hDatabase;

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

public void OnMapStart()
{
    CreateTimer(10.0, time_query_activity, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}

public Action time_query_activity(Handle timer, any data)
{
    for (int client = 1; client <= MaxClients; client++)
        if (IsValidClient(client) && !IsFakeClient(client) && IsPlayerAlive(client))
        {
            char sAuthID[32];
            GetClientAuthId(client, AuthId_Steam2, sAuthID, sizeof(sAuthID), false);
            char sIP[32];
            GetClientIP(client, sIP, sizeof(sIP));
            char sQuery[512];
            char sServer[64];
            int i_port = GetConVarInt(FindConVar("hostport"));
            if (i_port == 27015)
            {
                Format(sServer, sizeof(sServer), "ze_time");
            }
            else if (i_port == 27016)
            {
                Format(sServer, sizeof(sServer), "zr_time");
            }
            else if (i_port == 27017)
            {
                Format(sServer, sizeof(sServer), "mg_time");
            }
            else if (i_port == 27023)
            {
                Format(sServer, sizeof(sServer), "jb_time");
            }
            else
            {
                continue;
            }
            char sName[MAX_NAME_LENGTH];
            GetClientName(client, sName, sizeof(sName));
            int size2 = 2 * strlen(sName) + 1;
            char[] sEscapedName = new char[size2 + 1];
            g_hDatabase.Escape(sName, sEscapedName, size2 + 1);
            Format(sQuery, sizeof(sQuery), "update unloze_playtimestats.player_time set `%s` = `%s` + 10, player_name = '%s' where steam_id = '%s' and ipv4 = '%s'", sServer, sServer, sEscapedName, sAuthID, sIP);
            //LogError("sQuery: %s",  sQuery);
            g_hDatabase.Query(SQL_FinishedQuery, sQuery, _, DBPrio_High);
        }

    return Plugin_Continue;
}

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

public void SQL_OnDatabaseConnect(Database db, const char[] error, any data)
{
    if(!db || strlen(error))
    {
            LogError("Database error: %s", error);
            return;
    }
    g_hDatabase = db;
    for (int i = 1; i <= MaxClients; i++)
        OnClientPostAdminCheck(i);
}

public void OnClientPostAdminCheck(int client)
{
    if(!IsValidClient(client) || IsFakeClient(client))
        return;
    if (!g_hDatabase)
    {
        Database.Connect(SQL_OnDatabaseConnect, "unloze_playtimestats");
        return;
    }
    insert_client(client);
}

public void insert_client(int client)
{
    char sAuthID[32];
    GetClientAuthId(client, AuthId_Steam2, sAuthID, sizeof(sAuthID), false);
    char sName[MAX_NAME_LENGTH];
    GetClientName(client, sName, sizeof(sName));
    int size2 = 2 * strlen(sName) + 1;
    char[] sEscapedName = new char[size2 + 1];
    g_hDatabase.Escape(sName, sEscapedName, size2 + 1);
    char sIP[32];
    GetClientIP(client, sIP, sizeof(sIP));
    char sQuery[512];
    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', 0, 0, 0, 0) ON DUPLICATE KEY UPDATE `player_name` = '%s'", sAuthID, sIP, sEscapedName, sEscapedName);
    g_hDatabase.Query(SQL_FinishedQuery, sQuery, _, DBPrio_High);
}

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

stock bool IsValidClient(int client)
{
        if (client > 0 && client <= MaxClients && IsClientConnected(client) && IsClientInGame(client))
            return true;
        return false;
}