Alternative112

Dynamic Radio v1

Jul 1st, 2013
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. /*
  2.  
  3. Dynamic Radio Filterscript
  4. By Alternative112
  5.  
  6. DO NOT re-release without asking me!
  7.  
  8. Notes:
  9.  
  10. -The max station name in your .txt should be no higher than 40! It will make it format better in the dialog menu.
  11. */
  12.  
  13. #include <a_samp>
  14. #include <zcmd>
  15. #include <sscanf2>
  16.  
  17. #define MAX_GENRE_SIZE 20 //All your genres in the .txt should be lower in length than this.
  18. #define MAX_URL_SIZE 80
  19. #define MAX_STATIONS 20 //Change this to the amount of stations you have in your txt! I wish I could make this dynamic but PAWN needs to know how large arrays are prior to compilation
  20.  
  21. #define Public:%0(%1) forward %0(%1);\
  22. public %0(%1)
  23.  
  24. #define strequal(%0,%1)\
  25. !strcmp(%0, %1, true)
  26.  
  27. new customStream[MAX_PLAYERS][64]; //This is the URL of the custom stream that the player added himself.
  28. new currStreamUrl[MAX_PLAYERS][64]; //What the player is currently playing - used so we can restart the stream correctly with restartRadio(playerid)
  29. new bool:isListening[MAX_PLAYERS char];
  30.  
  31. enum
  32. info
  33. {
  34. stationName[40],
  35. url[MAX_URL_SIZE],
  36. genre[MAX_GENRE_SIZE],
  37. };
  38.  
  39. new stationInfo[MAX_STATIONS][info];
  40.  
  41. Public:playTimedStream(playerid, stream[], time, x, y, z, distance, usePos) {
  42. if (isListening{playerid} && IsPlayerConnected(playerid)) {
  43. StopAudioStreamForPlayer(playerid);
  44. PlayAudioStreamForPlayer(playerid, stream, x, y, z, distance, usePos);
  45.  
  46. isListening{playerid} = false;
  47.  
  48. if (time != 9999) {
  49. SetTimerEx("restartRadio", time, false, "i", playerid);
  50. }
  51. }
  52. return 1;
  53. }
  54.  
  55. Public:restartRadio(playerid) {
  56. if (!isListening{playerid} && IsPlayerConnected(playerid)) {
  57. PlayAudioStreamForPlayer(playerid, currStreamUrl[playerid], 0.0, 0.0, 0.0);
  58. isListening{playerid} = true;
  59. }
  60. return 1;
  61. }
  62.  
  63. stock ShowMainRadioMenu(playerid) {
  64. new str[500];
  65.  
  66. for (new i = 0; i < MAX_STATIONS; i++) {
  67. if (strlen(stationInfo[i][genre]) > 0) {
  68. for (new j = 0; j < MAX_STATIONS; j++) {
  69. if (strlen(stationInfo[j][genre]) > 0) {
  70. if (strfind(str, stationInfo[j][genre], true) == -1) {
  71. format(str, sizeof(str), "%s\n{00FF00}>> {FFFFFF}%s", str, stationInfo[j][genre]);
  72. }
  73. }
  74. }
  75. }
  76. }
  77.  
  78. format(str, sizeof(str), "Input a URL\nPlay Custom Stream: %s\nTurn Off Radio\n%s", customStream[playerid], str);
  79. ShowPlayerDialog(playerid, 700, DIALOG_STYLE_LIST, "Select a Category / Option!", str, "Open", "Cancel");
  80. return 1;
  81. }
  82.  
  83. stock loadStations() {
  84. if (!fexist("radioStations.txt")) {
  85. print("radioStations.txt does not exist! Creating it.");
  86. new File:fhandle = fopen("radioStations.txt", io_write);
  87. fwrite(fhandle, "");
  88. fclose(fhandle);
  89. }
  90.  
  91. new File:radioTxt = fopen("radioStations.txt", io_read);
  92.  
  93. new string[126];
  94.  
  95. new i = 0;
  96. while (fread(radioTxt, string)) {
  97. printf("String read is %s", string);
  98. new formatStr[20];
  99. format(formatStr, sizeof(formatStr), "p<,>s[40]s[%i]s[%i]", MAX_URL_SIZE, MAX_GENRE_SIZE);
  100.  
  101.  
  102. if (sscanf(string, formatStr, stationInfo[i][stationName], stationInfo[i][url], stationInfo[i][genre])) {
  103. print("There was an error loading that station...");
  104. }
  105. else {
  106. format(stationInfo[i][genre], strlen(stationInfo[i][genre]), "%s", stationInfo[i][genre]); //Get rid of /r/n!
  107.  
  108. printf("Loaded station %s with URL %s [Genre: %s]", stationInfo[i][stationName], stationInfo[i][url], stationInfo[i][genre]);
  109. }
  110. i++;
  111. }
  112. fclose(radioTxt);
  113.  
  114. print("----------Results---------");
  115. for (new j = 0; j < MAX_STATIONS; j++) {
  116. printf("%s, %s, %s", stationInfo[j][stationName], stationInfo[j][url], stationInfo[j][genre]);
  117. }
  118. return 1;
  119. }
  120.  
  121. public OnFilterScriptInit() {
  122. loadStations();
  123. return 1;
  124. }
  125.  
  126. public OnFilterScriptExit() {
  127. for (new i = 0; i < GetMaxPlayers(); i++) {
  128. if (isListening{i}) {
  129. StopAudioStreamForPlayer(i);
  130. SendClientMessage(i, 0xCC0000FF, "Your radio died.");
  131. }
  132. }
  133. return 1;
  134. }
  135.  
  136. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
  137. switch(dialogid) {
  138. case 700: { //Main Menu
  139. if(response)
  140. {
  141. switch(listitem) {
  142. case 0: { //Input custom station
  143. ShowPlayerDialog(playerid, 702, DIALOG_STYLE_INPUT, "Input URL", "Type your URL here!", "Play", "Back");
  144. }
  145. case 1: { //Play custom station
  146. if (strlen(customStream[playerid]) > 5 && strlen(customStream[playerid])) {
  147. format(currStreamUrl[playerid], MAX_URL_SIZE, customStream[playerid]);
  148. PlayAudioStreamForPlayer(playerid, customStream[playerid]);
  149. isListening{playerid} = true;
  150. }
  151. ShowMainRadioMenu(playerid);
  152. }
  153. case 2: { //Turn off radio
  154. StopAudioStreamForPlayer(playerid);
  155. format(currStreamUrl[playerid], MAX_URL_SIZE, "");
  156. SendClientMessage(playerid, 0xFFFF00FF, "The radio has been turned off.");
  157. isListening{playerid} = false;
  158. }
  159. default: { //Station listing
  160. new menuTxt[(MAX_STATIONS * MAX_URL_SIZE) + 24];
  161.  
  162. format(menuTxt, sizeof(menuTxt), "{CC0000}<< {FFFFFF}Back\n");
  163.  
  164. strdel(inputtext, 0, 3); //Strip the ">> "
  165.  
  166. for (new j = 0; j < MAX_STATIONS; j++) { //Then display each radio station in that genre
  167. if (strequal(stationInfo[j][genre], inputtext) && strlen(stationInfo[j][genre]) > 0) {
  168. format(menuTxt, sizeof(menuTxt), "%s\n%s", menuTxt, stationInfo[j][stationName]);
  169. }
  170. }
  171. ShowPlayerDialog(playerid, 701, DIALOG_STYLE_LIST, "Select a Station!", menuTxt, "Play!", "Back");
  172. }
  173. }
  174. }
  175. }
  176. case 701: { //Menu 2
  177. if (listitem == 0) { //Back button
  178. ShowMainRadioMenu(playerid);
  179. }
  180. else {
  181. for (new i = 0; i < MAX_STATIONS; i++) {
  182. if (strequal(stationInfo[i][stationName], inputtext)) {
  183. if (strlen(inputtext) < MAX_URL_SIZE && strlen(inputtext) > 5) {
  184. format(currStreamUrl[playerid], MAX_URL_SIZE, stationInfo[i][url]);
  185. PlayAudioStreamForPlayer(playerid, stationInfo[i][url]);
  186. isListening{playerid} = true;
  187. }
  188. else {
  189. SendClientMessage(playerid, 0xCC0000FF, "Oops, this station is broken....");
  190. }
  191. return 1;
  192. }
  193. }
  194. }
  195. }
  196. case 702: { //Custom URL
  197. if (response) {
  198. if (strlen(inputtext) < MAX_URL_SIZE && strlen(inputtext) > 5) {
  199. format(customStream[playerid], MAX_URL_SIZE, inputtext);
  200. format(currStreamUrl[playerid], MAX_URL_SIZE, inputtext);
  201. PlayAudioStreamForPlayer(playerid, inputtext);
  202. isListening{playerid} = true;
  203. }
  204. else {
  205. ShowMainRadioMenu(playerid);
  206. new str[52];
  207. format(str, sizeof(str), "Invalid URL, must be between 5 and %i characters.", MAX_URL_SIZE);
  208. SendClientMessage(playerid, 0xCC0000FF, str);
  209. }
  210. }
  211. else {
  212. ShowMainRadioMenu(playerid);
  213. }
  214. }
  215. }
  216. return 1;
  217. }
  218.  
  219. CMD:radio(playerid, params[]) {
  220. ShowMainRadioMenu(playerid);
  221. return 1;
  222. }
  223.  
  224. CMD:reloadstations(playerid, params[]) {
  225. if (IsPlayerAdmin(playerid)) {
  226. //Clear the array out before loading
  227. for (new i = 0; i < MAX_STATIONS; i++) {
  228. format(stationInfo[i][stationName], strlen(stationInfo[i][stationName]), "");
  229. format(stationInfo[i][url], strlen(stationInfo[i][url]), "");
  230. format(stationInfo[i][genre], strlen(stationInfo[i][genre]), "");
  231. }
  232. loadStations();
  233. SendClientMessage(playerid, 0xFFFF00FF, "Stations reloaded. If something went wrong, check the logs!");
  234. }
  235. return 1;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment