· 6 years ago · Apr 14, 2019, 08:02 AM
1#pragma semicolon 1
2#pragma newdecls required
3
4#include <sourcemod>
5
6#define CHAT_PREFIX "[EVICT]"
7#define PLUGIN_VERSION "2.0.1"
8
9Handle g_DB = INVALID_HANDLE;
10char g_Error[PLATFORM_MAX_PATH];
11char g_Query[PLATFORM_MAX_PATH];
12char g_sUserSteamId[MAXPLAYERS+1][PLATFORM_MAX_PATH];
13
14Handle ClientTimer[MAXPLAYERS+1];
15int Minutes[MAXPLAYERS+1];
16
17public Plugin myinfo = {
18 name = "[CSGO] Ore jucate",
19 author = "PorcusorulMagic",
20 description = "Arata orele jucate de tine pe server, respectiv de alt jucator.",
21 version = PLUGIN_VERSION,
22 url = "https://forums.alliedmods.net/showthread.php?p=2523586"
23};
24
25public void OnPluginStart()
26{
27 CreateConVar("sm_playtime_version", PLUGIN_VERSION, "Plugin version");
28 RegConsoleCmd("sm_time", Command_showTime, "Shows your played time.");
29 RegConsoleCmd("sm_ore", Command_showTime, "Shows your played time.");
30 RegConsoleCmd("sm_hours", Command_showTime, "Shows your played time.");
31 RegAdminCmd("sm_gettime", Command_getTime, ADMFLAG_SLAY, "Gets a player's played time.");
32
33 if(g_DB == INVALID_HANDLE)
34 g_DB = SQL_Connect ("default", false, g_Error, sizeof (g_Error));
35
36 if(g_DB == INVALID_HANDLE)
37 {
38 SetFailState ("Can't reach the database.");
39 return;
40 }
41
42 FormatEx (g_Query, sizeof (g_Query), "CREATE TABLE IF NOT EXISTS PlayTime (SteamID varchar(128) PRIMARY KEY, mins int(11) DEFAULT 0);");
43 SQL_Query (g_DB, g_Query);
44
45 SQL_SetCharset (g_DB, "utf8");
46}
47
48public void OnClientPutInServer(int client)
49{
50 if (IsValidClient(client))
51 {
52 if(!GetClientAuthId(client, AuthId_Engine, g_sUserSteamId[client], sizeof(g_sUserSteamId), true)) //always check for valid return
53 return;
54
55 FormatEx (g_Query, sizeof (g_Query), "SELECT mins FROM PlayTime WHERE SteamID = '%s' LIMIT 1;", g_sUserSteamId [ client ]);
56 SQL_TQuery (g_DB, LoadPlayerData, g_Query, client);
57 }
58
59 ClientTimer[client] = (CreateTimer(60.0, TimerAdd, client, TIMER_REPEAT));
60}
61
62void LoadPlayerData (Handle pDb, Handle pQuery, char[] Error, any Data)
63{
64 static int Id, RowsCount;
65
66 RowsCount = 0;
67 Id = view_as<int>(Data);
68
69 if (strlen(Error) > 0)
70 LogError("SQL_TQuery() @ LoadPlayerData() reported: %s", Error);
71
72 else if (IsValidClient(Id))
73 {
74
75 if (SQL_HasResultSet(pQuery))
76 RowsCount = SQL_HasResultSet(pQuery) ? SQL_GetRowCount(pQuery) : 0;
77
78 switch (RowsCount)
79 {
80 case 0:
81 {
82 FormatEx (g_Query, sizeof (g_Query), "INSERT INTO `PlayTime`(`SteamID`) VALUES ('%s')", g_sUserSteamId [ Id ]);
83 SQL_Query (g_DB, g_Query);
84 }
85
86 default:
87 {
88 SQL_FetchRow (pQuery);
89
90 Minutes[Id] = SQL_FetchInt (pQuery, 0);
91 }
92 }
93 CloseHandle (pDb);
94 }
95}
96
97public void OnClientDisconnect(int client)
98{
99 CloseHandle(ClientTimer[client]);
100 SaveTime(client);
101}
102
103public Action TimerAdd(Handle timer, int client)
104{
105 if(IsClientConnected(client) && IsClientInGame(client))
106 {
107 Minutes[client]++;
108 SaveTime(client);
109 }
110}
111
112public Action Command_showTime(int client, int args)
113{
114 static char totalTime[PLATFORM_MAX_PATH];
115
116 FormatEx (g_Query, sizeof (g_Query), "SELECT mins FROM PlayTime WHERE SteamID = '%s' LIMIT 1;", g_sUserSteamId [ client ]);
117 SQL_TQuery (g_DB, LoadPlayerData, g_Query, client);
118
119 SniperH_GetTimeStringMinutes(Minutes[client], totalTime, sizeof(totalTime));
120 PrintToChat(client, "\x01★ \x04%s \x01Total ore: \x04%s\x01.", CHAT_PREFIX, totalTime);
121 return Plugin_Handled;
122}
123
124public Action Command_getTime(int client, int args)
125{
126 if (args < 1)
127 {
128 PrintToChat(client, "\x01★ \x04%s \x01Foloseste: \x04!gettime \x02<target>", CHAT_PREFIX);
129 return Plugin_Handled;
130 }
131
132 char szTarget[64];
133 GetCmdArg(1, szTarget, sizeof(szTarget));
134
135 int target = FindTarget(client, szTarget, true, false);
136
137 static char totalTime[PLATFORM_MAX_PATH];
138
139 FormatEx (g_Query, sizeof (g_Query), "SELECT mins FROM PlayTime WHERE SteamID = '%s' LIMIT 1;", g_sUserSteamId [ client ]);
140 SQL_TQuery (g_DB, LoadPlayerData, g_Query, target);
141
142 SniperH_GetTimeStringMinutes(Minutes[target], totalTime, sizeof(totalTime));
143 PrintToChat(client, "\x01★ \x04%s \x01Acesta are: \x04%s\x01.", CHAT_PREFIX, totalTime);
144 return Plugin_Handled;
145}
146
147void SaveTime(int client)
148{
149 FormatEx (g_Query, sizeof (g_Query), "UPDATE `PlayTime` SET `mins` = '%d' WHERE `SteamID` = '%s'", Minutes[client], g_sUserSteamId [ client ]);
150 SQL_Query (g_DB, g_Query);
151}
152
153/* Fancy Stuff * Do NOT touch unless you know what you are doing! */
154
155int SniperH_GetTimeStringMinutes(int Mins, char[] Output, int Size)
156{
157 static int m_Hours, m_Mins;
158
159 m_Hours = 0;
160 m_Mins = SniperH_AbsInt(Mins);
161
162 if (m_Mins == 0)
163 return FormatEx(Output, Size, "0 minute");
164
165 while (m_Mins >= 60)
166 {
167 m_Hours++;
168
169 m_Mins -= 60;
170 }
171
172 if (m_Hours > 0)
173 {
174 if (m_Mins > 0)
175 return FormatEx(Output, Size, "%d ore %d minute", m_Hours, m_Mins);
176
177 return FormatEx(Output, Size, "%d ore", m_Hours);
178 }
179
180 return FormatEx(Output, Size, "%d minute", m_Mins);
181}
182
183int SniperH_AbsInt(int Value = 0)
184{
185 return Value >= 0 ? Value : -Value;
186}
187
188bool IsValidClient(int client)
189{
190 if (!(1 <= client <= MaxClients) || !IsClientInGame (client) || IsFakeClient(client))
191 return false;
192
193 return true;
194}