Time Played problem

Coding Help/Re-API Supported
Post Reply
NomaDD
Member
Member
Romania
Posts: 5
Joined: 6 Months Ago
Has thanked: 1 time
Been thanked: 1 time
Contact:

Time Played problem

#1

Post by NomaDD » 6 Months Ago

Hi again :D, i got this time played with sql support from raheem, and i made it to display in minutes, but when an user reach 100 minutes the database reset to 0 ( only in that user case )
  1.     #include <zombie_escape>
  2.     #include <nvault>
  3.      
  4.     new const g_szLogFile[] = "PlayedTime.log" // MySQL Errors log file
  5.      
  6.     // MySQL Table
  7.     new const g_szTable[] =
  8.     " \
  9.       CREATE TABLE IF NOT EXISTS `time_played` \
  10.       ( \
  11.           `SteamID` varchar(34) NOT NULL, \
  12.           `PlayedTime` int(16) NOT NULL, \
  13.           PRIMARY KEY (`SteamID`) \
  14.       ); \
  15.    "
  16.      
  17.     #define PLAYED_TIME_TASK 9100
  18.      
  19.     new const g_szVault_Name[] = "Played_Time"
  20.      
  21.     new g_iPlayedTime[33], g_iVaultHandler, Handle:g_hTupleHandler
  22.      
  23.     new g_pCvarSaveType, g_pCvarDBInfo[4]
  24.      
  25.     // Database
  26.     enum
  27.     {
  28.         Host = 0,
  29.         User,
  30.         Pass,
  31.         DB
  32.     }
  33.      
  34.     public plugin_natives()
  35.     {
  36.         // Native to return played time in seconds, plugin will save using nVault
  37.         register_native("ze_get_played_time", "native_ze_get_played_time", 1)
  38.     }
  39.      
  40.     public plugin_init()
  41.     {
  42.         register_plugin("Played Time", "1.0", "Raheem")
  43.        
  44.         // Cvars
  45.         g_pCvarSaveType = register_cvar("ze_played_time_save_type", "1") // 0 = nVault, 1 = SQL
  46.         g_pCvarDBInfo[Host] = register_cvar("ze_played_time_host", "localhost")
  47.         g_pCvarDBInfo[User] = register_cvar("ze_played_time_user", "user")
  48.         g_pCvarDBInfo[Pass] = register_cvar("ze_played_time_pass", "pass")
  49.         g_pCvarDBInfo[DB] = register_cvar("ze_played_time_dbname", "dbname")
  50.        
  51.         // Commands
  52.        /* register_clcmd("say /played", "ShowPlayedTime")
  53.         register_clcmd("say_team /played", "ShowPlayedTime")*/
  54.        
  55.         // Initialize MySQL - Delay 0.1 second required so we make sure that our zombie_escape.cfg already executed and cvars values loaded from it
  56.         set_task(0.1, "Delay_MySQL_Init")
  57.     }
  58.      
  59.     public Delay_MySQL_Init()
  60.     {
  61.         MySQL_Init()
  62.     }
  63.      
  64.     public MySQL_Init()
  65.     {
  66.         if (!get_pcvar_num(g_pCvarSaveType))
  67.             return
  68.        
  69.         new szHost[64], szUser[32], szPass[32], szDB[128]
  70.        
  71.         get_pcvar_string(g_pCvarDBInfo[Host], szHost, charsmax(szHost))
  72.         get_pcvar_string(g_pCvarDBInfo[User], szUser, charsmax(szUser))
  73.         get_pcvar_string(g_pCvarDBInfo[Pass], szPass, charsmax(szPass))
  74.         get_pcvar_string(g_pCvarDBInfo[DB], szDB, charsmax(szDB))
  75.        
  76.         g_hTupleHandler = SQL_MakeDbTuple(szHost, szUser, szPass, szDB)
  77.        
  78.         // Let's ensure that the g_hTupleHandler will be valid, we will access the database to make sure
  79.         new iErrorCode, szError[512], Handle:hSQLConnection
  80.        
  81.         hSQLConnection = SQL_Connect(g_hTupleHandler, iErrorCode, szError, charsmax(szError))
  82.        
  83.         if(hSQLConnection != Empty_Handle)
  84.         {
  85.             log_amx("[MySQL] Successfully connected to host: %s (ALL IS OK).", szHost)
  86.             SQL_FreeHandle(hSQLConnection)
  87.         }
  88.         else
  89.         {
  90.             // Disable plugin, and display the error
  91.             set_fail_state("Failed to connect to MySQL database: %s", szError)
  92.         }
  93.        
  94.         // Create our table
  95.         SQL_ThreadQuery(g_hTupleHandler, "QueryCreateTable", g_szTable)
  96.     }
  97.      
  98.     public QueryCreateTable(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
  99.     {
  100.         SQL_IsFail(iFailState, iError, szError, g_szLogFile)
  101.     }
  102.      
  103.     public ShowPlayedTime(id)
  104.     {
  105.         ze_colored_print(id, "!tYour played time!y: !g%i !tMinutes!y!", g_iPlayedTime[id]/60)
  106.     }
  107.      
  108.     public client_putinserver(id)
  109.     {
  110.         if(is_user_hltv(id) || is_user_bot(id))
  111.             return
  112.        
  113.         // Just 1 second delay
  114.         set_task(0.5, "DelayLoad", id)
  115.        
  116.         // Save played seconds
  117.         set_task(1.0, "IncreaseTimePlayed", id+PLAYED_TIME_TASK, _, _, "b")
  118.     }
  119.      
  120.     public DelayLoad(id)
  121.     {
  122.         // Load his time played
  123.         LoadPlayedTime(id)
  124.     }
  125.      
  126.     public IncreaseTimePlayed(taskid)
  127.     {
  128.         new id = taskid - PLAYED_TIME_TASK
  129.        
  130.         g_iPlayedTime[id]++
  131.     }
  132.      
  133.     public client_disconnected(id)
  134.     {
  135.         if(is_user_hltv(id) || is_user_bot(id))
  136.             return
  137.        
  138.         // Save played time
  139.         SavePlayedTime(id)
  140.        
  141.         remove_task(id+PLAYED_TIME_TASK)
  142.         remove_task(id)
  143.         g_iPlayedTime[id] = 0
  144.     }
  145.      
  146.     SavePlayedTime(id)
  147.     {
  148.         new szAuthID[35]
  149.         get_user_authid(id, szAuthID, charsmax(szAuthID))
  150.        
  151.         new szData[128]
  152.         num_to_str(g_iPlayedTime[id], szData, charsmax(szData))
  153.        
  154.         if (!get_pcvar_num(g_pCvarSaveType))
  155.         {
  156.             // Open the Vaults
  157.             g_iVaultHandler = nvault_open(g_szVault_Name)
  158.        
  159.             // Saves His Data
  160.             nvault_set(g_iVaultHandler, szAuthID, szData)
  161.        
  162.             // Close Vaults
  163.             nvault_close(g_iVaultHandler)
  164.         }
  165.         else
  166.         {
  167.             new szQuery[128]
  168.             formatex(szQuery, charsmax(szQuery), "UPDATE `time_played` SET `PlayedTime` = '%d' WHERE `SteamID` = '%s';", g_iPlayedTime[id]/60, szAuthID)
  169.             SQL_ThreadQuery(g_hTupleHandler, "QueryUpdateData", szQuery)
  170.         }
  171.     }
  172.      
  173.     public plugin_end()
  174.     {
  175.         if (get_pcvar_num(g_pCvarSaveType))
  176.         {
  177.             if (g_hTupleHandler != Empty_Handle)
  178.             {
  179.                 SQL_FreeHandle(g_hTupleHandler)
  180.             }
  181.         }
  182.     }
  183.      
  184.     public QueryUpdateData(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
  185.     {
  186.         SQL_IsFail(iFailState, iError, szError, g_szLogFile)
  187.     }
  188.      
  189.     LoadPlayedTime(id)
  190.     {
  191.         new szData[128], szAuthID[35]
  192.        
  193.         get_user_authid(id, szAuthID, charsmax(szAuthID))
  194.        
  195.         if (!get_pcvar_num(g_pCvarSaveType))
  196.         {
  197.             // Useless Variable
  198.             new iTimestamp, iExists
  199.            
  200.             // Open the Vault
  201.             g_iVaultHandler = nvault_open(g_szVault_Name)
  202.            
  203.             iExists = nvault_lookup(g_iVaultHandler, szAuthID, szData, charsmax(szData), iTimestamp)
  204.            
  205.             // Close Vault
  206.             nvault_close(g_iVaultHandler)
  207.            
  208.             if (!iExists)
  209.             {
  210.                 g_iPlayedTime[id] = 0
  211.                 SavePlayedTime(id)
  212.             }
  213.             else
  214.             {
  215.                 g_iPlayedTime[id] = str_to_num(szData)
  216.             }
  217.         }
  218.         else
  219.         {
  220.             new szQuery[128], szData[5]
  221.             formatex(szQuery, charsmax(szQuery), "SELECT `PlayedTime` FROM `time_played` WHERE ( `SteamID` = '%s' );", szAuthID)
  222.          
  223.             num_to_str(id, szData, charsmax(szData))
  224.             SQL_ThreadQuery(g_hTupleHandler, "QuerySelectData", szQuery, szData, charsmax(szData))
  225.         }
  226.     }
  227.      
  228.     public QuerySelectData(iFailState, Handle:hQuery, szError[], iError, szData[])
  229.     {
  230.         if(SQL_IsFail(iFailState, iError, szError, g_szLogFile))
  231.             return
  232.        
  233.         new id = str_to_num(szData)
  234.        
  235.         // No results for this query means that player not saved before
  236.         if(!SQL_NumResults(hQuery))
  237.         {
  238.             // This is new player
  239.             g_iPlayedTime[id] = 0
  240.            
  241.             // Get user steamid
  242.             new szAuthID[36]
  243.             get_user_authid(id, szAuthID, charsmax(szAuthID))
  244.            
  245.             // Insert his data to our database
  246.             new szQuery[128]
  247.            
  248.             formatex(szQuery, charsmax(szQuery), "INSERT INTO `time_played` (`SteamID`, `PlayedTime`) VALUES ('%s', '%d');", szAuthID, g_iPlayedTime[id]/60)
  249.             SQL_ThreadQuery(g_hTupleHandler, "QueryInsertData", szQuery)
  250.            
  251.             return
  252.         }
  253.        
  254.         new iPlayedTimeColumn = SQL_FieldNameToNum(hQuery, "PlayedTime")
  255.        
  256.         // Read the played time for this player
  257.         g_iPlayedTime[id] = SQL_ReadResult(hQuery, iPlayedTimeColumn)
  258.     }
  259.      
  260.     public QueryInsertData(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
  261.     {
  262.         SQL_IsFail(iFailState, iError, szError, g_szLogFile)
  263.     }
  264.      
  265.     public native_ze_get_played_time(id)
  266.     {
  267.         if(!is_user_connected(id))
  268.         {
  269.             log_error(AMX_ERR_NATIVE, "[ZE] Invalid Player id (%d)", id)
  270.             return -1;
  271.         }
  272.        
  273.         return g_iPlayedTime[id]/60
  274.     }
All I did was to divide g_iPlayedTime [id] by 60

User avatar
Raheem
Mod Developer
Mod Developer
Posts: 2189
Joined: 4 Years Ago
Has thanked: 49 times
Been thanked: 149 times
Age: 23
Contact:

#2

Post by Raheem » 6 Months Ago

Hi,

My plugin not good coded, and will in some cases cause resets. Although I coded but I did not use it.
Please use this: https://forums.alliedmods.net/showthread.php?t=201869
Tested and working fully good.
These users thanked the author Raheem for the post:
NomaDD (6 Months Ago)
He who fails to plan is planning to fail

_Joker_
Member
Member
Posts: 3
Joined: 1 Week Ago
Has thanked: 0
Been thanked: 0
Contact:

#3

Post by _Joker_ » 1 Week Ago

I need help !! When i change it To Say /online and i wanna time be saved not when i leave time rest to 00:00 i wanna it saved

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests