daily pastebin goal
17%
SHARE
TWEET

Untitled

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