· 9 years ago · Jan 25, 2017, 09:40 PM
1
2// ====[ INCLUDES ]===========================================================
3
4#include <sourcemod>
5#include <sdktools>
6#include <multicolors>
7
8#pragma semicolon 1
9
10#pragma tabsize 0
11
12// ====[ DEFINES ]=============================================================
13
14#define PLUGIN_NAME "lastonline"
15#define PLUGIN_VERSION "1.0.0"
16#define PLUGIN_DESCRIPTION "desc here"
17#define MSG_PREFIX "{default}[{green}LastOnline{default}]"
18
19// ====[ COLORS ]==============================================================
20
21// {default},{white},{grey},{team},{red},{lightred},{darkred},{green},{lightgreen},{lime},{olive},{lightolive},{blue},{lightblue},{purple},{lightpurple}
22
23// ====[ CVARS | HANDLES | VARIABLES ]=========================================
24
25new Handle:db;
26
27// ====[ PLUGIN ]==============================================================
28
29public Plugin:myinfo =
30{
31 name = PLUGIN_NAME,
32 author = "siimon",
33 description = PLUGIN_DESCRIPTION,
34 version = PLUGIN_VERSION,
35 url = "siimon.org"
36}
37
38// ====[ FUNCTIONS ]===========================================================
39
40public OnPluginStart()
41{
42 RegConsoleCmd("sm_loupdate", CMD_LastOnlineUpdate, "Update your own info in lastonline db");
43 RegConsoleCmd("sm_lo", CMD_LastOnline);
44
45
46 InitDB(db);
47
48
49}
50
51public OnClientPostAdminCheck(client)
52{
53
54 CreateTimer(12.0, Timer_LastOnline, client);
55
56
57}
58
59public Action:Timer_LastOnline(Handle:timer, any:client)
60{
61 AddPlayer(client);
62 LastOnlineJoinMSG(client);
63
64}
65
66public OnClientDisconnect(client)
67{
68 UpdatePlayer(client);
69}
70
71// ====[ DATABASE ]==============================================================
72
73InitDB(&Handle:DbHNDL)
74{
75
76 new String:Error[255];
77
78 DbHNDL = SQL_Connect("lastonline", true, Error, sizeof(Error));
79
80
81 if(DbHNDL == INVALID_HANDLE)
82 {
83 SetFailState(Error);
84 }
85
86 new String:Query[255];
87 Format(Query, sizeof(Query), "CREATE TABLE IF NOT EXISTS lastonline (steamid VARCHAR(32) , name VARCHAR(32), timestamp VARCHAR(32), PRIMARY KEY (steamid));");
88
89 SQL_LockDatabase(DbHNDL);
90
91 SQL_FastQuery(DbHNDL, Query);
92
93 SQL_UnlockDatabase(DbHNDL);
94
95}
96
97// ====[ COMMANDS ]==============================================================
98
99public Action:CMD_LastOnlineUpdate(client, args)
100{
101
102 UpdatePlayer(client);
103
104 return Plugin_Handled;
105}
106
107
108public Action:CMD_LastOnline(client, args)
109{
110
111 new String:Query[255];
112 new String:Id[21];
113
114 GetClientAuthString(client, Id, sizeof(Id));
115
116 Format(Query, sizeof(Query), "SELECT timestamp FROM lastonline WHERE steamid = '%s'",Id);
117 SQL_TQuery(db, LastOnlineCallback, Query);
118
119
120}
121
122// ====[ UPDATE | ADD PLAYER ]==============================================================
123
124AddPlayer(client)
125{
126 new String:Query[255];
127 new String:Id[21];
128 new String:Name[32];
129 new String:Time[70];
130
131
132 GetClientAuthString(client, Id, sizeof(Id));
133
134
135 GetClientName(client, Name, sizeof (Name));
136 FormatTime(Time, sizeof(Time), "%Y-%x:%X", GetTime ());
137
138 Format(Query, sizeof(Query), "REPLACE INTO lastonline VALUES ('%s', '%s', '%s')", Id, Name, Time);
139
140
141 // Send our Query to the Function
142 SQL_TQuery(db, SQL_ErrorCheckCallBack, Query);
143}
144
145
146
147UpdatePlayer(client)
148{
149 new String:Query[255];
150 new String:Id[21];
151 new String:Name[32];
152 new String:Time[70];
153
154 GetClientAuthString(client, Id, sizeof(Id));
155 GetClientName(client, Name, sizeof (Name));
156 FormatTime(Time, sizeof(Time), "%Y-%x:%X", GetTime ());
157
158 Format(Query, sizeof(Query), "UPDATE lastonline SET name = '%s' WHERE steamid = '%s'", Name, Id);
159 Format(Query, sizeof(Query), "UPDATE lastonline SET timestamp = '%s' WHERE steamid = '%s'", Time, Id);
160
161 PrintToConsole(client,"[LastOnline] databases have been updated!");
162
163 // Send our Query to the Function
164 SQL_TQuery(db, SQL_ErrorCheckCallBack, Query);
165}
166
167// ====[ JOINMSG ]==============================================================
168
169LastOnlineJoinMSG(client)
170{
171 new String:Query[255];
172 new String:Id[21];
173
174 GetClientAuthString(client, Id, sizeof(Id));
175
176 Format(Query, sizeof(Query), "SELECT timestamp FROM lastonline WHERE steamid = '%s'",Id);
177 SQL_TQuery(db, LastOnlineCallbackJoin, Query);
178}
179
180// ====[ CALLBACKS ]==============================================================
181
182public LastOnlineCallback(Handle:owner, Handle:hndl, const char[] error, any:data)
183{
184
185 int client = data;
186
187
188
189 if (hndl == INVALID_HANDLE) {
190
191 LogError("[SM] Query failed! %s", error);
192 return;
193
194 }
195
196 if (SQL_GetRowCount(hndl) < 1) {
197
198 CPrintToChat(client, "Your record is not in our database. Please try to rejoin our server!");
199 return;
200
201 }
202
203 new String:Time[70];
204
205 SQL_FetchRow(hndl);
206 SQL_FetchString(hndl, 0, Time, sizeof(Time));
207 CPrintToChat(client, "%s You were last online on our servers at {lime}%s{default}!", MSG_PREFIX, Time);
208
209
210 return;
211
212
213
214}
215
216public LastOnlineCallbackJoin(Handle:owner, Handle:hndl, const char[] error, any:data)
217{
218 int client = data;
219
220 if (hndl == INVALID_HANDLE) {
221
222 LogError("[SM] Query failed! %s", error);
223 return;
224
225 }
226
227 if (SQL_GetRowCount(hndl) < 1) {
228
229 CPrintToChat(client, "Your record is not in our database. Please try to rejoin our server!");
230 return;
231
232 }
233
234 new String:Time[70];
235 new String:Name[32];
236
237 GetClientName(client, Name, sizeof (Name));
238
239 SQL_FetchRow(hndl);
240 SQL_FetchString(hndl, 0, Time, sizeof(Time));
241 CPrintToChat(client, "*********************************************************");
242 CPrintToChat(client, "%s Welcome back {lime}%s{default} you were last online on our servers at {lime}%s{default}!", MSG_PREFIX, Name, Time);
243 CPrintToChat(client, "*********************************************************");
244
245
246 return;
247
248}
249
250public SQL_ErrorCheckCallBack(Handle:owner, Handle:hndl, const String:error[], any:data)
251{
252 if(hndl == INVALID_HANDLE)
253 {
254 SetFailState("Query failed! %s", error);
255 }
256}