SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 05/01/2014, 12:30 PM   #1
Face9000
Banned
 
Join Date: Jan 2010
Location: Pigland
Posts: 3,351
Reputation: 190
Default Optimize code

Hello, is there a way to optimize this code and remove useless things which increases server load?

pawn Code:
stock MySQL_Register(playerid, passwordstring[])
{
    new query1[570], pname[24];
    new Year, Month, Day, Hour, Minute, Second;
    getdate(Year, Month, Day);
    gettime(Hour, Minute, Second);
    new timeReg[200];
    format(timeReg, sizeof(timeReg), "%02d/%02d/%02d - %02d:%02d:%02d", Day,Month,Year,Hour,Minute,Second);
    new Bannedby[20];
    format(Bannedby, 20, "Not Banned");
    GetPlayerName(playerid, pname, 24);
    new buff[129];
    WP_Hash(buff, 129, passwordstring);
    format(query1, sizeof(query1), "INSERT INTO playerdata (user, password, Scores, Cash, Admin, Deaths, Nopm, Muted, Vip, Warn, RegisterDate, Jailed, AdminActions, Cookies, Rank, VipTime, Class, PrototypesStolen, KillAsUsaTeam, KillAsGermanyTeam, KillAsItalyTeam, KillAsJapanTeam, KillAsMercTeam, KillAsMexicoTeam, IP) VALUES('%s', '%s', 0, 0, 0, 0, 0, 0, 0, 0, '%s', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '%s')", pname, buff, timeReg, GetIP(playerid));
    mysql_query(query1);
    Logged[playerid] = 1;
    WindowForPlayer(playerid, "You are now ~r~registered ~w~to Battlefield War Heroes! Enjoy playing!");
    WelcomeDialog(playerid);
    new echo[130];
    new kname[24];
    GetPlayerName(playerid, kname, 24);
    format(echo,sizeof(echo),"0,3** System: %s (%d) (IP: %s) has registered a new account",pname,playerid,GetIP(playerid));
    IRC_GroupSay(gGroupID, IRC_ACHANNEL, echo);
    new admin[128];
    format(admin,sizeof(admin),"** System: %s (%d) (%s) has registered a new account.",pname,playerid,GetIP(playerid));
    SendMessageToAdmins(LIGHTBLUE,admin);
    new txd[128];
    format(txd,sizeof(txd),"%s (%d) (IP: %s) registered a new account",pname,playerid,GetIP(playerid));
    TextDrawSetString(RegTxd, txd);
    new welcome[130];
    format(welcome, sizeof(welcome), "Welcome to Battlefield War Heroes, ~r~%s! Type ~r~/rules ~w~and ~r~/cmds ~w~for server info.",pname);
    GameTextForPlayer(playerid,welcome,6000,4);
    return 1;
}

