Raised This Month: $ Target: $400
 0% 

Solved [HELP] Data saving


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Airkish
AlliedModders Donor
Join Date: Apr 2016
Location: Lithuania
Old 03-10-2017 , 20:21   [HELP] Data saving
Reply With Quote #1

Found an achievements plugin which fits my needs, however there's a problem, plugin should save data on MySQL but it doesn't.
Could you guys look through code and fix it?
Thanks.

PHP Code:
#include <amxmodx>
#include <cstrike>
#include <sqlx>

enum Color
{
    
NORMAL 1// clients scr_concolor cvar color
    
GREEN// Green Color
    
TEAM_COLOR// Red, grey, blue
    
GREY// grey
    
RED// Red
    
BLUE// Blue
}

new 
TeamName[][] = 
{
    
"",
    
"TERRORIST",
    
"CT",
    
"SPECTATOR"
}

new 
Host[]      = ""
new User[]      = ""
new Pass[]      = ""
new Db[]        = ""

new Handle:g_SqlTuple
new g_Error[512]

enum _:Achieves // Define the available Achievements, dont forget commas
{
    
MURDER,        // Done
    
SURVIVOR,    // Done
    
AIMKEY,        // Done
    
EXECUTION,    // Done
    
ONEHPHERO    // Done
}

new const 
AchName[Achieves][] = // Define the Achievements Names, dont forget commas
{
    
"I like Murdering",
    
"Survivor",
    
"Aimkey",
    
"Round of Execution",
    
"1 HP Hero"
}

new const 
AchDescription[Achieves][] = // Define the Achievements Description, dont forget commas
{
    
"Kill 500 Enemies",
    
"Surieve 250 Rounds",
    
"Make 125 Headshots",
    
"Kill 5 Enemies in one Round",
    
"Kill 5 Enemies with 1 HP"
}

new const 
AchRequirement[Achieves] = // Define Requirements. For non progress achievements use 1 (Like Secret Phrase), dont forget commas
{
    
500,
    
250,
    
125,
    
5,
    
5
}

new const 
prefix[] = "^4[ClanTag] Achievements:^3"

new Achievement[33][Achieves]
new 
iAchTag[33]
new 
szTemp[555 char]

public 
plugin_init() 
{
    
register_plugin("Achievements""5.0""fLaXXiE")
    
    
register_event("DeathMsg""Event_DeathMsg""a")
    
register_logevent("EventRoundStart"2"1=Round_Start")
    
    
register_clcmd("say /achievements""CmdAch")
    
register_clcmd("say /ach""CmdAch")
    
register_clcmd("say /a""CmdAch")
    
    
register_clcmd("say""CmdSay")
    
register_clcmd("say_team""CmdSayTeam")
    
    
set_task(1.0"MySql_Init")
}

public 
client_putinserver(iPlayer) {
    
Load_MySql(iPlayer)
}

public 
client_disconnect(iPlayer)
{
    
Save_MySql(iPlayer)
    
iAchTag[iPlayer] = 0
}

public 
plugin_end() {
    
SQL_FreeHandle(g_SqlTuple)
}

public 
CmdAch(iPlayer)
{
    new 
g_AchMenu menu_create("\wAchievements^n^n""Achievements_Handler")
    
    new 
Key[6]
    
    for(new 
iAch 0iAch AchievesiAch++)
    {
        if(
Achievement[iPlayer][iAch] == 0formatex(szTempcharsmax(szTemp), "\d%s - [%s] [%d/%d]"AchName[iAch], AchDescription[iAch], Achievement[iPlayer][iAch], AchRequirement[iAch])
        else if(
Achievement[iPlayer][iAch] >= AchRequirement[iAch]) formatex(szTempcharsmax(szTemp), "\y%s \w- [\rCOMPLETED\w]"AchName[iAch])
        else 
formatex(szTempcharsmax(szTemp), "\y%s \w- [\r%s\w]\w [\r%d\w/\r%d\w]"AchName[iAch], AchDescription[iAch], Achievement[iPlayer][iAch], AchRequirement[iAch])
        
menu_additem(g_AchMenuszTempKey)
    }
    
    
menu_setprop(g_AchMenuMPROP_EXITMEXIT_ALL)
    
menu_display(iPlayerg_AchMenu0)
}

public 
Achievements_Handler(iPlayerg_AchMenuitem

    if(
item == MENU_EXIT
    { 
        
menu_destroy(g_AchMenu)
        return 
PLUGIN_HANDLED 
    
}
    else
    {
        if(
Achievement[iPlayer][item] >= AchRequirement[item]) 
        { 
            
iAchTag[iPlayer] = item+1
            ColorChat 
(iPlayerGREY"%s You have chosen^4 %s^3 as your tag"prefixAchName[item]) 
        } 
        else 
        { 
                
ColorChat(iPlayerGREY"%s You have not unlocked the achievement yet."prefix
        } 
    } 
    return 
PLUGIN_CONTINUE 
}  

///////////////////////////////////////////////////
///////// START OF ACHIEVEMENT ACTIONS ////////////
///////////////////////////////////////////////////

public Event_DeathMsg()
{
    new 
iKiller read_data(1)
    new 
iVictim read_data(2)
    new 
szName[33]; get_user_name(iKillerszNamecharsmax(szName))
    
    if(
is_user_alive(iKiller) && get_user_team(iKiller) != get_user_team(iVictim))
    {
        
Achievement[iKiller][EXECUTION]++
        
        if(
Achievement[iKiller][EXECUTION] == AchRequirement[EXECUTION])
        {
            
ColorChat(0GREY"%s %s successfully unlocked the achievement %s!"prefixszNameAchName[EXECUTION])
        }
        
        if(
is_user_alive(iKiller) && get_user_team(iKiller) != get_user_team(iVictim))
        {
            
Achievement[iKiller][MURDER]++
        
            if(
Achievement[iKiller][MURDER] == AchRequirement[MURDER])
            {
                
ColorChat(0GREY"%s %s successfully unlocked the achievement %s!"prefixszNameAchName[MURDER])
            }
        
        }
        
        if(
get_user_health(iKiller) == 1)
        {
            
Achievement[iKiller][ONEHPHERO]++
            
            if(
Achievement[iKiller][ONEHPHERO] == AchRequirement[ONEHPHERO])
            {
                
ColorChat(0GREY"%s %s successfully unlocked the achievement %s!"prefixszNameAchName[ONEHPHERO])
            }
        }
        if(
read_data(3))
        {    
            
Achievement[iKiller][AIMKEY]++
            
            if(
Achievement[iKiller][AIMKEY] == AchRequirement[AIMKEY])
            {
                
ColorChat(0GREY"%s %s successfully unlocked the achievement %s!"prefixszNameAchName[AIMKEY])
            }
        }
    }
    return 
PLUGIN_CONTINUE
}

public 
EventRoundStart()
{
    new 
iPlayers[32], iNumiPlayerszName[33]
    
get_players(iPlayersiNum"a")
    
    for(new 
0iNumi++)
    {
        
iPlayer iPlayers[i]
        
get_user_name(iPlayerszNamecharsmax(szName))
        
        if(
Achievement[iPlayer][EXECUTION] < 5)
            
Achievement[iPlayer][EXECUTION] = 0
    
        
if(is_user_alive(iPlayer))
        {
            
Achievement[iPlayer][SURVIVOR]++
                
            if(
Achievement[iPlayer][SURVIVOR] == AchRequirement[SURVIVOR])
            {
                
ColorChat(0GREY"%s %s successfully unlocked the achievement %s!"prefixszNameAchName[SURVIVOR])
            }
        }
    }
    return 
PLUGIN_CONTINUE
}

///////////////////////////////////////////////////
////////// END OF ACHIEVEMENT ACTIONS /////////////
///////////////////////////////////////////////////

///////////////////////////////////////////////////
///////// START OF ACHIEVEMENT SAVING /////////////
///////////////////////////////////////////////////

public MySql_Init()
{
    
g_SqlTuple SQL_MakeDbTuple(Host,User,Pass,Db)
   
    new 
ErrorCode,Handle:SqlConnection SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
    
    if(
SqlConnection == Empty_Handle)
        
set_fail_state(g_Error)
       
    new 
Handle:Queries
    Queries 
SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS achievements(steamid varchar(32), tag varchar(32), murder SMALLINT(1), survivor SMALLINT(1), aimkey SMALLINT(1), threesome SMALLINT(1), onehphero SMALLINT(1))")

    if(!
SQL_Execute(Queries))
    {
        
SQL_QueryError(Queries,g_Error,charsmax(g_Error))
        
set_fail_state(g_Error)
    }
    
    
SQL_FreeHandle(Queries)
    
SQL_FreeHandle(SqlConnection)   
}

public 
Load_MySql(iPlayer)
{
    new 
szSteamId[32], szTemp[512]
    
get_user_authid(iPlayerszSteamIdcharsmax(szSteamId))
    
    new 
Data[1]
    
Data[0] = iPlayer

    format
(szTemp,charsmax(szTemp),"SELECT * FROM `achievements` WHERE (`achievements`.`steamid` = '%s')"szSteamId)
    
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1)
}

public 
register_client(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    if(
FailState == TQUERY_CONNECT_FAILED)
    {
        
log_amx("Load - Could not connect to SQL database.  [%d] %s"ErrcodeError)
    }
    else if(
FailState == TQUERY_QUERY_FAILED)
    {
        
log_amx("Load Query failed. [%d] %s"ErrcodeError)
    }

    new 
iPlayer
    iPlayer 
Data[0]
    
    if(
SQL_NumResults(Query) < 1
    {    
        new 
szSteamId[32]
        
get_user_authid(iPlayerszSteamIdcharsmax(szSteamId))
        
        if (
equal(szSteamId,"ID_PENDING"))
            return 
PLUGIN_HANDLED
            
        
new szTemp[512]
        
        
format(szTemp,charsmax(szTemp),"INSERT INTO `achievements` (`steamid` , `tag`, `murder`, `survivor`, `aimkey`, `execution`, `onehphero`)VALUES ('%s', '0', '0', '0', '0', '0', '0')"szSteamId)
        
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
    } 
    else 
    {
        
iAchTag[iPlayer]        = SQL_ReadResult(Query1)
        
Achievement[iPlayer][MURDER]    = SQL_ReadResult(Query2)
        
Achievement[iPlayer][SURVIVOR]    = SQL_ReadResult(Query3)
        
Achievement[iPlayer][AIMKEY]    = SQL_ReadResult(Query4)
        
Achievement[iPlayer][EXECUTION]    = SQL_ReadResult(Query5)
        
Achievement[iPlayer][ONEHPHERO]    = SQL_ReadResult(Query6)
    }
    
    return 
PLUGIN_HANDLED
}

public 
Save_MySql(iPlayer)
{
    new 
szSteamId[32], szTemp[512]
    
get_user_authid(iPlayerszSteamIdcharsmax(szSteamId))
    
    
format(szTemp,charsmax(szTemp),"UPDATE `achievements` SET `tag` = '%d', `murder` = '%d', `survivor` = '%d', `aimkey` = '%d', `execution` = '%d', `onehphero` = '%d' WHERE `achievements`.`steamid` = '%s';"iAchTag[iPlayer], Achievement[iPlayer][MURDER], Achievement[iPlayer][SURVIVOR], Achievement[iPlayer][AIMKEY], Achievement[iPlayer][EXECUTION], Achievement[iPlayer][ONEHPHERO], szSteamId)
    
    
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
}

public 
IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    
SQL_FreeHandle(Query)
    return 
PLUGIN_HANDLED
}
///////////////////////////////////////////////////
////////// END OF ACHIEVEMENT SAVING //////////////
///////////////////////////////////////////////////

///////////////////////////////////////////////////
///////// START OF ACHIEVEMENT COLORCHAT //////////
///////////////////////////////////////////////////

ColorChat(idColor:type, const msg[], {Float,Sql,Result,_}:...)
{
    static 
message[256];

    switch(
type)
    {
        case 
NORMAL// clients scr_concolor cvar color
        
{
            
message[0] = 0x01;
        }
        case 
GREEN// Green
        
{
            
message[0] = 0x04;
        }
        default: 
// White, Red, Blue
        
{
            
message[0] = 0x03;
        }
    }

    
vformat(message[1], 251msg4);

    
// Make sure message is not longer than 192 character. Will crash the server.
    
message[192] = '^0';

    static 
teamColorChangeindexMSG_Type;
    
    if(
id)
    {
        
MSG_Type MSG_ONE;
        
index id;
    } else {
        
index FindPlayer();
        
MSG_Type MSG_ALL;
    }
    
    
team get_user_team(index);
    
ColorChange ColorSelection(indexMSG_Typetype);

    
ShowColorMessage(indexMSG_Typemessage);
        
    if(
ColorChange)
    {
        
Team_Info(indexMSG_TypeTeamName[team]);
    }
}

ShowColorMessage(idtypemessage[])
{
    
message_begin(typeget_user_msgid("SayText"), _id);
    
write_byte(id)        
    
write_string(message);
    
message_end();    
}

Team_Info(idtypeteam[])
{
    
message_begin(typeget_user_msgid("TeamInfo"), _id);
    
write_byte(id);
    
write_string(team);
    
message_end();

    return 
1;
}

ColorSelection(indextypeColor:Type)
{
    switch(
Type)
    {
        case 
RED:
        {
            return 
Team_Info(indextypeTeamName[1]);
        }
        case 
BLUE:
        {
            return 
Team_Info(indextypeTeamName[2]);
        }
        case 
GREY:
        {
            return 
Team_Info(indextypeTeamName[0]);
        }
    }

    return 
0;
}

FindPlayer()
{
    static 
i;
    
= -1;

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

    return -
1;
}
///////////////////////////////////////////////////
///////// END OF ACHIEVEMENT COLORCHAT ////////////
///////////////////////////////////////////////////

/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ ansicpg1252\\ deff0\\ deflang1053{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ f0\\ fs16 \n\\ par }
*/ 
__________________

Last edited by Airkish; 03-11-2017 at 07:45.
Airkish is offline
yas17sin
Veteran Member
Join Date: Oct 2016
Location: Morocco/Sale
Old 03-10-2017 , 20:44   Re: [HELP] Data saving
Reply With Quote #2

maybe because you didn't fill the needed info, here :
PHP Code:
new Host[]      = ""
new User[]      = ""
new Pass[]      = ""
new Db[]        = "" 
__________________
FINISHED WORKING ON : Attack On Titan Mod (100% Done).

FB Acc : FaceBook Account.

pic: http://prntscr.com/fszkke not good quality

Last edited by yas17sin; 03-10-2017 at 20:44.
yas17sin is offline
Send a message via ICQ to yas17sin
Airkish
AlliedModders Donor
Join Date: Apr 2016
Location: Lithuania
Old 03-11-2017 , 03:26   Re: [HELP] Data saving
Reply With Quote #3

Quote:
Originally Posted by yas17sin View Post
maybe because you didn't fill the needed info, here :
PHP Code:
new Host[]      = ""
new User[]      = ""
new Pass[]      = ""
new Db[]        = "" 
That's not the case here, I just deleted it to not make it public.

P.S.
Plugin creates sql table
__________________
Airkish is offline
yas17sin
Veteran Member
Join Date: Oct 2016
Location: Morocco/Sale
Old 03-11-2017 , 08:53   Re: [HELP] Data saving
Reply With Quote #4

try to create the database and the table and fill it manual.
__________________
FINISHED WORKING ON : Attack On Titan Mod (100% Done).

FB Acc : FaceBook Account.

pic: http://prntscr.com/fszkke not good quality
yas17sin is offline
Send a message via ICQ to yas17sin
Airkish
AlliedModders Donor
Join Date: Apr 2016
Location: Lithuania
Old 03-11-2017 , 10:48   Re: [HELP] Data saving
Reply With Quote #5

Quote:
Originally Posted by yas17sin View Post
try to create the database and the table and fill it manual.
Nah, I just missed some parts when edited.
__________________
Airkish is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:29.


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