· 6 years ago · Jul 23, 2019, 06:00 PM
1#pragma semicolon 1
2
3#define DEBUG
4
5#include <sourcemod>
6#include <sdktools>
7#include <cstrike>
8#include <adminmenu>
9
10#define PLUGIN_AUTHOR "GetRektByNoob"
11#define PLUGIN_VERSION "1.00"
12
13#pragma newdecls required
14
15int Cooldown[MAXPLAYERS + 1][3];
16int MaxCases;
17Database gDB_Daily;
18
19public Plugin myinfo = {
20 name = "",
21 author = PLUGIN_AUTHOR,
22 description = "",
23 version = PLUGIN_VERSION,
24 url = "https://steamcommunity.com/profiles/765611988057643"
25};
26
27// COMMAND ************************************************************************************************************************************
28public void OnPluginStart() {
29 RegConsoleCmd("sm_rc", Command_ResetCooldown);
30 RegConsoleCmd("sm_a", Command_ReadValues);
31 RegConsoleCmd("sm_clear", Command_ClearTable);
32 RegConsoleCmd("sm_format", Command_Formater);
33 RegConsoleCmd("sm_max", Command_Max);
34
35 LoadTranslations("common.phrases");
36}
37
38Action Command_Max(int client, int args) {
39 char arg1[10];
40 GetCmdArg(1, arg1, sizeof(arg1));
41 MaxCases = StringToInt(arg1);
42}
43
44Action Command_Formater(int client, int args) {
45 char CreateTable[500], Update[500], Source[150];
46 PrintToServer("======================================================");
47 PrintToServer("Max Cases > %d", MaxCases);
48 PrintToServer("======================================================");
49
50 // Create Table
51 Format(CreateTable, sizeof(CreateTable), "CREATE TABLE IF NOT EXISTS case_Daily (SteamID TEXT PRIMARY KEY");
52 for(int i = 0; i < MaxCases; i++) {
53 Format(Source, sizeof(Source), ", Credit_%d BIGINT", i);
54 StrCat(CreateTable, sizeof(CreateTable), Source);
55 }
56 StrCat(CreateTable, sizeof(CreateTable), ");");
57 PrintToServer(CreateTable);
58
59 // Update Into UPDATE case_Daily SET credit1 = %d, credit2 = %d , credit3 = %d WHERE steamid = %d;
60 Format(Update, sizeof(Update), "UPDATE case_Daily SET Credit_0 = %d", Cooldown[client][0]);
61 for(int i = 1; i < MaxCases; i++) {
62 Format(Source, sizeof(Source), ", Credit_%d = %d", i, Cooldown[client][i]);
63 StrCat(Update, sizeof(Update), Source);
64 }
65
66 Format(Source, sizeof(Source)," WHERE steamid = %d;", GetSteamAccountID(client));
67 StrCat(Update, sizeof(Update), Source);
68 PrintToServer(Update);
69 PrintToServer("======================================================");
70}
71
72Action Command_ResetCooldown(int client, int args) {
73 if(args < 1) {
74 ReplyToCommand(client, "[SM] Usage : sm_rc <player|userid|@me>");
75 return Plugin_Handled;
76 }
77
78 char sArg1[50], sArg2[10];
79 GetCmdArg(1, sArg1, sizeof(sArg1));
80 int target = FindTarget(client, sArg1, true, false);
81
82 if(target == -1) {
83 return Plugin_Handled;
84 }
85
86 GetCmdArg(2, sArg2, sizeof(sArg2));
87 int iArg2 = StringToInt(sArg2);
88
89 for(int i = 0; i < 3; i++) {
90 Cooldown[target][i] = iArg2 + 5 * i;
91 Debug("Client Reseted > #%d : %d", i , Cooldown[client][i]);
92 }
93
94 ReplyToCommand(client, "%N\'s cooldown has been reseted!", client);
95 return Plugin_Handled;
96}
97
98Action Command_ReadValues(int client, int args) {
99 for(int i = 0 ; i < 3; i++) {
100 PrintToChatAll("#%d > %d", i, Cooldown[client][i]);
101 }
102}
103
104// CLEAR TABLE **************************************************************************************************************************
105Action Command_ClearTable(int client, int args) {
106 char sQuery[512];
107 gDB_Daily.Format(sQuery, sizeof(sQuery), "DELETE FROM case_Daily");
108 gDB_Daily.Query(SQL_QueryClearTable, sQuery);
109}
110
111void SQL_QueryClearTable(Database db, DBResultSet results, const char[] error, any data) {
112 if(!results) {
113 // Failed to save player
114 Debug("Failed to clear table");
115 } else {
116 Debug("Table cleard");
117 }
118}
119
120// CONNECT ************************************************************************************************************************************
121public void OnConfigsExecuted() {
122 Database.Connect(SQL_DailyConnect, "storage-local");
123}
124
125void SQL_DailyConnect(Database db, char[] error, any data) {
126 if(db == INVALID_HANDLE) {
127 Debug("Could not connect to database, SQL Error : %s", error);
128 return;
129 } else {
130 gDB_Daily = db;
131 char sQuery[512];
132 Debug("Creating table if not existed");
133 gDB_Daily.Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS case_Daily (steamid TEXT PRIMARY KEY, credit1 BIGINT, credit2 BIGINT, credit3 BIGINT);");
134 gDB_Daily.Query(SQL_QueryCreateTable, sQuery);
135 }
136}
137
138void SQL_QueryCreateTable(Database db, DBResultSet results, const char[] error, any data) {
139 if(!db) {
140 Debug("Failed to create table, SQL Error : %s", error);
141 }
142}
143
144// LOAD DATA ************************************************************************************************************************************
145
146public void OnClientPostAdminCheck(int client){
147 for(int i = 0; i < 3; i++) { // reset
148 Cooldown[client][i] = GetRandomInt(1000, 10000);
149 Debug("Client Connect > #%d : %d", i , Cooldown[client][i]);
150 }
151
152 int SteamID = GetSteamAccountID(client);
153 if(!SteamID) {
154 Debug("Client %N isnt authorized", client);
155 } else {
156 Debug("Loading client \"%N\"", client);
157 char sQuery[512];
158 gDB_Daily.Format(sQuery, sizeof(sQuery), "SELECT * FROM case_Daily WHERE steamid = \'%d\';", SteamID);
159 gDB_Daily.Query(SQL_LoadClient, sQuery, client);
160 }
161}
162
163void SQL_LoadClient(Database db, DBResultSet results, const char[] error, any data) {
164 if(!results) {
165 Debug("Failed to load client \"%N\", SQL Error : %s", data, error);
166 } else {
167 if(!SQL_FetchRow(results)) {
168 // first time client joins
169 Debug("Create new field to client \"%N\".", data);
170 int SteamID = GetSteamAccountID(data);
171 char sQuery[512];
172 db.Format(sQuery, sizeof(sQuery), "INSERT INTO case_Daily (steamid) VALUES (%d);", SteamID);
173 gDB_Daily.Query(SQL_FirstTime, sQuery, data);
174 } else {
175 // client already joined once
176 Debug("Fetching data to client \"%N\".",data);
177 for(int i = 0; i < 3; i++) {
178 Cooldown[data][i] = SQL_FetchInt(results, i + 1);
179 Debug("Client Fetching > #%d : %d", i , Cooldown[data][i]);
180 }
181 }
182 }
183}
184
185void SQL_FirstTime(Database db, DBResultSet results, const char[] error, any data) {
186 if(!results) {
187 // failed to create field for client
188 Debug("Failed create field to client \"%N\", SQL Error : %s", data, error);
189 for(int i = 0; i < 3; i++) {
190 Cooldown[data][i] = -1;
191 Debug("Client Failed New Field > #%d : %d", i , Cooldown[data][i]);
192 }
193 } else {
194 // created field for client
195 Debug("Created field to client \"%N\".", data);
196 int iNow = GetTime();
197 for(int i = 0; i < 3; i++) {
198 Cooldown[data][i] = iNow;
199 Debug("Client New Field > #%d : %d", i , Cooldown[data][i]);
200 }
201 }
202}
203
204// SAVE DATA ************************************************************************************************************************************
205
206public void OnClientDisconnect(int client) {
207 int SteamID = GetSteamAccountID(client);
208 char sQuery[512];
209 Debug("Satrted saving client \"%N\" data.", client);
210 Format(sQuery, sizeof(sQuery), "UPDATE case_Daily SET credit1 = %d, credit2 = %d , credit3 = %d WHERE steamid = %d;", Cooldown[client][0], Cooldown[client][1], Cooldown[client][2], SteamID);
211 gDB_Daily.Query(SQL_SaveData, sQuery, client, DBPrio_High);
212}
213
214void SQL_SaveData(Database db, DBResultSet results, const char[] error, any data) {
215 if(!db) {
216 // Failed to save player
217 Debug("Failed to save client \"%N\", SQL Error : %s", data, error);
218 } else {
219 Debug("Saved for client \"%N\" data!", data);
220 for(int i = 0; i < 3; i++) {
221 Debug("Client Saved Data > #%d : %d", i , Cooldown[data][i]);
222 }
223 }
224}
225
226// DEBUG ***************************************************
227
228void Debug(char[] Message, any ...) {
229 char Temp[500], Debug_Message[550];
230 VFormat(Temp, sizeof(Temp), Message, 2);
231 Format(Debug_Message, sizeof(Debug_Message),"[Debug] SQL Daily > %s", Temp);
232 PrintToServer(Debug_Message);
233}