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