· 6 years ago · Jul 22, 2019, 02:26 PM
1#pragma semicolon 1
2
3#define DEBUG
4
5#include <sourcemod>
6#include <sdktools>
7#include <cstrike>
8
9#define PLUGIN_AUTHOR "GetRektByNoob"
10#define PLUGIN_VERSION "1.00"
11
12#pragma newdecls required
13
14int Cooldown[MAXPLAYERS + 1][3];
15Database gDB_Daily;
16
17public Plugin myinfo = {
18 name = "",
19 author = PLUGIN_AUTHOR,
20 description = "",
21 version = PLUGIN_VERSION,
22 url = "https://steamcommunity.com/profiles/765611988057643"
23};
24
25// COMMAND ************************************************************************************************************************************
26public void OnPluginStart() {
27 RegAdminCmd("sm_rc", Command_ResetCooldown, ADMFLAG_CONVARS);
28 RegAdminCmd("sm_a", Command_ReadValues, ADMFLAG_CONVARS);
29}
30
31Action Command_ResetCooldown(int client, int args) {
32 if(args < 1) { ReplyToCommand(client, "[SM] Usage : sm_rc <player|userid|@me>"); }
33
34 char sArg1[50];
35 GetCmdArg(1, sArg1, sizeof(sArg1));
36 int target = FindTarget(client, sArg1, false, false);
37 if(target == -1) { ReplyToCommand(client, "[SM] Could not find target."); }
38
39 for(int i = 0; i < sizeof(Cooldown[]); i++) { Cooldown[target][i] = 0; }
40 ReplyToCommand(client, "%N\'s cooldown has been reseted!");
41}
42
43Action Command_ReadValues(int client, int args) {
44 for(int i = 0 ; i < sizeof(Cooldown[]); i++) {
45 PrintToChatAll("#%d > %d", i, Cooldown[client][i]);
46 }
47}
48
49// CONNECT ************************************************************************************************************************************
50public void OnConfigsExecuted() {
51 Database.Connect(SQL_DailyConnect, "storage-local");
52}
53
54void SQL_DailyConnect(Database db, char[] error, any data) {
55 if(db == INVALID_HANDLE) {
56 SetFailState("Could not connect to database, SQL Reason : %s", error);
57 return;
58 } else {
59 gDB_Daily = db;
60 char Query[512];
61 Format(Query, sizeof(Query), "CREATE TABLE IF NOT EXISTS case_Daily (steamid TEXT PRIMARY KEY, credit1 BIGINT, credit2 BIGINT, credit3 BIGINT);");
62 db.Query(SQL_QueryCreateTable, Query);
63 }
64}
65
66void SQL_QueryCreateTable(Database db, DBResultSet results, const char[] error, any data) {
67 if(!db) {
68 PrintToServer("Failed to create table, SQL Error : %s", error);
69 SetFailState("Failed to create table, SQL Error : %s", error);
70 }
71}
72
73// LOAD DATA ************************************************************************************************************************************
74public void OnClientAuthorized(int client, const char[] auth) {
75 PrintToServer("%N : %s", client , auth);
76 char Query[512];
77 Format(Query, sizeof(Query), "SELECT * FROM case_Daily WHERE steamid = \'%s\';", auth);
78 gDB_Daily.Query(SQL_LoadClient, Query, client);
79}
80
81void SQL_LoadClient(Database db, DBResultSet results, const char[] error, any data) {
82 if(!results) {
83 SetFailState("Failed to load client \"%N\", SQL Error : %s", data, error);
84 } else {
85 char Query[512];
86 if(!SQL_FetchRow(results)) {
87 // first time client joins
88 char SteamID[64];
89 GetClientAuthId(data, AuthId_Steam2, SteamID , sizeof(SteamID));
90 Format(Query, sizeof(Query), "INSERT INTO case_Daily VALUES (%s, %d, %d, %d);", SteamID, Cooldown[data][0], Cooldown[data][1], Cooldown[data][2]);
91 gDB_Daily.Query(SQL_FirstTime, Query);
92 } else {
93 // client already joined once
94 for(int i = 1; i < sizeof(Cooldown[]); i++)
95 Cooldown[data][i] = SQL_FetchInt(results, i);
96 }
97 }
98}
99
100void SQL_FirstTime(Database db, DBResultSet results, const char[] error, any data) {
101 if(!db) {
102 // failed to create field for client
103 PrintToServer("Failed to load client \"%N\", SQL Error : %s", data, error);
104 for(int i = 0; i < sizeof(Cooldown[]); i++)
105 Cooldown[data][i] = -1;
106 } else {
107 // created field for client
108 PrintToServer("First Time %N Connects", data);
109 int iNow = GetTime();
110 for(int i = 0; i < sizeof(Cooldown[]); i++)
111 Cooldown[data][i] = iNow;
112 }
113}
114
115// SAVE DATA ************************************************************************************************************************************
116public void OnClientDisconnect(int client) {
117 char Query[512], SteamID[32];
118 GetClientAuthId(client, AuthId_Steam2, SteamID, sizeof(SteamID));
119 Format(Query, sizeof(Query), "UPDATE case_Daily SET credit1 = %d, credit2 = %d , credit3 = %d WHERE steamid = \'%s\';", Cooldown[client][0], Cooldown[client][1], Cooldown[client][2], SteamID);
120 gDB_Daily.Query(SQL_SaveData, Query);
121}
122
123void SQL_SaveData(Database db, DBResultSet results, const char[] error, any data) {
124 if(!db) {
125 // Failed to save player
126 PrintToServer("Failed to save client \"%N\", SQL Error : %s", data, error);
127 } else {
128 PrintToServer("Saved %N data!", data);
129 }
130}