MySQL Stream Saving
#1

Hey,

So, what I want basically is that a command that's /savemyradio, it will open up a dialog and the player can paste his stream link, the one which he got from shoutcast.com. and when he clicks on okay on the dialog. It would save the the link he pasted into the database in the table called "Streams"
and when the player would type /myradio, it will show a dialog with the options
"Own Stream"
"Stop Radio"

Clicking on Own Stream, would get the data from Streams table and play it usng PlayAudioStreamForPlayer for the player, anywhere.
Stop radio, would simply stop it.
I haven't really worked with all these functions before, that's why I literally have no idea on how to execute them, any help with a bit of code would be appreciated.

Thanks,
Reply
#2

hmm use mysql
Reply
#3

If you're using this MySQL plugin (which you should be) then you just use the function mysql_pquery to send a query which call a callback once the query finishes
Example:
Код:
//in the command to insert
new tmpQuery[128];
mysql_format(1, tmpQuery, sizeof(tmpQuery), "INSERT INTO `STREAMS` (`LINK`) VALUES ('%e')", linkStr); 
//%e will escape the string and prevent SQL injection
mysql_pquery(1, tmpQuery, "OnInsertStreamFinish", "d", playerid);


//Then you have a callback somewhere else in the code
forward OnInsertStreamFinish(playerid);
public OnInsertStreamFinish(playerid);
{
    new num = cache_affected_rows();
    if(!num) return SendClientMessage(playerid, 0xFF0000FF, "Error while inserting"); //if num is 0 it failed
    else
    {
        SendClientMessage(playerid, 0x00FF00FF, "Successfully added!");
    }
    return 1;
}
Reply
#4

Quote:
Originally Posted by SloProKiller
Посмотреть сообщение
If you're using this MySQL plugin (which you should be) then you just use the function mysql_pquery to send a query which call a callback once the query finishes
Example:
Код:
//in the command to insert
new tmpQuery[128];
mysql_format(1, tmpQuery, sizeof(tmpQuery), "INSERT INTO `STREAMS` (`LINK`) VALUES ('%e')", linkStr); 
//%e will escape the string and prevent SQL injection
mysql_pquery(1, tmpQuery, "OnInsertStreamFinish", "d", playerid);


//Then you have a callback somewhere else in the code
forward OnInsertStreamFinish(playerid);
public OnInsertStreamFinish(playerid);
{
    new num = cache_affected_rows();
    if(!num) return SendClientMessage(playerid, 0xFF0000FF, "Error while inserting"); //if num is 0 it failed
    else
    {
        SendClientMessage(playerid, 0x00FF00FF, "Successfully added!");
    }
    return 1;
}
Here's what I understood, and tried to make.

Код:
CMD:savemyradio(playerid, params[])
    {
ShowPlayerDialog(playerid,DIALOG_Test, DIALOG_STYLE_INPUT, "Stream", "Enter your stream below:", "Save", "Cancel");
           return 1;
    }
Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == DIALOG_Test)
    {
        if(!response) /
        {
             SendClientMessage(playerid, 0x00FF00FF, "You clicked on cancel.!");
        }
         else // Pressed ENTER or clicked 'Login' button
        {
          new tmpQuery[128];
          mysql_format(1, tmpQuery, sizeof(tmpQuery), "INSERT INTO `STREAMS` (`LINK`) VALUES ('%e')", linkStr); 
          //%e will escape the string and prevent SQL injection
          mysql_pquery(1, tmpQuery, "OnInsertStreamFinish", "d", playerid);
        }
}
        return 1; // We handled a dialog, so return 1. Just like OnPlayerCommandText.
    }
Would this be in working order? if it is correct.
How can I call this data to load on the /myradio dialog and play it using PlayAudioStream.?
Reply
#5

Quote:
Originally Posted by SsHady
Посмотреть сообщение
Here's what I understood, and tried to make.

Код:
CMD:savemyradio(playerid, params[])
    {
ShowPlayerDialog(playerid,DIALOG_Test, DIALOG_STYLE_INPUT, "Stream", "Enter your stream below:", "Save", "Cancel");
           return 1;
    }
Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == DIALOG_Test)
    {
        if(!response) /
        {
             SendClientMessage(playerid, 0x00FF00FF, "You clicked on cancel.!");
        }
         else // Pressed ENTER or clicked 'Login' button
        {
          new tmpQuery[128];
          mysql_format(1, tmpQuery, sizeof(tmpQuery), "INSERT INTO `STREAMS` (`LINK`) VALUES ('%e')", linkStr); 
          //%e will escape the string and prevent SQL injection
          mysql_pquery(1, tmpQuery, "OnInsertStreamFinish", "d", playerid);
        }
}
        return 1; // We handled a dialog, so return 1. Just like OnPlayerCommandText.
    }
Would this be in working order? if it is correct.
How can I call this data to load on the /myradio dialog and play it using PlayAudioStream.?
Well you still need the OnInsertStreamFinish callback somewhere in the code

To get all the streams from the database you can use
Код:
mysql_pquery(1, "SELECT `LINK` FROM `STREAMS`", "OnLoadStreamsFinish");
I'd recommend loading the streams upon server startup and having a refresh command for admins as opposed to constantly refreshing it every time a player wants to see the streams.

The callback below will be called once it query execution finishes
Код:
forward OnLoadStreamsFinish();
public OnLoadStreamsFinish()
{
    new num = cache_get_row_count(); //gets number of rows (streams) returned by the SQL query
    if(num) //if number of streams isn't 0
    {
        new streams[10][128]; //array of 10 strings (128 chars each) - for demonstration purposes
        for(new i = 0; i < num; i++)
        {
            cache_get_row(i, 0, stream[i]); 
            //saves the first (and, in this case, only) column "LINK" into this array
            //should be a global if you want to use it elsewhere in the code
        }
    }
    return 1;
}
This will save all the links that are saved in the database into the streams array.
Reply
#6

Maybe you forgot "WHERE Name = '%e'", as i understand each player will have his own streams so its needed
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)