stock MySQL_Login(playerid)
{
    new query2[650], pname[24], savingstring[20];
    WP_Hash(query2, 650, savingstring);
    GetPlayerName(playerid, pname, 24);
    format(query2, sizeof(query2), "SELECT Scores, Cash, Admin, Deaths, Nopm, Muted, Vip, Warn, RegisterDate, Jailed, AdminActions, Cookies, Rank, VipTime, Class, PrototypesStolen, KillAsUsaTeam, KillAsGermanyTeam, KillAsItalyTeam, KillAsJapanTeam, KillAsMercTeam, KillAsMexicoTeam FROM playerdata WHERE user = '%s'", pname);
    mysql_query(query2);
    mysql_store_result();
    while(mysql_fetch_row_format(query2,"|"))
    {
        mysql_fetch_field_row(savingstring, "Scores"); SetPlayerScore(playerid, strval(savingstring));
        mysql_fetch_field_row(savingstring, "Cash"); MoneyGiven[playerid] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Admin"); PlayerInfo[playerid][Admin] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Deaths"); PlayerInfo[playerid][Deaths] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Nopm"); PlayerInfo[playerid][Nopm] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Muted"); PlayerInfo[playerid][Muted] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Vip"); PlayerInfo[playerid][Vip] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Warn"); PlayerInfo[playerid][Warn] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "RegisterDate"); PlayerInfo[playerid][RegisterDate] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Jailed"); PlayerInfo[playerid][Jailed] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "AdminActions"); PlayerInfo[playerid][AdminActions] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Cookies"); PlayerInfo[playerid][Cookies] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Rank"); PlayerInfo[playerid][Rank] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "VipTime"); PlayerInfo[playerid][VipTime] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Class"); PlayerInfo[playerid][Class] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "PrototypesStolen"); PlayerInfo[playerid][PrototypesStolen] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Exp"); PlayerInfo[playerid][Exp] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "Duty"); PlayerInfo[playerid][Duty] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "KillAsUsaTeam"); PlayerInfo[playerid][KillAsUsaTeam] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "KillAsGermanyTeam"); PlayerInfo[playerid][KillAsGermanyTeam] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "KillAsItalyTeam"); PlayerInfo[playerid][KillAsItalyTeam] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "KillAsJapanTeam"); PlayerInfo[playerid][KillAsJapanTeam] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "KillAsMercTeam"); PlayerInfo[playerid][KillAsMercTeam] = strval(savingstring);
        mysql_fetch_field_row(savingstring, "KillAsMexicoTeam"); PlayerInfo[playerid][KillAsMexicoTeam] = strval(savingstring);
    }
    mysql_free_result();
    JustLogged[playerid] = 1;
    Logged[playerid] = 1;
    UpdateStatsTextdraw(playerid);
    SendClientMessage(playerid,red,"------------------------------------------------------");
    new playername3[MAX_PLAYER_NAME];
    GetPlayerName(playerid, playername3, sizeof(playername3));
    new string[128];
    format(string,sizeof(string),"* System: Welcome back {F70505}%s (%d) {FFFFFF} - Your stats have been restored.",playername3,playerid);
    SCM(playerid,-1,string);
    SendClientMessage(playerid,red,"------------------------------------------------------");


    return 1;
}
Face9000 is offline   Reply With Quote
Old 05/01/2014, 12:38 PM   #2
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,067
Reputation: 2660
Default Re: Optimize code

For one, you could save their registration date as a (unix) timestamp, eliminating the need for getdate, gettime and the related variables. Also set default values in your columns. You're inserting only four variables. Furthermore, you're getting the player's name twice, to two different variables. I have no idea why. Lastly, seriously consider using threaded queries and caching. That's probably the biggest optimization of them all.
__________________
Vince is offline   Reply With Quote
Old 05/01/2014, 06:52 PM   #3
Face9000
Banned
 
Join Date: Jan 2010
Location: Pigland
Posts: 3,351
Reputation: 190
Default Re: Optimize code

Im new to mySQL, can you please show me how i can do threaded queries and use cache?
Face9000 is offline   Reply With Quote
Old 05/01/2014, 06:55 PM   #4
sammp
Gangsta
 
sammp's Avatar
 
Join Date: Jan 2014
Posts: 584
Reputation: 49
Default Re: Optimize code

http://forum.sa-mp.com/showthread.php?t=337810

That tutorial provides what you're looking for, Face9000.
sammp is offline   Reply With Quote
Old 05/01/2014, 10:43 PM   #5
Face9000
Banned
 
Join Date: Jan 2010
Location: Pigland
Posts: 3,351
Reputation: 190
Default Re: Optimize code

Thanks
Face9000 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Optimize this part of code Face9000 Scripting Help 3 29/12/2013 09:51 AM
Better way to optimize this? sim_sima Scripting Help 5 15/12/2013 07:20 PM
OPTIMIZE CODE!!!!!!!! Nadim Scripting Help 2 01/11/2013 03:43 PM
help me to optimize my code armyoftwo Help Archive 3 14/11/2010 10:19 AM


All times are GMT. The time now is 08:22 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.