SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Gamemode Scripts

Reply
 
Thread Tools Display Modes
Old 10/11/2016, 01:19 PM   #1
cv47necop
Little Clucker
 
cv47necop's Avatar
 
Join Date: May 2012
Location: South Korea
Posts: 15
Reputation: 21
Default [TUTORIAL and Gamemode] Easy-mysql_samp-gamemode (Login/Regist and Money, Skin SAVE , ServerThread)

Github : https://github.com/u4bi/Easy-mysql_samp-gamemode

Code:
/*@ mysql_mode @ https://github.com/u4bi
*/

#include <a_samp>
#include <a_mysql>
#include <foreach>
 
#define DL_LOGIN    100
#define DL_REGIST   101
main(){}
 
forward check(playerid);
forward regist(playerid, pass[]);
forward save(playerid);
forward load(playerid);
forward ServerThread();

//=================================================================================================================== 
/* MODEL

    @ USER   : USER_MODEL

               ID, NAME, PASS, MONEY, SKIN

    @ INGAME : INGAME_MODEL

               LOGIN : in login true/ not login false

    @ mysql : handle static

*/
enum USER_MODEL{
    ID,
    NAME[MAX_PLAYER_NAME],
    PASS[24],
    MONEY,
    SKIN,
}
new USER[MAX_PLAYERS][USER_MODEL];
 
enum INGAME_MODEL{
    bool:LOGIN
}
new INGAME[MAX_PLAYERS][INGAME_MODEL];

static mysql;
//===================================================================================================================
/* CALLBACK FUNCTION

    @ OnGameModeExit
    @ OnGameModeInit
    @ OnPlayerRequestClass
    @ OnDialogResponse
    @ OnPlayerCommendText
    @ OnPlayerDisconnect
*/

public OnGameModeExit(){return 1;}
public OnGameModeInit(){
    mode(); server(); dbcon(); thread();
    return 1;
}

public OnPlayerRequestClass(playerid, classid){
    
    if(INGAME[playerid][LOGIN]) return SendClientMessage(playerid,-1,"already login");
 
    join(playerid, check(playerid));
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]){
    
    if(!response) if(dialogid == DL_LOGIN || dialogid == DL_REGIST) return Kick(playerid);
 
    switch(dialogid){
        case DL_LOGIN  : checked(playerid, inputtext);
        case DL_REGIST : regist(playerid, inputtext);
    }
    return 1;
}

public OnPlayerCommandText(playerid, cmdtext[]){
    if(!strcmp("/sav", cmdtext)){
        
        if(!INGAME[playerid][LOGIN]) return SendClientMessage(playerid,-1,"not login");
 
        save(playerid);
        SendClientMessage(playerid,-1,"data save");
        return 1;
    }
    return 0;
}

public OnPlayerDisconnect(playerid, reason){
    
    if(INGAME[playerid][LOGIN]) save(playerid);
 
    cleaning(playerid);
    return 1;
}

//===================================================================================================================
/* REG/LOG CHECK MANAGER

    @checked(playerid, password[])
    @join(playerid, type)
*/

stock checked(playerid, password[]){
    
    if(strlen(password) == 0) return join(playerid, 1), SendClientMessage(playerid,-1,"password length");
    if(strcmp(password, USER[playerid][PASS])) return join(playerid, 1), SendClientMessage(playerid,-1,"login fail");
 
    SendClientMessage(playerid,-1,"login success");
    INGAME[playerid][LOGIN] = true;
    load(playerid);
    return 1;
}

stock join(playerid, type){
    switch(playerid, type){
        case 0 : ShowPlayerDialog(playerid, DL_REGIST, DIALOG_STYLE_PASSWORD, "manager", "Regist plz", "join", "quit");
        case 1 : ShowPlayerDialog(playerid, DL_LOGIN, DIALOG_STYLE_PASSWORD, "manager", "Login plz", "join", "quit");
    }
    return 1;
}

//===================================================================================================================
/* SQL

    @ check(playerid)
    @ regist(playerid, pass)
    @ save(playerid)
    @ load(playerid)
    @ escape(str[])
*/

public check(playerid){
    new query[128], result;
    GetPlayerName(playerid, USER[playerid][NAME], MAX_PLAYER_NAME);
 
    mysql_format(mysql, query, sizeof(query), "SELECT ID, PASS FROM `userlog_info` WHERE `NAME` = '%s' LIMIT 1", escape(USER[playerid][NAME]));
    mysql_query(mysql, query);
 
    result = cache_num_rows();
    if(result){
        USER[playerid][ID]      = cache_get_field_content_int(0, "ID");
        cache_get_field_content(0, "PASS", USER[playerid][PASS], mysql, 24);
    }
    return result;
}

public regist(playerid, pass[]){
 
    format(USER[playerid][PASS],24, "%s",pass);
 
    new query[256];
    mysql_format(mysql, query, sizeof(query), "INSERT INTO `userlog_info` (`NAME`,`PASS`,`MONEY`,`SKIN`) VALUES ('%s','%s',%d,%d)",
    escape(USER[playerid][NAME]), escape(USER[playerid][PASS]), USER[playerid][MONEY] = 1000, USER[playerid][SKIN] = 129);
 
    mysql_query(mysql, query);
    USER[playerid][ID] = cache_insert_id();
 
    SendClientMessage(playerid,-1,"regist success");
    INGAME[playerid][LOGIN] = true;
    spawn(playerid);
}

public save(playerid){
    new query[256];
    mysql_format(mysql, query, sizeof(query), "UPDATE `userlog_info` SET `MONEY`=%d,`SKIN`=%d WHERE `ID`=%d",
    USER[playerid][MONEY], USER[playerid][SKIN], USER[playerid][ID]);
 
    mysql_query(mysql, query);
}

public load(playerid){
    new query[256];
    mysql_format(mysql, query, sizeof(query), "SELECT * FROM `userlog_info` WHERE `ID` = %d LIMIT 1", USER[playerid][ID]);
    mysql_query(mysql, query);
 
    USER[playerid][MONEY]   = cache_get_field_content_int(0, "MONEY");
    USER[playerid][SKIN]    = cache_get_field_content_int(0, "SKIN");
 
    spawn(playerid);
}

stock escape(str[]){
    new result[24];
    mysql_real_escape_string(str, result);
    return result;
}

//===================================================================================================================
/* INGAME FUNCTION

    @ spawn(playerid)
*/

stock spawn(playerid){
 
    SetSpawnInfo(playerid, 0, USER[playerid][SKIN], 0.0, 0.0, 0.0, 180, 0, 0, 0, 0, 0, 0);
 
    SpawnPlayer(playerid);
    ResetPlayerMoney(playerid);
    GivePlayerMoney(playerid, USER[playerid][MONEY]);
}

//===================================================================================================================
/* INIT 

    @ mode()
    @ thread()
    @ server()
    @ dbcon()
    @ cleaning(playerid)
*/

stock mode(){}
stock thread(){ SetTimer("ServerThread", 500, true); }
stock server(){
    SetGameModeText("Blank Script");
    AddPlayerClass(0,0,0,0,0,0,0,0,0,0,0);
}

/* TODO

    GITHUB README.MD

*/
stock dbcon(){
    new db_key[4][128] = {"hostname", "username", "database", "password"}, db_value[4][128];
    new File:cfg=fopen("database.cfg", io_read), temp[64], tick =0;
 
    while(fread(cfg, temp)){
        if(strcmp(temp, db_key[tick])){
            new pos = strfind(temp, "=");
            strdel(temp, 0, pos+1);
            new len = strlen(temp);
            if(tick != 3)strdel(temp, len-2, len);
            db_value[tick] = temp;
        }
        tick++;
    }
    
    mysql = mysql_connect(db_value[0], db_value[1], db_value[2], db_value[3]);
    mysql_set_charset("euckr");
 
    if(!mysql_errno(mysql))print("db connection success.");
}

stock cleaning(playerid){
    new temp[USER_MODEL], temp2[INGAME_MODEL];
    USER[playerid] = temp;
    INGAME[playerid] = temp2;
}

//===================================================================================================================
/* SERVER THREAD
*/

public ServerThread(){
    foreach (new i : Player){ eventMoney(i);
    }
}

//===================================================================================================================
/* stock
    @ eventMoney(playerid)
    @ giveMoney(playerid, money)
*/

stock eventMoney(playerid){ giveMoney(playerid, 1);
}

stock giveMoney(playerid,money){
    ResetPlayerMoney(playerid);
    GivePlayerMoney(playerid, USER[playerid][MONEY]+=money);
}

team-devSAMPKOR : https://github.com/team-devSAMPKOR

KOR_Billiards_Gamemode_ver0.5
https://youtu.be/eX5DijQr60M
https://youtu.be/eSb1dBP0y_Q

email : u4bi@u4bi.com
Github : https://github.com/u4bi
******** : https://www.********.com/ymjcode
Let's be friends! ********! Github ! friend and follow please!!

Last edited by cv47necop; 10/11/2016 at 03:03 PM.
cv47necop is offline   Reply With Quote
Old 10/11/2016, 01:28 PM   #2
Slawiii
Huge Clucker
 
Slawiii's Avatar
 
Join Date: May 2015
Location: Morocco, Missour
Posts: 293
Reputation: 16
Default Re: [TUTORIAL and Gamemode] Easy-mysql_samp-gamemode (Login/Regist and Money, Skin SAVE)

Good job !
Slawiii is offline   Reply With Quote
Old 10/11/2016, 03:01 PM   #3
cv47necop
Little Clucker
 
cv47necop's Avatar
 
Join Date: May 2012
Location: South Korea
Posts: 15
Reputation: 21
Default Re: [TUTORIAL and Gamemode] Easy-mysql_samp-gamemode (Login/Regist and Money, Skin SAVE)

haha! thank you! : )
cv47necop 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
[GameMode] Easy Simple MySQL Login/Regist/Save GameMode cv47necop Gamemode Scripts 4 16/10/2016 01:01 AM
Problem in login and regist mamalzeus Scripting Help 3 01/12/2014 07:21 PM
[FilterScript] Save Kills - Score - Money - Skin - Deaths! [FS] Eminem 2ka9 Filterscripts 10 22/02/2014 02:57 PM
Uhm, how to add skin-save option to this gamemode [JnA]DukeNukem Help Archive 2 26/04/2011 01:32 PM


All times are GMT. The time now is 09:53 PM.


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