· 5 years ago · Dec 02, 2020, 09:12 AM
1#include <amxmodx>
2#include <amxmisc>
3#include <sqlx>
4#include <Hamsandwich>
5
6#pragma semicolon 1
7
8new g_iMax;
9
10new const TableName[] = "HlmodMusic_table";
11
12enum _:Music_prop{
13 eMusicName[64],
14 eMusicFile[64],
15};
16
17new g_iLoadedMusic;
18new iKey;
19new Array:g_aReadFile;
20new Array:g_aPlayMusic[33];
21new g_iMaxPlayers;
22new g_sSteamId[64];
23new Handle:SQLtuple;
24new g_cPrefix;
25
26public plugin_init()
27{
28 register_plugin("MVPZene", "0.1.0", "Turán*");
29
30 /*Kiszolgáló | Felhasználó | Jelszó | Adatbázis neve*/
31 SQLtuple = SQL_MakeDbTuple("db.synhosting.eu", "farkasroland1238", "Batman2001", "farkasroland1238");
32
33 register_logevent("PlayMusic", 2, "1=Round_End");
34 register_clcmd("say /zene", "p_musicmenu");
35
36 g_cPrefix = register_cvar("ServerPrefix", "[Sex+18]");//Ide ajánlom [], || jeleket használni mert chatnél csak sima %s van!
37
38 g_iMaxPlayers = get_maxplayers();
39
40 for(new i = 1; i <= g_iMaxPlayers; i++)
41 g_aPlayMusic[i] = ArrayCreate(1);
42
43 p_SQLCreateTable();
44}
45public get_prefix()
46{
47 new sPrefix[64];
48 get_pcvar_string(g_cPrefix, sPrefix, charsmax(sPrefix));
49 return sPrefix;
50}
51public p_musicmenu(id)
52{
53 if(!g_iLoadedMusic)
54 return;
55 new iMenu, iRow[7], sActive[64], eData[Music_prop];
56 iMenu = menu_create(fmt("\r%s \w~ \dZenekészlet", get_prefix()), "p_musicmenu_handler");
57 for(new i = 0; i < g_iLoadedMusic; i++)
58 {
59 ArrayGetArray(g_aReadFile, i, eData);
60 formatex(sActive, charsmax(sActive), ArrayFindValue(g_aPlayMusic[id], i) != -1 ? "\wAktiv":"\dNem aktiv");
61 num_to_str(i, iRow, charsmax(iRow));
62 menu_additem(iMenu, fmt("\rZene:\y %s\r - %s", eData[eMusicName], sActive), iRow, 0);
63 }
64
65 menu_setprop(iMenu, MPROP_EXITNAME, fmt("Kilépés"));
66 menu_display(id, iMenu);
67}
68public p_musicmenu_handler(id, iMenu, iMenuItem)
69{
70 if(iMenuItem == MENU_EXIT)
71 {
72 menu_destroy(iMenu);
73 return PLUGIN_HANDLED;
74 }
75
76 new sData[9], sName[MAX_NAME_LENGTH], iAccess, iCallback;
77 menu_item_getinfo(iMenu, iMenuItem, iAccess, sData, charsmax(sData), sName, charsmax(sName), iCallback);
78 iKey = str_to_num(sData);
79
80 new iFindValue = ArrayFindValue(g_aPlayMusic[id], iKey);
81 new eData[Music_prop];
82 ArrayGetArray(g_aReadFile, iKey, eData);
83
84 if(iFindValue != -1)
85 {
86 ArrayDeleteItem(g_aPlayMusic[id], iFindValue);
87 client_print_color(id, print_team_default, "^4%s^3 ~ ^1Sikeresen kikapcsoltad a(z)^3 %s^1 zenét.", get_prefix(), eData[eMusicName]);
88 p_SQLUpdate(id);p_musicmenu(id);
89 }
90 else
91 {
92 ArrayPushCell(g_aPlayMusic[id], iKey);
93 client_print_color(id, print_team_default, "^4%s^3 ~ ^1Sikeresen bekapcsoltad a(z)^3 %s^1 zenét.", get_prefix(), eData[eMusicName]);
94 p_SQLUpdate(id);p_musicmenu(id);
95 }
96
97 menu_destroy(iMenu);
98 return PLUGIN_HANDLED;
99}
100public PlayMusic()
101{
102 if(!g_iLoadedMusic)
103 return;
104
105 for(new i = 1; i < g_iMaxPlayers; i++)
106 p_startmusic(i);
107}
108public p_startmusic(id)
109{
110 new iArraySize = ArraySize(g_aPlayMusic[id]);
111 static eData[Music_prop];
112
113 if(!iArraySize || !g_iLoadedMusic)
114 return;
115
116 new iRandomMusic = random(iArraySize);
117 iRandomMusic = ArrayGetCell(g_aPlayMusic[id], iRandomMusic);
118
119 ArrayGetArray(g_aReadFile, iRandomMusic, eData);
120 client_cmd(id, "mp3 play sound/%s", eData[eMusicFile]);
121 client_print_color(id, print_team_default, "^4%s^3 ~^1 A zene cime:^3 %s", get_prefix(), eData[eMusicName]);
122}
123p_SQLCreateTable()
124{
125 static sQuery[512];
126 new iLen;
127
128 iLen += formatex(sQuery[iLen], charsmax(sQuery), "CREATE TABLE IF NOT EXISTS `%s` ", TableName);
129 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "( ");
130 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "`Id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, ");
131
132 for(new i = 0; i < g_iLoadedMusic; i++)
133 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "`Zene_%d` INT(11) NOT NULL,", i);
134
135 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "`MaxNumber` INT(11) NOT NULL,");
136 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "`SteamID` varchar(64) NOT NULL)");
137 SQL_ThreadQuery(SQLtuple, "p_SQLCreateTableThr", sQuery);
138}
139public p_SQLCreateTableThr(iFailState, Handle:sQuery, sError[], iErrcode, sData[])
140{
141 if(iFailState == TQUERY_CONNECT_FAILED)
142 set_fail_state("*DEBUG* [CreateTable] Nem sikerult csatlakozni az adatbazishoz.");
143 else if(iFailState == TQUERY_QUERY_FAILED)
144 set_fail_state("*DEBUG* [CreateTable] Lekerdezesi hiba");
145 if(iErrcode)
146 log_amx("*DEBUG* [CreateTable] - %s", sError);
147}
148public p_SQLLoadData(id)
149{
150 static sQuery[512];
151 new sData[2];
152
153 sData[0] = id;
154 sData[1] = get_user_userid(id);
155 get_user_authid(id, g_sSteamId, charsmax(g_sSteamId));
156
157 formatex(sQuery, charsmax(sQuery), "SELECT * FROM `%s` WHERE `SteamID`='%s';", TableName, g_sSteamId);
158 SQL_ThreadQuery(SQLtuple, "p_SQLLoadDatathr", sQuery, sData, 2);
159}
160public p_SQLLoadDatathr(iFailState, Handle:sQuery, sError[], iErrcode, sData[], iDataSize)
161{
162 if(iFailState == TQUERY_CONNECT_FAILED)
163 set_fail_state("*DEBUG* [LoadCheck] Nem sikerult csatlakozni az adatbazishoz.");
164 else if(iFailState == TQUERY_QUERY_FAILED)
165 set_fail_state("*DEBUG* [LoadCheck] Lekerdezesi hiba");
166 if(iErrcode)
167 log_amx("*DEBUG* [LoadCheck] - %s", sError);
168
169 new id = sData[0];
170
171 if(sData[1] != get_user_userid(id)) return;
172
173 new iRowsFound = SQL_NumRows(sQuery);
174 new iReadResult;
175
176 if(iRowsFound > 0)
177 {
178 for(new i = 0; i < g_iLoadedMusic; i++)
179 {
180 new sText[64];
181 formatex(sText, charsmax(sText), "Zene_%d", i);
182 iReadResult = SQL_ReadResult(sQuery, SQL_FieldNameToNum(sQuery, sText));
183
184 if(iReadResult)
185 ArrayPushCell(g_aPlayMusic[id], i);
186 }
187 }
188 else
189 {
190 p_SQLinsertTable(id);
191 p_SQLLoadData(id);
192 }
193}
194public p_SQLinsertTable(id)
195{
196 static sQuery[512];
197 new sData[2];
198
199 sData[0] = id;
200 sData[1] = get_user_userid(id);
201 get_user_authid(id, g_sSteamId, charsmax(g_sSteamId));
202
203 formatex(sQuery, charsmax(sQuery), "INSERT INTO `%s` (`SteamID`) VALUES('%s')", TableName, g_sSteamId);
204 SQL_ThreadQuery(SQLtuple, "p_SQLinsertTablethr", sQuery, sData, 2);
205}
206public p_SQLinsertTablethr(iFailState, Handle:sQuery, sError[], iErrcode, sData[], iDataSize)
207{
208 if(iFailState == TQUERY_CONNECT_FAILED || iFailState == TQUERY_QUERY_FAILED)
209 {
210 log_amx("*DEBUG* [Insert] - %s", sError);
211 return;
212 }
213 new id = sData[0];
214
215 if(sData[1] != get_user_userid(id)) return;
216}
217public p_SQLUpdate(id)
218{
219 static sQuery[512];
220 new sData[2], iLen;
221
222 sData[0] = id;
223 sData[1] = get_user_userid(id);
224 get_user_authid(id, g_sSteamId, charsmax(g_sSteamId));
225
226 iLen += formatex(sQuery[iLen], charsmax(sQuery), "UPDATE `%s` SET ", TableName);
227 new iFindValue = ArrayFindValue(g_aPlayMusic[id], iKey);
228
229 if(iFindValue != -1)
230 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "Zene_%d = '%i',", iKey, 1);
231 else
232 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "Zene_%d = '%i',", iKey, 0);
233
234 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "MaxNumber = '%i' WHERE `SteamID` = ^"%s^";", g_iMax, g_sSteamId);
235
236 SQL_ThreadQuery(SQLtuple, "p_SQLUpdatethr", sQuery, sData, 2);
237}
238public p_SQLUpdatethr(iFailState, Handle:sQuery, sError[], iErrcode, sData[], iDataSize)
239{
240 if(iFailState == TQUERY_CONNECT_FAILED || iFailState == TQUERY_QUERY_FAILED)
241 {
242 log_amx("*DEBUG* [Update] - %s", sError);return;
243 }
244 new id = sData[0];
245 if(sData[1] != get_user_userid(id)) return;
246}
247public client_authorized(id)
248{
249 if(is_user_bot(id))
250 return;
251
252 p_SQLLoadData(id);
253}
254public client_disconnected(id)
255{
256 if(is_user_bot(id))
257 return;
258
259 ArrayDestroy(g_aPlayMusic[id]);
260 ArrayDestroy(g_aReadFile);
261}
262public plugin_precache()
263{
264 g_aReadFile = ArrayCreate(Music_prop);
265
266 new sPath[256];
267 get_configsdir(sPath, charsmax(sPath));
268 format(sPath, charsmax(sPath), "%s/Zene.ini", sPath);
269
270 if(file_exists(sPath))
271 {
272 new sLineData[256], iLine, sData[64], sData2[64];
273 new iFile = fopen(sPath, "rt");
274
275 if(!iFile)
276 return;
277
278 while(!feof(iFile))
279 {
280 fgets(iFile, sLineData, charsmax(sLineData));
281 replace(sLineData, charsmax(sLineData), "^n", "");
282
283 if(sLineData[0] == ';' || !sLineData[0])
284 continue;
285
286 parse(sLineData, sData, charsmax(sData), sData2, charsmax(sData2));
287 remove_quotes(sData);
288 remove_quotes(sData2);
289
290 static eData[Music_prop];
291 copy(eData[eMusicName], sizeof(eData[eMusicName]) - 1, sData);
292 copy(eData[eMusicFile], sizeof(eData[eMusicFile]) - 1, sData2);
293
294 ArrayPushArray(g_aReadFile, eData);
295
296 precache_sound(eData[eMusicFile]);
297 iLine++;
298 }
299 fclose(iFile);
300 g_iLoadedMusic = ArraySize(g_aReadFile);
301 }
302}
303public plugin_end()
304{
305 for(new i = 1; i <= g_iMaxPlayers; i++)
306 ArrayDestroy(g_aPlayMusic[i]);
307
308 ArrayDestroy(g_aReadFile);
309}
310