AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Suggestions / Requests (https://forums.alliedmods.net/forumdisplay.php?f=12)
-   -   Some problems with mysql query's (https://forums.alliedmods.net/showthread.php?t=174280)

lqlqlq 12-18-2011 08:16

Some problems with mysql query's
 
Here is source:
Code:

#include <amxmodx>
#include <amxmisc>
#include <sqlx>

#define VERSION "0.6"

/*START - From colorchat.inc by Numb */
enum Color {
    NORMAL = 1,
    GREEN,
    TEAM_COLOR,
    GREY,
    RED,
    BLUE,
}

new TeamName[][] = {
    "",
    "TERRORIST",
    "CT",
    "SPECTATOR"
}
/*END - From colorchat.inc by Numb */

//MySQL Variables
new Handle:Sql_Tuple

//Store data variables
new iKills[33]

public plugin_init() {
    register_plugin("SMR", VERSION, "carbonated")
    register_clcmd("say /mr", "ShowRank")
    register_clcmd("say_team /mr", "ShowRank")
    register_event("DeathMsg", "Event_DeathMsg", "a")
    set_task(0.1, "mysql_connect")
}

public Event_DeathMsg() {
    new iKiller = read_data(1)
    new iVictim = read_data(2)
    if((0 < iKiller <= 33) && iKiller != iVictim) {
        iKills[iKiller]++
    }
}

public mysql_connect() {
    Sql_Tuple = SQL_MakeStdTuple( )
    if(Sql_Tuple == Empty_Handle)
    {
        set_fail_state( "MySQL Connection Problem" )
    }
    SQL_ThreadQuery( Sql_Tuple, "create_table", "CREATE TABLE IF NOT EXISTS simple_rank (nick VARCHAR(32) NOT NULL PRIMARY KEY, kills INT(12) NOT NULL, ip VARCHAR(15) NOT NULL);" )
}

public create_table(plFailState, Handle:myQuery, plError[ ], plErrorCode, plData[ ], plDataSize[ ], Float:plQueueTime) {
    switch(plFailState) {
        case TQUERY_CONNECT_FAILED:
        {
            log_amx("[SMR] Ne uspqh da se svurja, za da suzdam tablicata! (%i): %s", plErrorCode, plError)
        }
        case TQUERY_QUERY_FAILED:
        {
            log_amx("[SMR] Greshka sus zaqvkata za suzdavane na tablica! (%i): %s", plErrorCode, plError)
        }
    }
}

public plugin_end()
{
    SQL_FreeHandle(Sql_Tuple)
}

public Load_MySql(id)
{
    new szNick[32], Query[512]
    get_user_name(id, szNick, charsmax(szNick))
    new Data[2]
    Data[0] = id
    formatex(Query,charsmax(Query),"SELECT * FROM `simple_rank` WHERE (`simple_rank`.`nick` = '%s')", szNick)
    SQL_ThreadQuery(Sql_Tuple,"save_new_user", Query, Data, sizeof(Data))
}

public client_putinserver(id)
{
    Load_MySql(id)
}

public client_disconnect(id)
{
    new Nick[32]
    get_user_name(id, Nick, charsmax(Nick))
    save_user_information(id, Nick)
}

public client_infochanged(id)
{
    new OldName[32], NewName[32]
    get_user_name(id, OldName, charsmax(OldName))
    get_user_info(id, "name", NewName, charsmax(NewName))
    if(!equali(OldName, NewName))
    {
        new Query[512]
        formatex(Query, charsmax(Query), "UPDATE `simple_rank` SET kills='%i' WHERE nick='%s';", iKills[id], OldName)
        SQL_ThreadQuery(Sql_Tuple, "IgnoreHandle", Query)
        iKills[id] = 0
        Load_MySql(id)
    }
}

public save_new_user(plFailState, Handle:myQuery, plError[ ], plErrorCode, plData[ ], plDataSize[ ], Float:plQueueTime) {
    switch(plFailState) {
        case TQUERY_CONNECT_FAILED:
        {
            log_amx("[SMR] Ne uspqh da se svurja, za da dobavq nov igrach v tablicata! (%i): %s", plErrorCode, plError)
        }
        case TQUERY_QUERY_FAILED:
        {
            log_amx("[SMR] Greshka sus zaqvkata za dobavqna na igrach v tablicata! (%i): %s", plErrorCode, plError)
        }
        default:
        {
            new id
            id = plData[0]
            if(SQL_NumResults(myQuery) < 1)
            {
                new Nick[32], ip[15]
                get_user_name(id, Nick, charsmax(Nick))
                get_user_ip(id, ip, charsmax(ip), 0)
                new Query[512]
                formatex(Query, charsmax(Query), "INSERT INTO `simple_rank` (nick, kills, ip) VALUES('%s', '0', '%s');", Nick, ip)
                SQL_ThreadQuery(Sql_Tuple, "IgnoreHandle", Query)
            }
            else
            {
                new iPlayer = plData[0]
               
                new szName[32]
                SQL_ReadResult(myQuery, 0, szName, charsmax(szName))
               
                if(get_user_index(szName) == iPlayer)
                {
                    iKills[id] = SQL_ReadResult(myQuery, 1)
                }
            }
        }
    }
    return PLUGIN_CONTINUE
}

public save_user_information(id, Nick[32]) {
    new ip[15]
    get_user_name(id, Nick, charsmax(Nick))
    get_user_ip(id, ip, charsmax(ip), 0)
    new Query[512]
    formatex(Query, charsmax(Query), "UPDATE `simple_rank` SET kills='%i', ip='%s' WHERE nick='%s';", iKills[id], ip, Nick)
    SQL_ThreadQuery(Sql_Tuple, "save_user_information_query", Query)
}

public save_user_information_query(plFailState, Handle:myQuery, plError[ ], plErrorCode, plData[ ], plDataSize[ ], Float:plQueueTime) {
    switch(plFailState) {
        case TQUERY_CONNECT_FAILED:
        {
            log_amx("[SMR] Ne uspqh da se svurja, za da updeitna tablicata! (%i): %s", plErrorCode, plError)
        }
        case TQUERY_QUERY_FAILED:
        {
            log_amx("[SMR] Greshka sus zaqvkata za updeit na tablicata! (%i): %s", plErrorCode, plError)
        }
    }
    return PLUGIN_HANDLED
}

public IgnoreHandle(plFailState, Handle:myQuery, plError[ ], plErrorCode, plData[ ], plDataSize[ ], Float:plQueueTime) {
    SQL_FreeHandle(myQuery)
    return PLUGIN_HANDLED
}

public ShowRank(id) {
    for(new i; i < 32; i++)
    {
        if(is_user_connected(i)) {
            new Nick[32]
            get_user_name(id, Nick, charsmax(Nick))
            save_user_information(i, Nick)
        }
    }
    new Query[512]
    new Data[3]
    Data[0] = id
    formatex(Query, charsmax(Query), "SELECT COUNT(*) FROM `simple_rank` WHERE `kills` >= %d", iKills[id])
    SQL_ThreadQuery(Sql_Tuple, "ShowRankQuery", Query, Data, sizeof(Data))
    return PLUGIN_CONTINUE
}

public ShowRankQuery(plFailState, Handle:myQuery, plError[ ], plErrorCode, plData[ ], plDataSize[ ], Float:plQueueTime) {
    switch(plFailState) {
        case TQUERY_CONNECT_FAILED:
        {
            log_amx("[SMR] Ne uspqh da se svurja, za da izvleka informaciq za igracha! (%i): %s", plErrorCode, plError)
        }
        case TQUERY_QUERY_FAILED:
        {
            log_amx("[SMR] Greshka sus zaqvkata za informaciq za igracha! (%i): %s", plErrorCode, plError)
        }
        default:
        {
            new count = 0
            count = SQL_ReadResult(myQuery, 0)
            if(count == 0) {
                count = 1
            }
            new id
            id = plData[0]
            ColorChat(id,GREEN,"[SMR]^x01 Ti si na ^x03%i^x01 mqsto s ^x03%i^x01 ubiistva!", count, iKills[id])
        }
    }
    return PLUGIN_HANDLED
}

/*START - From colorchat.inc by Numb */
ColorChat(id, Color:type, const msg[], {Float, Sql, Result,_}:...) {
    new message[256]

    switch(type) {
        case NORMAL: message[0] = 0x01
        case GREEN: message[0] = 0x04
        default: message[0] = 0x03
    }

    vformat(message[1], 251, msg, 4)

    message[192] = '^0'

    new team, ColorChange, index, MSG_Type

    if(id) {
        MSG_Type = MSG_ONE
        index = id
    } else {
        index = FindPlayer()
        MSG_Type = MSG_ALL
    }

    team = get_user_team(index)
    ColorChange = ColorSelection(index, MSG_Type, type)

    ShowColorMessage(index, MSG_Type, message)

    if(ColorChange)
        Team_Info(index, MSG_Type, TeamName[team])
}

ShowColorMessage(id, type, message[]) {
    static bool:saytext_used
    static get_user_msgid_saytext

    if(!saytext_used) {
        get_user_msgid_saytext = get_user_msgid("SayText")
        saytext_used = true
    }

    message_begin(type, get_user_msgid_saytext, _, id)
    write_byte(id)
    write_string(message)
    message_end()
}

Team_Info(id, type, team[]) {
    static bool:teaminfo_used
    static get_user_msgid_teaminfo

    if(!teaminfo_used) {
        get_user_msgid_teaminfo = get_user_msgid("TeamInfo")
        teaminfo_used = true
    }

    message_begin(type, get_user_msgid_teaminfo, _, id)
    write_byte(id)
    write_string(team)
    message_end()

    return 1
}

ColorSelection(index, type, Color:Type) {
    switch(Type) {
        case RED: return Team_Info(index, type, TeamName[1])
        case BLUE: return Team_Info(index, type, TeamName[2])
        case GREY: return Team_Info(index, type, TeamName[0])
    }

    return 0
}

FindPlayer() {
    new i = -1

    while(i <= get_maxplayers()) {
        if(is_user_connected(++i))
            return i
    }

    return -1
}
/*END - From colorchat.inc by Numb */
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1026\\ f0\\ fs16 \n\\ par }
*/

Sometimes kills for some nicks is reset to 0, maybe the problem is on:
Code:

default:
        {
            new id
            id = plData[0]
            if(SQL_NumResults(myQuery) < 1)
            {
                new Nick[32], ip[15]
                get_user_name(id, Nick, charsmax(Nick))
                get_user_ip(id, ip, charsmax(ip), 0)
                new Query[512]
                formatex(Query, charsmax(Query), "INSERT INTO  `simple_rank` (nick, kills, ip) VALUES('%s', '0', '%s');", Nick, ip)
                SQL_ThreadQuery(Sql_Tuple, "IgnoreHandle", Query)


Can you re-code the script to work correctly ?
Thanks!


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

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