Advertisement
Guest User

Simple dynamic sql based mp3 player

a guest
Dec 25th, 2011
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 7.10 KB | None | 0 0
  1.  
  2. #include <a_samp>
  3. #include <a_mysql>
  4.  
  5. #define MAX_SONGS 100 //You can add more.
  6. #define main 0x9ACD32AA
  7.  
  8. #pragma tabsize 0
  9. #define MYSQL_HOST "hostname"
  10. #define MYSQL_NAME "name"
  11. #define MYSQL_DATABASE "databse"
  12. #define MYSQL_PASS "pass"
  13. new field[128][32],row[128];
  14. new isitplayingrudy[MAX_PLAYERS];
  15.  
  16. public OnFilterScriptInit()
  17. {
  18.     mysql_connect(MYSQL_HOST,MYSQL_NAME,MYSQL_DATABASE,MYSQL_PASS);
  19.     if(mysql_ping() != -1)
  20.     {
  21.         print(" ");
  22.         print("Connected to MySQL database...");
  23.         print(" ");
  24.     }
  25.     else
  26.     {
  27.         print(" ");
  28.         print("Unable to connected to MySQL database...");
  29.         print("You may have problem with the script...");
  30.         print("Please check the script for mysql connection..");
  31.         print(" ");
  32.         return 1;
  33.     }
  34.     Loadsongs();
  35.     print("\n--------------------------------------");
  36.     print(" SQL Based Simple Mp3 Player");
  37.     print("--------------------------------------\n");
  38.     return 1;
  39. }
  40.  
  41. public OnFilterScriptExit()
  42. {
  43.     print("unloaded.");
  44.     return 1;
  45. }
  46.  
  47. new String[256];
  48. new songname[MAX_SONGS];
  49. new songlink[MAX_SONGS];
  50. public OnPlayerDisconnect(playerid)
  51. {
  52.     isitplayingrudy[playerid] = -1;
  53.     return 1;
  54. }
  55. public OnPlayerConnect(playerid)
  56. {
  57.     isitplayingrudy[playerid] = -1;
  58.     return 1;
  59. }
  60.  
  61. public OnPlayerCommandText(playerid, cmdtext[])
  62. {
  63.     new cmd[128], idx;
  64.     cmd = strtok(cmdtext, idx);
  65.     if (strcmp("/nowplaying", cmdtext, true, 10) == 0)
  66.     {
  67.     if(isitplayingrudy[playerid] == 0) return SendClientMessage(playerid,main,"Your not playing anything.....");
  68.     format(String,sizeof(String),"Now playing: %s",isitplayingrudy[playerid]);
  69.     SendClientMessage(playerid,main,String);
  70.     return 1;
  71.     }
  72.     if (strcmp("/mp3", cmdtext, true, 10) == 0)
  73.     {
  74.     new count;
  75.     new tmp[128];
  76.     tmp = strtok(cmdtext, idx);
  77.     if(!strlen(tmp)){
  78.     for(new i = 0;i<MAX_SONGS;i++)
  79.     {
  80.         if(DoesSongExist(i))
  81.         {
  82.             format(String,128,"%s - ID: %d",songname[i],i);
  83.             SendClientMessage(playerid,main,String);
  84.         }
  85.     }
  86.     SendClientMessage(playerid,main,"USAGE: /mp3 [ID]");
  87.     return 1;
  88.     }
  89.     if(count == 0) return SendClientMessage(playerid,main,"No songs added at the moment.");
  90.     if(count > 0)
  91.     {
  92. //    ShowPlayerDialog(playerid, 1912, DIALOG_STYLE_LIST, "MP3 Player",String,"Play","Exit");
  93.     for(new i = 0;i<MAX_SONGS;i++)
  94.     {
  95.         if(DoesSongExist(i))
  96.         {
  97.          StopAudioStreamForPlayer(playerid);
  98.          PlayAudioStreamForPlayer(playerid, songlink[i]);
  99.          format(String,sizeof(String),"Now playing: %s",songname[i]);
  100.          SendClientMessage(playerid,main,String);
  101.          isitplayingrudy[playerid] = i;
  102.          }
  103.      }
  104.     }
  105.     return 1;
  106.     }
  107.     if (strcmp("/stopmusic", cmdtext, true, 10) == 0)
  108.     {
  109.     StopAudioStreamForPlayer(playerid);
  110.     return 1;
  111.     }
  112.     if (strcmp("/changesong", cmdtext, true, 10) == 0)
  113.     {
  114.     if(!IsPlayerAdmin(playerid)) return 0;
  115.     new tmp[128],tmp2[128];
  116.     tmp = strtok(cmdtext, idx);
  117.     tmp2 = strtok(cmdtext, idx);
  118.     if(!strlen(tmp) || !strlen(tmp2)) return SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: /changesong [ID] [Link] [Name]");
  119.     new songid = strval(tmp);
  120.     new length = strlen(cmdtext);
  121.     while ((idx < length) && (cmdtext[idx] <= ' '))
  122.     {
  123.     idx++;
  124.     }
  125.     new offset = idx;
  126.     new result[64];
  127.     while ((idx < length) && ((idx - offset) < (sizeof(result) - 1)))
  128.     {
  129.     result[idx - offset] = cmdtext[idx];
  130.     idx++;
  131.     }
  132.     if(!DoesSongExist(songid)) return SendClientMessage(playerid, main, "Song ID doesn't exist. To create do /createsong [id] [url] [name]");
  133.     format(String,sizeof(String),"INSERT INTO songs (ID,Name,link) VALUES (%d,%s,%s)",songid,result,tmp2);
  134.     mysql_query(String);
  135.     format(String,sizeof(String),"Song changed! To check /mp3 . ID: %d , Name: %s & Link: %s",songid,result,tmp2);
  136.     SendClientMessage(playerid,main,String);
  137.     return 1;
  138.     }
  139.     if (strcmp("/createsong", cmdtext, true, 10) == 0)
  140.     {
  141.     if(!IsPlayerAdmin(playerid)) return 0;
  142.     new tmp[128],tmp2[128];
  143.     tmp = strtok(cmdtext, idx);
  144.     tmp2 = strtok(cmdtext, idx);
  145.     if(!strlen(tmp) || !strlen(tmp2)) return SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: /createsong [ID] [Link] [Name]");
  146.     new songid = strval(tmp);
  147.     new length = strlen(cmdtext);
  148.     while ((idx < length) && (cmdtext[idx] <= ' '))
  149.     {
  150.     idx++;
  151.     }
  152.     new offset = idx;
  153.     new result[64];
  154.     while ((idx < length) && ((idx - offset) < (sizeof(result) - 1)))
  155.     {
  156.     result[idx - offset] = cmdtext[idx];
  157.     idx++;
  158.     }
  159.  
  160.     if(DoesSongExist(songid)) return SendClientMessage(playerid, main, "Song ID exist. To change do /changesong [id] [url] [name]");
  161.     format(String,sizeof(String),"INSERT INTO songs (ID,Name,link) VALUES (%d,%s,%s)",songid,result,tmp2);
  162.     mysql_query(String);
  163.     format(String,sizeof(String),"Song added! To check /mp3 . ID: %d , Name: %s & Link: %s",songid,result,tmp2);
  164.     SendClientMessage(playerid,main,String);
  165.     return 1;
  166.     }
  167.     return 0;
  168. }
  169.  
  170. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  171. {
  172. /*          for(new i = 0;i<MAX_SONGS;i++)
  173.             {
  174.             if(DoesSongExist(i))
  175.             {
  176. if(response)
  177.     {
  178.     switch(dialogid)
  179.         {
  180.  
  181.         case 1:
  182.             {
  183.             switch(listitem)
  184.             {
  185.                 case 0-100:
  186.                 {
  187.                     StopAudioStreamForPlayer(playerid);
  188.                     PlayAudioStreamForPlayer(playerid, songlink[i]);
  189.                 }
  190.                 }
  191.                 }
  192.             }
  193.             }
  194.     }
  195.     }*/
  196.     return 1;
  197. }
  198. stock DoesSongExist(orgid)
  199. {
  200.     format(String,sizeof(String),"SELECT * FROM songs WHERE `ID` = %d LIMIT 0,1",orgid);
  201.     mysql_query(String);
  202.     mysql_store_result();
  203.     if(mysql_num_rows() != 0)
  204.     {
  205.         mysql_free_result();
  206.         return 1;
  207.     }
  208.     if(mysql_num_rows() == 0)
  209.     {
  210.         mysql_free_result();
  211.         return 0;
  212.     }
  213.     return 1;
  214. }
  215. stock Loadsongs()
  216. {
  217.     new c;
  218.     c =0;
  219.     for(new i = 0;i<MAX_SONGS;i++)
  220.     {
  221.         format(String,48,"SELECT * FROM songs WHERE `ID` = %d LIMIT 0,1",i);
  222.         mysql_query(String);
  223.         mysql_store_result();
  224.         if(mysql_num_rows() == 0)
  225.         {
  226.             mysql_free_result();
  227.             continue;
  228.         }
  229.         if(mysql_num_rows() != 0)
  230.         {
  231.             mysql_fetch_row_format(row, "|");
  232.             explode(row, field, "|");
  233.             format(songname[i],100,"%s",field[1]);
  234.             format(songlink[i],100,"%s",field[2]);
  235.             mysql_free_result();
  236.             c++;
  237.         }
  238.     }
  239.     return printf("Total Songs: %d",c);
  240. }
  241.  
  242. strtok(const string[], &index)
  243. {
  244.     new length = strlen(string);
  245.     while ((index < length) && (string[index] <= ' '))
  246.     {
  247.         index++;
  248.     }
  249.  
  250.     new offset = index;
  251.     new result[20];
  252.     while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
  253.     {
  254.         result[index - offset] = string[index];
  255.         index++;
  256.     }
  257.     result[index - offset] = EOS;
  258.     return result;
  259. }
  260. explode(const sSource[], aExplode[][], const sDelimiter[] = " ", iVertices = sizeof aExplode, iLength = sizeof aExplode[]) // Created by Westie
  261. {
  262.     new
  263.         iNode,
  264.         iPointer,
  265.         iPrevious = -1,
  266.         iDelimiter = strlen(sDelimiter);
  267.  
  268.     while(iNode < iVertices)
  269.     {
  270.         iPointer = strfind(sSource, sDelimiter, false, iPointer);
  271.  
  272.         if(iPointer == -1)
  273.         {
  274.             strmid(aExplode[iNode], sSource, iPrevious, strlen(sSource), iLength);
  275.             break;
  276.         }
  277.         else
  278.         {
  279.             strmid(aExplode[iNode], sSource, iPrevious, iPointer, iLength);
  280.         }
  281.  
  282.         iPrevious = (iPointer += iDelimiter);
  283.         ++iNode;
  284.     }
  285.     return iPrevious;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement