Advertisement
Guest User

Ladivar's music system

a guest
Mar 16th, 2017
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.34 KB | None | 0 0
  1. //Music System by Montana™
  2. #include <a_samp>
  3. #include <a_mysql>
  4. #include <foreach>
  5. #include <[MV]_Youtube>
  6. #include <zcmd>
  7.  
  8. //Connect to host
  9. #define MYSQL_HOST "localhost"
  10. #define MYSQL_USER "root"
  11. #define MYSQL_DATABASE "sa-mp"
  12. #define MYSQL_PASSWORD ""
  13.  
  14. #define COLOR_ORANGE 0xFFA100FF
  15.  
  16. #define MusicDialog 1050
  17. #define PlaySong 1051
  18. #define AddSongLink 1052
  19. #define AddSongName 1053
  20. #define SongList 1054
  21. #define RemoveSong 1055
  22.  
  23. #define MAX_SONGS 1250
  24.  
  25. new mysql;
  26.  
  27. new PlayerSongName[MAX_PLAYERS][256], PlayerSongLink[MAX_PLAYERS][256];
  28. new PlayerSongSelect[MAX_PLAYERS][MAX_SONGS];
  29.  
  30. enum mInfo
  31. {
  32. ID,
  33. PlayerName[24],
  34. SongUsed,
  35. SongName[256],
  36. SongLink[256]
  37. }
  38. new MusicInfo[MAX_SONGS][mInfo];
  39.  
  40.  
  41. COMMAND:music(playerid, params[])
  42. {
  43. ShowPlayerDialog(playerid, MusicDialog, DIALOG_STYLE_LIST, "Music System", "Play Song\nMy Music\nAdd Music\nRemove Music\nStop Music", "Continue", "Cancel");
  44. return 1;
  45. }
  46.  
  47. public OnFilterScriptInit()
  48. {
  49. mysql_log(LOG_ERROR | LOG_WARNING | LOG_DEBUG);
  50. mysql = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DATABASE, MYSQL_PASSWORD);
  51. if(mysql_errno() != 0) // checks if the server failed to connected or not
  52. {
  53. print("[MYSQL]: Failed to connect to MySQL Database");
  54. }
  55. else
  56. {
  57. print("I have connected to the MySQL server.");
  58. }
  59. LoadMusic();
  60. return 1;
  61. }
  62.  
  63. public OnPlayerDisconnect(playerid)
  64. {
  65. return 1;
  66. }
  67.  
  68. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  69. {
  70. switch(dialogid)
  71. {
  72. case MusicDialog:
  73. {
  74. if(!response)
  75. {
  76. SendClientMessage(playerid, 0x42F3F198, "Music System closed.");
  77. return 1;
  78. }
  79. switch(listitem)
  80. {
  81. case 0:
  82. {
  83. ShowPlayerDialog(playerid, PlaySong, DIALOG_STYLE_INPUT, "Youtube link:", "Please enter the youtube link you wish to play example:\nhttps://www.youtube.com/watch?v=7csKaIhn8NY - Eminem - Legacy.", "Done", "Cancel");
  84. }
  85. case 1:
  86. {
  87. GetPlayerMusic(playerid);
  88. }
  89. case 2:
  90. {
  91. ShowPlayerDialog(playerid, AddSongLink, DIALOG_STYLE_INPUT, "Youtube link:", "Please enter the youtube link you would like to save for example:\nhttps://www.youtube.com/watch?v=7csKaIhn8NY - Eminem - Legacy.", "Done", "Cancel");
  92. }
  93. case 3:
  94. {
  95. GetPlayerMusicToDelete(playerid);
  96. }
  97. case 4:
  98. {
  99. StopYoutubeVideoForPlayer(playerid);
  100. }
  101. }
  102. }
  103. case PlaySong:
  104. {
  105. if(!response)
  106. {
  107. SendClientMessage(playerid, 0x42F3F198, "Music System closed.");
  108. return 1;
  109. }
  110. if(strfind(inputtext,"www.youtube.com",true) == -1) return SendClientMessage(playerid, 0x969696FF, "Error: Youtube links only please, if it doesn't work try an other."), ShowPlayerDialog(playerid, PlaySong, DIALOG_STYLE_INPUT, "Youtube link:", "Please enter the youtube link you would like to save for example:\nhttps://www.youtube.com/watch?v=7csKaIhn8NY - Eminem - Legacy.", "Done", "Cancel");
  111. PlayYoutubeVideoForPlayer(playerid, inputtext);
  112. }
  113. case AddSongLink:
  114. {
  115. if(!response)
  116. {
  117. SendClientMessage(playerid, 0x42F3F198, "Music System closed.");
  118. return 1;
  119. }
  120. if(strfind(inputtext,"www.youtube.com",true) == -1) return SendClientMessage(playerid, 0x969696FF, "Error: Youtube links only please, if it doesn't work try an other."), ShowPlayerDialog(playerid, AddSongLink, DIALOG_STYLE_INPUT, "Youtube link:", "Please enter the youtube link you would like to save for example:\nhttps://www.youtube.com/watch?v=7csKaIhn8NY - Eminem - Legacy.", "Done", "Cancel");
  121. format(PlayerSongLink[playerid], 256, inputtext);
  122. PlayYoutubeVideoForPlayer(playerid, inputtext);
  123. SendClientMessage(playerid, COLOR_ORANGE, "System: Testing the song, is this the right one and is it working? If not cancel it and try an other link.");
  124. ShowPlayerDialog(playerid, AddSongName, DIALOG_STYLE_INPUT, "Song or Artist Name:", "Please enter the song or artist name or both for example:\nHopsin - All your fault.", "Done", "Cancel");
  125. }
  126. case AddSongName:
  127. {
  128. if(!response)
  129. {
  130. SendClientMessage(playerid, 0x42F3F198, "Music System closed.");
  131. return 1;
  132. }
  133. if(strlen(inputtext) == 0 || strlen(inputtext) > 60)
  134. {
  135. SendClientMessage(playerid, 0x969696FF, "Invalid song name or artist, Length must be greater than 1 and less than 60.");
  136. ShowPlayerDialog(playerid, AddSongName, DIALOG_STYLE_INPUT, "Song or Artist Name:", "Please enter the song or artist name or both for example:\nHopsin - All your fault.", "Done", "Cancel");
  137. }
  138. format(PlayerSongName[playerid], 256, inputtext);
  139. SendClientMessage(playerid, COLOR_ORANGE, "System: We are now saving the song into our database, You can play it any time. /music then My Music.");
  140. new pname[24];
  141. GetPlayerName(playerid, pname, 24);
  142. new query[256];
  143. mysql_format(mysql, query, sizeof(query), "INSERT INTO `Music` (`Name`, `SongUsed`, `SongName`, `SongLink`) VALUES ('%e', 1, '%s', '%s')", pname, PlayerSongName[playerid], PlayerSongLink[playerid]);
  144. mysql_query(mysql, query);
  145. new str[256];
  146. format(str, sizeof(str), "System: You have saved youtube video %s into your music.", GetVideoTitle(playerid));
  147. SendClientMessage(playerid, COLOR_ORANGE, str);
  148. LoadMusic();
  149. }
  150. case SongList:
  151. {
  152. if(!response)
  153. {
  154. SendClientMessage(playerid, 0x42F3F198, "Music System closed.");
  155. return 1;
  156. }
  157. PlayYoutubeVideoForPlayer(playerid, MusicInfo[PlayerSongSelect[playerid][listitem]][SongLink]);
  158. LoadMusicLink(MusicInfo[PlayerSongSelect[playerid][listitem]][ID]);
  159. }
  160. case RemoveSong:
  161. {
  162. if(!response)
  163. {
  164. SendClientMessage(playerid, 0x42F3F198, "Music System closed.");
  165. return 1;
  166. }
  167. RemoveMusic(MusicInfo[PlayerSongSelect[playerid][listitem]][ID]);
  168. }
  169. }
  170. return 1;
  171. }
  172.  
  173. stock GetMusic()
  174. {
  175. for(new i = 1; i < MAX_SONGS;i++)
  176. {
  177. if(MusicInfo[i][SongUsed] == 0)
  178. {
  179. return i;
  180. }
  181. }
  182. return -1;
  183. }
  184.  
  185. stock RemoveMusic(id)
  186. {
  187. new query[128];
  188. mysql_format(mysql, query, sizeof(query), "DELETE FROM `Music` WHERE `ID`= %d", id);
  189. mysql_pquery(mysql, query);
  190. MusicInfo[id][SongUsed] = 0;
  191. }
  192.  
  193. stock GetPlayerMusic(playerid)
  194. {
  195. new sendername[24];
  196. GetPlayerName(playerid, sendername, 24);
  197. new string[956];
  198. //new str[956];
  199. new combine[956];
  200. new found = 0;
  201. new item = 0;
  202. for(new i = 0; i < MAX_SONGS; i++)
  203. {
  204. if(MusicInfo[i][SongUsed] == 1)
  205. {
  206. if(strcmp(MusicInfo[i][PlayerName], sendername, true) == 0)
  207. {
  208. format(string, sizeof (string), "%s %s\n", string, MusicInfo[i][SongName]);
  209. format(combine, sizeof(combine), "%s", string);
  210. found = 1;
  211. PlayerSongSelect[playerid][item] = i;
  212. item = item + 1;
  213. }
  214. }
  215. }
  216. if(found == 0) return SendClientMessage(playerid, 0x969696FF, "Error: You haven't saved any music into the database yet.");
  217. ShowPlayerDialog(playerid, SongList, DIALOG_STYLE_LIST, "Music Player", combine, "Play", "Cancel");
  218. return 1;
  219. }
  220.  
  221. stock GetPlayerMusicToDelete(playerid)
  222. {
  223. new sendername[24];
  224. GetPlayerName(playerid, sendername, 24);
  225. new string[956];
  226. //new str[956];
  227. new combine[956];
  228. new found = 0;
  229. new item = 0;
  230. for(new i = 0; i < MAX_SONGS; i++)
  231. {
  232. if(MusicInfo[i][SongUsed] == 1)
  233. {
  234. if(strcmp(MusicInfo[i][PlayerName], sendername, true) == 0)
  235. {
  236. format(string, sizeof (string), "%s %s\n", string, MusicInfo[i][SongName]);
  237. format(combine, sizeof(combine), "%s", string);
  238. found = 1;
  239. PlayerSongSelect[playerid][item] = i;
  240. item = item + 1;
  241. }
  242. }
  243. }
  244. if(found == 0) return SendClientMessage(playerid, 0x969696FF, "Error: You haven't saved any music into the database yet.");
  245. ShowPlayerDialog(playerid, RemoveSong, DIALOG_STYLE_LIST, "Music Player", combine, "Play", "Cancel");
  246. return 1;
  247. }
  248.  
  249. stock LoadMusic()
  250. {
  251. new query[256];
  252. for(new i = 1; i < MAX_SONGS; i++)
  253. {
  254. format(query, sizeof(query), "SELECT * FROM Music WHERE ID=%i", i);
  255. mysql_function_query(mysql, query, true, "LoadMusicSQL", "d", i);
  256. }
  257. return 1;
  258. }
  259.  
  260. forward LoadMusicSQL(id);
  261. public LoadMusicSQL(id)
  262. {
  263. new fields, rows;
  264. cache_get_data(rows, fields);
  265. if(rows)
  266. {
  267. if(MusicInfo[id][SongUsed] == 0)
  268. {
  269. new fetch[256];
  270. cache_get_field_content(0, "ID", fetch);
  271. MusicInfo[id][ID]= strval(fetch);
  272. cache_get_field_content(0, "Name", fetch);
  273. format(MusicInfo[id][PlayerName], 24, fetch);
  274. cache_get_field_content(0, "SongName", fetch);
  275. format(MusicInfo[id][SongName], 256, fetch);
  276. cache_get_field_content(0, "SongLink", fetch);
  277. format(MusicInfo[id][SongLink], 256, fetch);
  278. cache_get_field_content(0, "SongUsed", fetch);
  279. MusicInfo[id][SongUsed]= strval(fetch);
  280. print("I have loaded a song.");
  281. }
  282. }
  283. return 1;
  284. }
  285.  
  286. LoadMusicLink(id)
  287. {
  288. new query[256];
  289. format(query, sizeof(query), "SELECT * FROM Music WHERE ID=%i", id);
  290. mysql_function_query(mysql, query, true, "ReLoadSongLinkSQL", "d", id);
  291. }
  292.  
  293. forward ReLoadSongLinkSQL(id);
  294. public ReLoadSongLinkSQL(id)
  295. {
  296. new fields, rows;
  297. cache_get_data(rows, fields);
  298. if(rows)
  299. {
  300. if(MusicInfo[id][SongUsed] == 1)
  301. {
  302. new fetch[256];
  303. cache_get_field_content(0, "SongLink", fetch);
  304. format(MusicInfo[id][SongLink], 256, fetch);
  305. }
  306. }
  307. return 1;
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement