Guest User

Untitled

a guest
Mar 11th, 2010
1,389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 15.28 KB | None | 0 0
  1. /*
  2.  
  3.     MYSQL Example Script
  4.     Created by Calgon / FreddoX.
  5.    
  6.     - Credits to StrickenKid (MYSQL PLUGIN)
  7.     - Credits to Seif_ (new.pwn - colours)
  8.     - Credits to... FreddoX, for making this example
  9.    
  10.     There is an example of how to obtain data (Locations system) into arrays, etc.
  11.    
  12.     Enjoy, do what you wish with this script - It was made to mess around with!
  13.    
  14. */
  15.  
  16. #include <a_samp>
  17. #include <mysql>
  18.  
  19. // Colours
  20. #define WHITE       0xFFFFFFFF
  21. #define LIGHTBLUE   0x00C2ECFF
  22. #define GREY        0xCECECEFF
  23.  
  24. // Misc
  25. #define MAX_LOCATIONS       3
  26. #define VERSION             "MySQL 6.9"
  27.  
  28. // Connection
  29. #define HOST_ADDR   "localhost"
  30. #define HOST_USER   ""
  31. #define HOST_DATA   ""
  32. #define HOST_PASS   ""
  33.  
  34. enum PlayerEnum
  35. {
  36.     pPassword[ 30 ],
  37.     pAdminLevel,
  38.     pAuth,
  39.     pGroup,
  40.     pLastWorld,
  41.     pLastInterior,
  42.     pGender,
  43.     pAge,
  44.     pBankMoney,
  45.     pLastSkin,
  46.     pTutorial,
  47.     pDatabaseID,
  48.     Float: pLastPosX,
  49.     Float: pLastPosY,
  50.     Float: pLastPosZ,
  51. }
  52.  
  53. enum LocationsEnum
  54. {
  55.     Float: lPositionX,
  56.     Float: lPositionY,
  57.     Float: lPositionZ,
  58.     lPositionWorld,
  59.     lPositionInterior,
  60. }
  61.  
  62. new PlayerStatistics[ MAX_PLAYERS ][ PlayerEnum ];
  63. new Locations[ MAX_LOCATIONS ][ LocationsEnum ];
  64.  
  65.  
  66. main()
  67. {
  68. }
  69.  
  70. stock ResetPlayerVariables( playerid )
  71. {
  72.     PlayerStatistics[ playerid ][ pAuth ] = 0;
  73.     PlayerStatistics[ playerid ][ pAdminLevel ] = 0;
  74.     PlayerStatistics[ playerid ][ pGroup ] = 0;
  75.     PlayerStatistics[ playerid ][ pLastSkin ] = 0;
  76.     PlayerStatistics[ playerid ][ pLastWorld ] = 0;
  77.     PlayerStatistics[ playerid ][ pAge ] = 0;
  78.     PlayerStatistics[ playerid ][ pLastInterior ] = 0;
  79.     PlayerStatistics[ playerid ][ pTutorial ] = 0;
  80.     PlayerStatistics[ playerid ][ pDatabaseID ] = 0;
  81.     PlayerStatistics[ playerid ][ pBankMoney ] = 0;
  82.     PlayerStatistics[ playerid ][ pGender ] = 0;
  83.     PlayerStatistics[ playerid ][ pLastPosX ] = 0.00;
  84.     PlayerStatistics[ playerid ][ pLastPosY ] = 0.00;
  85.     PlayerStatistics[ playerid ][ pLastPosZ ] = 0.00;
  86.     return 1;
  87. }
  88.  
  89. stock InitLocations()
  90. {
  91.     new QueryString[ 128 ], i = 1;
  92.    
  93.     mysql_query( "SELECT `PositionID`, `PositionName`,`PositionX`, `PositionY`, `PositionZ`, `PositionInterior`, `PositionWorld` FROM `Positions`" );
  94.     mysql_store_result();
  95.    
  96.     if( mysql_num_rows() >= 1 )
  97.     {
  98.         while( mysql_fetch_row_data() )
  99.         {
  100.             mysql_fetch_field( "PositionX", QueryString );
  101.             Locations[ i ][ lPositionX ] = floatstr( QueryString );
  102.            
  103.             mysql_fetch_field( "PositionY", QueryString );
  104.             Locations[ i ][ lPositionY ] = floatstr( QueryString );
  105.            
  106.             mysql_fetch_field( "PositionZ", QueryString );
  107.             Locations[ i ][ lPositionZ ] = floatstr( QueryString );
  108.            
  109.             mysql_fetch_field( "PositionInterior", QueryString );
  110.             Locations[ i ][ lPositionInterior ] = strval( QueryString );
  111.            
  112.             mysql_fetch_field( "PositionWorld", QueryString );
  113.             Locations[ i ][ lPositionWorld ] = strval( QueryString );
  114.  
  115.             i++;
  116.         }
  117.     }
  118.    
  119.     mysql_free_result();
  120.    
  121.     return 1;
  122. }
  123.  
  124. stock InitPlayerConnection( playerid )
  125. {
  126.     ResetPlayerVariables( playerid );
  127.    
  128.     new Query[ 128 ], Name[ MAX_PLAYER_NAME ], EscapedName[ MAX_PLAYER_NAME ];
  129.    
  130.     GetPlayerName( playerid, Name, sizeof( Name ) );
  131.    
  132.     mysql_real_escape_string( Name, EscapedName );
  133.     #pragma unused Name
  134.    
  135.     format( Query, sizeof( Query ), "SELECT `UserID` FROM `Accounts` WHERE `Username` = '%s'", EscapedName );
  136.     mysql_query( Query );
  137.     mysql_store_result();
  138.    
  139.     if( mysql_num_rows() >= 1 )
  140.     {
  141.         if( mysql_num_rows() >= 2 )
  142.         {
  143.             mysql_free_result();
  144.             SendClientMessage( playerid, WHITE, "There seems to be duplicates of your account, please contact a high-level admin." );
  145.             Kick( playerid );
  146.         }
  147.         else
  148.         {
  149.             PlayerStatistics[ playerid ][ pDatabaseID ] = mysql_fetch_int();
  150.             mysql_free_result();
  151.            
  152.             ShowPlayerDialog( playerid, 1, DIALOG_STYLE_INPUT, "Login / Authentication Process", "Hello there!\n\nPlease enter your password to authenticate.", "Login", "Cancel" );
  153.             SendClientMessage( playerid, WHITE, "Your account exists. Please enter your password to proceed." );
  154.         }
  155.     }
  156.     else
  157.     {
  158.         mysql_free_result();
  159.         SendClientMessage( playerid, WHITE, "Your account does not yet exist. Enter a password to register one!" );
  160.         ShowPlayerDialog( playerid, 2, DIALOG_STYLE_INPUT, "Registration Process", "Hello there!\n\nPlease enter a password to register.", "Register", "Cancel" );
  161.     }
  162. }
  163.  
  164. public OnPlayerDisconnect( playerid, reason )
  165. {
  166.     SavePlayerAccount( playerid );
  167.     return 1;
  168. }
  169.  
  170. public OnGameModeInit()
  171. {
  172.     UsePlayerPedAnims();
  173.    
  174.     SetGameModeText( VERSION );
  175.    
  176.     new ip[ 32 ];
  177.  
  178.     GetServerVarAsString( "bind", ip, sizeof( ip ) );
  179.  
  180.     mysql_connect( HOST_ADDR, HOST_USER, HOST_PASS, HOST_DATA );
  181.  
  182.     InitLocations();
  183.    
  184.     return 1;
  185. }
  186.  
  187. public OnPlayerConnect( playerid )
  188. {
  189.     InitPlayerConnection( playerid );
  190.     return 1;
  191. }
  192.  
  193. public OnGameModeExit()
  194. {
  195.     mysql_close();
  196.     return 1;
  197. }
  198. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  199. {
  200.     switch( dialogid )
  201.     {
  202.         case 1:
  203.         {
  204.             GetFromAccount( PlayerStatistics[ playerid ][ pDatabaseID ], "Password", PlayerStatistics[ playerid ][ pPassword ] );
  205.  
  206.             if( strcmp( inputtext, PlayerStatistics[ playerid ][ pPassword ], true ) == 0 )
  207.             {
  208.                 new String[ 128 ], Name[ MAX_PLAYER_NAME ];
  209.                
  210.                 GetPlayerName( playerid, Name, sizeof( Name ) );
  211.                
  212.                 format( String, sizeof( String ), "Thank you for logging in, %s.", Name );
  213.                 SendClientMessage( playerid, WHITE, String );
  214.                
  215.                 PlayerStatistics[ playerid ][ pAuth ] = 1;
  216.                 LoadAccountVariables( playerid );
  217.             }
  218.             else
  219.             {
  220.                 SendClientMessage( playerid, WHITE, "Incorrect password. Please try again!" );
  221.                 ShowPlayerDialog( playerid, 1, DIALOG_STYLE_INPUT, "Login / Authentication Process", "Hello there!\n\nPlease enter your password to authenticate.", "Login", "Cancel" );
  222.             }
  223.         }
  224.        
  225.         case 2:
  226.         {
  227.             if( strlen( inputtext ) >= 30 )
  228.             {
  229.                 ShowPlayerDialog( playerid, 2, DIALOG_STYLE_INPUT, "Registration Process", "Hello there!\n\nPlease enter a password to register.", "Authenticate", "Cancel" );
  230.                 SendClientMessage( playerid, WHITE, "You must use a shorter password - has to be less than 30 characters." );
  231.             }
  232.             else
  233.             {
  234.                 if( strlen( inputtext ) >= 1 )
  235.                 {
  236.                     new EscapedPassword[ 30 ], Query[ 128 ], Name[ MAX_PLAYER_NAME ], EscapedName[ MAX_PLAYER_NAME ];
  237.                    
  238.                     GetPlayerName( playerid, Name, sizeof( Name ) );
  239.                    
  240.                     mysql_real_escape_string( Name, EscapedName );
  241.                     mysql_real_escape_string( inputtext, EscapedPassword );
  242.                    
  243.                     #pragma unused Name
  244.                    
  245.                     format( Query, sizeof( Query ), "INSERT INTO `Accounts` (Username, Password) VALUES('%s', '%s')", EscapedName, EscapedPassword );
  246.                     mysql_query( Query );
  247.                    
  248.                     SendClientMessage( playerid, WHITE, "Now, please login with the password you've just entered to proceed." );
  249.                     InitPlayerConnection( playerid );
  250.                 }
  251.                 else
  252.                 {
  253.                     ShowPlayerDialog( playerid, 2, DIALOG_STYLE_INPUT, "Registration Process", "Hello there!\n\nPlease enter a password to register.", "Authenticate", "Cancel" );
  254.                     SendClientMessage( playerid, WHITE, "Your password must be longer than 1 character..." );
  255.                 }
  256.             }
  257.         }
  258.        
  259.         case 3:
  260.         {
  261.             switch( listitem )
  262.             {
  263.                 case 0:
  264.                 {
  265.                     SendClientMessage( playerid, GREY, "--------------------------------------------------------------------------------------------------------------------------------" );
  266.                     SendClientMessage( playerid, WHITE, "Thank you for setting your Gender - You are a Male." );
  267.                     SendClientMessage( playerid, WHITE, "Now, please tell us how old your character is supposed to be? There is a set limit for roleplaying your character." );
  268.                     PlayerStatistics[ playerid ][ pGender ] = 1;
  269.                     ShowPlayerDialog( playerid, 4, DIALOG_STYLE_LIST, "How old is your character?", "18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n", "Select", "Cancel" );
  270.                     SendClientMessage( playerid, GREY, "--------------------------------------------------------------------------------------------------------------------------------" );
  271.                 }
  272.                 case 1:
  273.                 {
  274.                     SendClientMessage( playerid, GREY, "--------------------------------------------------------------------------------------------------------------------------------" );
  275.                     SendClientMessage( playerid, WHITE, "Thank you for setting your Gender - You are a Female." );
  276.                     SendClientMessage( playerid, WHITE, "Now, please tell us how old your character is supposed to be? There is a set limit for roleplaying your character." );
  277.                     PlayerStatistics[ playerid ][ pGender ] = 2;
  278.                     ShowPlayerDialog( playerid, 4, DIALOG_STYLE_LIST, "How old is your character?", "18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n", "Select", "Cancel" );
  279.                     SendClientMessage( playerid, GREY, "--------------------------------------------------------------------------------------------------------------------------------" );
  280.                 }
  281.             }
  282.         }
  283.        
  284.         case 4:
  285.         {
  286.             new String[ 128 ];
  287.            
  288.             PlayerStatistics[ playerid ][ pAge ] = 17+listitem;
  289.            
  290.             format( String, sizeof( String ), "You have set your character's age to %d - awesome!", PlayerStatistics[ playerid ][ pAge ] );
  291.             SendClientMessage( playerid, WHITE, String );
  292.            
  293.             SendClientMessage( playerid, WHITE, "Right, now tell us... Are you new to the server?" );
  294.             ShowPlayerDialog( playerid, 5, DIALOG_STYLE_LIST, "Are you new to our server?", "Yes, I'm new here.\nNo, I'm not new here.", "Select", "Cancel" );
  295.            
  296.             SendClientMessage( playerid, GREY, "--------------------------------------------------------------------------------------------------------------------------------" );
  297.         }
  298.        
  299.         case 5:
  300.         {
  301.             switch( listitem )
  302.             {
  303.                 case 0:
  304.                 {
  305.                 }
  306.                 case 1:
  307.                 {
  308.                     PlayerStatistics[ playerid ][ pTutorial ] = 0;
  309.                    
  310.                     SendClientMessage( playerid, WHITE, "Okay - You're ready to go! Thanks for registering, we hope you have fun on the server!" );
  311.                     SendClientMessage( playerid, GREY, "--------------------------------------------------------------------------------------------------------------------------------" );
  312.  
  313.                     TogglePlayerSpectating( playerid, false );
  314.                     SetSpawnInfo( playerid, 0, PlayerStatistics[ playerid ][ pLastSkin ], Locations[ 1 ][ lPositionX ], Locations[ 1 ][ lPositionY ], Locations[ 1 ][ lPositionZ ], 90, 0, 0, 0, 0, 0, 0 );
  315.                     SpawnPlayer( playerid );
  316.                 }
  317.             }
  318.         }
  319.     }
  320. }
  321.  
  322. stock SavePlayerAccount( playerid )
  323. {
  324.     new String[ 255 ];
  325.    
  326.     if( PlayerStatistics[ playerid ][ pAuth ] == 1 )
  327.     {
  328.         format( String, sizeof( String ), "UPDATE `Accounts` SET `Age` = '%d', `Password` = '%s', `AdminLevel` = '%d', `Tutorial` = '%d', `Group` = '%d', `Gender` = '%d' WHERE `UserID` = '%d'", PlayerStatistics[ playerid ][ pAge ], PlayerStatistics[ playerid ][ pPassword ], PlayerStatistics[ playerid ][ pAdminLevel ], PlayerStatistics[ playerid ][ pTutorial ], PlayerStatistics[ playerid ][ pGroup ], PlayerStatistics[ playerid ][ pGender ], PlayerStatistics[ playerid ][ pDatabaseID ] );
  329.         mysql_query( String );
  330.  
  331.         GetPlayerPos( playerid, PlayerStatistics[ playerid ][ pLastPosX ], PlayerStatistics[ playerid ][ pLastPosY ], PlayerStatistics[ playerid ][ pLastPosZ ] );
  332.         format( String, sizeof( String ), "UPDATE `Accounts` SET `LastPosX` = '%f', `LastPosY` = '%f', `LastPosZ` = '%f', `LastInterior` = '%d', `LastWorld` = '%d' WHERE `UserID` = '%d'", PlayerStatistics[ playerid ][ pLastPosX ], PlayerStatistics[ playerid ][ pLastPosY ], PlayerStatistics[ playerid ][ pLastPosZ ], GetPlayerInterior( playerid ), GetPlayerVirtualWorld( playerid ), PlayerStatistics[ playerid ][ pDatabaseID ] );
  333.         mysql_query( String );
  334.  
  335.         format( String, sizeof( String ), "UPDATE `Accounts` SET `BankMoney` = '%d' WHERE `UserID` = '%d'", PlayerStatistics[ playerid ][ pBankMoney ], PlayerStatistics[ playerid ][ pDatabaseID ] );
  336.         mysql_query( String );
  337.     }
  338.     return 1;
  339. }
  340.  
  341. stock LoadAccountVariables( playerid )
  342. {
  343.     if( IsPlayerConnected( playerid ) )
  344.     {
  345.         new DataString[ 128 ], Query[ 128 ];
  346.        
  347.         format( Query, sizeof( Query ), "SELECT * FROM `Accounts` WHERE `UserID` = '%d'", PlayerStatistics[ playerid ][ pDatabaseID ] );
  348.         mysql_query( Query );
  349.         mysql_store_result();
  350.  
  351.         mysql_fetch_field( "AdminLevel", DataString );
  352.         PlayerStatistics[ playerid ][ pAdminLevel ] = strval( DataString );
  353.        
  354.         mysql_fetch_field( "Group", DataString );
  355.         PlayerStatistics[ playerid ][ pGroup ] = strval( DataString );
  356.        
  357.         mysql_fetch_field( "LastPosX", DataString );
  358.         PlayerStatistics[ playerid ][ pLastPosX ] = floatstr( DataString );
  359.        
  360.         mysql_fetch_field( "LastPosY", DataString );
  361.         PlayerStatistics[ playerid ][ pLastPosY ] = floatstr( DataString );
  362.        
  363.         mysql_fetch_field( "LastPosZ", DataString );
  364.         PlayerStatistics[ playerid ][ pLastPosZ ] = floatstr( DataString );
  365.        
  366.         mysql_fetch_field( "LastWorld", DataString );
  367.         PlayerStatistics[ playerid ][ pLastSkin ] = strval( DataString );
  368.        
  369.         mysql_fetch_field( "LastInterior", DataString );
  370.         PlayerStatistics[ playerid ][ pLastInterior ] = strval( DataString );
  371.        
  372.         mysql_fetch_field( "Tutorial", DataString );
  373.         PlayerStatistics[ playerid ][ pTutorial ] = strval( DataString );
  374.  
  375.         mysql_fetch_field( "Gender", DataString );
  376.         PlayerStatistics[ playerid ][ pGender ] = strval( DataString );
  377.        
  378.         mysql_fetch_field( "BankMoney", DataString );
  379.         PlayerStatistics[ playerid ][ pBankMoney ] = strval( DataString );
  380.        
  381.         if( PlayerStatistics[ playerid ][ pTutorial] >= 1 )
  382.         {
  383.             ShowPlayerDialog( playerid, 3, DIALOG_STYLE_LIST, "What is your Gender?", "Male\nGender", "Select", "Cancel" );
  384.             TogglePlayerSpectating( playerid, true );
  385.             SendClientMessage( playerid, LIGHTBLUE, "Hello there! You're new to our server." );
  386.         }
  387.         else
  388.         {
  389.             SetSpawnInfo( playerid, 0, PlayerStatistics[ playerid ][ pLastSkin ], PlayerStatistics[ playerid ][ pLastPosX ], PlayerStatistics[ playerid ][ pLastPosY ], PlayerStatistics[ playerid ][ pLastPosZ ], 0, 0, 0, 0, 0, 0, 0 );
  390.             SpawnPlayer( playerid );
  391.         }
  392.        
  393.         mysql_free_result();
  394.     }
  395.     else
  396.     {
  397.         printf( "[error] LoadAccountVariables() was called, but to a non-connected ID.", playerid );
  398.     }
  399. }
  400.  
  401. stock GetFromAccount( dbid, obtaining[], holdingvar[] )
  402. {
  403.     new Query[ 128 ];
  404.     format( Query, sizeof( Query ), "SELECT `%s` FROM `Accounts` WHERE `UserID` = '%d'", obtaining, dbid );
  405.     mysql_query( Query );
  406.     mysql_store_result();
  407.    
  408.     if( mysql_fetch_row( holdingvar ) == 1 )
  409.     {
  410.         mysql_free_result();
  411.     }
  412.    
  413.     return 1;
  414. }
Advertisement
Add Comment
Please, Sign In to add comment