AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   MYSQL - My first plugin (https://forums.alliedmods.net/showthread.php?t=337529)

Grafi_ 04-26-2022 14:51

MYSQL - My first plugin
 
Hi, i want to create my advertisement plugin with database.
I would like to make it work so that the plugin sends chat messages that can be changed in the database, but I don't know how to start writing a plugin with database. Do you have any tutorials?

Bacardi 04-26-2022 15:19

Re: MYSQL - My first plugin
 
https://wiki.alliedmods.net/SQL_(SourceMod_Scripting)

*mention, do you have own database (MySQL) ?
And have you ever done SQL commands ?

Grafi_ 04-26-2022 15:30

Re: MYSQL - My first plugin
 
Quote:

Originally Posted by Bacardi (Post 2778012)
*mention, do you have own database (MySQL) ?

You mean do I have a database management tool? If so, I don't really have one XD

Quote:

Originally Posted by Bacardi (Post 2778012)
And have you ever done SQL commands ?

No :(

Bacardi 04-26-2022 16:21

Re: MYSQL - My first plugin
 
With dedicated MySQL host, you can share database with multiple game servers.
And easier way to use with PHP web pages etc. etc.
- Do you host more than one SRCDS ?

Sourcemod can run local SQL database called SQLite, it cannot connect to others server, only to one.
- You need program or web-browser-addon to open SQLite database file to edit. Which is some how tedious.
- SQLite has some differences from MySQL commands.

Then there is KeyValue files, you can edit with normal text editor but you need keep structure rigth.
Look like this:
https://github.com/ErikMinekus/sm-ad...rtisements.txt

So, do you still want to use database or would it be easier to use txt file (KeyValues) ?

Grafi_ 04-27-2022 10:33

Re: MYSQL - My first plugin
 
Quote:

Originally Posted by Bacardi (Post 2778019)
With dedicated MySQL host, you can share database with multiple game servers.
And easier way to use with PHP web pages etc. etc.
- Do you host more than one SRCDS ?

Sourcemod can run local SQL database called SQLite, it cannot connect to others server, only to one.
- You need program or web-browser-addon to open SQLite database file to edit. Which is some how tedious.
- SQLite has some differences from MySQL commands.

Then there is KeyValue files, you can edit with normal text editor but you need keep structure rigth.
Look like this:
https://github.com/ErikMinekus/sm-ad...rtisements.txt

So, do you still want to use database or would it be easier to use txt file (KeyValues) ?

Yes i still want database because on 3 servers i will be able to change messages and add them more easily.

Grafi_ 04-27-2022 12:19

Re: MYSQL - My first plugin
 
Quote:

Originally Posted by Grafi_ (Post 2778098)
Yes i still want database because on 3 servers i will be able to change messages and add them more easily.

HTML Code:

#include <sourcemod>
#include <sdktools>

Database g_hDB;

public void OnPluginStart()
{
    SQL_TConnect(Connect, "advertisement");
}

public void OnMapStart()
{
    timer = CreateTimer(45.0, MessageRepeat, _, TIMER_REPEAT)
}

public void Connect(Database db, const char[] error, any data)
{
        if(db == null)
                SetFailState("Unable to connect to database: %s", error);
       
        g_hDB = db;
       
        char sQuery[256];
        g_hDB.Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS `advertisement` ('message' varchar(256) NOT NULL)");
        g_hDB.Query(OnSQLConnectCallback, sQuery);
}

public void OnSQLConnectCallback(Database db, DBResultSet results, const char[] error, any data)
{
        if (results == null)
        {
                LogError("Query failure: %s", error);
                return;
        }
}

public Action MessageRepeat(Handle timer, any data)
{
    char sBuffer[256];
    Format(sBuffer, sizeof(sBuffer), "SELECT 'message' FROM 'advertisement'")
    DBResultSet query = SQL_TQUERY(db, sBuffer)
    if (query == null)
    {
        char sError[256];
        SQL_GetError(db, sError, sizeof(sError))
        PrintToServer("Error %s", sError)
    }
    else
    {
        // *I'd like to have something like this here someday* printochat(message from database) *messages displayed one by one from the database)
    }
}

public void Check(Database db, DBResultSet results, const char[] error, any data)
{
        if (results == null)
                LogError("Query failure: %s", error);
}

I know it is very weak but I tried to do something, could you advise me what I should do? XD
And I know the code is full of bugs

Bacardi 04-28-2022 17:12

Re: MYSQL - My first plugin
 
1 Attachment(s)
...here something.

- You need configure databases.cfg
Code:

"Databases"
{
        "driver_default"                "mysql"
       
        // When specifying "host", you may use an IP address, a hostname, or a socket file path
       
        "default"
        {
                "driver"                        "default"
                "host"                                "localhost"
                "database"                        "sourcemod"
                "user"                                "root"
                "pass"                                ""
                //"timeout"                        "0"
                //"port"                        "0"
        }
       
        "storage-local"
        {
                "driver"                        "sqlite"
                "database"                        "sourcemod-local"
        }

        "clientprefs"
        {
                "driver"                        "sqlite"
                "host"                                "localhost"
                "database"                        "clientprefs-sqlite"
                "user"                                "root"
                "pass"                                ""
                //"timeout"                        "0"
                //"port"                        "0"
        }

        "my_connect_configure"
        {
                "driver"                        "mysql"
                "host"                                "localhost"
                "database"                        "database_test"
                "user"                                "root"
                "pass"                                ""
                //"timeout"                        "0"
                //"port"                        "0"
        }

}

- Plugin create connection, everytime when map start.
After database queries, it close connection.
= You can however, create connection once and leave it open. It's up to you.

- Plugin is looking (and create table if query result is null) table called "table_advertisement"
And add first message when table is created.

- Plugin print all messages from table, into server console

Austin 04-29-2022 03:14

Re: MYSQL - My first plugin
 
https://sqlitebrowser.org/dl/

KateUhlerredalfias 05-11-2022 08:05

Re: MYSQL - My first plugin
 
As someone who's written a storage engine plugin, I've found that executing a query from within a MySQL plugin is incredibly difficult. MySQL isn't re-entrant, due to locking within the MySQL process. You could use the MySQL client api (as suggested by chris) to connect to the same server. But it is 99% likely you will simply deadlock the whole omegle.2yu.co server.

Rohanlogs 05-14-2022 17:17

Re: MYSQL - My first plugin
 
Quote:

Originally Posted by KateUhlerredalfias (Post 2779154)
As someone who's written a storage engine plugin, I've found that executing a query from within a MySQL plugin is incredibly difficult. MySQL isn't re-entrant, due to locking within the MySQL process. You could use the MySQL client api (as suggested by chris) to connect to the same server. But it is 99% likely you will simply deadlock the whole server.

I dunno what you mean, MySQL works just fine with sourcemod even with some complex queries.
Its far more superior in data storing than anything else in my opinion when you're dealing with a lot of data. Of course depends what kind of plugin you're doing but still..
Its not that difficult, just organize your code well. The OPs question is quite simple.
He just wants to pull chat advertisements from the database which Bacardi gave an example script of already.

There's no deadlocking involved with such a simple plugin unless your code is somehow really bad..
You aren't doing that much inserting/updating from multiple sessions in this case that'd cause a deadlock in this situation.
InnoDB also detects deadlocks by default and rolls back the transactions to break the lock.


All times are GMT -4. The time now is 20:27.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.