Hi guys,
Im not in my country yet and when i back i want to do something with times, can someone learn me how to store times? Do not give me links, explanation on pm or here.
Thanks.
Benoist3012
08-03-2015 15:48
Re: SQL or clientprefs - Store Times
If you are going to store times for more than 1 server then use sql else, you can use cookies depend on what you want to do.
TheUnderTaker
08-03-2015 15:50
Re: SQL or clientprefs - Store Times
Quote:
Originally Posted by Benoist3012
(Post 2328004)
If you are going to store times for more than 1 server then use sql else, you can use cookies depend on what you want to do.
Need on than 1 servers... But i can do it twice.
I know cookies but dont know how to store times.
Btw i need to know how i dont need method names..
Benoist3012
08-03-2015 15:59
Re: SQL or clientprefs - Store Times
Quote:
Originally Posted by TheUnderTaker
(Post 2328007)
Need on than 1 servers... But i can do it twice.
I know cookies but dont know how to store times.
Btw i need to know how i dont need method names..
Well actually store time with cookies is easy. First you need to create the name
PHP Code:
Handle htimePlayed; public void OnPluginStart() { hTimePlayed = RegClientCookie("sm_time_played", "Time played on the server", CookieAccess_Protected); } //CookieAccess_Protected this block anyone from modifying it public OnClientDisconnect(client) { if (!IsValidClient(client)) return; int time = 0; char oldtime[64]; if (!IsFakeClient(client) && AreClientCookiesCached(client)) { GetClientCookie(client, hTimePlayed, oldtime, sizeof(oldtime)); time = StringToInt(oldtime); time += TimePlayed[client]; //I let you create a timer and add up 1 each second IntToString(time, oldtime, sizeof(oldtime)); SetClientCookie(client, hTimePlayed, oldtime); } }
A little scrap of code for you
TheUnderTaker
08-06-2015 09:05
Re: SQL or clientprefs - Store Times
Quote:
Originally Posted by Benoist3012
(Post 2328010)
Well actually store time with cookies is easy. First you need to create the name
PHP Code:
Handle htimePlayed; public void OnPluginStart() { hTimePlayed = RegClientCookie("sm_time_played", "Time played on the server", CookieAccess_Protected); } //CookieAccess_Protected this block anyone from modifying it public OnClientDisconnect(client) { if (!IsValidClient(client)) return; int time = 0; char oldtime[64]; if (!IsFakeClient(client) && AreClientCookiesCached(client)) { GetClientCookie(client, hTimePlayed, oldtime, sizeof(oldtime)); time = StringToInt(oldtime); time += TimePlayed[client]; //I let you create a timer and add up 1 each second IntToString(time, oldtime, sizeof(oldtime)); SetClientCookie(client, hTimePlayed, oldtime); } }
A little scrap of code for you
What do you mean in
PHP Code:
time += TimePlayed[client]; //I let you create a timer and add up 1 each second
what timer? If you can just do it yourself on the way you are replying(why not .-.) So it would be perfect :)
asherkin
08-06-2015 09:29
Re: SQL or clientprefs - Store Times
Don't use clientprefs for things that are not client preferences.
TheUnderTaker
08-06-2015 09:39
Re: SQL or clientprefs - Store Times
Quote:
Originally Posted by asherkin
(Post 2329616)
Don't use clientprefs for things that are not client preferences.
I just want for my private vip plugin store times in sql not else... Can you help :-)?
// Make SQLite connection new String:error[255]; new Handle:db = SQL_Connect("storage-local", true, error, sizeof(error));
if (db == INVALID_HANDLE) { SetFailState("Could not connect: %s", error); }
// Query player time data from database new String:query[256]; GetClientAuthId(client, AuthId_Steam2, query, sizeof(query)); Format(query, sizeof(query), "SELECT time FROM player_times WHERE steamid = '%s'", query);
new Handle:result = SQL_Query(db, query); // Start query
CloseHandle(db); // Close database connection
if(result == INVALID_HANDLE) // Invalid data { PrintToChat(client, "Couldn't get data from database"); return Plugin_Handled; }
new Float:client_time = GetClientTime(client); new Float:database_time;
if(SQL_FetchRow(result)) // Found result { database_time = SQL_FetchFloat(result, 0); // Grab first result }
// Make SQLite connection new String:error[255]; new Handle:db = SQL_Connect("storage-local", true, error, sizeof(error));
if (db == INVALID_HANDLE) { SetFailState("Could not connect: %s", error); }
// Query player time data from database new String:query[256]; GetClientAuthId(client, AuthId_Steam2, query, sizeof(query)); Format(query, sizeof(query), "SELECT time FROM player_times WHERE steamid = '%s'", query);
new Handle:result = SQL_Query(db, query); // Start query
CloseHandle(db); // Close database connection
if(result == INVALID_HANDLE) // Invalid data { PrintToChat(client, "Couldn't get data from database"); return Plugin_Handled; }
new Float:client_time = GetClientTime(client); new Float:database_time;
if(SQL_FetchRow(result)) // Found result { database_time = SQL_FetchFloat(result, 0); // Grab first result }
// Make SQLite connection
new String:error[255];
new Handle:db = SQL_Connect("storage-local", true, error, sizeof(error));
if (db == INVALID_HANDLE)
{
SetFailState("Could not connect: %s", error);
}
// Query player time data from database
new String:query[256];
GetClientAuthId(client, AuthId_Steam2, query, sizeof(query));
Format(query, sizeof(query), "SELECT time FROM player_times WHERE steamid = '%s'", query);
new Handle:result = SQL_Query(db, query); // Start query
CloseHandle(db); // Close database connection
if(result == INVALID_HANDLE) // Invalid data
{
PrintToChat(client, "Couldn't get data from database");
return Plugin_Handled;
}
new Float:client_time = GetClientTime(client);
new Float:database_time;
if(SQL_FetchRow(result)) // Found result
{
database_time = SQL_FetchFloat(result, 0); // Grab first result
}
public event(Handle:event, const String:name[], bool:dontBroadcast)
{
new client = GetClientOfUserId(GetEventInt(event, "userid"));
if(!IsFakeClient(client))
{
// Make connection
new String:error[255];
new Handle:db = SQL_Connect("storage-local", true, error, sizeof(error));
if (db == INVALID_HANDLE)
{
SetFailState("Could not connect: %s", error);
}
// Create table
if(!SQL_FastQuery(db, "CREATE TABLE IF NOT EXISTS player_times (steamid varchar(20) NOT NULL, time float NOT NULL, PRIMARY KEY (steamid))"))
{
SQL_GetError(db, error, sizeof(error));
CloseHandle(db);
SetFailState("Could create table: %s", error);
}
new String:buffer[20];
new String:query[256];
new Float:time = GetClientTime(client);
// Which, SQLite or MySQL ?
SQL_ReadDriver(db, buffer, sizeof(buffer));
if(StrEqual(buffer, "sqlite", false))
{
// SQLite - UPDATE , INSERT player data
GetClientAuthId(client, AuthId_Steam2, buffer, sizeof(buffer));
Format(query, sizeof(query), "UPDATE player_times SET time = time + %f WHERE steamid ='%s'", time, buffer);
SQL_FastQuery(db, query);
Format(query, sizeof(query), "INSERT OR IGNORE INTO player_times (steamid,time) VALUES ('%s', %f)", buffer, time);
SQL_FastQuery(db, query);
}
else
{
// MySQL - INSERT INTO... ON DUPLICATE KEY UPDATE player data
GetClientAuthId(client, AuthId_Steam2, buffer, sizeof(buffer));
Format(query, sizeof(query), "INSERT INTO player_times (steamid,time) VALUES ('%s', %f) ON DUPLICATE KEY UPDATE time=time+%f;", buffer, time, time);
SQL_FastQuery(db, query);
}
CloseHandle(db); // Close database connection
}
}
Wow! Thank you very much! I will try it, Btw I made a countdown plugin, Do you know how to make for example 120 seconds to 02:00? And it will count? I mean it will show mins, hours, days, not only seconds.