Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.00 KB | None | 0 0
  1. /*
  2.     Example MySQL connect/register/login filterscript (updated for SA:MP 0.3)
  3.    
  4.     This filterscript just works with 1.0.3R2
  5. */
  6.  
  7. #include <a_samp>
  8. #include <a_mysql>
  9.  
  10. //MySQL Configuration
  11. #define SQL_HOST "localhost"
  12. #define SQL_DB "database"
  13. #define SQL_USER "user"
  14. #define SQL_PASS "password"
  15.  
  16. #define TABLENAME "users"
  17.  
  18. #define CONNECT_THREAD_ID 1
  19. #define REG_THREAD_ID 2
  20. #define LOGIN_THREAD_ID 3
  21.  
  22. #define GREY 0xAFAFAFAA
  23. #define RED 0xFF0000AA
  24. #define YELLOW 0xFFFF00AA
  25. #define LIGHTBLUE 0x33CCFFAA
  26.  
  27. #define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
  28.  
  29. new
  30.     bool:LoggedIn[MAX_PLAYERS] = { false, ... },
  31.     bool:AccRegistered[MAX_PLAYERS] = { false, ... },
  32.     Wrongattempt[MAX_PLAYERS],
  33.     TimerSet[MAX_PLAYERS],
  34.     pLogtimer[MAX_PLAYERS],
  35.     PlayerMoney[MAX_PLAYERS],
  36.     CONNECT_PLAYER_ID = INVALID_PLAYER_ID,
  37.     LOGIN_PLAYER_ID = INVALID_PLAYER_ID,
  38.     REG_PLAYER_ID = INVALID_PLAYER_ID;
  39.    
  40. public OnFilterScriptInit()
  41. {
  42.     mysql_debug(1);
  43.     new Connection = mysql_connect(SQL_HOST, SQL_USER,SQL_DB, SQL_PASS);
  44.     if(Connection)
  45.     {
  46.         new dest[200];
  47.         mysql_stat(dest);
  48.         printf(dest);
  49.         printf(">> MySQL Register Script successfully initialized");
  50.         SetupTable();
  51.     }
  52. }
  53.  
  54. SetupTable()
  55. {
  56.     return mysql_query("CREATE TABLE IF NOT EXISTS `"TABLENAME"`(`id` int(11) NOT NULL auto_increment PRIMARY KEY,`Username` varchar(30) NOT NULL,`Password` varchar(50) NOT NULL,`Money` int(10) NOT NULL default '0')");
  57. }
  58.  
  59. public OnQueryFinish(query[],resultid)
  60. {
  61.     new string[128],pName[MAX_PLAYER_NAME+1];
  62.     switch(resultid)
  63.     {
  64.         case CONNECT_THREAD_ID:
  65.         {
  66.             if(IsPlayerConnected(CONNECT_PLAYER_ID))
  67.             {
  68.                 GetPlayerName(CONNECT_PLAYER_ID,pName,sizeof pName);
  69.                 mysql_store_result();
  70.                 if(mysql_num_rows() > 0)
  71.                 {
  72.                     format(string,sizeof(string),">> This account (%s) is registered.Please login by using /login [pass]",pName);
  73.                     SendClientMessage(CONNECT_PLAYER_ID,GREY,string);
  74.                     AccRegistered[CONNECT_PLAYER_ID] = true;
  75.                     pLogtimer[CONNECT_PLAYER_ID] = SetTimerEx("LoginKick",30000,0,"d",CONNECT_PLAYER_ID);
  76.                 }
  77.                 else
  78.                 {
  79.                     format(string,sizeof(string),">> Welcome %s, you can register by using /register [pass]",pName);
  80.                     SendClientMessage(CONNECT_PLAYER_ID,GREY,string);
  81.                     AccRegistered[CONNECT_PLAYER_ID] = false;
  82.                 }
  83.                 mysql_free_result();
  84.             }
  85.             CONNECT_PLAYER_ID = INVALID_PLAYER_ID;
  86.             return 0;
  87.         }
  88.         case REG_THREAD_ID:
  89.         {
  90.             if(IsPlayerConnected(REG_PLAYER_ID))
  91.             {
  92.                 GetPlayerName(REG_PLAYER_ID,pName,sizeof pName);
  93.                 format(string,sizeof(string),">> Account %s successfully registered - Remember your password for later use.",pName);
  94.                 SendClientMessage(REG_PLAYER_ID,GREY,string);
  95.                 SendClientMessage(REG_PLAYER_ID,GREY,"You have been automatically logged in");
  96.                 LoggedIn[REG_PLAYER_ID] = true;
  97.                 AccRegistered[REG_PLAYER_ID] = true;
  98.             }
  99.             REG_PLAYER_ID = INVALID_PLAYER_ID;
  100.             return 0;
  101.         }
  102.         case LOGIN_THREAD_ID:
  103.         {
  104.             if(IsPlayerConnected(LOGIN_PLAYER_ID))
  105.             {
  106.                 mysql_store_result();
  107.                 if(mysql_num_rows() == 1)
  108.                 {
  109.                     PlayerMoney[LOGIN_PLAYER_ID] = mysql_fetch_int();
  110.                     GivePlayerMoney(LOGIN_PLAYER_ID,PlayerMoney[LOGIN_PLAYER_ID]);
  111.                     LoggedIn[LOGIN_PLAYER_ID] = true;
  112.                     format(string,sizeof(string),">> You have been successfully logged in. (Money: %d)",PlayerMoney[LOGIN_PLAYER_ID]);
  113.                     SendClientMessage(LOGIN_PLAYER_ID,GREY,string);
  114.                     mysql_free_result();
  115.                 }
  116.                 else
  117.                 {
  118.                     Wrongattempt[LOGIN_PLAYER_ID] += 1;
  119.                     printf("Bad log in attempt by %s (Total attempts: %d)",pName,Wrongattempt[LOGIN_PLAYER_ID]);
  120.                     if(Wrongattempt[LOGIN_PLAYER_ID] >= 3)
  121.                     {
  122.                         SendClientMessage(LOGIN_PLAYER_ID,RED,">> You have been kicked.( 3 times wrong pass )");
  123.                         mysql_free_result();
  124.                         return Kick(LOGIN_PLAYER_ID);
  125.                     }
  126.                     mysql_free_result();
  127.                     SendClientMessage(LOGIN_PLAYER_ID,RED,">> Wrong Password");
  128.                 }
  129.             }
  130.             LOGIN_PLAYER_ID = INVALID_PLAYER_ID;
  131.             return 0;
  132.         }
  133.     }
  134.     return 1;
  135. }
  136.  
  137. public OnFilterScriptExit()
  138. {
  139.     print("MySQL Register / Login Script has been unloaded");
  140.     if(mysql_ping()) mysql_close();
  141. }
  142.  
  143. MySQLCheck()
  144. {
  145.     if(mysql_ping() == -1)
  146.     {
  147.         mysql_connect(SQL_HOST, SQL_USER,SQL_DB, SQL_PASS);
  148.     }
  149.     return 1;
  150. }
  151.  
  152. RegisterAccount(playerid,pass[])
  153. {
  154.     MySQLCheck();
  155.     new
  156.         pName[MAX_PLAYER_NAME],
  157.         query[256];
  158.        
  159.     GetPlayerName(playerid,pName,sizeof(pName));
  160.     mysql_real_escape_string(pName,pName);
  161.     mysql_real_escape_string(pass,pass);
  162.     format(query,sizeof(query),"INSERT INTO `"TABLENAME"` (Username,Password) VALUES ('%s',md5('%s'))",pName,pass);
  163.     mysql_query(query,REG_THREAD_ID);
  164.     REG_PLAYER_ID = playerid;
  165.     return 1;
  166. }
  167.  
  168. LoginPlayer(playerid,pass[])
  169. {
  170.     new
  171.         pName[MAX_PLAYER_NAME],
  172.         query[256];
  173.  
  174.     GetPlayerName(playerid,pName,sizeof(pName));
  175.     MySQLCheck();
  176.    
  177.     mysql_real_escape_string(pName,pName);
  178.     mysql_real_escape_string(pass,pass);
  179.     format(query,sizeof(query),"SELECT Money FROM `"TABLENAME"` WHERE Username = '%s' AND Password = md5('%s') LIMIT 1",pName,pass);
  180.     mysql_query(query,LOGIN_THREAD_ID);
  181.     LOGIN_PLAYER_ID = playerid;
  182.     return 1;
  183. }
  184.  
  185. public OnPlayerDisconnect(playerid,reason)
  186. {
  187.     if(pLogtimer[playerid] != 0) KillTimer(pLogtimer[playerid]);
  188.     new
  189.         query[300],
  190.         pName[MAX_PLAYER_NAME];
  191.        
  192.     GetPlayerName(playerid,pName,sizeof(pName));
  193.        
  194.     if(LoggedIn[playerid])
  195.     {
  196.         new Float:arm;
  197.         GetPlayerArmour(playerid,arm);
  198.         format(query,sizeof(query),"UPDATE `"TABLENAME"` SET `Money`='%d' WHERE (`Username` = '%s')",GetPlayerMoney(playerid),pName);
  199.         mysql_query(query);
  200.     }
  201.     return 1;
  202. }
  203.  
  204. public OnPlayerConnect(playerid)
  205. {
  206.     new
  207.         query[256],
  208.         pname[MAX_PLAYER_NAME];
  209.     Wrongattempt[playerid] = 0;
  210.     LoggedIn[playerid] = false;
  211.     TimerSet[playerid] = 0;
  212.    
  213.     GetPlayerName(playerid,pname,sizeof(pname));
  214.     MySQLCheck();
  215.     format(query,sizeof(query),"SELECT * FROM `"TABLENAME"` WHERE Username = '%s'",pname);
  216.     mysql_query(query,CONNECT_THREAD_ID);
  217.     CONNECT_PLAYER_ID = playerid;
  218.     SetupTable();
  219.     return 1;
  220. }
  221.  
  222. forward LoginKick(playerid);
  223. public LoginKick(playerid)
  224. {
  225.     if(!LoggedIn[playerid]) KickEx(playerid,"Not logged in");
  226.     else
  227.     {
  228.         KillTimer(pLogtimer[playerid]);
  229.         pLogtimer[playerid] = 0;
  230.     }
  231.     return 1;
  232. }
  233.  
  234. public OnPlayerCommandText(playerid, cmdtext[])
  235. {
  236.     dcmd(register,8,cmdtext);
  237.     dcmd(login,5,cmdtext);
  238.     return 0;
  239. }
  240.  
  241. dcmd_login(playerid, params[])
  242. {
  243.     if(LoggedIn[playerid])
  244.     {
  245.         return SendClientMessage(playerid,RED,">> You are already logged in");
  246.     }
  247.     if(!AccRegistered[playerid])
  248.     {
  249.         return SendClientMessage(playerid,RED,">> This Account is not registered. ( Use /register [pass] )");
  250.     }
  251.     if(!strlen(params))
  252.     {
  253.         return SendClientMessage(playerid,RED,"SYNTAX: /login [password]");
  254.     }
  255.     LoginPlayer(playerid,params);
  256.     return true;
  257. }
  258.  
  259. dcmd_register(playerid, params[])
  260. {
  261.     new pName[MAX_PLAYER_NAME];
  262.     GetPlayerName(playerid,pName,sizeof(pName));
  263.     if(AccRegistered[playerid])
  264.     {
  265.         return SendClientMessage(playerid,RED,">> This account is already registered. ( /login [pass] )");
  266.     }
  267.     if(LoggedIn[playerid])
  268.     {
  269.         return SendClientMessage(playerid,RED,">> You are already logged in");
  270.     }
  271.     if(!strlen(params))
  272.     {
  273.         return SendClientMessage(playerid,RED,"SYNTAX: /register [password]");
  274.     }
  275.     if(strlen(params) < 6)
  276.     {
  277.         return SendClientMessage(playerid,RED,">> The password should contain 6 characters at least.");
  278.     }
  279.     RegisterAccount(playerid,params);
  280.     return 1;
  281. }
  282.  
  283. stock KickEx(playerid,reason[])
  284. {
  285.     new
  286.         string[1000],
  287.         MsgAll[128],
  288.         pName[MAX_PLAYER_NAME];
  289.     GetPlayerName(playerid,pName,sizeof(pName));
  290.     format(string,sizeof(string),"You have been kicked: ");
  291.     strcat(string,reason,sizeof(string));
  292.     SendClientMessage(playerid,RED,string);
  293.     Kick(playerid);
  294.     format(MsgAll,sizeof(MsgAll),">> %s has been kicked.(Reason: %s)",pName,reason);
  295.     SendClientMessageToAll(GREY,MsgAll);
  296.     return 1;
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement