· 8 years ago · Mar 06, 2018, 12:42 PM
1#pragma semicolon 1
2#pragma newdecls required
3
4#include <sourcemod>
5
6Database g_dDatabase = null;
7
8ConVar g_cEntry = null;
9
10int g_iCreateTables = -1;
11
12enum ePolls
13{
14 eID = 0,
15 bool:eStatus,
16 eCreated,
17 eExipre,
18 String:eTitle[64]
19};
20
21int g_iPolls[ePolls];
22ArrayList g_aPolls = null;
23
24enum eOptions
25{
26 eID = 0,
27 ePoll,
28 String:eOption[32]
29}
30
31int g_iOptions[eOptions];
32ArrayList g_aOptions = null;
33
34public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
35{
36 RegPluginLibrary("mvotes");
37
38 return APLRes_Success;
39}
40
41public Plugin myinfo =
42{
43 name = "MySQL Votes",
44 author = "Bara",
45 description = "",
46 version = "1.0.0",
47 url = "github.com/Bara"
48};
49
50public void OnPluginStart()
51{
52 g_cEntry = CreateConVar("mvotes_database_entry", "mvotes", "Name for the ttt entry in your databases.cfg");
53}
54
55public void OnConfigsExecuted()
56{
57 char sEntry[32];
58 g_cEntry.GetString(sEntry, sizeof(sEntry));
59
60 if (!SQL_CheckConfig(sEntry))
61 {
62 SetFailState("[MVotes] (OnConfigsExecuted) We can't find the entry \"%s\" in your databases.cfg", sEntry);
63 return;
64 }
65
66 Database.Connect(sqlConnect, sEntry);
67}
68
69public void sqlConnect(Database db, const char[] error, any data)
70{
71 if(db == null || strlen(error) > 0)
72 {
73 SetFailState("[MVotes] (sqlConnect) Database handle is invalid! (Error: %s)", error);
74 return;
75 }
76
77 g_dDatabase = db;
78
79 LogMessage("[MVotes] (sqlConnect) Connection was successful!");
80
81 g_dDatabase.SetCharset("utf8mb4");
82
83 CreateTables();
84}
85
86void CreateTables()
87{
88 g_iCreateTables = 0;
89
90 char sPolls[] = "CREATE TABLE IF NOT EXISTS `mvotes_polls` \
91 ( \
92 `id` int(11) NOT NULL AUTO_INCREMENT, \
93 `status` tinyint(1) NOT NULL, \
94 `title` varchar(64) NOT NULL, \
95 `created` int(11) NOT NULL, \
96 `expire` int(11) NOT NULL, \
97 `ip` varchar(18) NOT NULL, \
98 `port` int(6) NOT NULL, \
99 PRIMARY KEY (`id`) \
100 ) ENGINE=InnoDB CHARSET=utf8mb4;";
101 g_dDatabase.Query(sqlCreateTables, sPolls, StringToInt("mvotes_polls"));
102
103 char sOptions[] = "CREATE TABLE IF NOT EXISTS `mvotes_options` \
104 ( \
105 `id` int(11) NOT NULL AUTO_INCREMENT, \
106 `poll` int(11) NOT NULL, \
107 `option` varchar(32) NOT NULL, \
108 PRIMARY KEY (`id`) \
109 ) ENGINE=InnoDB CHARSET=utf8mb4;";
110 g_dDatabase.Query(sqlCreateTables, sOptions, StringToInt("mvotes_options"));
111
112 char sVotes[] = "CREATE TABLE IF NOT EXISTS `mvotes_votes` \
113 ( \
114 `id` int(11) NOT NULL AUTO_INCREMENT, \
115 `time` int(11) NOT NULL, \
116 `poll` int(11) NOT NULL, \
117 `option` int(11) NOT NULL, \
118 `communityid` varchar(24) NOT NULL, \
119 `name` varchar(128) NOT NULL, \
120 PRIMARY KEY (`id`) \
121 ) ENGINE=InnoDB CHARSET=utf8mb4;";
122 g_dDatabase.Query(sqlCreateTables, sVotes, StringToInt("mvotes_votes"));
123}
124
125public void sqlCreateTables(Database db, DBResultSet results, const char[] error, any data)
126{
127 if(db == null || strlen(error) > 0)
128 {
129 char sBuffer[16];
130 IntToString(data, sBuffer, sizeof(sBuffer));
131 SetFailState("[MVotes] (sqlCreateTables) Query (%s) failed: %s", sBuffer, error);
132 return;
133 }
134 else
135 {
136 g_iCreateTables++;
137
138 if (g_iCreateTables == 3)
139 {
140 LoadPollsAndOptions();
141 }
142 }
143}
144
145void LoadPollsAndOptions()
146{
147 if (g_aPolls != null)
148 {
149 g_aPolls.Clear();
150 }
151
152 if (g_aOptions != null)
153 {
154 g_aOptions.Clear();
155 }
156
157 g_aOptions = new ArrayList(sizeof(g_iOptions));
158 g_aPolls = new ArrayList(sizeof(g_iPolls));
159
160 char sPolls[] = "SELECT id, status, title, created, expire FROM mvotes_polls WHERE status == 1;";
161
162 g_dDatabase.Query(sqlLoadPolls, sPolls);
163}
164
165public void sqlLoadPolls(Database db, DBResultSet results, const char[] error, any data)
166{
167 if(db == null || strlen(error) > 0)
168 {
169 SetFailState("[MVotes] (sqlLoadPolls) Query failed: %s", error);
170 return;
171 }
172 else
173 {
174 if (results.RowCount > 0 && results.FetchRow())
175 {
176 int iPoll = results.FetchInt(0);
177 bool bStatus = view_as<bool>(results.FetchInt(1));
178
179 char sTitle[64];
180 results.FetchString(2, sTitle, sizeof(sTitle));
181
182 int iCreated = results.FetchInt(3);
183 int iExpire = results.FetchInt(4);
184
185 if (iExpire < GetTime())
186 {
187 bStatus = false;
188 UpdatePollStatus(iPoll);
189 }
190
191 if (bStatus)
192 {
193 int iPolls[ePolls];
194
195 iPolls[eID] = iPoll;
196 iPolls[eStatus] = bStatus;
197 iPolls[eCreated] = iCreated;
198 iPolls[eExipre] = iExpire;
199 Format(iPolls[eTitle], sizeof(sTitle), sTitle);
200
201 g_aPolls.PushArray(iPolls[0]);
202
203 char sOptions[128];
204 Format(sOptions, sizeof(sOptions), "SELECT id, poll, option FROM mvotes_options WHERE poll = %d;", iPoll);
205 g_dDatabase.Query(sqlLoadOptions, sOptions);
206 }
207 }
208 }
209}
210
211public void sqlLoadOptions(Database db, DBResultSet results, const char[] error, any data)
212{
213 if(db == null || strlen(error) > 0)
214 {
215 SetFailState("[MVotes] (sqlLoadOptions) Query failed: %s", error);
216 return;
217 }
218 else
219 {
220 if (results.RowCount > 0 && results.FetchRow())
221 {
222 int iOptionID = results.FetchInt(0);
223 int iPoll = results.FetchInt(1);
224
225 char sOption[64];
226 results.FetchString(2, sOption, sizeof(sOption));
227
228 int iOption[ePolls];
229
230 iOption[eID] = iOptionID;
231 iOption[ePoll] = iPoll;
232 Format(iOption[eOption], sizeof(sOption), sOption);
233
234 g_aOptions.PushArray(iOption[0]);
235 }
236 }
237}
238
239void UpdatePollStatus(int poll)
240{
241 char sUpdate[128];
242 Format(sUpdate, sizeof(sUpdate), "UPDATE mvotes_polls SET status = 0 WHERE id = %d;", poll);
243 g_dDatabase.Query(sqlUpdatePollStatus, sUpdate);
244}
245
246public void sqlUpdatePollStatus(Database db, DBResultSet results, const char[] error, any data)
247{
248 if(db == null || strlen(error) > 0)
249 {
250 SetFailState("[MVotes] (sqlUpdatePollStatus) Query failed: %s", error);
251 return;
252 }
253}