Advertisement
B-Matt

[SA-MP] MySQL Gamemode by B-Matt

Apr 4th, 2013
1,671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 7.19 KB | None | 0 0
  1. /*
  2.     Author: B-Matt
  3.     Script version: v1.0.0
  4.     Changelog: 4.4.2013. (Created)
  5.     WEB: https://gas-locator.com
  6. */
  7.  
  8. #include <a_samp>
  9. #include <a_mysql>
  10.  
  11. #define NEW_SKIN        299
  12.  
  13. #define MYSQL_HOST      "localhost"
  14. #define MYSQL_USER      "root"
  15. #define MYSQL_PASS      ""
  16. #define MYSQL_BASE      "sa-mp"
  17.  
  18. main()
  19. {
  20.     print("\n----------------------------------");
  21.     print(" MySQL Gamemode by B-Matt");
  22.     print("----------------------------------\n");
  23. }
  24. //Player Enum
  25. enum pInfo
  26. {
  27.     pSQLid,
  28.     pPass[50],
  29.     pMoney
  30. }
  31. new PlayerInfo[MAX_PLAYERS][pInfo];
  32. //Global vars
  33. new dbHandle, query[1000];
  34.  
  35.  
  36. // NAME: GetName
  37. // PURPOSE: Getting player name.
  38. stock GetName(playerid)
  39. {
  40.     new pName[MAX_PLAYER_NAME];
  41.     GetPlayerName(playerid, pName, sizeof(pName));
  42.     return pName;
  43. }
  44. // NAME: OnGameModeInit
  45. // PURPOSE: Connecting gamemode with MySQL database,
  46. //           setting playerclass and setting gamemode text.
  47. public OnGameModeInit()
  48. {
  49.     SetGameModeText("MySQL Script v1.0");
  50.     AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
  51.     dbHandle = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_BASE,MYSQL_PASS); //We connect our gamemode with MySQL database
  52.     mysql_debug(1);
  53.     return 1;
  54. }
  55. // NAME: OnGameModeExit
  56. // PURPOSE: Disconnecting gamemode with MySQL
  57. //           database and turning of gamemode.
  58. public OnGameModeExit()
  59. {
  60.     mysql_close();
  61.     return 1;
  62. }
  63.  
  64. public OnPlayerRequestClass(playerid, classid)
  65. {
  66.     SetSpawnInfo( playerid, 0, 0, 1958.33, 1343.12, 15.36, 269.15, 26, 36, 28, 150, 0, 0 );
  67. }
  68. // NAME: OnPlayerConnect
  69. // PURPOSE: Getting player name from database. Then we are
  70. //           going to 'CheckPlayer' for checking player in database.
  71. public OnPlayerConnect(playerid)
  72. {
  73.     mysql_format(1, query, "SELECT * FROM `players` WHERE `Name` = '%e' LIMIT 0,1", GetName(playerid));
  74.     mysql_function_query(1, query, true, "CheckPlayer", "i", playerid);
  75.     return 1;
  76. }
  77.  
  78. // NAME: CheckPlayer
  79. // PURPOSE: Checking is player in a database (login) or not (register).
  80. forward CheckPlayer(playerid);
  81. public CheckPlayer(playerid)
  82. {
  83.     new rows, fields, string[128];
  84.     cache_get_data(rows, fields);
  85.     if(!rows)
  86.     {
  87.         format(string, sizeof(string), "{FFFFFF}Welcome {A3FF00}%s{FFFFFF} on my server. Input your password and register account.", GetName(playerid));
  88.         ShowPlayerDialog(playerid, 0, DIALOG_STYLE_PASSWORD, "Registration", string, "Registration", "Quit");
  89.     }
  90.     else
  91.     {
  92.         new temp[12];
  93.         cache_get_row(0, 1, temp); PlayerInfo[playerid][pSQLid] = strval(temp);
  94.         cache_get_row(0, 2, PlayerInfo[playerid][pPass]);
  95.         cache_get_row(0, 3, temp); PlayerInfo[playerid][pMoney] = strval(temp);
  96.         format(string, sizeof(string), "{FFFFFF}Welcome back {A3FF00}%s{FFFFFF}, your account is ready for use. Input your password and login.", GetName(playerid));
  97.         ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, "Login", string, "Login", "Quit");
  98.     }
  99.     return 1;
  100. }
  101. // NAME: OnAccountCreate
  102. // PURPOSE: When MySQL query finish he will come here and insert MySQL id.
  103. forward OnAccountCreate(playerid);
  104. public OnAccountCreate(playerid)
  105. {
  106.     PlayerInfo[playerid][pSQLid] = mysql_insert_id();
  107.     return 1;
  108. }
  109. // NAME: OnPlayerDisconnect
  110. // PURPOSE: When player disconnects we'll update our database with new data.
  111. public OnPlayerDisconnect(playerid, reason)
  112. {
  113.     mysql_format(1, query, "UPDATE `players` SET `Money` = '%d' WHERE `Name` = '%e' LIMIT 1", GetPlayerMoney(playerid), GetName(playerid));
  114.     mysql_function_query(1, query, false, "", "");
  115.     return 1;
  116. }
  117.  
  118. public OnPlayerSpawn(playerid)
  119. {
  120.     return 1;
  121. }
  122.  
  123. public OnPlayerDeath(playerid, killerid, reason)
  124. {
  125.     return 1;
  126. }
  127.  
  128. public OnVehicleSpawn(vehicleid)
  129. {
  130.     return 1;
  131. }
  132.  
  133. public OnVehicleDeath(vehicleid, killerid)
  134. {
  135.     return 1;
  136. }
  137.  
  138. public OnPlayerText(playerid, text[])
  139. {
  140.     return 1;
  141. }
  142.  
  143. public OnPlayerCommandText(playerid, cmdtext[])
  144. {
  145.     return 0;
  146. }
  147.  
  148. public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
  149. {
  150.     return 1;
  151. }
  152.  
  153. public OnPlayerExitVehicle(playerid, vehicleid)
  154. {
  155.     return 1;
  156. }
  157.  
  158. public OnPlayerStateChange(playerid, newstate, oldstate)
  159. {
  160.     return 1;
  161. }
  162.  
  163. public OnPlayerEnterCheckpoint(playerid)
  164. {
  165.     return 1;
  166. }
  167.  
  168. public OnPlayerLeaveCheckpoint(playerid)
  169. {
  170.     return 1;
  171. }
  172.  
  173. public OnPlayerEnterRaceCheckpoint(playerid)
  174. {
  175.     return 1;
  176. }
  177.  
  178. public OnPlayerLeaveRaceCheckpoint(playerid)
  179. {
  180.     return 1;
  181. }
  182.  
  183. public OnRconCommand(cmd[])
  184. {
  185.     return 1;
  186. }
  187.  
  188. public OnPlayerRequestSpawn(playerid)
  189. {
  190.     return 1;
  191. }
  192.  
  193. public OnObjectMoved(objectid)
  194. {
  195.     return 1;
  196. }
  197.  
  198. public OnPlayerObjectMoved(playerid, objectid)
  199. {
  200.     return 1;
  201. }
  202.  
  203. public OnPlayerPickUpPickup(playerid, pickupid)
  204. {
  205.     return 1;
  206. }
  207.  
  208. public OnVehicleMod(playerid, vehicleid, componentid)
  209. {
  210.     return 1;
  211. }
  212.  
  213. public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
  214. {
  215.     return 1;
  216. }
  217.  
  218. public OnVehicleRespray(playerid, vehicleid, color1, color2)
  219. {
  220.     return 1;
  221. }
  222.  
  223. public OnPlayerSelectedMenuRow(playerid, row)
  224. {
  225.     return 1;
  226. }
  227.  
  228. public OnPlayerExitedMenu(playerid)
  229. {
  230.     return 1;
  231. }
  232.  
  233. public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
  234. {
  235.     return 1;
  236. }
  237.  
  238. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  239. {
  240.     return 1;
  241. }
  242.  
  243. public OnRconLoginAttempt(ip[], password[], success)
  244. {
  245.     return 1;
  246. }
  247.  
  248. public OnPlayerUpdate(playerid)
  249. {
  250.     return 1;
  251. }
  252.  
  253. public OnPlayerStreamIn(playerid, forplayerid)
  254. {
  255.     return 1;
  256. }
  257.  
  258. public OnPlayerStreamOut(playerid, forplayerid)
  259. {
  260.     return 1;
  261. }
  262.  
  263. public OnVehicleStreamIn(vehicleid, forplayerid)
  264. {
  265.     return 1;
  266. }
  267.  
  268. public OnVehicleStreamOut(vehicleid, forplayerid)
  269. {
  270.     return 1;
  271. }
  272. // NAME: OnDialogResponse
  273. // PURPOSE: Our dialogs. 0 is for registration dialog and 1 is for login dialog.
  274. //          When player input his data we'll insert that data in our database.
  275. //          If player is registered we will check if password is valid and then continue.
  276. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  277. {
  278.     switch(dialogid)
  279.     {
  280.         case 0:
  281.         {
  282.             new pName[MAX_PLAYER_NAME+1], pass[50];
  283.             if(!response) return Kick(playerid);
  284.             if(strlen(inputtext) > 50) return SendClientMessage(playerid, -1, "Your password is too long!");
  285.            
  286.             mysql_real_escape_string(GetName(playerid),pName);
  287.             mysql_real_escape_string(inputtext,pass);
  288.            
  289.             format(query, sizeof(query), "INSERT INTO `Players` (Name, Password, Money) VALUES ('%s', '%s', '%d')", pName, pass, 1000);
  290.             mysql_function_query(dbHandle, query, false, "OnAccountCreate", "i", playerid);
  291.            
  292.             SpawnPlayer(playerid);
  293.             GivePlayerMoney(playerid, 1000);
  294.         }
  295.         case 1:
  296.         {
  297.             new string[128];
  298.             if(!response) return Kick(playerid);
  299.             if(strcmp(inputtext, PlayerInfo[playerid][pPass], true) == 0)
  300.             {
  301.                 SpawnPlayer(playerid);
  302.                 GivePlayerMoney(playerid, PlayerInfo[playerid][pMoney]);
  303.             }
  304.             else
  305.             {
  306.                 format(string, sizeof(string), "{FFFFFF}Welcome {A3FF00}%s{FFFFFF} your account is ready for use.\n Input your password and login.\n {FF0000}Wrong password{FFFFFF}!", GetName(playerid));
  307.                 ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, "Login", string, "Login", "Quit");
  308.             }
  309.         }
  310.     }
  311.     return 1;
  312. }
  313.  
  314. public OnPlayerClickPlayer(playerid, clickedplayerid, source)
  315. {
  316.     return 1;
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement