Announcer System

Started by Maximiliano, Sep 17, 2019, 03:56 PM

Previous topic - Next topic

Maximiliano

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:
[spoiler]
Command: /announcer
SubCommands:

  • add
  • remove
  • list
  • on/off
  • interval
[/spoiler]

Code:
[spoiler]
Put in OnScriptLoad:
[noae][noae][noae][noae][noae]db <- ConnectSQL( "DataBase.db" );
dofile("scripts/_AnnouncerSystem.nut");
Announcer.CreateTable();
Announcer.StartTimer();
[/noae][/noae][/noae][/noae][/noae]

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

Put at the end of the main.nut:
[noae][noae][noae][noae][noae]//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;
}
[/noae][/noae][/noae][/noae][/noae]

Create a file .nut called _AnnouncerSystem with the following code:
Pastebin: https://pastebin.com/MHTiQDNu
[noae][noae][noae][noae][noae]
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);
        }
    }
};
[/noae][/noae][/noae][/noae][/noae][/spoiler]


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

Good job! This looks interesting
if( !sucess ) tryAgain();
Thanks to the VCMP community. It was the happiest period of my life.

Xmair

What does this basically do? Provide some information regarding your snippet.

Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

Maximiliano

Quote from: Xmair on Sep 17, 2019, 05:43 PMWhat 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.

=RK=MarineForce

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


show error in this          AnnouncerTimer = _Timer.Create(this, Announcer.Say, AnnouncerInterval, 0); that _Timer is not exists.

AroliS^

Quote from: =RK=MarineForce on Mar 24, 2020, 07:46 AMfunction StartTimer()
    {
        AnnouncerTimer = _Timer.Create(this, Announcer.Say, AnnouncerInterval, 0);
        Announcer_Status = true;
    }


show error in this          AnnouncerTimer = _Timer.Create(this, Announcer.Say, AnnouncerInterval, 0); that _Timer is not exists.

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