Announcer System

Maximiliano

  • Newbie
  • Posts: 16
Announcer System
«  »Last edited
Well, this script was created with the purpose of making it easy for users to create ads, who many newbies may have problems with Timers or other things.
With this script you can create ads through commands, without the need to edit the script.
You can create, delete, modify the interval, deactivate and activate the ads, this script can be of help for administrators who want to modify, create, or delete an ad without the need to contact a Scripter / Developer.


Requeriments: S.L.C Extended Timers ( http://forum.vc-mp.org/?topic=1487.0 ).

Commands:
(click to show/hide)
Command: /announcer
SubCommands:

  • add
  • remove
  • list
  • on/off
  • interval

Code:
(click to show/hide)
Put in OnScriptLoad:
Code: [Select]
db <- ConnectSQL( "DataBase.db" );
dofile("scripts/_AnnouncerSystem.nut");
Announcer.CreateTable();
Announcer.StartTimer();


Put in onPlayerCommand:
Code: [Select]
if (cmd == "announcer")
{
    if (text) Announcer.Command(text, player);
    else MessagePlayer("Sintax: /announcer add/remove/interval/list/off/on", player);
}


Put at the end of the main.nut:
Code: [Select]
//Credits: I don't know who created this function. :-\
function GetTok(string, separator, n, ...)
{
local m = vargv.len() > 0 ? vargv[0] : n,
  tokenized = split(string, separator),
  text = "";

if (n > tokenized.len() || n < 1) return null;
for (; n <= m; n++)
{
text += text == "" ? tokenized[n-1] : separator + tokenized[n-1];
}
return text;
}

//Credits: jWeb ( http://forum.vc-mp.org/?topic=4273.msg31614#msg31614 )
function SearchAndReplace(str, search, replace) {
    local li = 0, ci = str.find(search, li), res = "";
    while (ci != null) {
        if (ci > 0) {
            res += str.slice(li, ci);
            res += replace;
        } else res += replace;
        li = ci + search.len(), ci = str.find(search, li);
    }
    if (str.len() > 0) res += str.slice(li);
    return res;
}


Create a file .nut called _AnnouncerSystem with the following code:
Pastebin: https://pastebin.com/MHTiQDNu
Code: [Select]

AnnouncerInterval <- 120000; // Default interval = 2 minutes.
AnuncioID <- 1;
AnnouncerTimmer <- null;
Announcer_Prefix <- "[#B7C7C0][[#0FF195]Announcer[#B7C7C0]] [#27E79B]"
Announcer_Status <- false;

Announcer <- {

    function CreateTable()
    {
        QuerySQL( db, "CREATE TABLE IF NOT EXISTS Announcer ( ID NUMERIC, Message TEXT, Owner TEXT )" );
    }

    function AddAnnounce(Message, Owner)
    {
        local GetID = QuerySQL( db, "SELECT * FROM Announcer ORDER BY ID DESC LIMIT 1"), id = 1;

        if( GetSQLColumnData( GetID, 0 ) ) id = ( id + GetSQLColumnData( GetID, 0 ).tointeger() );
        QuerySQL( db, "INSERT INTO Announcer VALUES ( '"+ id +"','" + Message + "', '" + Owner + "' )" );
    }

    function RemoveAnnouncer(id)
    {
        QuerySQL( db, "DELETE FROM Announcer WHERE ID='" + id + "'" );
        Announcer.ReorganizarIDs();
    }

    function ReorganizarIDs()
    {
        local q = QuerySQL( db, "SELECT * FROM Announcer"), i = 1;
        while( GetSQLColumnData( q, 0 ) )
        {
            QuerySQL( db, "UPDATE Announcer SET ID='" + i + "' WHERE ID='" + GetSQLColumnData( q, 0 ) + "'" );
            i++;
            GetSQLNextRow( q );
        }
    }


    function GetAnnounceList(player)
    {
        local q = QuerySQL( db, "SELECT * FROM Announcer"), ann, i = 1;

        while ( GetSQLColumnData( q, 0 ) )
        {
            if ( GetSQLColumnData( q, 1 ) != null)
            {
                ann = true;
                MessagePlayer("[#FE2E64]ID: "+ GetSQLColumnData( q, 0 ) + " Message: "+ GetSQLColumnData( q, 1 ) +" - Author: "+ GetSQLColumnData( q, 2 ), player);
            }
            i++;
    GetSQLNextRow( q );
        }
        if (!ann) MessagePlayer("There are no ads.", player);
    }

    function UpdateInterval(time) //time = minutes
    {
        time = (time * 60000);
        Announcer.StopTimer();
        AnnouncerInterval = time;
        Announcer.StartTimer();
    }

    function StartTimer()
    {
        AnnouncerTimmer = _Timer.Create(this, Announcer.Say, AnnouncerInterval, 0);
        Announcer_Status = true;
    }

    function StopTimer()
    {
        _Timer.Destroy(AnnouncerTimmer);
        Announcer_Status = false;
    }

    function Say()
    {
        local q = QuerySQL( db, "SELECT * FROM Announcer WHERE ID='" + AnuncioID + "'" );

        if(!GetSQLColumnData( q, 1 ))
        {

            if (AnuncioID != 1)
            {
                AnuncioID = 1;
                Announcer.Say();
               
            }
            else
            {
                Announcer.StopTimer();
                print("There isn't announcements, so the timer has been deactivated.")
            }
           
        }
        else
        {
           AnuncioID++;
           Message( Announcer_Prefix + GetSQLColumnData( q, 1 ) );
        }
    }

    function GetInterval()
    {
        return (AnnouncerInterval / 60000);
    }


    function Command(text, player)
    {
        local subcommand = GetTok( text, " ", 1 );
        switch ( subcommand.tolower() )
        {
            case "add":
            local Announce = SearchAndReplace(text, "add ","");
            if(Announce != null)
            {
                if(Announcer_Status == false) MessagePlayer("Your announcer is off, you can turn it on using /announcer on", player);
                Announcer.AddAnnounce(Announce, player);
                MessagePlayer("Announce added!", player);
            }
            else MessagePlayer("/announcer add -message-", player)
            break;

            case "remove":
            case "delete":
            local id = GetTok( text, " ", 2 );
            local q = QuerySQL( db, "SELECT * FROM Announcer WHERE ID='"+ id +"'");

            if( GetSQLColumnData( q, 0 ) )
            {
                Announcer.RemoveAnnouncer(id);
                MessagePlayer("Message: "+ GetSQLColumnData( q, 1 ) +" deleted.", player);
            }
            else MessagePlayer("/announcer remove -ID-", player)

            break;

            case "list":
            Announcer.GetAnnounceList(player);
            break;

            case "interval":
            local time = GetTok( text, " ", 2 ).tointeger();

            if (time == 0) MessagePlayer("You cant set interval 0. Reason: It will generate spam.", player);
            else
            {
                Announcer.UpdateInterval(time);
                MessagePlayer("Interval updated!", player);
            }
            break;

            case "on":
            Announcer.StartTimer();
            MessagePlayer("Announcer actived!", player);
            break;

            case "off":
            Announcer.StopTimer();
            MessagePlayer("Announcer desactived!", player);
            break;

            default:
            MessagePlayer("Sintax: /announcer add/remove/interval/list/off/on", player);
        }
    }
};


Note: This is my first script that I publish, so I will be very grateful to all the comments that give a suggestion to improve/ optimize the code. Thanks in advance ;D!



MatheuS

  • Wiki Contributor
  • LBR | Co-Leader & Brasil Real RPG | Developer
  • Posts: 449
Code: [Select]
if( !sucess ) tryAgain();

Xmair

  • Wiki Contributor
  • Posts: 879
Re: Announcer System
« Reply #2,  »
What does this basically do? Provide some information regarding your snippet.
VU Full Member | VCCNR Developer | KTB Developer | EAD Ex-Scripter

Maximiliano

  • Newbie
  • Posts: 16
Re: Announcer System
« Reply #3,  »
Quote from Xmair on September 17th, 06:43 PM
What does this basically do? Provide some information regarding your snippet.
Oh sorry, I forgot to explain what it's.
In summary, with this script you can create ads through commands, without the need to edit the script.