whitetiiger

SAMP MySQL V39-5 Example

Jul 15th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 7.71 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3.  
  4. native WP_Hash(buffer[], len, const str[]);
  5.  
  6. #define WHITE 0xFFFFFFAA
  7. #define GREY 0xAFAFAFAA
  8. #define RED 0xFF0000AA
  9. #define YELLOW 0xFFFF00AA
  10. #define LIGHTBLUE 0x33CCFFAA
  11.  
  12. #define CHAT_WHITE "{FFFFFF}"
  13. #define CHAT_GREY "{AFAFAF}"
  14. #define CHAT_RED "{FF0000}"
  15. #define CHAT_YELLOW "{FFFF00}"
  16. #define CHAT_LIGHTBLUE "{33CCFF}"
  17.  
  18. forward OnPlayerDataLoaded(playerid, race_check);
  19. forward OnPlayerRegister(playerid);
  20.  
  21.  
  22. //MySQL configuration
  23. #define SQL_HOST "127.0.0.1"
  24. #define SQL_DB "database"
  25. #define SQL_USER "username"
  26. #define SQL_PASS "password"
  27.  
  28. //MySQL connection handle
  29. new g_SQL = -1;
  30.  
  31. //player data
  32. enum E_PLAYERS
  33. {
  34.     ID,
  35.     Name[MAX_PLAYER_NAME],
  36.     Password[129],
  37.     Money,
  38.    
  39.     bool:IsLoggedIn,
  40.     bool:IsRegistered,
  41.     LoginAttempts,
  42.     LoginTimer
  43. };
  44. new Player[MAX_PLAYERS][E_PLAYERS];
  45.  
  46. new g_MysqlRaceCheck[MAX_PLAYERS];
  47.  
  48. //dialog data
  49. enum
  50. {
  51.     DIALOG_INVALID,
  52.     DIALOG_UNUSED,
  53.    
  54.     DIALOG_LOGIN,
  55.     DIALOG_REGISTER,
  56. };
  57.  
  58.  
  59. /*
  60.  * SA-MP callbacks
  61.  */
  62.  
  63. public OnGameModeInit()
  64. {
  65.     mysql_log(LOG_ERROR | LOG_WARNING, LOG_TYPE_HTML); //logs errors and warnings into a nice .html log file
  66.     g_SQL = mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS);
  67.    
  68.     SetupPlayerTable();
  69.     return 1;
  70. }
  71.  
  72. public OnGameModeExit()
  73. {
  74.     //save all player data before closing connection
  75.     for(new p=0; p < MAX_PLAYERS; ++p)
  76.         if(IsPlayerConnected(p))
  77.             UpdatePlayerData(p);
  78.  
  79.     mysql_close();
  80.     return 1;
  81. }
  82.  
  83. public OnPlayerConnect(playerid)
  84. {
  85.     g_MysqlRaceCheck[playerid]++;
  86.     //reset player data
  87.     for(new E_PLAYERS:e; e < E_PLAYERS; ++e)
  88.         Player[playerid][e] = 0;
  89.  
  90.     new query[128];
  91.     GetPlayerName(playerid, Player[playerid][Name], MAX_PLAYER_NAME);
  92.  
  93.     //send a query to recieve all the stored player data from the table
  94.     mysql_format(g_SQL, query, sizeof(query), "SELECT * FROM `players` WHERE `username` = '%e' LIMIT 1", Player[playerid][Name]);
  95.     mysql_tquery(g_SQL, query, "OnPlayerDataLoaded", "dd", playerid, g_MysqlRaceCheck[playerid]);
  96.     return 1;
  97. }
  98.  
  99. public OnPlayerDataLoaded(playerid, race_check)
  100. {
  101.     /*  race condition check:
  102.         player A connects -> SELECT query is fired -> this query takes very long
  103.         while the query is still processing, player A with playerid 2 disconnects
  104.         player B joins now with playerid 2 -> our laggy SELECT query is finally finished, but for the wrong player
  105.        
  106.         what do we do against it?
  107.         we create a connection count for each playerid and increase it everytime the playerid connects or disconnects
  108.         we also pass the current value of the connection count to our OnPlayerDataLoaded callback
  109.         then we check if current connection count is the same as connection count we passed to the callback
  110.         if yes, everything is okay, if not, we just kick the player
  111.     */
  112.     if(race_check != g_MysqlRaceCheck[playerid])
  113.         return Kick(playerid);
  114.    
  115.    
  116.     new string[128];
  117.     if(cache_num_rows() > 0)
  118.     {
  119.         AssignPlayerData(playerid);
  120.        
  121.         format(string, sizeof(string), CHAT_WHITE "This account (" CHAT_YELLOW "%s" CHAT_WHITE ") is registered. Please login by entering your password in the field below:", Player[playerid][Name]);
  122.         ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", string, "Login", "Abort");
  123.         Player[playerid][IsRegistered] = true;
  124.     }
  125.     else
  126.     {
  127.         format(string, sizeof(string), CHAT_WHITE "Welcome " CHAT_YELLOW "%s" CHAT_WHITE ", you can register by entering your password in the field below:", Player[playerid][Name]);
  128.         ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Registration", string, "Register", "Abort");
  129.         Player[playerid][IsRegistered] = false;
  130.     }
  131.     return 1;
  132. }
  133.  
  134. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  135. {
  136.     if(dialogid == DIALOG_INVALID || dialogid == DIALOG_UNUSED)
  137.         return 1;
  138.  
  139.     switch(dialogid)
  140.     {
  141.         case DIALOG_LOGIN:
  142.         {
  143.             if(!response)
  144.                 return Kick(playerid);
  145.  
  146.             if(strlen(inputtext) <= 5)
  147.                 return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login",
  148.                     CHAT_RED "Your password must be longer than 5 characters!\n" CHAT_WHITE "Please enter your password in the field below:",
  149.                     "Login", "Abort");
  150.  
  151.             new hashed_pass[129];
  152.             WP_Hash(hashed_pass, sizeof(hashed_pass), inputtext);
  153.            
  154.             if(strcmp(hashed_pass, Player[playerid][Password]) == 0)
  155.             {
  156.                 //correct password, spawn the player
  157.                 ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "Login", "You have been successfully logged in.", "Okay", "");
  158.                 Player[playerid][IsLoggedIn] = true;
  159.                
  160.                 SetSpawnInfo(playerid, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0);
  161.                 SpawnPlayer(playerid);
  162.             }
  163.             else
  164.             {
  165.                 Player[playerid][LoginAttempts]++;
  166.                 if(Player[playerid][LoginAttempts] >= 3)
  167.                 {
  168.                     ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "Login", CHAT_RED "You have mistyped your password too often (3 times).", "Okay", "");
  169.                     DelayedKick(playerid);
  170.                 }
  171.                 else
  172.                     ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", CHAT_RED "Wrong password!\n" CHAT_WHITE "Please enter your password in the field below:", "Login", "Abort");
  173.             }
  174.         }
  175.        
  176.         case DIALOG_REGISTER:
  177.         {
  178.             if(!response)
  179.                 return Kick(playerid);
  180.                
  181.             if(strlen(inputtext) <= 5)
  182.                 return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Registration",
  183.                     CHAT_RED "Your password must be longer than 5 characters!\n" CHAT_WHITE "Please enter your password in the field below:",
  184.                     "Register", "Abort");
  185.                    
  186.             new query[256];
  187.             WP_Hash(Player[playerid][Password], 129, inputtext);
  188.             mysql_format(g_SQL, query, sizeof(query), "INSERT INTO `players` (`username`, `password`) VALUES ('%e', '%s')", Player[playerid][Name], Player[playerid][Password]);
  189.             mysql_tquery(g_SQL, query, "OnPlayerRegister", "d", playerid);
  190.         }
  191.        
  192.         default:
  193.             return 0;
  194.     }
  195.     return 1;
  196. }
  197.  
  198. public OnPlayerRegister(playerid)
  199. {
  200.     Player[playerid][ID] = cache_insert_id();
  201.    
  202.     ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "Registration", "Account successfully registered, you have been automatically logged in.", "Okay", "");
  203.     Player[playerid][IsLoggedIn] = true;
  204.     Player[playerid][IsRegistered] = true;
  205.    
  206.     SetSpawnInfo(playerid, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0);
  207.     SpawnPlayer(playerid);
  208.     return 1;
  209. }
  210.  
  211. public OnPlayerSpawn(playerid)
  212. {
  213.     ResetPlayerMoney(playerid);
  214.     GivePlayerMoney(playerid, Player[playerid][Money]);
  215.     return 1;
  216. }
  217.  
  218. public OnPlayerDisconnect(playerid,reason)
  219. {
  220.     g_MysqlRaceCheck[playerid]++;
  221.     UpdatePlayerData(playerid);
  222.     return 1;
  223. }
  224.  
  225.  
  226. /*
  227.  * functions
  228.  */
  229.  
  230. AssignPlayerData(playerid)
  231. {
  232.     Player[playerid][ID] = cache_get_field_content_int(0, "id");
  233.     cache_get_field_content(0, "password", Player[playerid][Password], g_SQL, 129);
  234.     Player[playerid][Money] = cache_get_field_content_int(0, "money");
  235.     return 1;
  236. }
  237.  
  238. UpdatePlayerData(playerid)
  239. {
  240.     if(Player[playerid][IsLoggedIn] == false)
  241.         return 0;
  242.        
  243.     new query[128];
  244.     mysql_format(g_SQL, query, sizeof(query), "UPDATE `players` SET `money` = '%d' WHERE `id` = '%d' LIMIT 1", Player[playerid][Money], Player[playerid][ID]);
  245.     mysql_tquery(g_SQL, query);
  246.     return 1;
  247. }
  248.  
  249. SetupPlayerTable()
  250. {
  251.     mysql_query(g_SQL, "CREATE TABLE IF NOT EXISTS `players` (`id` int(11) NOT NULL auto_increment PRIMARY KEY,`username` varchar(30) NOT NULL,`password` varchar(130) NOT NULL,`money` int(10) NOT NULL default '0')", false);
  252.     return 1;
  253. }
  254.  
  255.  
  256. DelayedKick(playerid, time=500)
  257. {
  258.     SetTimerEx("_KickPlayerDelayed", time, false, "d", playerid);
  259.     return 1;
  260. }
  261.  
  262. forward _KickPlayerDelayed(playerid);
  263. public _KickPlayerDelayed(playerid)
  264. {
  265.     Kick(playerid);
  266.     return 1;
  267. }
  268.  
  269. main() {}
Add Comment
Please, Sign In to add comment