Advertisement
Guest User

Radiosystem von Ch4oS

a guest
Jul 5th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 8.15 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3. #include <ocmd>
  4. #include <sscanf2>
  5.  
  6. #define _MYSQL_HOST     "localhost"
  7. #define _MYSQL_USER     "root"
  8. #define _MYSQL_PASS     "root"
  9. #define _MYSQL_DB       "samp"
  10.  
  11. #if !defined RADIO_STREAMS_DEBUG
  12.     #define RADIO_STREAMS_DEBUG 0
  13. #endif
  14.  
  15. #define DIALOG_CARMENU_RADIO        300
  16. #define DIALOG_RADIO_ADD_NAME       301
  17. #define DIALOG_RADIO_ADD_URL        302
  18. #define DIALOG_RADIO_ADD_CHECK      303
  19. #define DIALOG_RADIO_MANAGE_LIST    304
  20.  
  21. new MySQL_Connection;
  22. new CarRadio[MAX_VEHICLES],
  23.     CarRadioName[MAX_PLAYERS][32],
  24.     CarRadioURL[MAX_PLAYERS][128];
  25.  
  26. stock DB_Connect() {
  27.     #if RADIO_STREAMS_DEBUG == 1
  28.         print("\n[MySQL]: Es wird versucht eine sichere Verbindung zur Datenbank herzustellen...");
  29.     #endif
  30.     MySQL_Connection = mysql_connect(_MYSQL_HOST, _MYSQL_USER, _MYSQL_DB, _MYSQL_PASS);
  31.     #if RADIO_STREAMS_DEBUG == 1
  32.         printf("%s %s %s %s %i", _MYSQL_HOST, _MYSQL_USER, _MYSQL_DB, _MYSQL_PASS, MySQL_Connection);
  33.     #endif
  34.     if(MySQL_Connection) {
  35.         #if RADIO_STREAMS_DEBUG == 1
  36.             print("[MySQL]: Die Verbindung zur Datenbank wurde erfolgreich hergestellt!");
  37.         #endif
  38.         CheckTables();
  39.         return 1;
  40.     }
  41.     #if RADIO_STREAMS_DEBUG == 1
  42.         print("[MySQL]: Es konnte beim ersten Versuch keine Verbindung zur Datenbank hergestellt werden.");
  43.         print("[MySQL]: Es wird Versucht die Verbindung erneut herzustellen");
  44.     #endif
  45.     MySQL_Connection = mysql_connect(_MYSQL_HOST, _MYSQL_USER, _MYSQL_DB, _MYSQL_PASS);
  46.     if(MySQL_Connection) {
  47.         #if RADIO_STREAMS_DEBUG == 1
  48.             print("[MySQL]: Die Verbindung zur Datenbank wurde erfolgreich bem zweiten Versuch hergestellt!");
  49.         #endif
  50.         CheckTables();
  51.         return 1;
  52.     }
  53.     #if RADIO_STREAMS_DEBUG == 1
  54.         print("[MySQL]: Die Verbindung konnte auch im zweiten Versuch nicht hergestellt werden.");
  55.         print("[MySQL]: Der Server beendet sich nun selber.");
  56.     #endif
  57.     SendRconCommand("exit");
  58.     return 1;
  59. }
  60.  
  61. stock CheckTables() {
  62.     mysql_query(MySQL_Connection, "CREATE TABLE IF NOT EXISTS `radiostations` (`id` int(11) NOT NULL AUTO_INCREMENT,`radio_name` text NOT NULL,`radio_url` text NOT NULL,`radio_active` tinyint(1) NOT NULL DEFAULT '1',PRIMARY KEY (`id`)) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;");
  63.     return 1;
  64. }
  65.  
  66. public OnFilterScriptInit()
  67. {
  68.     print("\n Radio System von Adrian aka Ch4oS\n");
  69.  
  70.     DB_Connect();
  71.     return 1;
  72. }
  73.  
  74. public OnFilterScriptExit()
  75. {
  76.     return 1;
  77. }
  78.  
  79. public OnVehicleSpawn(vehicleid)
  80. {
  81.     CarRadio[vehicleid] = 0;
  82.     return 1;
  83. }
  84.  
  85. public OnPlayerStateChange(playerid, newstate, oldstate) {
  86.     new vehicleid = GetPlayerVehicleID(playerid);
  87.     if(newstate == PLAYER_STATE_DRIVER || newstate == PLAYER_STATE_PASSENGER) {
  88.         if(CarRadio[vehicleid] != 0) {
  89.             new _query[128];
  90.             format(_query, sizeof _query, "SELECT * FROM radiostations WHERE id='%i' LIMIT 1", CarRadio[vehicleid]);
  91.             mysql_query(MySQL_Connection, _query);
  92.             new _temp[128];
  93.             cache_get_field_content(0, "radio_url", _temp);
  94.             PlayAudioStreamForPlayer(playerid, _temp);
  95.         }
  96.     }
  97.     if(newstate == PLAYER_STATE_ONFOOT) {
  98.         StopAudioStreamForPlayer(playerid);
  99.     }
  100.     return 1;
  101. }
  102.  
  103. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  104. {
  105.     new _query[512], _string[512];
  106.     switch(dialogid) {
  107.         case DIALOG_CARMENU_RADIO: {
  108.             if(response) {
  109.                 format(_query, sizeof _query, "SELECT * FROM radiostations WHERE radio_name='%s' LIMIT 1", inputtext);
  110.                 mysql_query(MySQL_Connection, _query);
  111.                 new _temp[128];
  112.                 cache_get_field_content(0, "radio_url", _temp);
  113.                 PlayAudioStreamForPlayer(playerid, _temp);
  114.                 cache_get_field_content(0, "radio_name", _temp);
  115.                 format(_string, sizeof _string, "Du hast den Radiosender auf %s gestellt.", _temp);
  116.                 SendClientMessage(playerid, -1, _string);
  117.                 CarRadio[GetPlayerVehicleID(playerid)] = cache_get_field_content_int(0, "id");
  118.             } else StopAudioStreamForPlayer(playerid);
  119.         }
  120.         case DIALOG_RADIO_ADD_NAME: {
  121.             if(response) {
  122.                 if(!strlen(inputtext) || strlen(inputtext) < 6)return SendClientMessage(playerid, 0xAFAFAFFF, "Der Name ist zu kurz."), ShowPlayerDialog(playerid, DIALOG_RADIO_ADD_NAME, DIALOG_STYLE_INPUT, "Radio hinzufügen", "Gebe den Namen des Radios ein.", "Weiter", "Abbruch");
  123.                 if(strlen(inputtext) > 32)return SendClientMessage(playerid, 0xAFAFAFFF, "Der Name ist zu lang."), ShowPlayerDialog(playerid, DIALOG_RADIO_ADD_NAME, DIALOG_STYLE_INPUT, "Radio hinzufügen", "Gebe den Namen des Radios ein.", "Weiter", "Abbruch");
  124.                 ShowPlayerDialog(playerid, DIALOG_RADIO_ADD_URL, DIALOG_STYLE_INPUT, "Radio hinzufügen", "Gebe die URL des Radios ein.", "Weiter", "Zurück");
  125.                 format(CarRadioName[playerid], 32, "%s", inputtext);
  126.             }
  127.         }
  128.         case DIALOG_RADIO_ADD_URL: {
  129.             if(response) {
  130.                 if(!strlen(inputtext) || strlen(inputtext) < 6)return SendClientMessage(playerid, 0xAFAFAFFF, "Die URL ist zu kurz."), ShowPlayerDialog(playerid, DIALOG_RADIO_ADD_URL, DIALOG_STYLE_INPUT, "Radio hinzufügen", "Gebe die URL des Radios ein.", "Weiter", "Zurück");
  131.                 if(strlen(inputtext) > 128)return SendClientMessage(playerid, 0xAFAFAFFF, "Die URL ist zu lang."), ShowPlayerDialog(playerid, DIALOG_RADIO_ADD_URL, DIALOG_STYLE_INPUT, "Radio hinzufügen", "Gebe die URL des Radios ein.", "Weiter", "Zurück");
  132.                 format(CarRadioURL[playerid], 128, "%s", inputtext);
  133.                 format(_string, sizeof _string, "Radio Name: %s\nRadio URL: %s\n\nWollen sie die Radiostation hinzufügen?", CarRadioName[playerid], CarRadioURL[playerid]);
  134.                 ShowPlayerDialog(playerid, DIALOG_RADIO_ADD_CHECK, DIALOG_STYLE_INPUT, "Radio hinzufügen", _string, "Ja", "Nein");
  135.             }
  136.         }
  137.         case DIALOG_RADIO_ADD_CHECK: {
  138.             if(response) {
  139.                 format(_query, sizeof _query, "INSERT INTO radiostations (`radio_name`, `radio_url`) VALUES ('%s', '%s')", CarRadioName[playerid], CarRadioURL[playerid]);
  140.                 mysql_query(MySQL_Connection, _query);
  141.                 SendClientMessage(playerid, -1, "Du hast erfolgreich eine Radiostation hinzugefügt.");
  142.             }
  143.         }
  144.         case DIALOG_RADIO_MANAGE_LIST: {
  145.             if(response) {
  146.                 ShowPlayerDialog(playerid, DIALOG_RADIO_ADD_NAME, DIALOG_STYLE_INPUT, "Radio hinzufügen", "Gebe den Namen des Radios ein.", "Weiter", "Abbruch");
  147.             } else if(!response) {
  148.                 new _start, _end;
  149.                 _start = strfind(inputtext, "[");
  150.                 _end = strfind(inputtext, "]");
  151.                 strdel(inputtext, 0, _start+1);
  152.                 strdel(inputtext, _end, strlen(inputtext));
  153.                 new _id = strval(inputtext);
  154.                 format(_query, sizeof _query, "DELETE FROM radiostations WHERE id='%i'", _id);
  155.                 mysql_query(MySQL_Connection, _query);
  156.                 SendClientMessage(playerid, -1, "Du hast erfolgreich einen Radiosender gelöscht.");
  157.             }
  158.         }
  159.     }
  160.     return 1;
  161. }
  162.  
  163. ocmd:carradio(playerid, params[]) {
  164.     if(GetPlayerVehicleSeat(playerid) != 1 && GetPlayerVehicleSeat(playerid) != 0)return SendClientMessage(playerid, 0xAFAFAFFF, "Du bist kein Fahrer oder Beifahrer eines Fahrzeuges!");
  165.     mysql_query(MySQL_Connection, "SELECT * FROM radiostations WHERE radio_active='1'");
  166.     new _rows, _fields;
  167.     cache_get_data(_rows, _fields);
  168.     if(!_rows)return SendClientMessage(playerid, 0xAFAFAFFF, "Es sind keine Radiosender vorhanden.");
  169.     new _radios, _dstring[1028];
  170.     while(_radios < _rows) {
  171.         new _temp[32];
  172.         cache_get_field_content(_radios, "radio_name", _temp);
  173.         format(_dstring, sizeof _dstring, "%s%s\n", _dstring, _temp);
  174.         _radios++;
  175.     }
  176.     ShowPlayerDialog(playerid, DIALOG_CARMENU_RADIO, DIALOG_STYLE_LIST, "Radiosender", _dstring, "Einschalten", "Ausschalten");
  177.     return 1;
  178. }
  179.  
  180. ocmd:listradios(playerid, params[]) {
  181.     if(!IsPlayerAdmin(playerid))return SendClientMessage(playerid, 0xAFAFAFFF, "Du bist kein Administrator.");
  182.     mysql_query(MySQL_Connection, "SELECT * FROM radiostations WHERE radio_active='1'");
  183.     new _rows, _fields;
  184.     cache_get_data(_rows, _fields);
  185.     if(!_rows)return ShowPlayerDialog(playerid, DIALOG_RADIO_MANAGE_LIST, DIALOG_STYLE_LIST, "Radiosender", "Es sind keine Radiosender vorhanden", "Hinzufügen", "");
  186.     new _radios, _dstring[1028];
  187.     while(_radios < _rows) {
  188.         new _temp[32];
  189.         cache_get_field_content(_radios, "radio_name", _temp);
  190.         format(_dstring, sizeof _dstring, "%s[%i] %s\n", cache_get_field_content_int(_radios, "id"), _dstring, _temp);
  191.         _radios++;
  192.     }
  193.     ShowPlayerDialog(playerid, DIALOG_RADIO_MANAGE_LIST, DIALOG_STYLE_LIST, "Radiosender", _dstring, "Hinzufügen", "Löschen");
  194.     return 1;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement