Advertisement
Guest User

Radiosystem von Ch4oS

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