[FilterScript] [MySQL-R39]Music Player
#1

Hello!
I made a simple music player script, which I like to share with you.


[ About ]

Time to make: 15 minutes
Lines of code: 168
Used includes: a_samp, a_msql, zcmd, sscanf2


[ Pictures ]
IMGUR

[ MySQL Table]
Code:
CREATE TABLE `songs` (
  `ID` tinyint(4) NOT NULL,
  `Artist` text NOT NULL,
  `Song` text NOT NULL,
  `Link` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


ALTER TABLE `songs`
  ADD PRIMARY KEY (`ID`);


ALTER TABLE `songs`
  MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT;
[ Script ]
Code:
#include 	<a_samp>
#include  	<a_mysql>
#include 	<sscanf2>
#include	<zcmd>

//»Defines
#define MAX_SONGS 100
#define DIALOG_SONG 100

//»Enum
enum music
{
m_id,
m_artist[32],
m_song[32],
m_link[96]
}

//»Vars
new sql,songs,mInfo[MAX_SONGS][music];

//»Forwards
forward LoadSongs();
forward InsertSong(a[],b[],c[]);

public OnFilterScriptInit()
{
	mysql_log(LOG_ALL, LOG_TYPE_HTML);
	sql = mysql_connect("127.0.0.1", "root", "music", "");
	if(mysql_errno() != 0)
 	{
	        print("[MySQL] » ( ! )Failed to connect to the database!");
	}
 	else
  	{
	        print("[MySQL] » Successful connection to the database!");
	}
 	print("[MusicPlayer] » Loading songs..");
	mysql_tquery(sql, "SELECT * FROM songs", "LoadSongs");
	return 1;
}

public OnFilterScriptExit()
{
	mysql_close(sql);
	return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	if(dialogid == DIALOG_SONG)
	{
		if(response)
		{
			if(listitem == 0)
			{
				StopAudioStreamForPlayer(playerid);
		  		SendClientMessage(playerid,0xf03c3cAA,"Music stopped!");
			}
			else
			{
	            PlayAudioStreamForPlayer(playerid,mInfo[listitem-1][m_link]);
	            new string[100];
	            format(string,100,"Music started! / Performer: %s / Song : %s /",mInfo[listitem-1][m_artist],mInfo[listitem-1][m_song]);
				SendClientMessage(playerid,0x8a3cf0AA,string);
			}
		}
	}
	return 0;
}

public LoadSongs()
{
	for(new i = 0; i < cache_get_row_count(); i++)
	{
		mInfo[i][m_id] = cache_get_row_int(i,0);
		cache_get_row(i, 1,mInfo[i][m_artist],1,32);
		cache_get_row(i, 2,mInfo[i][m_song],1,32);
		cache_get_row(i, 3,mInfo[i][m_link],1,96);
		songs ++;
	}
	printf("[MusicPlayer] » %d song(s) loaded from the database.",songs);
	return true;
}


public InsertSong(a[],b[],c[])
{
	mInfo[songs][m_id] = cache_insert_id();
	strins(mInfo[songs][m_artist],a,0);
	strins(mInfo[songs][m_song],b,0);
	strins(mInfo[songs][m_link],c,0);
	songs++;
	return true;
}

stock ShowMusic(playerid)
{
	new str[512] = "Performer\t-\tSong\n{f03c3c}Stop music\n",str2[100],count;
	for(new i = 0; i < songs; i++)
	{
		format(str2,sizeof(str2),"%s\t-\t%s\n",mInfo[i][m_artist],mInfo[i][m_song]);
		strcat(str,str2);
		count = 1;
	}
	if(count != 1) return SendClientMessage(playerid,0xf03c3cAA,"There is nothing to play. :/");
	ShowPlayerDialog(playerid,DIALOG_SONG,DIALOG_STYLE_TABLIST_HEADERS,"MusicPlayer",str, "Select", "Exit");
	return true;
}

stock Compare(comp[], with[]) // by fl0rian
{
	new LenghtComp = strlen(comp);
	new LenghtWith = strlen(with);
	new Character;
	if( LenghtComp != LenghtWith ) return false;
	for( new i = 0; i < LenghtComp; i++ )
	{
	    if( comp[i] == with[i] )
	    {
	        Character++;
		}
	}
	if( LenghtComp == Character ) return true;
	return false;
}

CMD:mp(playerid,params[])
{
	#pragma unused params
	ShowMusic(playerid);
	return true;
}


CMD:newsong(playerid,params[])
{
	new artist[32],song[32],link[96],string[96];
	if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,0xf03c3cAA,"You are not authorized to use this command!!");
	if(sscanf(params, "s[32]s[32]s[96]",artist,song,link)) return SendClientMessage(playerid,0xf03c3cAA,"/newsong [Performer] [Song] [Link to mp3]");
	for(new i = 0; i < songs; i++)
	{
		if(Compare(mInfo[i][m_artist],artist) && Compare(mInfo[i][m_song],song)) return SendClientMessage(playerid,0xf03c3cAA,"This Song from the performer is already added!");
	}
	new query[256];
	mysql_format(sql, query, 256, "INSERT INTO songs (Artist,Song,Link) VALUES ('%s','%s','%s')",artist,song,link);
	mysql_tquery(sql, query,"InsertSong","sss",artist,song,link);
	format(string,96,"You sucessfully added a song! Performer: %s , Song : %s",artist,song);
	SendClientMessage(playerid,0x1cd657AA,string);
	return true;
}


CMD:refresh(playerid,params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,0xf03c3cAA,"You are not authorized to use this command!!");
	for(new i = 0; i < songs; i++)
	{
		mInfo[i][m_id] = 0;
		mInfo[i][m_artist] = EOS;
		mInfo[i][m_song] = EOS;
		mInfo[i][m_link] = EOS;
	}
	songs = 0;
	mysql_tquery(sql, "SELECT * FROM songs", "LoadSongs");
	SendClientMessage(playerid,0x1cd657AA,"You sucessfully refreshed the song list!");
	return true;
}
Reply
#2

Why not just do
PHP Code:

CREATE TABLE 
`songs` (
  `
IDint(11NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `
Artisttext NOT NULL,
  `
Songtext NOT NULL,
  `
Linktext NOT NULL
ENGINE=InnoDB DEFAULT CHARSET=latin1
and why INT? I double anyone would store 2147483647 songs (which is the maximum value of int)
I would go with TINYINT UNSIGNED and get 255 slots

Reference: https://dev.mysql.com/doc/refman/5.7...ger-types.html
Reply
#3

Thanks for the suggestions!
My phpMyAdmin exported the table like this.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)