Advertisement
HoussamMaroc

AddMusic.inc

Aug 6th, 2017
2,697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <a_samp>
  2. #include zcmd
  3. stock NRNULL[] = "\1\0";
  4. #define NPMAX_ITEMS  500
  5. #define NPMAX_CATEGORIES  30
  6. #define NPINVALID_ITEM_ID    -1
  7. #define NPMAX_CATEGORY_LENGTH 75
  8. #define NPMAX_ITEM_LENGTH    900
  9. #define NPMAX_STREAM_URL_LENGTH   900
  10. #define NPCATEGORIES_DIALOG_ID    1553
  11. #define NPLAYER_DIALOG_ID 1552
  12.  
  13. enum nPlayerItem
  14. {
  15.     npCategoryID,
  16.     npTitle[NPMAX_ITEM_LENGTH],
  17.     npStreamURL[NPMAX_STREAM_URL_LENGTH]
  18. }
  19. new PlayerItem[NPMAX_ITEMS][nPlayerItem], nCategories[NPMAX_ITEMS][NPMAX_CATEGORY_LENGTH], nrCATEGORY_COUNT = 0, npITEM_COUNT = 0, npDIALOG_STRING[1028];
  20. /*
  21. native AddCategoryEx(category,name[]);
  22. native DeleteCategoryEx(categoryid);
  23. native AddMusic(categoryid, musicname[],musicurl[]);
  24. native AddMusicToCateg(category,musicname[],musicurl[]);
  25. native ShowDialog(playerid);
  26. */
  27. stock AddCategory(name[NPMAX_CATEGORY_LENGTH])
  28. {
  29.     new CATEGORY_ID = NPINVALID_ITEM_ID;
  30.     if(strlen(name) >= 1 && strlen(name) <= NPMAX_CATEGORY_LENGTH)
  31.     {
  32.         if(nrCATEGORY_COUNT < NPMAX_CATEGORIES)
  33.         {
  34.             for(new i=0;i<NPMAX_CATEGORIES;i++)
  35.             {
  36.                 if(!strlen(nCategories[i]))
  37.                 {
  38.                     format(nCategories[i], NPMAX_CATEGORY_LENGTH, name);
  39.                     CATEGORY_ID = i;
  40.                     nrCATEGORY_COUNT++;
  41.                     return CATEGORY_ID;
  42.                 }
  43.             }
  44.         }
  45.     }
  46.     return CATEGORY_ID;
  47. }
  48. stock AddCategoryEx(category,name[NPMAX_CATEGORY_LENGTH])
  49. {
  50.     category = AddCategory(name);
  51.     return category;
  52. }
  53.  
  54. stock DeleteCategoryEx(categoryid)
  55. {
  56.     format(nCategories[categoryid], NPMAX_CATEGORY_LENGTH, "");
  57.     for(new i=0;i<npITEM_COUNT;i++)
  58.     {
  59.         if(PlayerItem[i][npCategoryID] == categoryid && strlen(PlayerItem[i][npTitle]) >= 1)
  60.         {
  61.             format(PlayerItem[i][npTitle], NPMAX_ITEM_LENGTH, "");
  62.             npITEM_COUNT--;
  63.         }
  64.     }
  65.     nrCATEGORY_COUNT--;
  66.     return true;
  67. }
  68.  
  69. stock AddMusic(categoryid, name[NPMAX_ITEM_LENGTH], streamurl[NPMAX_STREAM_URL_LENGTH])
  70. {
  71.     new ITEM_ID = -1;
  72.     if(strlen(name) >= 1 && strlen(name) <= NPMAX_ITEM_LENGTH)
  73.     {
  74.         if(npITEM_COUNT < NPMAX_ITEMS)
  75.         {
  76.             for(new i=0;i<NPMAX_ITEMS;i++)
  77.             {
  78.                 if(!strlen(PlayerItem[i][npTitle]))
  79.                 {
  80.                     ITEM_ID = i;
  81.                     PlayerItem[i][npCategoryID] = categoryid;
  82.                     format(PlayerItem[i][npTitle], NPMAX_ITEM_LENGTH, name);
  83.                     format(PlayerItem[i][npStreamURL], NPMAX_STREAM_URL_LENGTH, streamurl);
  84.                     npITEM_COUNT++;
  85.                     return ITEM_ID;
  86.                 }
  87.             }
  88.         }
  89.     }
  90.     return ITEM_ID;
  91. }
  92.  
  93. stock ShowDialog(playerid)
  94. {
  95.     format(npDIALOG_STRING, sizeof(npDIALOG_STRING), "");
  96.     for(new i=0;i<nrCATEGORY_COUNT;i++)
  97.     {
  98.         if(!i)
  99.         {
  100.             if(strlen(nCategories[i]) >= 1) format(npDIALOG_STRING, sizeof(npDIALOG_STRING), nCategories[i]);
  101.         }
  102.         else
  103.         {
  104.             if(strlen(nCategories[i]) >= 1) format(npDIALOG_STRING, sizeof(npDIALOG_STRING), "%s\n%s", npDIALOG_STRING, nCategories[i]);
  105.         }
  106.     }
  107.     if(strlen(npDIALOG_STRING) >= 1) ShowPlayerDialog(playerid, NPCATEGORIES_DIALOG_ID, DIALOG_STYLE_LIST, "Select Category", npDIALOG_STRING, "Select", "Cancel");
  108.     else ShowPlayerDialog(playerid, 0, DIALOG_STYLE_LIST, "Select Category", "No Category Found","Select", "Cancel");
  109.     return true;
  110. }
  111.  
  112. stock StopMusic(playerid)
  113. {
  114.     StopAudioStreamForPlayer(playerid);
  115.     return true;
  116. }
  117.  
  118. stock PlayMusic(playerid, itemid)
  119. {
  120.     StopMusic(playerid);
  121.     PlayAudioStreamForPlayer(playerid, PlayerItem[itemid][npStreamURL]);
  122.     return true;
  123. }
  124.  
  125. stock ItemIDFromDialogOrder(playerid, listitem)
  126. {
  127.     new categoryid = GetPVarInt(playerid, "Category"), current_array_id, bool:breakloop = false, id = -1;
  128.     for(new i=0;i<npITEM_COUNT;i++)
  129.     {
  130.         if(PlayerItem[i][npCategoryID] == categoryid && strlen(PlayerItem[i][npTitle]) >= 1)
  131.         {
  132.             if(!breakloop)
  133.             {
  134.                 if(current_array_id == listitem)
  135.                 {
  136.                     id = i;
  137.                     breakloop = true;
  138.                 }
  139.                 current_array_id++;
  140.             }
  141.         }
  142.     }
  143.     return id;
  144. }
  145. stock CategoryIDFromDialogOrder(listitem)
  146. {
  147.     new current_array_id, bool:breakloop = false, id = -1;
  148.     for(new i=0;i<nrCATEGORY_COUNT;i++)
  149.     {
  150.         if(strlen(nCategories[i]) >= 1)
  151.         {
  152.             if(!breakloop)
  153.             {
  154.                 if(current_array_id == listitem)
  155.                 {
  156.                     id = i;
  157.                     breakloop = true;
  158.                 }
  159.                 current_array_id++;
  160.             }
  161.         }
  162.     }
  163.     return id;
  164. }
  165. stock ItemCountFromCategory(categoryid)
  166. {
  167.     new count = 0;
  168.     for(new i=0;i<npITEM_COUNT;i++)
  169.     {
  170.         if(PlayerItem[i][npCategoryID] == categoryid && strlen(PlayerItem[i][npTitle]) >= 1)
  171.         {
  172.             count++;
  173.         }
  174.     }
  175.     return count;
  176. }
  177. stock CategoryBrowse(playerid, listitem)
  178. {
  179.     format(npDIALOG_STRING, sizeof(npDIALOG_STRING), "");
  180.     new categoryid = CategoryIDFromDialogOrder(listitem);
  181.     SetPVarInt(playerid, "Category", categoryid);
  182.     if(ItemCountFromCategory(categoryid) >= 1)
  183.     {
  184.         for(new i=0;i<npITEM_COUNT;i++)
  185.         {
  186.             if(PlayerItem[i][npCategoryID] == categoryid)
  187.             {
  188.                 if(!i)
  189.                 {
  190.                     if(strlen(PlayerItem[i][npTitle]) >= 1) format(npDIALOG_STRING, sizeof(npDIALOG_STRING), PlayerItem[i][npTitle]);
  191.                 }
  192.                 else
  193.                 {
  194.                     if(strlen(PlayerItem[i][npTitle]) >= 1) format(npDIALOG_STRING, sizeof(npDIALOG_STRING), "%s\n%s", npDIALOG_STRING, PlayerItem[i][npTitle]);
  195.                 }
  196.             }
  197.         }
  198.         new dialog_title[100];
  199.         format(dialog_title, sizeof(dialog_title), "%s", nCategories[categoryid]);
  200.         if(strlen(npDIALOG_STRING) >= 1) ShowPlayerDialog(playerid, NPLAYER_DIALOG_ID, DIALOG_STYLE_LIST, dialog_title, npDIALOG_STRING, "Play", "Back");
  201.     }
  202.     else
  203.     {
  204.         ShowDialog(playerid);
  205.     }
  206.     return true;
  207. }
  208. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  209. {
  210.     switch(dialogid)
  211.     {
  212.         case NPLAYER_DIALOG_ID:
  213.         {
  214.             if(response)
  215.             {
  216.                 new item_id = ItemIDFromDialogOrder(playerid, listitem);
  217.                 PlayMusic(playerid, item_id);
  218.             }
  219.             else
  220.             {
  221.                 ShowDialog(playerid);
  222.             }
  223.         }
  224.         case NPCATEGORIES_DIALOG_ID:
  225.         {
  226.             if(response)
  227.             {
  228.                 CategoryBrowse(playerid, listitem);
  229.             }
  230.         }
  231.     }
  232.     if (inputtext[0]) return CallLocalFunction("nplayer_OnDialogResponse","iiiis",playerid,dialogid,response,listitem,inputtext);
  233.     else return CallLocalFunction("nplayer_OnDialogResponse","iiiis",playerid,dialogid,response,listitem,NRNULL);
  234. }
  235.  
  236. CMD:music(playerid, params[])
  237. {
  238.     ShowDialog(playerid);
  239.     return 1;
  240. }
  241.  
  242. #if defined _ALS_OnDialogResponse
  243.     #undef OnDialogResponse
  244. #else
  245.     #define _ALS_OnDialogResponse
  246. #endif
  247. #define OnDialogResponse nplayer_OnDialogResponse
  248. forward nplayer_OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement