· 8 years ago · Aug 19, 2018, 07:48 PM
1#include <sourcemod>
2
3#pragma semicolon 1
4
5Database g_hDatabase;
6
7char g_sSteamid[MAXPLAYERS + 1][32];
8
9char g_sName[MAXPLAYERS + 1][256];
10char g_sPassword[MAXPLAYERS + 1][256];
11char g_sEmail[MAXPLAYERS + 1][256];
12
13bool g_bRegister;
14
15public void OnPluginStart()
16{
17 Database.Connect(SQL_Connection, "vipsystem");
18}
19
20public void SQL_Connection(Database hDatabase, const char[] sError, int iData)
21{
22 if (hDatabase == null)
23 ThrowError(sError);
24
25 else
26 {
27 g_hDatabase = hDatabase;
28
29 g_hDatabase.Query(SQL_Error, "CREATE TABLE IF NOT EXISTS players (steam_id VARCHAR(32) NOT NULL PRIMARY KEY, account_name VARCHAR(32) NOT NULL, account_password VARCHAR(32) NOT NULL, account_email VARCHAR(32) NOT NULL)");
30 }
31}
32
33public void SQL_Error(Database hDatabase, DBResultSet hResults, const char[] sError, int iData)
34{
35 if (hResults == null)
36 ThrowError(sError);
37}
38
39public void OnClientPostAdminCheck(int iClient)
40{
41 if (IsValidClient(iClient))
42 {
43 GetClientAuthId(iClient, AuthId_Steam2, g_sSteamid[iClient], sizeof(g_sSteamid[]));
44
45 Menu hMenu = new Menu(Menu_Authorization);
46
47 hMenu.SetTitle("Choose Your Option.");
48
49 hMenu.AddItem("", "Login");
50 hMenu.AddItem("", "Register");
51
52 hMenu.Display(iClient, MENU_TIME_FOREVER);
53 }
54}
55
56public int Menu_Authorization(Menu hMenu, MenuAction hAction, int iClient, int iParam)
57{
58 switch (hAction)
59 {
60 case MenuAction_Select:
61 {
62 switch (iParam)
63 {
64 case 0:
65 {
66 char sQuery[256];
67 //Format(sQuery, sizeof(sQuery), "SELECT * FROM players WHERE steam_id = \"%s\"", g_sSteamid[iClient]);
68 Format(sQuery, sizeof(sQuery), "SELECT * FROM players WHERE steam_id = '%s'", g_sSteamid[iClient]);
69
70 g_hDatabase.Query(SQL_Authorization, sQuery, GetClientUserId(iClient));
71
72 }
73
74 case 1:
75 g_bRegister = true;
76 }
77 }
78
79 case MenuAction_End:
80 delete hMenu;
81 }
82 //char sQuery[256];
83 //SQL_TQuery(g_hDatabase, SQL_ErrorCheckCallBack, sQuery);
84}
85
86public void SQL_Authorization(Database hDatabase, DBResultSet hResults, const char[] sError, int iData)
87{
88 if (hResults == null)
89 ThrowError(sError);
90
91 int iClient = GetClientOfUserId(iData);
92
93 if (IsValidClient(iClient))
94 {
95 if (hResults.RowCount != 0)
96 {
97 hResults.FetchRow();
98
99 hResults.FetchString(1, g_sSteamid[iClient], sizeof(g_sSteamid[]));
100 hResults.FetchString(2, g_sName[iClient], sizeof(g_sName[]));
101 hResults.FetchString(3, g_sPassword[iClient], sizeof(g_sPassword[]));
102 //hResults.FetchString(4, g_sEmail[iClient], sizeof(g_sEmail[]));
103 }
104
105 else
106 PrintToChat(iClient, "Your account wasn't found! You may register.");
107 }
108 //SQL_TQuery(g_hDatabase, SQL_ErrorCheckCallBack, sQuery);
109 SQL_TQuery(g_hDatabase, SQL_ErrorCheckCallBack, hResults.FetchRow());
110}
111
112public Action OnClientSayCommand(int iClient, const char[] sCommand, const char[] sArgs)
113{
114 if (g_bRegister)
115 {
116 char sName[64], sPassword1[64], sPassword2[64], sEmail[64];
117
118 GetCmdArg(1, sName, sizeof(sName));
119 GetCmdArg(2, sPassword1, sizeof(sPassword1));
120 GetCmdArg(3, sPassword2, sizeof(sPassword2));
121 GetCmdArg(4, sEmail, sizeof(sEmail));
122
123 if (sPassword1[0] != sPassword2[0])
124 {
125 PrintToChat(iClient, "Invalid password confirmation.");
126
127 return Plugin_Handled;
128 }
129
130 char sQuery[256];
131 //Format(sQuery, sizeof(sQuery), "INSERT INTO players (steam_id, account_name, account_password, account_email) VALUES (\"%s\", \"%s\", \"%s\", \"%s\")", g_sSteamid[iClient], sName, sPassword1, sEmail);
132 Format(sQuery, sizeof(sQuery), "INSERT INTO players (steam_id, account_name, account_password, account_email) VALUES ('%s', '%s', '%s', '%s')", g_sSteamid[iClient], sName, sPassword1, sEmail);
133
134 g_hDatabase.Query(SQL_Error, sQuery);
135
136 g_bRegister = false;
137
138 return Plugin_Handled;
139 }
140
141 return Plugin_Continue;
142}
143
144public SQL_ErrorCheckCallBack(Handle:owner, Handle:hDatabase, const String:error[], any:data)
145{
146 // This is just an errorcallback for function who normally don't return any data
147 if(hDatabase == INVALID_HANDLE)
148 {
149 SetFailState("Query failed! %s", error);
150 }
151}
152
153stock bool IsValidClient(int iClient)
154{
155 if (!(0 < iClient <= MaxClients) || !IsClientInGame(iClient) || IsFakeClient(iClient))
156 return false;
157
158 return true;
159}