Eduardo_AC icon

Private Messages System

Eduardo_AC | PRO | 03/20/18 08:59:56 PM UTC | 0 ⭐ | 8435 👁️ | Never ⏰ | []
Pawn |

7.62 KB

|

None

|

0 👍

/

0 👎

#if defined _pm_included
    #endinput
#endif
#define _pm_included
 
/*
        Private Messages System by F1N4L
 
        ________________________________________
 
        SendPrivateMessageToPlayerName(from_playerid, to_player_name[], message[], bool:case_sensitive)
        SendPrivateMessageToPlayerID(from_playerid, to_playerid, message[])
        ShowPrivateMessages(player_name[], messages[])
        RemovePrivateMessageByID(player_name[], line_id)
        ClearPrivateMessages(player_name[])
*/
 
#define FILE_SAVE_MODE                  2 // 1 = Txt File | 2 = MySQL
 
#if defined FILE_SAVE_MODE
    #if FILE_SAVE_MODE < 1 || FILE_SAVE_MODE > 2
        #error [PM Inc] Invalid save mode. Please select between 1-2.
    #endif
#endif
 
// -- Include Config
#if FILE_SAVE_MODE == 1
    #define PRIVATE_MESSAGE_PATH            "Private Messages/%s.pm"
#elseif FILE_SAVE_MODE == 2
    #define MYSQL_HOST                      "127.0.0.1"
    #define MYSQL_USER                      "root"
    #define MYSQL_PASS                      ""
    #define MYSQL_DB                        "samp_db"
#endif
 
#define MAX_PRIVATE_MESSAGE_LINES       (10) // Per file
#define MAX_PRIVATE_MESSAGE_LENGHT      (100) // Per line
 
// -- PM Error Code
#define PM_ERR_NONE                     (0)
#define PM_ERR_INVALID_PLAYER_ID        (1)
#define PM_ERR_EXCEEDED_FILE_LINES      (2)
 
// -- Personalized Format
#define PRIVATE_MESSAGE_FORMAT          "[PM]: %s disse: %s" // [1]-From Player | [2]-Message
 
// -- Forward's
forward OnPlayerSendPrivateMessageOn(from_playerid, to_playerid, message[]);
forward OnPlayerSendPrivateMessageOff(from_playerid, to_player_name[], message[], error);
 
#if FILE_SAVE_MODE == 2
    static MySQL:MyConnnection;
 
    public OnGameModeInit()
    {
        MyConnnection = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
 
        if(mysql_errno(MyConnnection) != 0)
            printf("Erro ao conectar-se na base de dados MySQL (%d)", mysql_errno(MyConnnection));
 
        #if defined PM_OnGameModeInit
            PM_OnGameModeInit();
        #endif
        return true;
    }
    #if defined _ALS_OnGameModeInit
        #undef OnGameModeInit
    #else
        #define _ALS_OnGameModeInit
    #endif
    #define OnGameModeInit PM_OnGameModeInit
    #if defined PM_OnGameModeInit
        forward PM_OnGameModeInit();
    #endif
#endif
 
stock SendPrivateMessageToPlayerName(from_playerid, to_player_name[MAX_PLAYER_NAME], message[MAX_PRIVATE_MESSAGE_LENGHT - MAX_PLAYER_NAME - 19], bool:case_sensitive)
{
    static player_name[MAX_PLAYER_NAME];
 
    for(new i = 0, x = GetPlayerPoolSize()+1; i < x; ++ i)
    {
        GetPlayerName(i, player_name, MAX_PLAYER_NAME);
 
        if(strcmp(to_player_name, player_name, case_sensitive, MAX_PLAYER_NAME) == 0)
        {
            OnPlayerSendPrivateMessageOn(from_playerid, i, message);
            return true;
        }
    }
 
    SaveNewPrivateMessage(from_playerid, to_player_name, message);
 
    return true;
}
 
//                                                                          100                         24            PRIVATE_MESSAGE_FORMAT ([%d] [PM]: %s disse: )
//                                                                          |                           |             |
stock SendPrivateMessageToPlayerID(from_playerid, to_playerid, message[MAX_PRIVATE_MESSAGE_LENGHT - MAX_PLAYER_NAME - 19])
{
    new to_playerid_str[4];
 
    valstr(to_playerid_str, to_playerid, false);
 
    if(!IsPlayerConnected(to_playerid))
        return OnPlayerSendPrivateMessageOff(from_playerid, to_playerid_str, message, PM_ERR_INVALID_PLAYER_ID);
 
    OnPlayerSendPrivateMessageOn(from_playerid, to_playerid, message);
 
    return true;
}
 
 
stock ShowPrivateMessages(player_name[], messages[])
{
    #if FILE_SAVE_MODE == 1
        static lines_str[MAX_PRIVATE_MESSAGE_LENGHT], get_message[MAX_PRIVATE_MESSAGE_LENGHT], player_file[MAX_PLAYER_NAME+20];
        new lines_count;
 
        format(player_file, sizeof player_file, PRIVATE_MESSAGE_PATH, player_name);
 
        if(!fexist(player_file))
            return false;
 
        new File:_priv_file = fopen(player_file, io_read);
 
        while(fread(_priv_file, lines_str))
        {
            lines_count ++;
 
            format(get_message, sizeof get_message, "%d | %s", lines_count, lines_str);
            strcat(messages, get_message, 1024);
        }
    #elseif FILE_SAVE_MODE == 2
        static string[129], Cache:rows, id, from_playerid[MAX_PLAYER_NAME], get_message[MAX_PRIVATE_MESSAGE_LENGHT], message[MAX_PRIVATE_MESSAGE_LENGHT];
        
        mysql_format(MyConnnection, string, sizeof string, "SELECT * FROM `private messages` WHERE `to player` = '%e' LIMIT %d", player_name, MAX_PRIVATE_MESSAGE_LINES);
        rows = mysql_query(MyConnnection, string);
 
        if(cache_num_rows())
        {
            for(new x = 0; x < cache_num_rows(); ++ x)
            {
                cache_get_value_name_int(x, "id", id);
                cache_get_value_name(x, "from player", from_playerid);
                cache_get_value_name(x, "message", message);
 
                format(get_message, sizeof get_message, "%d | "PRIVATE_MESSAGE_FORMAT"\n", id, from_playerid, message);
 
                strcat(messages, get_message, 1024);
            }
        }
 
        cache_delete(rows);
    #endif
 
    return true;
}
 
 
stock RemovePrivateMessageByID(player_name[], line_id)
{
    if(line_id < 1 || line_id > MAX_PRIVATE_MESSAGE_LINES)
            return false;
 
    #if FILE_SAVE_MODE == 1
        static player_file[MAX_PLAYER_NAME+20];
        format(player_file, sizeof player_file, PRIVATE_MESSAGE_PATH, player_name);
 
        new File:_priv_file = fopen(player_file, io_read), lines_count, lines_str[MAX_PRIVATE_MESSAGE_LENGHT], save_str[MAX_PRIVATE_MESSAGE_LINES * MAX_PRIVATE_MESSAGE_LENGHT];
        
        while(fread(_priv_file, lines_str))
        {
            lines_count ++;
            
            if(lines_count == line_id)
                continue;
 
            strcat(save_str, lines_str, 1024);
        }
 
        fclose(_priv_file);
 
        _priv_file = fopen(player_file, io_write);
        fwrite(_priv_file, save_str);
        fclose(_priv_file);
    #elseif FILE_SAVE_MODE == 2
        static string[100];
        mysql_format(MyConnnection, string, sizeof string, "DELETE FROM `private messages` WHERE `id` = '%i' AND `to player` = '%e'", line_id, player_name);
        mysql_tquery(MyConnnection, string);
    #endif
 
    return true;
}
 
stock ClearPrivateMessages(player_name[])
{
    #if FILE_SAVE_MODE == 1
        static player_file[MAX_PLAYER_NAME+20];
        format(player_file, sizeof player_file, PRIVATE_MESSAGE_PATH, player_name);
 
        if(fexist(player_file))
            fremove(player_file);
        else return false;
    #elseif FILE_SAVE_MODE == 2
        static string[90];
        mysql_format(MyConnnection, string, sizeof string, "DELETE FROM `private messages` WHERE `to player` = '%e'", player_name);
        mysql_tquery(MyConnnection, string);
    #endif
 
    return true;
}
 
static stock SaveNewPrivateMessage(from_playerid, to_player_name[MAX_PLAYER_NAME], message[])
{
    static player_name[MAX_PLAYER_NAME], get_message[MAX_PRIVATE_MESSAGE_LENGHT], save_str[MAX_PRIVATE_MESSAGE_LENGHT];
    get_message[0] = EOS;
    
    GetPlayerName(from_playerid, player_name, MAX_PLAYER_NAME);
    strcat(get_message, message, MAX_PRIVATE_MESSAGE_LENGHT);
    format(save_str, sizeof save_str, PRIVATE_MESSAGE_FORMAT"\r\n", player_name, get_message);
 
    #if FILE_SAVE_MODE == 1
        static to_player_file[MAX_PLAYER_NAME+20], lines_str[MAX_PRIVATE_MESSAGE_LENGHT];
        new lines_count;
 
        format(to_player_file, sizeof to_player_file, PRIVATE_MESSAGE_PATH, to_player_name);
 
        new File:_priv_file = fopen(to_player_file, io_readwrite);
        
        while(fread(_priv_file, lines_str))
        {
            lines_count ++;
            if(lines_count >= MAX_PRIVATE_MESSAGE_LINES)
            {
                OnPlayerSendPrivateMessageOff(from_playerid, to_player_name, get_message, PM_ERR_EXCEEDED_FILE_LINES);
                return false;
            }
        }
 
        fwrite(_priv_file, save_str);
        fclose(_priv_file);
 
        OnPlayerSendPrivateMessageOff(from_playerid, to_player_name, get_message, PM_ERR_NONE);
    #elseif FILE_SAVE_MODE == 2
        static string[300];
        mysql_format(MyConnnection, string, sizeof string, "INSERT INTO `private messages` (`from player`, `to player`, `message`) VALUES ('%e', '%e', '%e')", player_name, to_player_name, get_message);
        mysql_tquery(MyConnnection, string);
    #endif
 
    return true;
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